Coverage for manila/api/views/share_accesses.py: 100%
45 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 (c) 2016 Red Hat, 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.
16from manila.api import common
17from manila.common import constants
18from manila.share import api as share_api
21class ViewBuilder(common.ViewBuilder):
22 """Model a share access API response as a python dictionary."""
24 _collection_name = 'share_accesses'
25 _detail_version_modifiers = [
26 "add_access_key",
27 "translate_transitional_statuses",
28 "add_created_at_and_updated_at",
29 "add_access_rule_metadata_field",
30 ]
32 def list_view(self, request, accesses):
33 """View of a list of share accesses."""
34 return {'access_list': [self.summary_view(request, access)['access']
35 for access in accesses]}
37 def _redact_restricted_fields(self, access, access_dict):
38 if access.get('restricted', False):
39 fields_to_redact = ['access_key', 'access_to']
40 for field in fields_to_redact:
41 access_dict[field] = '******'
42 return access_dict
44 def summary_view(self, request, access):
45 """Summarized view of a single share access."""
46 access_dict = {
47 'id': access.get('id'),
48 'access_level': access.get('access_level'),
49 'access_to': access.get('access_to'),
50 'access_type': access.get('access_type'),
51 'state': access.get('state'),
52 }
53 self.update_versioned_resource_dict(
54 request, access_dict, access)
55 access_dict = self._redact_restricted_fields(access, access_dict)
56 return {'access': access_dict}
58 def view(self, request, access):
59 """Generic view of a single share access."""
60 access_dict = {
61 'id': access.get('id'),
62 'share_id': access.get('share_id'),
63 'access_level': access.get('access_level'),
64 'access_to': access.get('access_to'),
65 'access_type': access.get('access_type'),
66 'state': access.get('state'),
67 }
68 self.update_versioned_resource_dict(
69 request, access_dict, access)
70 access_dict = self._redact_restricted_fields(access, access_dict)
71 return {'access': access_dict}
73 def view_metadata(self, request, metadata):
74 """View of a share access rule metadata."""
75 return {'metadata': metadata}
77 @common.ViewBuilder.versioned_method("2.21")
78 def add_access_key(self, context, access_dict, access):
79 access_dict['access_key'] = access.get('access_key')
81 @common.ViewBuilder.versioned_method("2.33")
82 def add_created_at_and_updated_at(self, context, access_dict, access):
83 access_dict['created_at'] = access.get('created_at')
84 access_dict['updated_at'] = access.get('updated_at')
86 @common.ViewBuilder.versioned_method("2.45")
87 def add_access_rule_metadata_field(self, context, access_dict, access):
88 metadata = access.get('share_access_rules_metadata') or {}
89 metadata = {item['key']: item['value'] for item in metadata}
90 access_dict['metadata'] = metadata
92 @common.ViewBuilder.versioned_method("1.0", "2.27")
93 def translate_transitional_statuses(self, context, access_dict, access):
94 """In 2.28, the per access rule status was (re)introduced."""
95 api = share_api.API()
96 share = api.get(context, access['share_id'])
98 if (share['access_rules_status'] ==
99 constants.SHARE_INSTANCE_RULES_SYNCING):
100 access_dict['state'] = constants.STATUS_NEW
101 else:
102 access_dict['state'] = share['access_rules_status']