Coverage for manila/api/views/shares.py: 98%
100 statements
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2026-02-18 22:19 +0000
1# Copyright 2013 OpenStack LLC.
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.
16from manila.api import common
17from manila.common import constants
18from manila import policy
21class ViewBuilder(common.ViewBuilder):
22 """Model a server API response as a python dictionary."""
24 _collection_name = 'shares'
25 _detail_version_modifiers = [
26 "add_snapshot_support_field",
27 "add_task_state_field",
28 "modify_share_type_field",
29 "remove_export_locations",
30 "add_access_rules_status_field",
31 "add_replication_fields",
32 "add_user_id",
33 "add_create_share_from_snapshot_support_field",
34 "add_revert_to_snapshot_support_field",
35 "translate_access_rules_status",
36 "add_share_group_fields",
37 "add_mount_snapshot_support_field",
38 "add_progress_field",
39 "translate_creating_from_snapshot_status",
40 "add_share_recycle_bin_field",
41 "add_source_backup_id_field",
42 "add_encryption_key_ref_field",
43 ]
45 def summary_list(self, request, shares, count=None):
46 """Show a list of shares without many details."""
47 return self._list_view(self.summary, request, shares, count)
49 def detail_list(self, request, shares, count=None):
50 """Detailed view of a list of shares."""
51 return self._list_view(self.detail, request, shares, count)
53 def summary(self, request, share):
54 """Generic, non-detailed view of a share."""
55 return {
56 'share': {
57 'id': share.get('id'),
58 'name': share.get('display_name'),
59 'links': self._get_links(request, share['id'])
60 }
61 }
63 def detail(self, request, share):
64 """Detailed view of a single share."""
65 context = request.environ['manila.context']
66 metadata = share.get('share_metadata')
67 if metadata: 67 ↛ 68line 67 didn't jump to line 68 because the condition on line 67 was never true
68 metadata = {item['key']: item['value'] for item in metadata}
69 else:
70 metadata = {}
72 export_locations = share.get('export_locations', [])
74 share_instance = share.get('instance') or {}
76 if share_instance.get('share_type'):
77 share_type = share_instance.get('share_type').get('name')
78 else:
79 share_type = share_instance.get('share_type_id')
81 share_dict = {
82 'id': share.get('id'),
83 'size': share.get('size'),
84 'availability_zone': share_instance.get('availability_zone'),
85 'created_at': share.get('created_at'),
86 'status': share.get('status'),
87 'name': share.get('display_name'),
88 'description': share.get('display_description'),
89 'project_id': share.get('project_id'),
90 'snapshot_id': share.get('snapshot_id'),
91 'share_network_id': share_instance.get('share_network_id'),
92 'share_proto': share.get('share_proto'),
93 'export_location': share.get('export_location'),
94 'metadata': metadata,
95 'share_type': share_type,
96 'volume_type': share_type,
97 'links': self._get_links(request, share['id']),
98 'is_public': share.get('is_public'),
99 'export_locations': export_locations,
100 }
102 self.update_versioned_resource_dict(request, share_dict, share)
104 if policy.check_is_host_admin(context):
105 share_dict['share_server_id'] = share_instance.get(
106 'share_server_id')
107 share_dict['host'] = share_instance.get('host')
108 return {'share': share_dict}
110 @common.ViewBuilder.versioned_method("2.2")
111 def add_snapshot_support_field(self, context, share_dict, share):
112 share_dict['snapshot_support'] = share.get('snapshot_support')
114 @common.ViewBuilder.versioned_method("2.5")
115 def add_task_state_field(self, context, share_dict, share):
116 share_dict['task_state'] = share.get('task_state')
118 @common.ViewBuilder.versioned_method("2.6")
119 def modify_share_type_field(self, context, share_dict, share):
120 share_instance = share.get('instance') or {}
122 share_type = share_instance.get('share_type_id')
124 share_type_name = None
125 if share_instance.get('share_type'):
126 share_type_name = share_instance.get('share_type').get('name')
128 share_dict.update({
129 'share_type_name': share_type_name,
130 'share_type': share_type,
131 })
133 @common.ViewBuilder.versioned_method("2.9")
134 def remove_export_locations(self, context, share_dict, share):
135 share_dict.pop('export_location')
136 share_dict.pop('export_locations')
138 @common.ViewBuilder.versioned_method("2.10")
139 def add_access_rules_status_field(self, context, share_dict, share):
140 share_dict['access_rules_status'] = share.get('access_rules_status')
142 @common.ViewBuilder.versioned_method('2.11')
143 def add_replication_fields(self, context, share_dict, share):
144 share_dict['replication_type'] = share.get('replication_type')
145 share_dict['has_replicas'] = share['has_replicas']
147 @common.ViewBuilder.versioned_method("2.16")
148 def add_user_id(self, context, share_dict, share):
149 share_dict['user_id'] = share.get('user_id')
151 @common.ViewBuilder.versioned_method("2.24")
152 def add_create_share_from_snapshot_support_field(self, context,
153 share_dict, share):
154 share_dict['create_share_from_snapshot_support'] = share.get(
155 'create_share_from_snapshot_support')
157 @common.ViewBuilder.versioned_method("2.27")
158 def add_revert_to_snapshot_support_field(self, context, share_dict, share):
159 share_dict['revert_to_snapshot_support'] = share.get(
160 'revert_to_snapshot_support')
162 @common.ViewBuilder.versioned_method("2.10", "2.27")
163 def translate_access_rules_status(self, context, share_dict, share):
164 if (share['access_rules_status'] ==
165 constants.SHARE_INSTANCE_RULES_SYNCING):
166 share_dict['access_rules_status'] = constants.STATUS_OUT_OF_SYNC
168 @common.ViewBuilder.versioned_method("2.31")
169 def add_share_group_fields(self, context, share_dict, share):
170 share_dict['share_group_id'] = share.get(
171 'share_group_id')
172 share_dict['source_share_group_snapshot_member_id'] = share.get(
173 'source_share_group_snapshot_member_id')
175 @common.ViewBuilder.versioned_method("2.32")
176 def add_mount_snapshot_support_field(self, context, share_dict, share):
177 share_dict['mount_snapshot_support'] = share.get(
178 'mount_snapshot_support')
180 def _list_view(self, func, request, shares, count=None):
181 """Provide a view for a list of shares."""
182 shares_list = [func(request, share)['share'] for share in shares]
183 shares_links = self._get_collection_links(request,
184 shares,
185 self._collection_name)
186 shares_dict = dict(shares=shares_list)
188 if count is not None:
189 shares_dict['count'] = count
190 if shares_links:
191 shares_dict['shares_links'] = shares_links
193 return shares_dict
195 @common.ViewBuilder.versioned_method("1.0", "2.53")
196 def translate_creating_from_snapshot_status(self, context, share_dict,
197 share):
198 if share.get('status') == constants.STATUS_CREATING_FROM_SNAPSHOT:
199 share_dict['status'] = constants.STATUS_CREATING
201 @common.ViewBuilder.versioned_method("2.54")
202 def add_progress_field(self, context, share_dict, share):
203 share_dict['progress'] = share.get('progress')
205 @common.ViewBuilder.versioned_method("2.69")
206 def add_share_recycle_bin_field(self, context, share_dict, share):
207 share_dict['is_soft_deleted'] = share.get('is_soft_deleted')
208 share_dict['scheduled_to_be_deleted_at'] = share.get(
209 'scheduled_to_be_deleted_at')
211 @common.ViewBuilder.versioned_method("2.80")
212 def add_source_backup_id_field(self, context, share_dict, share):
213 share_dict['source_backup_id'] = share.get('source_backup_id')
215 @common.ViewBuilder.versioned_method("2.90")
216 def add_encryption_key_ref_field(self, context, share_dict, share):
217 share_dict['encryption_key_ref'] = share.get('instance', {}).get(
218 'encryption_key_ref')