Coverage for manila/api/v2/share_group_snapshots.py: 96%
173 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 2015 Alex Meade
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 http import client as http_client
18from oslo_log import log
19from oslo_utils import uuidutils
20import webob
21from webob import exc
23from manila.api import common
24from manila.api.openstack import wsgi
25import manila.api.views.share_group_snapshots as share_group_snapshots_views
26from manila import db
27from manila import exception
28from manila.i18n import _
29import manila.share_group.api as share_group_api
31LOG = log.getLogger(__name__)
32SG_GRADUATION_VERSION = '2.55'
35class ShareGroupSnapshotController(wsgi.Controller, wsgi.AdminActionsMixin):
36 """The share group snapshots API controller for the OpenStack API."""
38 resource_name = 'share_group_snapshot'
39 _view_builder_class = (
40 share_group_snapshots_views.ShareGroupSnapshotViewBuilder)
42 def __init__(self):
43 super(ShareGroupSnapshotController, self).__init__()
44 self.share_group_api = share_group_api.API()
46 def _get_share_group_snapshot(self, context, sg_snapshot_id):
47 try:
48 return self.share_group_api.get_share_group_snapshot(
49 context, sg_snapshot_id)
50 except exception.NotFound:
51 msg = _("Share group snapshot %s not found.") % sg_snapshot_id
52 raise exc.HTTPNotFound(explanation=msg)
54 @wsgi.Controller.authorize('get')
55 def _show(self, req, id):
56 """Return data about the given share group snapshot."""
57 context = req.environ['manila.context']
58 sg_snapshot = self._get_share_group_snapshot(context, id)
59 return self._view_builder.detail(req, sg_snapshot)
61 @wsgi.Controller.api_version('2.31', '2.54', experimental=True)
62 def show(self, req, id):
63 return self._show(req, id)
65 @wsgi.Controller.api_version(SG_GRADUATION_VERSION) # noqa
66 def show(self, req, id): # pylint: disable=function-redefined # noqa F811
67 return self._show(req, id)
69 @wsgi.Controller.authorize('delete')
70 def _delete_group_snapshot(self, req, id):
71 """Delete a share group snapshot."""
72 context = req.environ['manila.context']
73 LOG.info("Delete share group snapshot with id: %s",
74 id, context=context)
75 sg_snapshot = self._get_share_group_snapshot(context, id)
76 try:
77 self.share_group_api.delete_share_group_snapshot(
78 context, sg_snapshot)
79 except exception.InvalidShareGroupSnapshot as e:
80 raise exc.HTTPConflict(explanation=e.msg)
81 return webob.Response(status_int=http_client.ACCEPTED)
83 @wsgi.Controller.api_version('2.31', '2.54', experimental=True)
84 def delete(self, req, id):
85 return self._delete_group_snapshot(req, id)
87 @wsgi.Controller.api_version(SG_GRADUATION_VERSION) # noqa
88 def delete(self, req, id): # pylint: disable=function-redefined # noqa F811
89 return self._delete_group_snapshot(req, id)
91 @wsgi.Controller.api_version('2.31', '2.54', experimental=True)
92 def index(self, req):
93 """Returns a summary list of share group snapshots."""
94 return self._get_share_group_snaps(req, is_detail=False)
96 @wsgi.Controller.api_version(SG_GRADUATION_VERSION) # noqa
97 def index(self, req): # pylint: disable=function-redefined # noqa F811
98 """Returns a summary list of share group snapshots."""
99 return self._get_share_group_snaps(req, is_detail=False)
101 @wsgi.Controller.api_version('2.31', '2.54', experimental=True)
102 def detail(self, req):
103 """Returns a detailed list of share group snapshots."""
104 return self._get_share_group_snaps(req, is_detail=True)
106 @wsgi.Controller.api_version(SG_GRADUATION_VERSION) # noqa
107 def detail(self, req): # pylint: disable=function-redefined # noqa F811
108 """Returns a detailed list of share group snapshots."""
109 return self._get_share_group_snaps(req, is_detail=True)
111 @wsgi.Controller.authorize('get_all')
112 def _get_share_group_snaps(self, req, is_detail):
113 """Returns a list of share group snapshots."""
114 context = req.environ['manila.context']
116 search_opts = {}
117 search_opts.update(req.GET)
119 # Remove keys that are not related to group attrs
120 search_opts.pop('limit', None)
121 search_opts.pop('offset', None)
122 sort_key = search_opts.pop('sort_key', 'created_at')
123 sort_dir = search_opts.pop('sort_dir', 'desc')
125 snaps = self.share_group_api.get_all_share_group_snapshots(
126 context, detailed=is_detail, search_opts=search_opts,
127 sort_dir=sort_dir, sort_key=sort_key)
129 limited_list = common.limited(snaps, req)
131 if is_detail:
132 snaps = self._view_builder.detail_list(req, limited_list)
133 else:
134 snaps = self._view_builder.summary_list(req, limited_list)
135 return snaps
137 @wsgi.Controller.authorize('update')
138 def _update_group_snapshot(self, req, id, body):
139 """Update a share group snapshot."""
140 context = req.environ['manila.context']
141 key = 'share_group_snapshot'
142 if not self.is_valid_body(body, key):
143 msg = _("'%s' is missing from the request body.") % key
144 raise exc.HTTPBadRequest(explanation=msg)
146 sg_snapshot_data = body[key]
147 valid_update_keys = {
148 'name',
149 'description',
150 }
151 invalid_fields = set(sg_snapshot_data.keys()) - valid_update_keys
152 if invalid_fields:
153 msg = _("The fields %s are invalid or not allowed to be updated.")
154 raise exc.HTTPBadRequest(explanation=msg % invalid_fields)
156 sg_snapshot = self._get_share_group_snapshot(context, id)
157 sg_snapshot = self.share_group_api.update_share_group_snapshot(
158 context, sg_snapshot, sg_snapshot_data)
159 return self._view_builder.detail(req, sg_snapshot)
161 @wsgi.Controller.api_version('2.31', '2.54', experimental=True)
162 def update(self, req, id, body):
163 return self._update_group_snapshot(req, id, body)
165 @wsgi.Controller.api_version(SG_GRADUATION_VERSION) # noqa
166 def update(self, req, id, body): # pylint: disable=function-redefined # noqa F811
167 return self._update_group_snapshot(req, id, body)
169 @wsgi.Controller.authorize('create')
170 def _create(self, req, body):
171 """Creates a new share group snapshot."""
172 context = req.environ['manila.context']
174 if not self.is_valid_body(body, 'share_group_snapshot'):
175 msg = _("'share_group_snapshot' is missing from the request body.")
176 raise exc.HTTPBadRequest(explanation=msg)
178 share_group_snapshot = body.get('share_group_snapshot', {})
180 share_group_id = share_group_snapshot.get('share_group_id')
181 if not share_group_id:
182 msg = _("Must supply 'share_group_id' attribute.")
183 raise exc.HTTPBadRequest(explanation=msg)
184 if not uuidutils.is_uuid_like(share_group_id):
185 msg = _("The 'share_group_id' attribute must be a uuid.")
186 raise exc.HTTPBadRequest(explanation=msg)
188 kwargs = {"share_group_id": share_group_id}
189 if 'name' in share_group_snapshot:
190 kwargs['name'] = share_group_snapshot.get('name')
191 if 'description' in share_group_snapshot:
192 kwargs['description'] = share_group_snapshot.get('description')
194 try:
195 new_snapshot = self.share_group_api.create_share_group_snapshot(
196 context, **kwargs)
197 except exception.ShareGroupNotFound as e:
198 raise exc.HTTPBadRequest(explanation=e.msg)
199 except exception.InvalidShareGroup as e:
200 raise exc.HTTPConflict(explanation=e.msg)
202 return self._view_builder.detail(req, dict(new_snapshot.items()))
204 @wsgi.Controller.api_version('2.31', '2.54', experimental=True)
205 @wsgi.response(202)
206 def create(self, req, body):
207 return self._create(req, body)
209 @wsgi.Controller.api_version(SG_GRADUATION_VERSION) # noqa
210 @wsgi.response(202)
211 def create(self, req, body): # pylint: disable=function-redefined # noqa F811
212 return self._create(req, body)
214 @wsgi.Controller.authorize('get')
215 def _members(self, req, id):
216 """Returns a list of share group snapshot members."""
217 context = req.environ['manila.context']
219 snaps = self.share_group_api.get_all_share_group_snapshot_members(
220 context, id)
222 limited_list = common.limited(snaps, req)
224 snaps = self._view_builder.member_list(req, limited_list)
225 return snaps
227 @wsgi.Controller.api_version('2.31', '2.54', experimental=True)
228 def members(self, req, id):
229 return self._members(req, id)
231 @wsgi.Controller.api_version(SG_GRADUATION_VERSION) # noqa
232 def members(self, req, id): # pylint: disable=function-redefined # noqa F811
233 return self._members(req, id)
235 def _update(self, *args, **kwargs):
236 db.share_group_snapshot_update(*args, **kwargs)
238 def _get(self, *args, **kwargs):
239 return self.share_group_api.get_share_group_snapshot(*args, **kwargs)
241 def _delete(self, context, resource, force=True):
242 db.share_group_snapshot_destroy(context.elevated(), resource['id'])
244 @wsgi.Controller.api_version('2.31', '2.54', experimental=True)
245 @wsgi.action('reset_status')
246 def share_group_snapshot_reset_status(self, req, id, body):
247 return self._reset_status(req, id, body)
249 # pylint: disable=function-redefined
250 @wsgi.Controller.api_version(SG_GRADUATION_VERSION) # noqa
251 @wsgi.action('reset_status')
252 def share_group_snapshot_reset_status(self, req, id, body): # noqa F811
253 return self._reset_status(req, id, body)
255 # pylint: enable=function-redefined
256 @wsgi.Controller.api_version('2.31', '2.54', experimental=True)
257 @wsgi.action('force_delete')
258 def share_group_snapshot_force_delete(self, req, id, body):
259 return self._force_delete(req, id, body)
261 # pylint: disable=function-redefined
262 @wsgi.Controller.api_version(SG_GRADUATION_VERSION) # noqa
263 @wsgi.action('force_delete')
264 def share_group_snapshot_force_delete(self, req, id, body): # noqa F811
265 return self._force_delete(req, id, body)
268def create_resource():
269 return wsgi.Resource(ShareGroupSnapshotController())