Coverage for manila/api/views/export_locations.py: 98%

41 statements  

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

1# Copyright (c) 2015 Mirantis Inc. 

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 

16import copy 

17 

18from oslo_utils import strutils 

19 

20from manila.api import common 

21 

22 

23class ViewBuilder(common.ViewBuilder): 

24 """Model export-locations API responses as a python dictionary.""" 

25 

26 _collection_name = "export_locations" 

27 

28 _detail_version_modifiers = [ 

29 'add_preferred_path_attribute', 

30 'add_metadata_attribute', 

31 ] 

32 

33 def _get_export_location_view(self, request, export_location, 

34 detail=False, replica=False): 

35 

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

37 

38 view = { 

39 'id': export_location['uuid'], 

40 'path': export_location['path'], 

41 } 

42 self.update_versioned_resource_dict(request, view, export_location) 

43 if context.is_admin: 

44 view['share_instance_id'] = export_location['share_instance_id'] 

45 view['is_admin_only'] = export_location['is_admin_only'] 

46 

47 if detail: 

48 view['created_at'] = export_location['created_at'] 

49 view['updated_at'] = export_location['updated_at'] 

50 

51 if replica: 

52 share_instance = export_location['share_instance'] 

53 view['replica_state'] = share_instance['replica_state'] 

54 view['availability_zone'] = share_instance['availability_zone'] 

55 

56 return {'export_location': view} 

57 

58 def summary(self, request, export_location, replica=False): 

59 """Summary view of a single export location.""" 

60 return self._get_export_location_view( 

61 request, export_location, detail=False, replica=replica) 

62 

63 def detail(self, request, export_location, replica=False): 

64 """Detailed view of a single export location.""" 

65 return self._get_export_location_view( 

66 request, export_location, detail=True, replica=replica) 

67 

68 def _list_export_locations(self, req, export_locations, 

69 detail=False, replica=False): 

70 """View of export locations list.""" 

71 view_method = self.detail if detail else self.summary 

72 return { 

73 self._collection_name: [ 

74 view_method(req, elocation, replica=replica)['export_location'] 

75 for elocation in export_locations 

76 ]} 

77 

78 def detail_list(self, request, export_locations): 

79 """Detailed View of export locations list.""" 

80 return self._list_export_locations(request, export_locations, 

81 detail=True) 

82 

83 def summary_list(self, request, export_locations, replica=False): 

84 """Summary View of export locations list.""" 

85 return self._list_export_locations(request, export_locations, 

86 detail=False, replica=replica) 

87 

88 @common.ViewBuilder.versioned_method('2.14') 

89 def add_preferred_path_attribute(self, context, view_dict, 

90 export_location): 

91 view_dict['preferred'] = strutils.bool_from_string( 

92 export_location['el_metadata'].get('preferred')) 

93 

94 @common.ViewBuilder.versioned_method('2.87') 

95 def add_metadata_attribute(self, context, view_dict, 

96 export_location): 

97 metadata = export_location.get('el_metadata') 

98 meta_copy = copy.copy(metadata) 

99 meta_copy.pop('preferred', None) 

100 view_dict['metadata'] = meta_copy