Coverage for manila/api/views/share_replicas.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Copyright 2015 Goutham Pacha Ravi 

2# All Rights Reserved. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); you may 

5# not use this file except in compliance with the License. You may obtain 

6# a copy of the License at 

7# 

8# http://www.apache.org/licenses/LICENSE-2.0 

9# 

10# Unless required by applicable law or agreed to in writing, software 

11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 

12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 

13# License for the specific language governing permissions and limitations 

14# under the License. 

15 

16from manila.api import common 

17 

18 

19class ReplicationViewBuilder(common.ViewBuilder): 

20 """Model a server API response as a python dictionary.""" 

21 

22 _collection_name = 'share_replicas' 

23 _collection_route_name = "share-replicas" 

24 _collection_links = 'share_replica_links' 

25 

26 _detail_version_modifiers = [ 

27 "add_cast_rules_to_readonly_field", 

28 ] 

29 

30 def summary_list(self, request, replicas): 

31 """Summary view of a list of replicas.""" 

32 return self._list_view(self.summary, request, replicas) 

33 

34 def detail_list(self, request, replicas): 

35 """Detailed view of a list of replicas.""" 

36 return self._list_view(self.detail, request, replicas) 

37 

38 def summary(self, request, replica): 

39 """Generic, non-detailed view of a share replica.""" 

40 

41 replica_dict = { 

42 'id': replica.get('id'), 

43 'share_id': replica.get('share_id'), 

44 'status': replica.get('status'), 

45 'replica_state': replica.get('replica_state'), 

46 } 

47 return {'share_replica': replica_dict} 

48 

49 def detail(self, request, replica): 

50 """Detailed view of a single replica.""" 

51 context = request.environ['manila.context'] 

52 

53 replica_dict = { 

54 'id': replica.get('id'), 

55 'share_id': replica.get('share_id'), 

56 'availability_zone': replica.get('availability_zone'), 

57 'created_at': replica.get('created_at'), 

58 'status': replica.get('status'), 

59 'share_network_id': replica.get('share_network_id'), 

60 'replica_state': replica.get('replica_state'), 

61 'updated_at': replica.get('updated_at'), 

62 } 

63 

64 if context.is_admin: 

65 replica_dict['share_server_id'] = replica.get('share_server_id') 

66 replica_dict['host'] = replica.get('host') 

67 

68 self.update_versioned_resource_dict(request, replica_dict, replica) 

69 return {'share_replica': replica_dict} 

70 

71 def _list_view(self, func, request, replicas): 

72 """Provide a view for a list of replicas.""" 

73 

74 replicas_list = [func(request, replica)['share_replica'] 

75 for replica in replicas] 

76 

77 replica_links = self._get_collection_links(request, 

78 replicas, 

79 self._collection_name) 

80 replicas_dict = {self._collection_name: replicas_list} 

81 

82 if replica_links: 

83 replicas_dict[self._collection_links] = replica_links 

84 

85 return replicas_dict 

86 

87 @common.ViewBuilder.versioned_method("2.30") 

88 def add_cast_rules_to_readonly_field(self, context, replica_dict, replica): 

89 if context.is_admin: 

90 replica_dict['cast_rules_to_readonly'] = replica.get( 

91 'cast_rules_to_readonly', False)