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

51 statements  

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

1# Copyright 2012 OpenStack Foundation. 

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 oslo_config import cfg 

17 

18from manila.api import common 

19from manila.common import constants 

20from manila.share import share_types 

21 

22CONF = cfg.CONF 

23 

24 

25class ViewBuilder(common.ViewBuilder): 

26 

27 _collection_name = 'types' 

28 

29 _detail_version_modifiers = [ 

30 "add_is_public_attr_core_api_like", 

31 "add_is_public_attr_extension_like", 

32 "add_inferred_optional_extra_specs", 

33 "add_description_attr", 

34 "add_is_default_attr" 

35 ] 

36 

37 def show(self, request, share_type, brief=False): 

38 """Trim away extraneous share type attributes.""" 

39 

40 extra_specs = share_type.get('extra_specs', {}) 

41 required_extra_specs = share_type.get('required_extra_specs', {}) 

42 

43 # Remove non-tenant-visible extra specs in a non-admin context 

44 if not request.environ['manila.context'].is_admin: 

45 extra_spec_names = share_types.get_tenant_visible_extra_specs() 

46 extra_specs = self._filter_extra_specs(extra_specs, 

47 extra_spec_names) 

48 required_extra_specs = self._filter_extra_specs( 

49 required_extra_specs, extra_spec_names) 

50 

51 trimmed = { 

52 'id': share_type.get('id'), 

53 'name': share_type.get('name'), 

54 'extra_specs': extra_specs, 

55 'required_extra_specs': required_extra_specs, 

56 } 

57 self.update_versioned_resource_dict(request, trimmed, share_type) 

58 if brief: 

59 return trimmed 

60 else: 

61 return dict(volume_type=trimmed, share_type=trimmed) 

62 

63 @common.ViewBuilder.versioned_method("2.7") 

64 def add_is_public_attr_core_api_like(self, context, share_type_dict, 

65 share_type): 

66 share_type_dict['share_type_access:is_public'] = share_type.get( 

67 'is_public', True) 

68 

69 @common.ViewBuilder.versioned_method("1.0", "2.6") 

70 def add_is_public_attr_extension_like(self, context, share_type_dict, 

71 share_type): 

72 share_type_dict['os-share-type-access:is_public'] = share_type.get( 

73 'is_public', True) 

74 

75 @common.ViewBuilder.versioned_method("2.24") 

76 def add_inferred_optional_extra_specs(self, context, share_type_dict, 

77 share_type): 

78 

79 # NOTE(cknight): The admin sees exactly which extra specs have been set 

80 # on the type, but in order to know how shares of a type will behave, 

81 # the user must also see the default values of any public extra specs 

82 # that aren't explicitly set on the type. 

83 if not context.is_admin: 

84 for extra_spec in constants.ExtraSpecs.INFERRED_OPTIONAL_MAP: 

85 if extra_spec not in share_type_dict['extra_specs']: 

86 share_type_dict['extra_specs'][extra_spec] = ( 

87 constants.ExtraSpecs.INFERRED_OPTIONAL_MAP[extra_spec]) 

88 

89 def index(self, request, share_types): 

90 """Index over trimmed share types.""" 

91 share_types_list = [self.show(request, share_type, True) 

92 for share_type in share_types] 

93 return dict(volume_types=share_types_list, 

94 share_types=share_types_list) 

95 

96 def share_type_access(self, request, share_type): 

97 """Return a dictionary view of the projects with access to type.""" 

98 projects = [ 

99 {'share_type_id': share_type['id'], 'project_id': project_id} 

100 for project_id in share_type['projects'] 

101 ] 

102 return {'share_type_access': projects} 

103 

104 def _filter_extra_specs(self, extra_specs, valid_keys): 

105 return {key: value for key, value in extra_specs.items() 

106 if key in valid_keys} 

107 

108 @common.ViewBuilder.versioned_method("2.41") 

109 def add_description_attr(self, context, share_type_dict, share_type): 

110 share_type_dict['description'] = share_type.get('description') 

111 

112 @common.ViewBuilder.versioned_method("2.46") 

113 def add_is_default_attr(self, context, share_type_dict, share_type): 

114 is_default = False 

115 type_name = share_type.get('name') 

116 default_name = CONF.default_share_type 

117 

118 if default_name is not None: 

119 is_default = default_name == type_name 

120 share_type_dict['is_default'] = is_default