Coverage for manila/api/views/share_snapshots.py: 96%
43 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 NetApp
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
19class ViewBuilder(common.ViewBuilder):
20 """Model a server API response as a python dictionary."""
22 _collection_name = 'snapshots'
23 _detail_version_modifiers = [
24 "add_provider_location_field",
25 "add_project_and_user_ids",
26 "add_metadata"
27 ]
29 def summary_list(self, request, snapshots, count=None):
30 """Show a list of share snapshots without many details."""
31 return self._list_view(self.summary, request, snapshots, count)
33 def detail_list(self, request, snapshots, count=None):
34 """Detailed view of a list of share snapshots."""
35 return self._list_view(self.detail, request, snapshots, count)
37 def summary(self, request, snapshot):
38 """Generic, non-detailed view of an share snapshot."""
39 return {
40 'snapshot': {
41 'id': snapshot.get('id'),
42 'name': snapshot.get('display_name'),
43 'links': self._get_links(request, snapshot['id'])
44 }
45 }
47 def detail(self, request, snapshot):
48 """Detailed view of a single share snapshot."""
49 snapshot_dict = {
50 'id': snapshot.get('id'),
51 'share_id': snapshot.get('share_id'),
52 'share_size': snapshot.get('share_size'),
53 'created_at': snapshot.get('created_at'),
54 'status': snapshot.get('aggregate_status'),
55 'name': snapshot.get('display_name'),
56 'description': snapshot.get('display_description'),
57 'size': snapshot.get('size'),
58 'share_proto': snapshot.get('share_proto'),
59 'links': self._get_links(request, snapshot['id']),
60 }
62 self.update_versioned_resource_dict(request, snapshot_dict, snapshot)
64 return {'snapshot': snapshot_dict}
66 @common.ViewBuilder.versioned_method("2.12")
67 def add_provider_location_field(self, context, snapshot_dict, snapshot):
68 # NOTE(xyang): Only retrieve provider_location for admin.
69 if context.is_admin:
70 snapshot_dict['provider_location'] = snapshot.get(
71 'provider_location')
73 @common.ViewBuilder.versioned_method("2.17")
74 def add_project_and_user_ids(self, context, snapshot_dict, snapshot):
75 snapshot_dict['user_id'] = snapshot.get('user_id')
76 snapshot_dict['project_id'] = snapshot.get('project_id')
78 @common.ViewBuilder.versioned_method("2.73")
79 def add_metadata(self, context, snapshot_dict, snapshot):
80 metadata = snapshot.get('share_snapshot_metadata')
81 if metadata: 81 ↛ 82line 81 didn't jump to line 82 because the condition on line 81 was never true
82 metadata = {item['key']: item['value'] for item in metadata}
83 else:
84 metadata = {}
85 snapshot_dict['metadata'] = metadata
87 def _list_view(self, func, request, snapshots, count=None):
88 """Provide a view for a list of share snapshots."""
89 snapshots_list = [func(request, snapshot)['snapshot']
90 for snapshot in snapshots]
91 snapshots_links = self._get_collection_links(request,
92 snapshots,
93 self._collection_name)
94 snapshots_dict = {self._collection_name: snapshots_list}
96 if count is not None:
97 snapshots_dict['count'] = count
98 if snapshots_links:
99 snapshots_dict['share_snapshots_links'] = snapshots_links
101 return snapshots_dict
103 def detail_access(self, request, access):
104 access = {
105 'snapshot_access': {
106 'id': access['id'],
107 'access_type': access['access_type'],
108 'access_to': access['access_to'],
109 'state': access['state'],
110 }
111 }
112 return access
114 def detail_list_access(self, request, access_list):
115 return {
116 'snapshot_access_list':
117 ([self.detail_access(request, access)['snapshot_access']
118 for access in access_list])
119 }