Debian Codegraph @

manila_tempest_tests/services/share/v2/json/shares_client.py

raw
1 # Copyright 2015 Andrew Kerr
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
16 import json
17 import re
18 import time
19 from urllib import parse
20
21 from tempest import config
22 from tempest.lib.common import rest_client
23 from tempest.lib.common.utils import data_utils
24 from tempest.lib import exceptions
25
26 from manila_tempest_tests.common import constants
27 from manila_tempest_tests import share_exceptions
28 from manila_tempest_tests import utils
29
30 CONF = config.CONF
31 LATEST_MICROVERSION = CONF.share.max_api_microversion
32 EXPERIMENTAL = {'X-OpenStack-Manila-API-Experimental': 'True'}
33
34
35 class SharesV2Client(rest_client.RestClient):
36 """Tempest REST client for Manila.
37
38 It handles shares and access to it in OpenStack.
39 """
40 api_version = 'v2'
41
42 def __init__(self, auth_provider, **kwargs):
43 super(SharesV2Client, self).__init__(auth_provider, **kwargs)
44 self.API_MICROVERSIONS_HEADER = 'x-openstack-manila-api-version'
45 self.share_protocol = None
46 if CONF.share.enable_protocols:
47 self.share_protocol = CONF.share.enable_protocols[0]
48 self.share_network_id = CONF.share.share_network_id
49 self.share_size = CONF.share.share_size
50
51 def inject_microversion_header(self, headers, version,
52 extra_headers=False):
53 """Inject the required manila microversion header."""
54 new_headers = self.get_headers()
55 new_headers[self.API_MICROVERSIONS_HEADER] = version
56 if extra_headers and headers:
57 new_headers.update(headers)
58 elif headers:
59 new_headers = headers
60 return new_headers
61
62 def verify_request_id(self, response):
63 response_headers = [r.lower() for r in response.keys()]
64 assert_msg = ("Response is missing request ID. Response "
65 "headers are: %s") % response
66 assert 'x-compute-request-id' in response_headers, assert_msg
67
68 # Overwrite all http verb calls to inject the micro version header
69 def post(self, url, body, headers=None, extra_headers=False,
70 version=LATEST_MICROVERSION):
71 headers = self.inject_microversion_header(headers, version,
72 extra_headers=extra_headers)
73 resp, body = super(SharesV2Client, self).post(url, body,
74 headers=headers)
75 self.verify_request_id(resp)
76 return resp, body
77
78 def get(self, url, headers=None, extra_headers=False,
79 version=LATEST_MICROVERSION):
80 headers = self.inject_microversion_header(headers, version,
81 extra_headers=extra_headers)
82 resp, body = super(SharesV2Client, self).get(url, headers=headers)
83 self.verify_request_id(resp)
84 return resp, body
85
86 def delete(self, url, headers=None, body=None, extra_headers=False,
87 version=LATEST_MICROVERSION):
88 headers = self.inject_microversion_header(headers, version,
89 extra_headers=extra_headers)
90 resp, body = super(SharesV2Client, self).delete(url, headers=headers,
91 body=body)
92 self.verify_request_id(resp)
93 return resp, body
94
95 def patch(self, url, body, headers=None, extra_headers=False,
96 version=LATEST_MICROVERSION):
97 headers = self.inject_microversion_header(headers, version,
98 extra_headers=extra_headers)
99 return super(SharesV2Client, self).patch(url, body, headers=headers)
100
101 def put(self, url, body, headers=None, extra_headers=False,
102 version=LATEST_MICROVERSION):
103 headers = self.inject_microversion_header(headers, version,
104 extra_headers=extra_headers)
105 resp, body = super(SharesV2Client, self).put(url, body,
106 headers=headers)
107 self.verify_request_id(resp)
108 return resp, body
109
110 def head(self, url, headers=None, extra_headers=False,
111 version=LATEST_MICROVERSION):
112 headers = self.inject_microversion_header(headers, version,
113 extra_headers=extra_headers)
114 resp, body = super(SharesV2Client, self).head(url, headers=headers)
115 self.verify_request_id(resp)
116 return resp, body
117
118 def copy(self, url, headers=None, extra_headers=False,
119 version=LATEST_MICROVERSION):
120 headers = self.inject_microversion_header(headers, version,
121 extra_headers=extra_headers)
122 resp, body = super(SharesV2Client, self).copy(url, headers=headers)
123 self.verify_request_id(resp)
124 return resp, body
125
126 def reset_state(self, s_id, status="error", s_type="shares",
127 headers=None, version=LATEST_MICROVERSION,
128 action_name=None):
129 """Resets the state of a share, snapshot, cg, or a cgsnapshot.
130
131 status: available, error, creating, deleting, error_deleting
132 s_type: shares, share_instances, snapshots, consistency-groups,
133 cgsnapshots.
134 """
135 if action_name is None:
136 if utils.is_microversion_gt(version, "2.6"):
137 action_name = 'reset_status'
138 else:
139 action_name = 'os-reset_status'
140 body = {action_name: {"status": status}}
141 body = json.dumps(body)
142 resp, body = self.post("%s/%s/action" % (s_type, s_id), body,
143 headers=headers, extra_headers=True,
144 version=version)
145 self.expected_success(202, resp.status)
146 return rest_client.ResponseBody(resp, body)
147
148 def force_delete(self, s_id, s_type="shares", headers=None,
149 version=LATEST_MICROVERSION, action_name=None):
150 """Force delete share or snapshot.
151
152 s_type: shares, snapshots
153 """
154 if action_name is None:
155 if utils.is_microversion_gt(version, "2.6"):
156 action_name = 'force_delete'
157 else:
158 action_name = 'os-force_delete'
159 body = {action_name: None}
160 body = json.dumps(body)
161 resp, body = self.post("%s/%s/action" % (s_type, s_id), body,
162 headers=headers, extra_headers=True,
163 version=version)
164 self.expected_success(202, resp.status)
165 return rest_client.ResponseBody(resp, body)
166
167 @staticmethod
168 def _get_base_url(endpoint):
169 url = parse.urlparse(endpoint)
170 # Get any valid path components before the version string
171 # regex matches version str & everything after (examples: v1, v2, v1.2)
172 base_path = re.split(r'(^|/)+v\d+(\.\d+)?', url.path)[0]
173 base_url = url._replace(path=base_path)
174 return parse.urlunparse(base_url) + '/'
175
176 def send_microversion_request(self, version=None, script_name=None):
177 """Prepare and send the HTTP GET Request to the base URL.
178
179 Extracts the base URL from the shares_client endpoint and makes a GET
180 request with the microversions request header.
181 :param version: The string to send for the value of the microversion
182 header, or None to omit the header.
183 :param script_name: The first part of the URL (v1 or v2), or None to
184 omit it.
185 """
186
187 headers = self.get_headers()
188 url, headers, body = self.auth_provider.auth_request(
189 'GET', 'shares', headers, None, self.filters)
190 url = self._get_base_url(url)
191 if script_name:
192 url += script_name + '/'
193 if version:
194 headers[self.API_MICROVERSIONS_HEADER] = version
195
196 # Handle logging because raw_request doesn't log anything
197 start = time.time()
198 self._log_request_start('GET', url)
199 resp, resp_body = self.raw_request(url, 'GET', headers=headers)
200 end = time.time()
201 self._log_request(
202 'GET', url, resp, secs=(end - start), resp_body=resp_body)
203 self.response_checker('GET', resp, resp_body)
204 resp_body = json.loads(resp_body)
205 return resp, resp_body
206
207 def _parse_resp(self, body, top_key_to_verify=None):
208 return super(SharesV2Client, self)._parse_resp(
209 body, top_key_to_verify=top_key_to_verify)
210
211 def _is_resource_deleted(self, func, res_id, resource_name, **kwargs):
212 try:
213 res = func(res_id, **kwargs)[resource_name]
214 except exceptions.NotFound:
215 return True
216
217 if res.get('status') in ['error_deleting', 'error']:
218 # Resource has "error_deleting" status and can not be deleted.
219 resource_type = func.__name__.split('_', 1)[-1]
220 raise share_exceptions.ResourceReleaseFailed(
221 res_type=resource_type, res_id=res_id)
222 return False
223
224 def wait_for_resource_deletion(self, *args, **kwargs):
225 """Waits for a resource to be deleted."""
226 start_time = int(time.time())
227 while True:
228 if self.is_resource_deleted(*args, **kwargs):
229 return
230 if int(time.time()) - start_time >= self.build_timeout:
231 raise exceptions.TimeoutException
232 time.sleep(self.build_interval)
233
234 def update_share(self, share_id, version=LATEST_MICROVERSION, **kwargs):
235 body = json.dumps({'share': kwargs})
236 resp, body = self.put("shares/%s" % share_id, body, version=version)
237 self.expected_success(200, resp.status)
238 body = json.loads(body)
239 return rest_client.ResponseBody(resp, body)
240
241 def rename_snapshot(self, snapshot_id, name, desc=None,
242 version=LATEST_MICROVERSION):
243 body = {"snapshot": {"display_name": name}}
244 if desc is not None:
245 body["snapshot"].update({"display_description": desc})
246 body = json.dumps(body)
247 resp, body = self.put("snapshots/%s" % snapshot_id, body,
248 version=version)
249 self.expected_success(200, resp.status)
250 body = json.loads(body)
251 return rest_client.ResponseBody(resp, body)
252
253 def _map_security_service_and_share_network(self, sn_id, ss_id,
254 action="add",
255 version=LATEST_MICROVERSION):
256 # sn_id: id of share_network_entity
257 # ss_id: id of security service entity
258 # action: add, remove
259 data = {
260 "%s_security_service" % action: {
261 "security_service_id": ss_id,
262 }
263 }
264 body = json.dumps(data)
265 resp, body = self.post("share-networks/%s/action" % sn_id, body,
266 version=version)
267 self.expected_success(200, resp.status)
268 body = json.loads(body)
269 return rest_client.ResponseBody(resp, body)
270
271 def add_sec_service_to_share_network(self, sn_id, ss_id,
272 version=LATEST_MICROVERSION):
273 return self._map_security_service_and_share_network(
274 sn_id, ss_id, version=version)
275
276 def remove_sec_service_from_share_network(
277 self, sn_id, ss_id, version=LATEST_MICROVERSION):
278 return self._map_security_service_and_share_network(
279 sn_id, ss_id, "remove", version=version)
280
281 def list_sec_services_for_share_network(
282 self, sn_id, version=LATEST_MICROVERSION):
283 resp, body = self.get("security-services?share_network_id=%s" % sn_id,
284 version=version)
285 self.expected_success(200, resp.status)
286 body = json.loads(body)
287 return rest_client.ResponseBody(resp, body)
288
289 def add_access_to_share_type(
290 self, share_type_id, project_id, version=LATEST_MICROVERSION):
291 uri = 'types/%s/action' % share_type_id
292 post_body = {'project': project_id}
293 post_body = json.dumps({'addProjectAccess': post_body})
294 resp, body = self.post(uri, post_body, version=version)
295 self.expected_success(202, resp.status)
296 return rest_client.ResponseBody(resp, body)
297
298 def remove_access_from_share_type(
299 self, share_type_id, project_id, version=LATEST_MICROVERSION):
300 uri = 'types/%s/action' % share_type_id
301 post_body = {'project': project_id}
302 post_body = json.dumps({'removeProjectAccess': post_body})
303 resp, body = self.post(uri, post_body, version=version)
304 self.expected_success(202, resp.status)
305 return rest_client.ResponseBody(resp, body)
306
307 def get_default_share_type(self, version=LATEST_MICROVERSION):
308 resp, body = self.get("types/default", version=version)
309 self.expected_success(200, resp.status)
310 body = json.loads(body)
311 return rest_client.ResponseBody(resp, body)
312
313 def get_limits(self, version=LATEST_MICROVERSION):
314 resp, body = self.get("limits", version=version)
315 self.expected_success(200, resp.status)
316 body = json.loads(body)
317 return rest_client.ResponseBody(resp, body)
318
319 def list_extensions(self, version=LATEST_MICROVERSION):
320 resp, body = self.get("extensions", version=version)
321 self.expected_success(200, resp.status)
322 body = json.loads(body)
323 return rest_client.ResponseBody(resp, body)
324
325 def list_pools(self, detail=False, search_opts=None,
326 version=LATEST_MICROVERSION):
327 """Get list of scheduler pools."""
328 uri = 'scheduler-stats/pools'
329 if detail:
330 uri += '/detail'
331 if search_opts:
332 uri += "?%s" % parse.urlencode(search_opts)
333 resp, body = self.get(uri, version=version)
334 self.expected_success(200, resp.status)
335 body = json.loads(body)
336 return rest_client.ResponseBody(resp, body)
337
338 def list_share_servers(self, search_opts=None,
339 version=LATEST_MICROVERSION):
340 """Get list of share servers."""
341 uri = "share-servers"
342 if search_opts:
343 uri += "?%s" % parse.urlencode(search_opts)
344 resp, body = self.get(uri, version=version)
345 self.expected_success(200, resp.status)
346 body = json.loads(body)
347 return rest_client.ResponseBody(resp, body)
348
349 def delete_share_server(self, share_server_id,
350 version=LATEST_MICROVERSION):
351 """Delete share server by its ID."""
352 uri = "share-servers/%s" % share_server_id
353 resp, body = self.delete(uri, version=version)
354 self.expected_success(202, resp.status)
355 return rest_client.ResponseBody(resp, body)
356
357 def show_share_server_details(
358 self, share_server_id, version=LATEST_MICROVERSION):
359 """Get share server details only."""
360 uri = "share-servers/%s/details" % share_server_id
361 resp, body = self.get(uri, version=version)
362 self.expected_success(200, resp.status)
363 body = json.loads(body)
364 return rest_client.ResponseBody(resp, body)
365
366 def is_resource_deleted(self, *args, **kwargs):
367 """Verifies whether provided resource deleted or not.
368
369 :param kwargs: dict with expected keys 'share_id', 'snapshot_id',
370 :param kwargs: 'sn_id', 'ss_id', 'vt_id' and 'server_id'
371 :raises share_exceptions.InvalidResource
372 """
373 if "share_instance_id" in kwargs:
374 return self._is_resource_deleted(
375 self.get_share_instance, kwargs.get("share_instance_id"),
376 "share_instance")
377 elif "share_group_id" in kwargs:
378 return self._is_resource_deleted(
379 self.get_share_group, kwargs.get("share_group_id"),
380 "share_group")
381 elif "share_group_snapshot_id" in kwargs:
382 return self._is_resource_deleted(
383 self.get_share_group_snapshot,
384 kwargs.get("share_group_snapshot_id"), "share_group_snapshot")
385 elif "share_group_type_id" in kwargs:
386 return self._is_resource_deleted(
387 self.get_share_group_type, kwargs.get("share_group_type_id"),
388 "share_group_type")
389 elif "replica_id" in kwargs:
390 return self._is_resource_deleted(
391 self.get_share_replica, kwargs.get("replica_id"),
392 "share_replica")
393 elif "message_id" in kwargs:
394 return self._is_resource_deleted(
395 self.get_message, kwargs.get("message_id"), "message")
396 elif "share_network_subnet_id" in kwargs:
397 subnet_kwargs = {
398 "share_network_id": kwargs["sn_id"]}
399 return self._is_resource_deleted(
400 self.get_subnet, kwargs.get("share_network_subnet_id"),
401 "share_network_subnet", **subnet_kwargs
402 )
403 elif "share_id" in kwargs:
404 if "rule_id" in kwargs:
405 rule_id = kwargs.get("rule_id")
406 share_id = kwargs.get("share_id")
407 rules = self.list_access_rules(share_id)['access_list']
408 for rule in rules:
409 if rule["id"] == rule_id:
410 return False
411 return True
412 else:
413 return self._is_resource_deleted(
414 self.get_share, kwargs.get("share_id"), "share")
415 elif "snapshot_id" in kwargs:
416 return self._is_resource_deleted(
417 self.get_snapshot, kwargs.get("snapshot_id"), "snapshot")
418 elif "sn_id" in kwargs:
419 return self._is_resource_deleted(
420 self.get_share_network, kwargs.get("sn_id"), "share_network")
421 elif "ss_id" in kwargs:
422 return self._is_resource_deleted(
423 self.get_security_service, kwargs.get("ss_id"),
424 "security_service")
425 elif "vt_id" in kwargs:
426 return self._is_resource_deleted(
427 self.get_volume_type, kwargs.get("vt_id"), "volume_type")
428 elif "st_id" in kwargs:
429 return self._is_resource_deleted(
430 self.get_share_type, kwargs.get("st_id"), "share_type")
431 elif "server_id" in kwargs:
432 return self._is_resource_deleted(
433 self.show_share_server, kwargs.get("server_id"),
434 "share_server")
435 elif "backup_id" in kwargs:
436 return self._is_resource_deleted(
437 self.get_share_backup, kwargs.get("backup_id"),
438 "share_backup")
439 else:
440 raise share_exceptions.InvalidResource(
441 message=str(kwargs))
442
443 ###############
444
445 def create_share(self, share_protocol=None, size=None,
446 name=None, snapshot_id=None, description=None,
447 metadata=None, share_network_id=None,
448 share_type_id=None, is_public=False,
449 share_group_id=None, availability_zone=None,
450 version=LATEST_MICROVERSION, experimental=False,
451 scheduler_hints=None, encryption_key_ref=None):
452 headers = EXPERIMENTAL if experimental else None
453 metadata = metadata or {}
454 scheduler_hints = scheduler_hints or {}
455 if name is None:
456 name = data_utils.rand_name("tempest-created-share")
457 if description is None:
458 description = data_utils.rand_name("tempest-created-share-desc")
459 if size is None:
460 size = self.share_size
461 if share_protocol is None:
462 share_protocol = self.share_protocol
463 if share_protocol is None:
464 raise share_exceptions.ShareProtocolNotSpecified()
465 post_body = {
466 "share": {
467 "share_proto": share_protocol,
468 "description": description,
469 "snapshot_id": snapshot_id,
470 "name": name,
471 "size": size,
472 "metadata": metadata,
473 "is_public": is_public,
474 }
475 }
476 if availability_zone:
477 post_body["share"]["availability_zone"] = availability_zone
478 if share_network_id:
479 post_body["share"]["share_network_id"] = share_network_id
480 if share_type_id:
481 post_body["share"]["share_type"] = share_type_id
482 if share_group_id:
483 post_body["share"]["share_group_id"] = share_group_id
484 if scheduler_hints:
485 post_body["share"]["scheduler_hints"] = scheduler_hints
486 if encryption_key_ref:
487 post_body["share"]["encryption_key_ref"] = encryption_key_ref
488
489 body = json.dumps(post_body)
490 resp, body = self.post("shares", body, headers=headers,
491 extra_headers=experimental, version=version)
492 self.expected_success(200, resp.status)
493 body = json.loads(body)
494 return rest_client.ResponseBody(resp, body)
495
496 def list_shares(self, detailed=False, params=None,
497 version=LATEST_MICROVERSION, experimental=False):
498 """Get list of shares w/o filters."""
499 headers = EXPERIMENTAL if experimental else None
500 uri = 'shares/detail' if detailed else 'shares'
501 uri += '?%s' % parse.urlencode(params) if params else ''
502 resp, body = self.get(uri, headers=headers, extra_headers=experimental,
503 version=version)
504 self.expected_success(200, resp.status)
505 body = json.loads(body)
506 return rest_client.ResponseBody(resp, body)
507
508 def list_shares_in_recycle_bin(self, detailed=False,
509 params=None, version=LATEST_MICROVERSION,
510 experimental=False):
511 """Get list of shares in recycle bin with w/o filters."""
512 headers = EXPERIMENTAL if experimental else None
513 uri = 'shares/detail' if detailed else 'shares'
514 uri += '?is_soft_deleted=true'
515 uri += '&%s' % parse.urlencode(params) if params else ''
516 resp, body = self.get(uri, headers=headers, extra_headers=experimental,
517 version=version)
518 self.expected_success(200, resp.status)
519 body = json.loads(body)
520 return rest_client.ResponseBody(resp, body)
521
522 def list_shares_with_detail(self, params=None,
523 version=LATEST_MICROVERSION,
524 experimental=False):
525 """Get detailed list of shares w/o filters."""
526 return self.list_shares(detailed=True, params=params,
527 version=version, experimental=experimental)
528
529 def get_share(self, share_id, version=LATEST_MICROVERSION,
530 experimental=False):
531 headers = EXPERIMENTAL if experimental else None
532 resp, body = self.get("shares/%s" % share_id, headers=headers,
533 extra_headers=experimental, version=version)
534 self.expected_success(200, resp.status)
535 body = json.loads(body)
536 return rest_client.ResponseBody(resp, body)
537
538 def get_share_export_location(
539 self, share_id, export_location_uuid, version=LATEST_MICROVERSION):
540 resp, body = self.get(
541 "shares/%(share_id)s/export_locations/%(el_uuid)s" % {
542 "share_id": share_id, "el_uuid": export_location_uuid},
543 version=version)
544 self.expected_success(200, resp.status)
545 body = json.loads(body)
546 return rest_client.ResponseBody(resp, body)
547
548 def list_share_export_locations(
549 self, share_id, version=LATEST_MICROVERSION):
550 resp, body = self.get(
551 "shares/%(share_id)s/export_locations" % {"share_id": share_id},
552 version=version)
553 self.expected_success(200, resp.status)
554 body = json.loads(body)
555 return rest_client.ResponseBody(resp, body)
556
557 def delete_share(self, share_id, params=None,
558 version=LATEST_MICROVERSION):
559 uri = "shares/%s" % share_id
560 uri += '?%s' % (parse.urlencode(params) if params else '')
561 resp, body = self.delete(uri, version=version)
562 self.expected_success(202, resp.status)
563 return rest_client.ResponseBody(resp, body)
564
565 def soft_delete_share(self, share_id, version=LATEST_MICROVERSION):
566 post_body = {"soft_delete": None}
567 body = json.dumps(post_body)
568 resp, body = self.post(
569 "shares/%s/action" % share_id, body, version=version)
570 self.expected_success(202, resp.status)
571 return rest_client.ResponseBody(resp, body)
572
573 def restore_share(self, share_id, version=LATEST_MICROVERSION):
574 post_body = {"restore": None}
575 body = json.dumps(post_body)
576 resp, body = self.post(
577 "shares/%s/action" % share_id, body, version=version)
578 self.expected_success(202, resp.status)
579 return rest_client.ResponseBody(resp, body)
580
581 ###############
582 def create_share_transfer(self, share_id, name=None,
583 version=LATEST_MICROVERSION):
584 if name is None:
585 name = data_utils.rand_name("tempest-created-share-transfer")
586 post_body = {
587 "transfer": {
588 "share_id": share_id,
589 "name": name
590 }
591 }
592 body = json.dumps(post_body)
593 resp, body = self.post("share-transfers", body, version=version)
594 self.expected_success(202, resp.status)
595 body = json.loads(body)
596 return rest_client.ResponseBody(resp, body)
597
598 def delete_share_transfer(self, transfer_id, version=LATEST_MICROVERSION):
599 resp, body = self.delete("share-transfers/%s" % transfer_id,
600 version=version)
601 self.expected_success(200, resp.status)
602 return rest_client.ResponseBody(resp, body)
603
604 def list_share_transfers(self, detailed=False, params=None,
605 version=LATEST_MICROVERSION):
606 """Get list of share transfers w/o filters."""
607 uri = 'share-transfers/detail' if detailed else 'share-transfers'
608 uri += '?%s' % parse.urlencode(params) if params else ''
609 resp, body = self.get(uri, version=version)
610 self.expected_success(200, resp.status)
611 body = json.loads(body)
612 return rest_client.ResponseBody(resp, body)
613
614 def get_share_transfer(self, transfer_id, version=LATEST_MICROVERSION):
615 resp, body = self.get("share-transfers/%s" % transfer_id,
616 version=version)
617 self.expected_success(200, resp.status)
618 body = json.loads(body)
619 return rest_client.ResponseBody(resp, body)
620
621 def accept_share_transfer(self, transfer_id, auth_key,
622 clear_access_rules=False,
623 version=LATEST_MICROVERSION):
624 post_body = {
625 "accept": {
626 "auth_key": auth_key,
627 "clear_access_rules": clear_access_rules
628 }
629 }
630 body = json.dumps(post_body)
631 resp, body = self.post("share-transfers/%s/accept" % transfer_id,
632 body, version=version)
633 self.expected_success(202, resp.status)
634 return rest_client.ResponseBody(resp, body)
635
636 ###############
637
638 def get_instances_of_share(self, share_id, version=LATEST_MICROVERSION):
639 resp, body = self.get("shares/%s/instances" % share_id,
640 version=version)
641 self.expected_success(200, resp.status)
642 body = json.loads(body)
643 return rest_client.ResponseBody(resp, body)
644
645 def list_share_instances(self, version=LATEST_MICROVERSION,
646 params=None):
647 uri = 'share_instances'
648 uri += '?%s' % parse.urlencode(params) if params else ''
649 resp, body = self.get(uri, version=version)
650 self.expected_success(200, resp.status)
651 body = json.loads(body)
652 return rest_client.ResponseBody(resp, body)
653
654 def get_share_instance(self, instance_id, version=LATEST_MICROVERSION):
655 resp, body = self.get("share_instances/%s" % instance_id,
656 version=version)
657 self.expected_success(200, resp.status)
658 body = json.loads(body)
659 return rest_client.ResponseBody(resp, body)
660
661 def get_share_instance_export_location(
662 self, instance_id, export_location_uuid,
663 version=LATEST_MICROVERSION):
664 resp, body = self.get(
665 "share_instances/%(instance_id)s/export_locations/%(el_uuid)s" % {
666 "instance_id": instance_id, "el_uuid": export_location_uuid},
667 version=version)
668 self.expected_success(200, resp.status)
669 body = json.loads(body)
670 return rest_client.ResponseBody(resp, body)
671
672 def list_share_instance_export_locations(
673 self, instance_id, version=LATEST_MICROVERSION):
674 resp, body = self.get(
675 "share_instances/%s/export_locations" % instance_id,
676 version=version)
677 self.expected_success(200, resp.status)
678 body = json.loads(body)
679 return rest_client.ResponseBody(resp, body)
680
681 ###############
682
683 def extend_share(self, share_id, new_size, version=LATEST_MICROVERSION,
684 action_name=None, force=False):
685 if action_name is None:
686 if utils.is_microversion_gt(version, "2.6"):
687 action_name = 'extend'
688 else:
689 action_name = 'os-extend'
690
691 post_body = {
692 action_name: {
693 "new_size": new_size,
694 }
695 }
696
697 if utils.is_microversion_gt(version, "2.63"):
698 post_body[action_name]["force"] = force
699
700 body = json.dumps(post_body)
701 resp, body = self.post(
702 "shares/%s/action" % share_id, body, version=version)
703 self.expected_success(202, resp.status)
704 return rest_client.ResponseBody(resp, body)
705
706 def shrink_share(self, share_id, new_size, version=LATEST_MICROVERSION,
707 action_name=None):
708 if action_name is None:
709 if utils.is_microversion_gt(version, "2.6"):
710 action_name = 'shrink'
711 else:
712 action_name = 'os-shrink'
713 post_body = {
714 action_name: {
715 "new_size": new_size,
716 }
717 }
718 body = json.dumps(post_body)
719 resp, body = self.post(
720 "shares/%s/action" % share_id, body, version=version)
721 self.expected_success(202, resp.status)
722 return rest_client.ResponseBody(resp, body)
723
724 ###############
725
726 def manage_share(self, service_host, protocol, export_path,
727 share_type_id, name=None, description=None,
728 is_public=False, version=LATEST_MICROVERSION,
729 url=None, share_server_id=None):
730 post_body = {
731 "share": {
732 "export_path": export_path,
733 "service_host": service_host,
734 "protocol": protocol,
735 "share_type": share_type_id,
736 "name": name,
737 "description": description,
738 "is_public": is_public,
739 }
740 }
741 if share_server_id is not None:
742 post_body['share']['share_server_id'] = share_server_id
743 if url is None:
744 if utils.is_microversion_gt(version, "2.6"):
745 url = 'shares/manage'
746 else:
747 url = 'os-share-manage'
748 body = json.dumps(post_body)
749 resp, body = self.post(url, body, version=version)
750 self.expected_success(200, resp.status)
751 body = json.loads(body)
752 return rest_client.ResponseBody(resp, body)
753
754 def unmanage_share(self, share_id, version=LATEST_MICROVERSION, url=None,
755 action_name=None, body=None):
756 if url is None:
757 if utils.is_microversion_gt(version, "2.6"):
758 url = 'shares'
759 else:
760 url = 'os-share-unmanage'
761 if action_name is None:
762 if utils.is_microversion_gt(version, "2.6"):
763 action_name = 'action'
764 else:
765 action_name = 'unmanage'
766 if body is None and utils.is_microversion_gt(version, "2.6"):
767 body = json.dumps({'unmanage': {}})
768 resp, body = self.post(
769 "%(url)s/%(share_id)s/%(action_name)s" % {
770 'url': url, 'share_id': share_id, 'action_name': action_name},
771 body,
772 version=version)
773 self.expected_success(202, resp.status)
774 return rest_client.ResponseBody(resp, body)
775
776 ###############
777
778 def create_snapshot(self, share_id, name=None, description=None,
779 force=False, metadata=None,
780 version=LATEST_MICROVERSION):
781 if name is None:
782 name = data_utils.rand_name("tempest-created-share-snap")
783 if description is None:
784 description = data_utils.rand_name(
785 "tempest-created-share-snap-desc")
786 post_body = {
787 "snapshot": {
788 "name": name,
789 "force": force,
790 "description": description,
791 "share_id": share_id,
792 }
793 }
794 if utils.is_microversion_ge(version, "2.73") and metadata:
795 post_body["snapshot"]["metadata"] = metadata
796
797 body = json.dumps(post_body)
798 resp, body = self.post("snapshots", body, version=version)
799 self.expected_success(202, resp.status)
800 body = json.loads(body)
801 return rest_client.ResponseBody(resp, body)
802
803 def get_snapshot(self, snapshot_id, version=LATEST_MICROVERSION):
804 resp, body = self.get("snapshots/%s" % snapshot_id, version=version)
805 self.expected_success(200, resp.status)
806 body = json.loads(body)
807 return rest_client.ResponseBody(resp, body)
808
809 def list_snapshots(self, detailed=False, params=None,
810 version=LATEST_MICROVERSION):
811 """Get list of share snapshots w/o filters."""
812 uri = 'snapshots/detail' if detailed else 'snapshots'
813 uri += '?%s' % parse.urlencode(params) if params else ''
814 resp, body = self.get(uri, version=version)
815 self.expected_success(200, resp.status)
816 body = json.loads(body)
817 return rest_client.ResponseBody(resp, body)
818
819 def list_snapshots_for_share(self, share_id, detailed=False,
820 version=LATEST_MICROVERSION):
821 """Get list of snapshots for given share."""
822 uri = ('snapshots/detail?share_id=%s' % share_id
823 if detailed else 'snapshots?share_id=%s' % share_id)
824 resp, body = self.get(uri, version=version)
825 self.expected_success(200, resp.status)
826 body = json.loads(body)
827 return rest_client.ResponseBody(resp, body)
828
829 def list_snapshots_with_detail(self, params=None,
830 version=LATEST_MICROVERSION):
831 """Get detailed list of share snapshots w/o filters."""
832 return self.list_snapshots(detailed=True, params=params,
833 version=version)
834
835 def delete_snapshot(self, snap_id, version=LATEST_MICROVERSION):
836 resp, body = self.delete("snapshots/%s" % snap_id, version=version)
837 self.expected_success(202, resp.status)
838 return rest_client.ResponseBody(resp, body)
839
840 def manage_snapshot(self, share_id, provider_location,
841 name=None, description=None,
842 version=LATEST_MICROVERSION,
843 driver_options=None):
844 if name is None:
845 name = data_utils.rand_name("tempest-manage-snapshot")
846 if description is None:
847 description = data_utils.rand_name("tempest-manage-snapshot-desc")
848 post_body = {
849 "snapshot": {
850 "share_id": share_id,
851 "provider_location": provider_location,
852 "name": name,
853 "description": description,
854 "driver_options": driver_options if driver_options else {},
855 }
856 }
857 url = 'snapshots/manage'
858 body = json.dumps(post_body)
859 resp, body = self.post(url, body, version=version)
860 self.expected_success(202, resp.status)
861 body = json.loads(body)
862 return rest_client.ResponseBody(resp, body)
863
864 def unmanage_snapshot(self, snapshot_id, version=LATEST_MICROVERSION,
865 body=None):
866 url = 'snapshots'
867 action_name = 'action'
868 if body is None:
869 body = json.dumps({'unmanage': {}})
870 resp, body = self.post(
871 "%(url)s/%(snapshot_id)s/%(action_name)s" % {
872 'url': url, 'snapshot_id': snapshot_id,
873 'action_name': action_name},
874 body,
875 version=version)
876 self.expected_success(202, resp.status)
877 return rest_client.ResponseBody(resp, body)
878
879 def snapshot_reset_state(self, snapshot_id,
880 status=constants.STATUS_AVAILABLE,
881 version=LATEST_MICROVERSION):
882 return self.reset_state(
883 snapshot_id, status=status, s_type='snapshots', version=version)
884
885 ###############
886
887 def revert_to_snapshot(self, share_id, snapshot_id,
888 version=LATEST_MICROVERSION):
889 url = 'shares/%s/action' % share_id
890 body = json.dumps({'revert': {'snapshot_id': snapshot_id}})
891 resp, body = self.post(url, body, version=version)
892 self.expected_success(202, resp.status)
893 return rest_client.ResponseBody(resp, body)
894
895 ###############
896
897 def create_share_type_extra_specs(self, share_type_id, extra_specs,
898 version=LATEST_MICROVERSION):
899 url = "types/%s/extra_specs" % share_type_id
900 post_body = json.dumps({'extra_specs': extra_specs})
901 resp, body = self.post(url, post_body, version=version)
902 self.expected_success(200, resp.status)
903 body = json.loads(body)
904 return rest_client.ResponseBody(resp, body)
905
906 def get_share_type_extra_spec(self, share_type_id, extra_spec_name,
907 version=LATEST_MICROVERSION):
908 uri = "types/%s/extra_specs/%s" % (share_type_id, extra_spec_name)
909 resp, body = self.get(uri, version=version)
910 self.expected_success(200, resp.status)
911 body = json.loads(body)
912 return rest_client.ResponseBody(resp, body)
913
914 def get_share_type_extra_specs(self, share_type_id, params=None,
915 version=LATEST_MICROVERSION):
916 uri = "types/%s/extra_specs" % share_type_id
917 if params is not None:
918 uri += '?%s' % parse.urlencode(params)
919 resp, body = self.get(uri, version=version)
920 self.expected_success(200, resp.status)
921 body = json.loads(body)
922 return rest_client.ResponseBody(resp, body)
923
924 def update_share_type_extra_spec(self, share_type_id, spec_name,
925 spec_value, version=LATEST_MICROVERSION):
926 uri = "types/%s/extra_specs/%s" % (share_type_id, spec_name)
927 extra_spec = {spec_name: spec_value}
928 post_body = json.dumps(extra_spec)
929 resp, body = self.put(uri, post_body, version=version)
930 self.expected_success(200, resp.status)
931 body = json.loads(body)
932 return rest_client.ResponseBody(resp, body)
933
934 def update_share_type_extra_specs(self, share_type_id, extra_specs,
935 version=LATEST_MICROVERSION):
936 uri = "types/%s/extra_specs" % share_type_id
937 extra_specs = {"extra_specs": extra_specs}
938 post_body = json.dumps(extra_specs)
939 resp, body = self.post(uri, post_body, version=version)
940 self.expected_success(200, resp.status)
941 body = json.loads(body)
942 return rest_client.ResponseBody(resp, body)
943
944 def delete_share_type_extra_spec(self, share_type_id, extra_spec_name,
945 version=LATEST_MICROVERSION):
946 uri = "types/%s/extra_specs/%s" % (share_type_id, extra_spec_name)
947 resp, body = self.delete(uri, version=version)
948 self.expected_success(202, resp.status)
949 return rest_client.ResponseBody(resp, body)
950
951 ###############
952
953 def show_share_server(self, share_server_id, version=LATEST_MICROVERSION):
954 """Get share server info."""
955 uri = "share-servers/%s" % share_server_id
956 resp, body = self.get(uri, version=version)
957 self.expected_success(200, resp.status)
958 body = json.loads(body)
959 return rest_client.ResponseBody(resp, body)
960
961 ###############
962
963 def get_snapshot_instance(self, instance_id, version=LATEST_MICROVERSION):
964 resp, body = self.get("snapshot-instances/%s" % instance_id,
965 version=version)
966 self.expected_success(200, resp.status)
967 body = json.loads(body)
968 return rest_client.ResponseBody(resp, body)
969
970 def list_snapshot_instances(self, detail=False, snapshot_id=None,
971 version=LATEST_MICROVERSION):
972 """Get list of share snapshot instances."""
973 uri = "snapshot-instances%s" % ('/detail' if detail else '')
974 if snapshot_id is not None:
975 uri += '?snapshot_id=%s' % snapshot_id
976 resp, body = self.get(uri, version=version)
977 self.expected_success(200, resp.status)
978 body = json.loads(body)
979 return rest_client.ResponseBody(resp, body)
980
981 def reset_snapshot_instance_status(self, instance_id,
982 status=constants.STATUS_AVAILABLE,
983 version=LATEST_MICROVERSION):
984 """Reset the status."""
985 uri = 'snapshot-instances/%s/action' % instance_id
986 post_body = {
987 'reset_status': {
988 'status': status
989 }
990 }
991 body = json.dumps(post_body)
992 resp, body = self.post(uri, body, extra_headers=True, version=version)
993 self.expected_success(202, resp.status)
994 return rest_client.ResponseBody(resp, body)
995
996 def get_snapshot_instance_export_location(
997 self, instance_id, export_location_uuid,
998 version=LATEST_MICROVERSION):
999 resp, body = self.get(
1000 "snapshot-instances/%(instance_id)s/export-locations/%("
1001 "el_uuid)s" % {
1002 "instance_id": instance_id,
1003 "el_uuid": export_location_uuid},
1004 version=version)
1005 self.expected_success(200, resp.status)
1006 body = json.loads(body)
1007 return rest_client.ResponseBody(resp, body)
1008
1009 def list_snapshot_instance_export_locations(
1010 self, instance_id, version=LATEST_MICROVERSION):
1011 resp, body = self.get(
1012 "snapshot-instances/%s/export-locations" % instance_id,
1013 version=version)
1014 self.expected_success(200, resp.status)
1015 body = json.loads(body)
1016 return rest_client.ResponseBody(resp, body)
1017
1018 ###############
1019
1020 def _get_access_action_name(self, version, action):
1021 if utils.is_microversion_gt(version, "2.6"):
1022 return action.split('os-')[-1]
1023 return action
1024
1025 def create_access_rule(self, share_id, access_type="ip",
1026 access_to="0.0.0.0", access_level=None,
1027 version=LATEST_MICROVERSION, metadata=None,
1028 action_name=None, lock_visibility=False,
1029 lock_deletion=False):
1030 post_body = {
1031 self._get_access_action_name(version, 'os-allow_access'): {
1032 "access_type": access_type,
1033 "access_to": access_to,
1034 "access_level": access_level,
1035 }
1036 }
1037 if metadata is not None:
1038 post_body['allow_access']['metadata'] = metadata
1039 if lock_visibility:
1040 post_body['allow_access']['lock_visibility'] = True
1041 if lock_deletion:
1042 post_body['allow_access']['lock_deletion'] = True
1043 body = json.dumps(post_body)
1044 resp, body = self.post(
1045 "shares/%s/action" % share_id, body, version=version,
1046 extra_headers=True)
1047 self.expected_success(200, resp.status)
1048 body = json.loads(body)
1049 return rest_client.ResponseBody(resp, body)
1050
1051 def list_access_rules(self, share_id, version=LATEST_MICROVERSION,
1052 metadata=None, action_name=None):
1053 if utils.is_microversion_lt(version, "2.45"):
1054 body = {
1055 self._get_access_action_name(version, 'os-access_list'): None
1056 }
1057 resp, body = self.post(
1058 "shares/%s/action" % share_id, json.dumps(body),
1059 version=version)
1060 self.expected_success(200, resp.status)
1061 else:
1062 return self.list_access_rules_with_new_API(
1063 share_id, metadata=metadata, version=version,
1064 action_name=action_name)
1065 body = json.loads(body)
1066 return rest_client.ResponseBody(resp, body)
1067
1068 def list_access_rules_with_new_API(self, share_id, metadata=None,
1069 version=LATEST_MICROVERSION,
1070 action_name=None):
1071 metadata = metadata or {}
1072 query_string = ''
1073
1074 params = sorted(
1075 [(k, v) for (k, v) in list(metadata.items()) if v])
1076 if params:
1077 query_string = "&%s" % parse.urlencode(params)
1078
1079 url = 'share-access-rules?share_id=%s' % share_id + query_string
1080 resp, body = self.get(url, version=version)
1081 self.expected_success(200, resp.status)
1082 body = json.loads(body)
1083 return rest_client.ResponseBody(resp, body)
1084
1085 def delete_access_rule(self, share_id, rule_id,
1086 version=LATEST_MICROVERSION, action_name=None,
1087 unrestrict=False):
1088 post_body = {
1089 self._get_access_action_name(version, 'os-deny_access'): {
1090 "access_id": rule_id,
1091 }
1092 }
1093 if unrestrict:
1094 post_body['deny_access']['unrestrict'] = True
1095 body = json.dumps(post_body)
1096 resp, body = self.post(
1097 "shares/%s/action" % share_id, body, version=version)
1098 self.expected_success(202, resp.status)
1099 return rest_client.ResponseBody(resp, body)
1100
1101 def get_access_rule(self, access_id, version=LATEST_MICROVERSION):
1102 resp, body = self.get("share-access-rules/%s" % access_id,
1103 version=version)
1104 self.expected_success(200, resp.status)
1105 body = json.loads(body)
1106 return rest_client.ResponseBody(resp, body)
1107
1108 def update_access_rule(self, access_id, access_level,
1109 version=LATEST_MICROVERSION):
1110 url = 'share-access-rules/%s' % access_id
1111 body = {'update_access': {"access_level": access_level}}
1112 resp, body = self.put(url, json.dumps(body), version=version)
1113 self.expected_success(200, resp.status)
1114 body = json.loads(body)
1115 return rest_client.ResponseBody(resp, body)
1116
1117 def update_access_metadata(self, access_id, metadata,
1118 version=LATEST_MICROVERSION):
1119 url = 'share-access-rules/%s/metadata' % access_id
1120 body = {"metadata": metadata}
1121 resp, body = self.put(url, json.dumps(body), version=version)
1122 self.expected_success(200, resp.status)
1123 body = json.loads(body)
1124 return rest_client.ResponseBody(resp, body)
1125
1126 def delete_access_metadata(self, access_id, key,
1127 version=LATEST_MICROVERSION):
1128 url = "share-access-rules/%s/metadata/%s" % (access_id, key)
1129 resp, body = self.delete(url, version=version)
1130 self.expected_success(200, resp.status)
1131 return rest_client.ResponseBody(resp, body)
1132
1133 ###############
1134
1135 def list_availability_zones(self, url='availability-zones',
1136 version=LATEST_MICROVERSION):
1137 """Get list of availability zones."""
1138 if url is None:
1139 if utils.is_microversion_gt(version, "2.6"):
1140 url = 'availability-zones'
1141 else:
1142 url = 'os-availability-zone'
1143 resp, body = self.get(url, version=version)
1144 self.expected_success(200, resp.status)
1145 body = json.loads(body)
1146 return rest_client.ResponseBody(resp, body)
1147
1148 ###############
1149
1150 def list_services(self, params=None, url=None,
1151 version=LATEST_MICROVERSION):
1152 """List services."""
1153 if url is None:
1154 if utils.is_microversion_gt(version, "2.6"):
1155 url = 'services'
1156 else:
1157 url = 'os-services'
1158 if params:
1159 url += '?%s' % parse.urlencode(params)
1160 resp, body = self.get(url, version=version)
1161 self.expected_success(200, resp.status)
1162 body = json.loads(body)
1163 return rest_client.ResponseBody(resp, body)
1164
1165 ###############
1166
1167 def list_share_types(self, params=None, default=False,
1168 version=LATEST_MICROVERSION):
1169 uri = 'types'
1170 if default:
1171 uri += '/default'
1172 if params is not None:
1173 uri += '?%s' % parse.urlencode(params)
1174 resp, body = self.get(uri, version=version)
1175 self.expected_success(200, resp.status)
1176 body = json.loads(body)
1177 return rest_client.ResponseBody(resp, body)
1178
1179 def create_share_type(self, name, is_public=True,
1180 version=LATEST_MICROVERSION, **kwargs):
1181 if utils.is_microversion_gt(version, "2.6"):
1182 is_public_keyname = 'share_type_access:is_public'
1183 else:
1184 is_public_keyname = 'os-share-type-access:is_public'
1185 post_body = {
1186 'name': name,
1187 'extra_specs': kwargs.get('extra_specs'),
1188 is_public_keyname: is_public,
1189 }
1190 if kwargs.get('description'):
1191 post_body['description'] = kwargs.get('description')
1192 post_body = json.dumps({'share_type': post_body})
1193 resp, body = self.post('types', post_body, version=version)
1194 self.expected_success(200, resp.status)
1195 body = json.loads(body)
1196 return rest_client.ResponseBody(resp, body)
1197
1198 def update_share_type(self, share_type_id, name=None,
1199 is_public=None, description=None,
1200 version=LATEST_MICROVERSION):
1201 post_body = {}
1202 if is_public is not None:
1203 post_body.update({"share_type_access:is_public": is_public})
1204 if name is not None:
1205 post_body.update({"name": name})
1206 if description is not None:
1207 post_body.update({"description": description})
1208 post_body = json.dumps({'share_type': post_body})
1209 resp, body = self.put("types/%s" % share_type_id, post_body,
1210 version=version)
1211 self.expected_success(200, resp.status)
1212 body = json.loads(body)
1213 return rest_client.ResponseBody(resp, body)
1214
1215 def delete_share_type(self, share_type_id, version=LATEST_MICROVERSION):
1216 resp, body = self.delete("types/%s" % share_type_id, version=version)
1217 self.expected_success(202, resp.status)
1218 return rest_client.ResponseBody(resp, body)
1219
1220 def get_share_type(self, share_type_id, version=LATEST_MICROVERSION):
1221 resp, body = self.get("types/%s" % share_type_id, version=version)
1222 self.expected_success(200, resp.status)
1223 body = json.loads(body)
1224 return rest_client.ResponseBody(resp, body)
1225
1226 def list_access_to_share_type(self, share_type_id,
1227 version=LATEST_MICROVERSION,
1228 action_name=None):
1229 if action_name is None:
1230 if utils.is_microversion_gt(version, "2.6"):
1231 action_name = 'share_type_access'
1232 else:
1233 action_name = 'os-share-type-access'
1234 url = 'types/%(st_id)s/%(action_name)s' % {
1235 'st_id': share_type_id, 'action_name': action_name}
1236 resp, body = self.get(url, version=version)
1237 # [{"share_type_id": "%st_id%", "project_id": "%project_id%"}, ]
1238 self.expected_success(200, resp.status)
1239 body = json.loads(body)
1240 return rest_client.ResponseBody(resp, body)
1241
1242 ###############
1243
1244 @staticmethod
1245 def _get_quotas_url(version):
1246 if utils.is_microversion_gt(version, "2.6"):
1247 return 'quota-sets'
1248 return 'os-quota-sets'
1249
1250 @staticmethod
1251 def _get_quotas_url_arguments_as_str(user_id=None, share_type=None):
1252 args_str = ''
1253 if not (user_id is None or share_type is None):
1254 args_str = "?user_id=%s&share_type=%s" % (user_id, share_type)
1255 elif user_id is not None:
1256 args_str = "?user_id=%s" % user_id
1257 elif share_type is not None:
1258 args_str = "?share_type=%s" % share_type
1259 return args_str
1260
1261 def default_quotas(self, tenant_id, url=None, version=LATEST_MICROVERSION):
1262 if url is None:
1263 url = self._get_quotas_url(version)
1264 url += '/%s' % tenant_id
1265 resp, body = self.get("%s/defaults" % url, version=version)
1266 self.expected_success(200, resp.status)
1267 body = json.loads(body)
1268 return rest_client.ResponseBody(resp, body)
1269
1270 def show_quotas(self, tenant_id, user_id=None, share_type=None, url=None,
1271 version=LATEST_MICROVERSION):
1272 if url is None:
1273 url = self._get_quotas_url(version)
1274 url += '/%s' % tenant_id
1275 url += self._get_quotas_url_arguments_as_str(user_id, share_type)
1276 resp, body = self.get(url, version=version)
1277 self.expected_success(200, resp.status)
1278 body = json.loads(body)
1279 return rest_client.ResponseBody(resp, body)
1280
1281 def reset_quotas(self, tenant_id, user_id=None, share_type=None, url=None,
1282 version=LATEST_MICROVERSION):
1283 if url is None:
1284 url = self._get_quotas_url(version)
1285 url += '/%s' % tenant_id
1286 url += self._get_quotas_url_arguments_as_str(user_id, share_type)
1287 resp, body = self.delete(url, version=version)
1288 self.expected_success(202, resp.status)
1289 return rest_client.ResponseBody(resp, body)
1290
1291 def detail_quotas(self, tenant_id, user_id=None, share_type=None, url=None,
1292 version=LATEST_MICROVERSION):
1293 if url is None:
1294 url = self._get_quotas_url(version)
1295 url += '/%s/detail' % tenant_id
1296 url += self._get_quotas_url_arguments_as_str(user_id, share_type)
1297 resp, body = self.get(url, version=version)
1298 self.expected_success(200, resp.status)
1299 body = json.loads(body)
1300 return rest_client.ResponseBody(resp, body)
1301
1302 def update_quotas(self, tenant_id, user_id=None, shares=None,
1303 snapshots=None, gigabytes=None, snapshot_gigabytes=None,
1304 share_networks=None,
1305 share_groups=None, share_group_snapshots=None,
1306 force=True, share_type=None, share_replicas=None,
1307 replica_gigabytes=None, encryption_keys=None, url=None,
1308 version=LATEST_MICROVERSION):
1309 if url is None:
1310 url = self._get_quotas_url(version)
1311 url += '/%s' % tenant_id
1312 url += self._get_quotas_url_arguments_as_str(user_id, share_type)
1313
1314 put_body = {"tenant_id": tenant_id}
1315 if force:
1316 put_body["force"] = "true"
1317 if shares is not None:
1318 put_body["shares"] = shares
1319 if snapshots is not None:
1320 put_body["snapshots"] = snapshots
1321 if gigabytes is not None:
1322 put_body["gigabytes"] = gigabytes
1323 if snapshot_gigabytes is not None:
1324 put_body["snapshot_gigabytes"] = snapshot_gigabytes
1325 if share_networks is not None:
1326 put_body["share_networks"] = share_networks
1327 if share_groups is not None:
1328 put_body["share_groups"] = share_groups
1329 if share_group_snapshots is not None:
1330 put_body["share_group_snapshots"] = share_group_snapshots
1331 if share_replicas is not None:
1332 put_body["share_replicas"] = share_replicas
1333 if replica_gigabytes is not None:
1334 put_body["replica_gigabytes"] = replica_gigabytes
1335 if encryption_keys is not None:
1336 put_body["encryption_keys"] = encryption_keys
1337 put_body = json.dumps({"quota_set": put_body})
1338
1339 resp, body = self.put(url, put_body, version=version)
1340 self.expected_success(200, resp.status)
1341 body = json.loads(body)
1342 return rest_client.ResponseBody(resp, body)
1343
1344 ###############
1345
1346 def create_share_group(self, name=None, description=None,
1347 share_group_type_id=None, share_type_ids=(),
1348 share_network_id=None,
1349 source_share_group_snapshot_id=None,
1350 availability_zone=None,
1351 version=LATEST_MICROVERSION):
1352 """Create a new share group."""
1353 uri = 'share-groups'
1354 post_body = {}
1355 headers, extra_headers = utils.get_extra_headers(
1356 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1357 if name:
1358 post_body['name'] = name
1359 if description:
1360 post_body['description'] = description
1361 if share_group_type_id:
1362 post_body['share_group_type_id'] = share_group_type_id
1363 if share_type_ids:
1364 post_body['share_types'] = share_type_ids
1365 if source_share_group_snapshot_id:
1366 post_body['source_share_group_snapshot_id'] = (
1367 source_share_group_snapshot_id)
1368 if share_network_id:
1369 post_body['share_network_id'] = share_network_id
1370 if availability_zone:
1371 post_body['availability_zone'] = availability_zone
1372 body = json.dumps({'share_group': post_body})
1373
1374 resp, body = self.post(uri, body, headers=headers,
1375 extra_headers=extra_headers, version=version)
1376
1377 self.expected_success(202, resp.status)
1378 body = json.loads(body)
1379 return rest_client.ResponseBody(resp, body)
1380
1381 def delete_share_group(self, share_group_id, version=LATEST_MICROVERSION):
1382 """Delete a share group."""
1383 uri = 'share-groups/%s' % share_group_id
1384 headers, extra_headers = utils.get_extra_headers(
1385 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1386 resp, body = self.delete(uri, headers=headers,
1387 extra_headers=extra_headers, version=version)
1388 self.expected_success(202, resp.status)
1389 return rest_client.ResponseBody(resp, body)
1390
1391 def list_share_groups(self, detailed=False, params=None,
1392 version=LATEST_MICROVERSION):
1393 """Get list of share groups w/o filters."""
1394 uri = 'share-groups%s' % ('/detail' if detailed else '')
1395 uri += '?%s' % (parse.urlencode(params) if params else '')
1396 headers, extra_headers = utils.get_extra_headers(
1397 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1398 resp, body = self.get(uri, headers=headers,
1399 extra_headers=extra_headers, version=version)
1400 self.expected_success(200, resp.status)
1401 body = json.loads(body)
1402 return rest_client.ResponseBody(resp, body)
1403
1404 def get_share_group(self, share_group_id, version=LATEST_MICROVERSION):
1405 """Get share group info."""
1406 uri = 'share-groups/%s' % share_group_id
1407 headers, extra_headers = utils.get_extra_headers(
1408 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1409 resp, body = self.get(uri, headers=headers,
1410 extra_headers=extra_headers, version=version)
1411 self.expected_success(200, resp.status)
1412 body = json.loads(body)
1413 return rest_client.ResponseBody(resp, body)
1414
1415 def update_share_group(self, share_group_id, name=None, description=None,
1416 version=LATEST_MICROVERSION, **kwargs):
1417 """Update an existing share group."""
1418 uri = 'share-groups/%s' % share_group_id
1419 headers, extra_headers = utils.get_extra_headers(
1420 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1421 post_body = {}
1422 if name:
1423 post_body['name'] = name
1424 if description:
1425 post_body['description'] = description
1426 if kwargs:
1427 post_body.update(kwargs)
1428 body = json.dumps({'share_group': post_body})
1429
1430 resp, body = self.put(uri, body, headers=headers,
1431 extra_headers=extra_headers, version=version)
1432
1433 self.expected_success(200, resp.status)
1434 body = json.loads(body)
1435 return rest_client.ResponseBody(resp, body)
1436
1437 def share_group_reset_state(self, share_group_id, status='error',
1438 version=LATEST_MICROVERSION):
1439 headers, _junk = utils.get_extra_headers(
1440 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1441 return self.reset_state(
1442 share_group_id, status=status, s_type='share-groups',
1443 headers=headers, version=version)
1444
1445 def share_group_force_delete(self, share_group_id,
1446 version=LATEST_MICROVERSION):
1447 headers, _junk = utils.get_extra_headers(
1448 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1449 return self.force_delete(
1450 share_group_id, s_type='share-groups', headers=headers,
1451 version=version)
1452
1453 ###############
1454
1455 def create_share_group_type(self, name=None, share_types=(),
1456 is_public=None, group_specs=None,
1457 version=LATEST_MICROVERSION):
1458 """Create a new share group type."""
1459 uri = 'share-group-types'
1460 headers, extra_headers = utils.get_extra_headers(
1461 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1462 post_body = {}
1463 if isinstance(share_types, (tuple, list)):
1464 share_types = list(share_types)
1465 else:
1466 share_types = [share_types]
1467 if name is not None:
1468 post_body['name'] = name
1469 if share_types:
1470 post_body['share_types'] = share_types
1471 if is_public is not None:
1472 post_body['is_public'] = is_public
1473 if group_specs:
1474 post_body['group_specs'] = group_specs
1475 body = json.dumps({'share_group_type': post_body})
1476 resp, body = self.post(uri, body, headers=headers,
1477 extra_headers=extra_headers, version=version)
1478 self.expected_success(200, resp.status)
1479 body = json.loads(body)
1480 return rest_client.ResponseBody(resp, body)
1481
1482 def list_share_group_types(self, detailed=False, params=None,
1483 version=LATEST_MICROVERSION):
1484 """Get list of share group types."""
1485 uri = 'share-group-types%s' % ('/detail' if detailed else '')
1486 uri += '?%s' % (parse.urlencode(params) if params else '')
1487 headers, extra_headers = utils.get_extra_headers(
1488 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1489 resp, body = self.get(uri, headers=headers,
1490 extra_headers=extra_headers, version=version)
1491 self.expected_success(200, resp.status)
1492 body = json.loads(body)
1493 return rest_client.ResponseBody(resp, body)
1494
1495 def get_share_group_type(self, share_group_type_id,
1496 version=LATEST_MICROVERSION):
1497 """Get share group type info."""
1498 uri = 'share-group-types/%s' % share_group_type_id
1499 headers, extra_headers = utils.get_extra_headers(
1500 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1501 resp, body = self.get(uri, headers=headers,
1502 extra_headers=extra_headers, version=version)
1503 self.expected_success(200, resp.status)
1504 body = json.loads(body)
1505 return rest_client.ResponseBody(resp, body)
1506
1507 def get_default_share_group_type(self, version=LATEST_MICROVERSION):
1508 """Get default share group type info."""
1509 uri = 'share-group-types/default'
1510 headers, extra_headers = utils.get_extra_headers(
1511 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1512 resp, body = self.get(uri, headers=headers,
1513 extra_headers=extra_headers, version=version)
1514 self.expected_success(200, resp.status)
1515 body = json.loads(body)
1516 return rest_client.ResponseBody(resp, body)
1517
1518 def delete_share_group_type(self, share_group_type_id,
1519 version=LATEST_MICROVERSION):
1520 """Delete an existing share group type."""
1521 uri = 'share-group-types/%s' % share_group_type_id
1522 headers, extra_headers = utils.get_extra_headers(
1523 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1524 resp, body = self.delete(uri, headers=headers,
1525 extra_headers=extra_headers, version=version)
1526 self.expected_success(204, resp.status)
1527 return rest_client.ResponseBody(resp, body)
1528
1529 def add_access_to_share_group_type(self, share_group_type_id, project_id,
1530 version=LATEST_MICROVERSION):
1531 uri = 'share-group-types/%s/action' % share_group_type_id
1532 headers, extra_headers = utils.get_extra_headers(
1533 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1534 post_body = {'project': project_id}
1535 post_body = json.dumps({'addProjectAccess': post_body})
1536 resp, body = self.post(uri, post_body, headers=headers,
1537 extra_headers=extra_headers, version=version)
1538 self.expected_success(202, resp.status)
1539 return rest_client.ResponseBody(resp, body)
1540
1541 def remove_access_from_share_group_type(self, share_group_type_id,
1542 project_id,
1543 version=LATEST_MICROVERSION):
1544 uri = 'share-group-types/%s/action' % share_group_type_id
1545 headers, extra_headers = utils.get_extra_headers(
1546 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1547 post_body = {'project': project_id}
1548 post_body = json.dumps({'removeProjectAccess': post_body})
1549 resp, body = self.post(uri, post_body, headers=headers,
1550 extra_headers=extra_headers, version=version)
1551 self.expected_success(202, resp.status)
1552 return rest_client.ResponseBody(resp, body)
1553
1554 def list_access_to_share_group_type(self, share_group_type_id,
1555 version=LATEST_MICROVERSION):
1556 uri = 'share-group-types/%s/access' % share_group_type_id
1557 headers, extra_headers = utils.get_extra_headers(
1558 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1559 resp, body = self.get(uri, headers=headers,
1560 extra_headers=extra_headers, version=version)
1561 self.expected_success(200, resp.status)
1562 body = json.loads(body)
1563 return rest_client.ResponseBody(resp, body)
1564
1565 ###############
1566
1567 def create_share_group_type_specs(self, share_group_type_id,
1568 group_specs_dict,
1569 version=LATEST_MICROVERSION):
1570 url = "share-group-types/%s/group-specs" % share_group_type_id
1571 headers, extra_headers = utils.get_extra_headers(
1572 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1573 post_body = json.dumps({'group_specs': group_specs_dict})
1574 resp, body = self.post(url, post_body, headers=headers,
1575 extra_headers=extra_headers, version=version)
1576 self.expected_success(200, resp.status)
1577 body = json.loads(body)
1578 return rest_client.ResponseBody(resp, body)
1579
1580 def get_share_group_type_spec(self, share_group_type_id, group_spec_key,
1581 version=LATEST_MICROVERSION):
1582 uri = "share-group-types/%s/group-specs/%s" % (
1583 share_group_type_id, group_spec_key)
1584 headers, extra_headers = utils.get_extra_headers(
1585 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1586 resp, body = self.get(uri, headers=headers,
1587 extra_headers=extra_headers, version=version)
1588 self.expected_success(200, resp.status)
1589 body = json.loads(body)
1590 return rest_client.ResponseBody(resp, body)
1591
1592 def get_share_group_type_specs(self, share_group_type_id, params=None,
1593 version=LATEST_MICROVERSION):
1594 uri = "share-group-types/%s/group-specs" % share_group_type_id
1595 headers, extra_headers = utils.get_extra_headers(
1596 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1597 if params is not None:
1598 uri += '?%s' % parse.urlencode(params)
1599 resp, body = self.get(uri, headers=headers,
1600 extra_headers=extra_headers, version=version)
1601 self.expected_success(200, resp.status)
1602 body = json.loads(body)
1603 return rest_client.ResponseBody(resp, body)
1604
1605 def update_share_group_type_spec(self, share_group_type_id, group_spec_key,
1606 group_spec_value,
1607 version=LATEST_MICROVERSION):
1608 uri = "share-group-types/%s/group-specs/%s" % (
1609 share_group_type_id, group_spec_key)
1610 headers, extra_headers = utils.get_extra_headers(
1611 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1612 group_spec = {group_spec_key: group_spec_value}
1613 post_body = json.dumps(group_spec)
1614 resp, body = self.put(uri, post_body, headers=headers,
1615 extra_headers=extra_headers, version=version)
1616 self.expected_success(200, resp.status)
1617 body = json.loads(body)
1618 return rest_client.ResponseBody(resp, body)
1619
1620 def update_share_group_type_specs(self, share_group_type_id,
1621 group_specs_dict,
1622 version=LATEST_MICROVERSION):
1623 return self.create_share_group_type_specs(
1624 share_group_type_id, group_specs_dict, version=version)
1625
1626 def delete_share_group_type_spec(self, share_group_type_id, group_spec_key,
1627 version=LATEST_MICROVERSION):
1628 uri = "share-group-types/%s/group-specs/%s" % (
1629 share_group_type_id, group_spec_key)
1630 headers, extra_headers = utils.get_extra_headers(
1631 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1632 resp, body = self.delete(uri, headers=headers,
1633 extra_headers=extra_headers, version=version)
1634 self.expected_success(204, resp.status)
1635 return rest_client.ResponseBody(resp, body)
1636
1637 ###############
1638
1639 def create_share_group_snapshot(self, share_group_id, name=None,
1640 description=None,
1641 version=LATEST_MICROVERSION):
1642 """Create a new share group snapshot of an existing share group."""
1643 uri = 'share-group-snapshots'
1644 headers, extra_headers = utils.get_extra_headers(
1645 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1646 post_body = {'share_group_id': share_group_id}
1647 if name:
1648 post_body['name'] = name
1649 if description:
1650 post_body['description'] = description
1651 body = json.dumps({'share_group_snapshot': post_body})
1652 resp, body = self.post(uri, body, headers=headers,
1653 extra_headers=extra_headers, version=version)
1654 self.expected_success(202, resp.status)
1655 body = json.loads(body)
1656 return rest_client.ResponseBody(resp, body)
1657
1658 def delete_share_group_snapshot(self, share_group_snapshot_id,
1659 version=LATEST_MICROVERSION):
1660 """Delete an existing share group snapshot."""
1661 uri = 'share-group-snapshots/%s' % share_group_snapshot_id
1662 headers, extra_headers = utils.get_extra_headers(
1663 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1664 resp, body = self.delete(uri, headers=headers,
1665 extra_headers=extra_headers, version=version)
1666 self.expected_success(202, resp.status)
1667 return rest_client.ResponseBody(resp, body)
1668
1669 def list_share_group_snapshots(self, detailed=False, params=None,
1670 version=LATEST_MICROVERSION):
1671 """Get list of share group snapshots w/o filters."""
1672 uri = 'share-group-snapshots%s' % ('/detail' if detailed else '')
1673 uri += '?%s' % (parse.urlencode(params) if params else '')
1674 headers, extra_headers = utils.get_extra_headers(
1675 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1676 resp, body = self.get(uri, headers=headers,
1677 extra_headers=extra_headers, version=version)
1678 self.expected_success(200, resp.status)
1679 body = json.loads(body)
1680 return rest_client.ResponseBody(resp, body)
1681
1682 def get_share_group_snapshot(self, share_group_snapshot_id,
1683 version=LATEST_MICROVERSION):
1684 """Get share group snapshot info."""
1685 uri = 'share-group-snapshots/%s' % share_group_snapshot_id
1686 headers, extra_headers = utils.get_extra_headers(
1687 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1688 resp, body = self.get(uri, headers=headers,
1689 extra_headers=extra_headers, version=version)
1690 self.expected_success(200, resp.status)
1691 body = json.loads(body)
1692 return rest_client.ResponseBody(resp, body)
1693
1694 def update_share_group_snapshot(self, share_group_snapshot_id, name=None,
1695 description=None,
1696 version=LATEST_MICROVERSION):
1697 """Update an existing share group snapshot."""
1698 uri = 'share-group-snapshots/%s' % share_group_snapshot_id
1699 headers, extra_headers = utils.get_extra_headers(
1700 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1701 post_body = {}
1702 if name:
1703 post_body['name'] = name
1704 if description:
1705 post_body['description'] = description
1706 body = json.dumps({'share_group_snapshot': post_body})
1707 resp, body = self.put(uri, body, headers=headers,
1708 extra_headers=extra_headers, version=version)
1709 self.expected_success(200, resp.status)
1710 body = json.loads(body)
1711 return rest_client.ResponseBody(resp, body)
1712
1713 def share_group_snapshot_reset_state(self, share_group_snapshot_id,
1714 status='error',
1715 version=LATEST_MICROVERSION):
1716 headers, _junk = utils.get_extra_headers(
1717 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1718 return self.reset_state(
1719 share_group_snapshot_id, status=status, headers=headers,
1720 s_type='group-snapshots', version=version)
1721
1722 def share_group_snapshot_force_delete(self, share_group_snapshot_id,
1723 version=LATEST_MICROVERSION):
1724 headers, _junk = utils.get_extra_headers(
1725 version, constants.SHARE_GROUPS_GRADUATION_VERSION)
1726 return self.force_delete(
1727 share_group_snapshot_id, s_type='share-group-snapshots',
1728 headers=headers, version=version)
1729
1730 ###############
1731
1732 def manage_share_server(self, host, share_network_id, identifier,
1733 driver_options=None, version=LATEST_MICROVERSION,
1734 share_network_subnet_id=None):
1735 body = {
1736 'share_server': {
1737 'host': host,
1738 'share_network_id': share_network_id,
1739 'identifier': identifier,
1740 'driver_options': driver_options if driver_options else {},
1741 }
1742 }
1743 if share_network_subnet_id:
1744 body['share_server']['share_network_subnet_id'] = (
1745 share_network_subnet_id)
1746
1747 body = json.dumps(body)
1748 resp, body = self.post('share-servers/manage', body,
1749 extra_headers=True, version=version)
1750 self.expected_success(202, resp.status)
1751 body = json.loads(body)
1752 return rest_client.ResponseBody(resp, body)
1753
1754 def unmanage_share_server(self, share_server_id,
1755 version=LATEST_MICROVERSION):
1756 body = json.dumps({'unmanage': None})
1757 resp, body = self.post('share-servers/%s/action' % share_server_id,
1758 body, extra_headers=True, version=version)
1759 self.expected_success(202, resp.status)
1760 return rest_client.ResponseBody(resp, body)
1761
1762 def share_server_reset_state(self, share_server_id,
1763 status=constants.SERVER_STATE_ACTIVE,
1764 version=LATEST_MICROVERSION):
1765 return self.reset_state(
1766 share_server_id, status=status, s_type='share-servers',
1767 version=version)
1768
1769 ###############
1770
1771 def migrate_share(self, share_id, host,
1772 force_host_assisted_migration=False,
1773 new_share_network_id=None, writable=False,
1774 preserve_metadata=False, preserve_snapshots=False,
1775 nondisruptive=False, new_share_type_id=None,
1776 version=LATEST_MICROVERSION):
1777
1778 body = {
1779 'migration_start': {
1780 'host': host,
1781 'force_host_assisted_migration': force_host_assisted_migration,
1782 'new_share_network_id': new_share_network_id,
1783 'new_share_type_id': new_share_type_id,
1784 'writable': writable,
1785 'preserve_metadata': preserve_metadata,
1786 'preserve_snapshots': preserve_snapshots,
1787 'nondisruptive': nondisruptive,
1788 }
1789 }
1790
1791 body = json.dumps(body)
1792 resp, body = self.post('shares/%s/action' % share_id, body,
1793 headers=EXPERIMENTAL, extra_headers=True,
1794 version=version)
1795 return rest_client.ResponseBody(resp, body)
1796
1797 def migration_complete(self, share_id, version=LATEST_MICROVERSION,
1798 action_name='migration_complete'):
1799 post_body = {
1800 action_name: None,
1801 }
1802 body = json.dumps(post_body)
1803 resp, body = self.post('shares/%s/action' % share_id, body,
1804 headers=EXPERIMENTAL, extra_headers=True,
1805 version=version)
1806 return rest_client.ResponseBody(resp, body)
1807
1808 def migration_cancel(self, share_id, version=LATEST_MICROVERSION,
1809 action_name='migration_cancel'):
1810 post_body = {
1811 action_name: None,
1812 }
1813 body = json.dumps(post_body)
1814 resp, body = self.post('shares/%s/action' % share_id, body,
1815 headers=EXPERIMENTAL, extra_headers=True,
1816 version=version)
1817 return rest_client.ResponseBody(resp, body)
1818
1819 def migration_get_progress(self, share_id, version=LATEST_MICROVERSION,
1820 action_name='migration_get_progress'):
1821 post_body = {
1822 action_name: None,
1823 }
1824 body = json.dumps(post_body)
1825 resp, body = self.post('shares/%s/action' % share_id, body,
1826 headers=EXPERIMENTAL, extra_headers=True,
1827 version=version)
1828 body = json.loads(body)
1829 return rest_client.ResponseBody(resp, body)
1830
1831 def reset_task_state(
1832 self, share_id, task_state, version=LATEST_MICROVERSION,
1833 action_name='reset_task_state'):
1834 post_body = {
1835 action_name: {
1836 'task_state': task_state,
1837 }
1838 }
1839 body = json.dumps(post_body)
1840 resp, body = self.post('shares/%s/action' % share_id, body,
1841 headers=EXPERIMENTAL, extra_headers=True,
1842 version=version)
1843 return rest_client.ResponseBody(resp, body)
1844
1845 def create_share_replica(self, share_id, availability_zone=None,
1846 scheduler_hints=None, share_network_id=None,
1847 version=LATEST_MICROVERSION):
1848 """Add a share replica of an existing share."""
1849 uri = "share-replicas"
1850 post_body = {
1851 'share_id': share_id,
1852 'availability_zone': availability_zone,
1853 }
1854
1855 if scheduler_hints:
1856 post_body["scheduler_hints"] = scheduler_hints
1857 if share_network_id:
1858 post_body['share_network_id'] = share_network_id
1859
1860 headers, extra_headers = utils.get_extra_headers(
1861 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
1862 body = json.dumps({'share_replica': post_body})
1863 resp, body = self.post(uri, body,
1864 headers=headers,
1865 extra_headers=extra_headers,
1866 version=version)
1867 self.expected_success(202, resp.status)
1868 body = json.loads(body)
1869 return rest_client.ResponseBody(resp, body)
1870
1871 def get_share_replica(self, replica_id, version=LATEST_MICROVERSION):
1872 """Get the details of share_replica."""
1873 headers, extra_headers = utils.get_extra_headers(
1874 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
1875 resp, body = self.get("share-replicas/%s" % replica_id,
1876 headers=headers,
1877 extra_headers=extra_headers,
1878 version=version)
1879 self.expected_success(200, resp.status)
1880 body = json.loads(body)
1881 return rest_client.ResponseBody(resp, body)
1882
1883 def list_share_replicas(self, share_id=None, version=LATEST_MICROVERSION):
1884 """Get list of replicas."""
1885 uri = "share-replicas/detail"
1886 uri += ("?share_id=%s" % share_id) if share_id is not None else ''
1887 headers, extra_headers = utils.get_extra_headers(
1888 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
1889 resp, body = self.get(uri, headers=headers,
1890 extra_headers=extra_headers, version=version)
1891 self.expected_success(200, resp.status)
1892 body = json.loads(body)
1893 return rest_client.ResponseBody(resp, body)
1894
1895 def list_share_replicas_summary(self, share_id=None,
1896 version=LATEST_MICROVERSION):
1897 """Get summary list of replicas."""
1898 uri = "share-replicas"
1899 uri += ("?share_id=%s" % share_id) if share_id is not None else ''
1900 headers, extra_headers = utils.get_extra_headers(
1901 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
1902 resp, body = self.get(uri, headers=headers,
1903 extra_headers=extra_headers, version=version)
1904 self.expected_success(200, resp.status)
1905 body = json.loads(body)
1906 return rest_client.ResponseBody(resp, body)
1907
1908 def delete_share_replica(self, replica_id, version=LATEST_MICROVERSION):
1909 """Delete share_replica."""
1910 uri = "share-replicas/%s" % replica_id
1911 headers, extra_headers = utils.get_extra_headers(
1912 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
1913 resp, body = self.delete(uri,
1914 headers=headers,
1915 extra_headers=extra_headers,
1916 version=version)
1917 self.expected_success(202, resp.status)
1918 return rest_client.ResponseBody(resp, body)
1919
1920 def promote_share_replica(self, replica_id, expected_status=202,
1921 version=LATEST_MICROVERSION):
1922 """Promote a share replica to active state."""
1923 uri = "share-replicas/%s/action" % replica_id
1924 post_body = {
1925 'promote': None,
1926 }
1927 body = json.dumps(post_body)
1928 headers, extra_headers = utils.get_extra_headers(
1929 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
1930 resp, body = self.post(uri, body,
1931 headers=headers,
1932 extra_headers=extra_headers,
1933 version=version)
1934 self.expected_success(expected_status, resp.status)
1935 try:
1936 body = json.loads(body)
1937 except json.decoder.JSONDecodeError:
1938 pass
1939 return rest_client.ResponseBody(resp, body)
1940
1941 def list_share_replica_export_locations(self, replica_id,
1942 expected_status=200,
1943 version=LATEST_MICROVERSION):
1944 uri = "share-replicas/%s/export-locations" % replica_id
1945 headers, extra_headers = utils.get_extra_headers(
1946 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
1947 resp, body = self.get(uri, headers=headers,
1948 extra_headers=extra_headers, version=version)
1949 self.expected_success(expected_status, resp.status)
1950 body = json.loads(body)
1951 return rest_client.ResponseBody(resp, body)
1952
1953 def get_share_replica_export_location(self, replica_id,
1954 export_location_id,
1955 expected_status=200,
1956 version=LATEST_MICROVERSION):
1957 uri = "share-replicas/%s/export-locations/%s" % (replica_id,
1958 export_location_id)
1959 headers, extra_headers = utils.get_extra_headers(
1960 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
1961 resp, body = self.get(uri, headers=headers,
1962 extra_headers=extra_headers, version=version)
1963 self.expected_success(expected_status, resp.status)
1964 body = json.loads(body)
1965 return rest_client.ResponseBody(resp, body)
1966
1967 def reset_share_replica_status(self, replica_id,
1968 status=constants.STATUS_AVAILABLE,
1969 version=LATEST_MICROVERSION):
1970 """Reset the status."""
1971 uri = 'share-replicas/%s/action' % replica_id
1972 post_body = {
1973 'reset_status': {
1974 'status': status
1975 }
1976 }
1977 body = json.dumps(post_body)
1978 headers, extra_headers = utils.get_extra_headers(
1979 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
1980 resp, body = self.post(uri, body,
1981 headers=headers,
1982 extra_headers=extra_headers,
1983 version=version)
1984 self.expected_success(202, resp.status)
1985 return rest_client.ResponseBody(resp, body)
1986
1987 def reset_share_replica_state(self, replica_id,
1988 state=constants.REPLICATION_STATE_ACTIVE,
1989 version=LATEST_MICROVERSION):
1990 """Reset the replication state of a replica."""
1991 uri = 'share-replicas/%s/action' % replica_id
1992 post_body = {
1993 'reset_replica_state': {
1994 'replica_state': state
1995 }
1996 }
1997 body = json.dumps(post_body)
1998 headers, extra_headers = utils.get_extra_headers(
1999 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
2000 resp, body = self.post(uri, body,
2001 headers=headers,
2002 extra_headers=extra_headers,
2003 version=version)
2004 self.expected_success(202, resp.status)
2005 return rest_client.ResponseBody(resp, body)
2006
2007 def resync_share_replica(self, replica_id, expected_result=202,
2008 version=LATEST_MICROVERSION):
2009 """Force an immediate resync of the replica."""
2010 uri = 'share-replicas/%s/action' % replica_id
2011 post_body = {
2012 'resync': None
2013 }
2014 body = json.dumps(post_body)
2015 headers, extra_headers = utils.get_extra_headers(
2016 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
2017 resp, body = self.post(uri, body,
2018 headers=headers,
2019 extra_headers=extra_headers,
2020 version=version)
2021 self.expected_success(expected_result, resp.status)
2022 return rest_client.ResponseBody(resp, body)
2023
2024 def force_delete_share_replica(self, replica_id,
2025 version=LATEST_MICROVERSION):
2026 """Force delete a replica."""
2027 uri = 'share-replicas/%s/action' % replica_id
2028 post_body = {
2029 'force_delete': None
2030 }
2031 body = json.dumps(post_body)
2032 headers, extra_headers = utils.get_extra_headers(
2033 version, constants.SHARE_REPLICA_GRADUATION_VERSION)
2034 resp, body = self.post(uri, body,
2035 headers=headers,
2036 extra_headers=extra_headers,
2037 version=version)
2038 self.expected_success(202, resp.status)
2039 return rest_client.ResponseBody(resp, body)
2040
2041 def list_share_networks(self, detailed=False, params=None,
2042 version=LATEST_MICROVERSION):
2043 """Get list of share networks w/o filters."""
2044 uri = 'share-networks/detail' if detailed else 'share-networks'
2045 uri += '?%s' % parse.urlencode(params) if params else ''
2046 resp, body = self.get(uri, version=version)
2047 self.expected_success(200, resp.status)
2048 body = json.loads(body)
2049 return rest_client.ResponseBody(resp, body)
2050
2051 def list_share_networks_with_detail(self, params=None,
2052 version=LATEST_MICROVERSION):
2053 """Get detailed list of share networks w/o filters."""
2054 return self.list_share_networks(
2055 detailed=True, params=params, version=version)
2056
2057 def get_share_network(self, share_network_id, version=LATEST_MICROVERSION):
2058 resp, body = self.get("share-networks/%s" % share_network_id,
2059 version=version)
2060 self.expected_success(200, resp.status)
2061 body = json.loads(body)
2062 return rest_client.ResponseBody(resp, body)
2063
2064 def create_share_network(self, version=LATEST_MICROVERSION, **kwargs):
2065 """Create a share network.
2066
2067 :param version: API microversion to use (default: LATEST_MICROVERSION)
2068 :param kwargs: Share network parameters (name, description,
2069 neutron_net_id, neutron_subnet_id, etc.)
2070 :return: ResponseBody containing the created share_network
2071 """
2072 body = json.dumps({"share_network": kwargs})
2073 resp, body = self.post("share-networks", body, version=version)
2074 self.expected_success(200, resp.status)
2075 body = json.loads(body)
2076 return rest_client.ResponseBody(resp, body)
2077
2078 def update_share_network(self, sn_id, version=LATEST_MICROVERSION,
2079 **kwargs):
2080 """Update a share network.
2081
2082 :param sn_id: Share network ID to update
2083 :param version: API microversion to use (default: LATEST_MICROVERSION)
2084 :param kwargs: Share network parameters to update (name,
2085 description, etc.)
2086 :return: ResponseBody containing the updated share_network
2087 """
2088 body = json.dumps({"share_network": kwargs})
2089 resp, body = self.put("share-networks/%s" % sn_id, body,
2090 version=version)
2091 self.expected_success(200, resp.status)
2092 body = json.loads(body)
2093 return rest_client.ResponseBody(resp, body)
2094
2095 def delete_share_network(self, sn_id, version=LATEST_MICROVERSION):
2096 """Delete a share network.
2097
2098 :param sn_id: Share network ID to delete
2099 :param version: API microversion to use (default: LATEST_MICROVERSION)
2100 :return: ResponseBody
2101 """
2102 resp, body = self.delete("share-networks/%s" % sn_id, version=version)
2103 self.expected_success(202, resp.status)
2104 return rest_client.ResponseBody(resp, body)
2105
2106 ###############
2107
2108 def get_share_backup(self, backup_id, version=LATEST_MICROVERSION):
2109 """Returns the details of a single backup."""
2110 resp, body = self.get("share-backups/%s" % backup_id,
2111 headers=EXPERIMENTAL,
2112 extra_headers=True,
2113 version=version)
2114 self.expected_success(200, resp.status)
2115 body = json.loads(body)
2116 return rest_client.ResponseBody(resp, body)
2117
2118 def list_share_backups(self, share_id=None, version=LATEST_MICROVERSION):
2119 """Get list of backups."""
2120 uri = "share-backups/detail"
2121 if share_id:
2122 uri += (f'?share_id={share_id}')
2123 resp, body = self.get(uri, headers=EXPERIMENTAL,
2124 extra_headers=True, version=version)
2125 self.expected_success(200, resp.status)
2126 body = json.loads(body)
2127 return rest_client.ResponseBody(resp, body)
2128
2129 def create_share_backup(self, share_id, name=None, description=None,
2130 backup_options=None, version=LATEST_MICROVERSION):
2131 """Create a share backup."""
2132 if name is None:
2133 name = data_utils.rand_name("tempest-created-share-backup")
2134 if description is None:
2135 description = data_utils.rand_name(
2136 "tempest-created-share-backup-desc")
2137 post_body = {
2138 'share_backup': {
2139 'name': name,
2140 'description': description,
2141 'share_id': share_id,
2142 'backup_options': backup_options,
2143 }
2144 }
2145 body = json.dumps(post_body)
2146 resp, body = self.post('share-backups', body,
2147 headers=EXPERIMENTAL,
2148 extra_headers=True,
2149 version=version)
2150
2151 self.expected_success(202, resp.status)
2152 body = json.loads(body)
2153 return rest_client.ResponseBody(resp, body)
2154
2155 def delete_share_backup(self, backup_id, version=LATEST_MICROVERSION):
2156 """Delete share backup."""
2157 uri = "share-backups/%s" % backup_id
2158 resp, body = self.delete(uri,
2159 headers=EXPERIMENTAL,
2160 extra_headers=True,
2161 version=version)
2162 self.expected_success(202, resp.status)
2163 return rest_client.ResponseBody(resp, body)
2164
2165 def restore_share_backup(self, backup_id, version=LATEST_MICROVERSION):
2166 """Restore share backup."""
2167 uri = "share-backups/%s/action" % backup_id
2168 body = {'restore': None}
2169 resp, body = self.post(uri, json.dumps(body),
2170 headers=EXPERIMENTAL,
2171 extra_headers=True,
2172 version=version)
2173 self.expected_success(202, resp.status)
2174 body = json.loads(body)
2175 return rest_client.ResponseBody(resp, body)
2176
2177 def update_share_backup(self, backup_id, name=None, description=None,
2178 version=LATEST_MICROVERSION):
2179 """Update share backup."""
2180 uri = "share-backups/%s" % backup_id
2181 post_body = {}
2182 if name:
2183 post_body['name'] = name
2184 if description:
2185 post_body['description'] = description
2186
2187 body = json.dumps({'share_backup': post_body})
2188 resp, body = self.put(uri, body,
2189 headers=EXPERIMENTAL,
2190 extra_headers=True,
2191 version=version)
2192 self.expected_success(200, resp.status)
2193 body = json.loads(body)
2194 return rest_client.ResponseBody(resp, body)
2195
2196 def reset_state_share_backup(self, backup_id,
2197 status=constants.STATUS_AVAILABLE,
2198 version=LATEST_MICROVERSION):
2199
2200 uri = "share-backups/%s/action" % backup_id
2201 body = {'reset_status': {'status': status}}
2202 resp, body = self.post(uri, json.dumps(body),
2203 headers=EXPERIMENTAL,
2204 extra_headers=True,
2205 version=LATEST_MICROVERSION)
2206 self.expected_success(202, resp.status)
2207 return rest_client.ResponseBody(resp, body)
2208
2209 ################
2210
2211 def create_snapshot_access_rule(self, snapshot_id, access_type="ip",
2212 access_to="0.0.0.0/0"):
2213 body = {
2214 "allow_access": {
2215 "access_type": access_type,
2216 "access_to": access_to
2217 }
2218 }
2219 resp, body = self.post("snapshots/%s/action" % snapshot_id,
2220 json.dumps(body), version=LATEST_MICROVERSION)
2221 self.expected_success(202, resp.status)
2222 body = json.loads(body)
2223 return rest_client.ResponseBody(resp, body)
2224
2225 def list_snapshot_access_rules(self, snapshot_id,
2226 version=LATEST_MICROVERSION):
2227 resp, body = self.get("snapshots/%s/access-list" % snapshot_id,
2228 version=version)
2229 body = json.loads(body)
2230 return rest_client.ResponseBody(resp, body)
2231
2232 def get_snapshot_access_rule(self, snapshot_id, rule_id,
2233 version=LATEST_MICROVERSION):
2234 resp, body = self.get("snapshots/%s/access-list" % snapshot_id,
2235 version=version)
2236 body = json.loads(body)
2237 body = rest_client.ResponseBody(resp, body)
2238 found_rules = [
2239 r for r in body['snapshot_access_list'] if r['id'] == rule_id
2240 ]
2241 return found_rules[0] if len(found_rules) > 0 else None
2242
2243 def delete_snapshot_access_rule(self, snapshot_id, rule_id):
2244 body = {
2245 "deny_access": {
2246 "access_id": rule_id,
2247 }
2248 }
2249 resp, body = self.post("snapshots/%s/action" % snapshot_id,
2250 json.dumps(body), version=LATEST_MICROVERSION)
2251 self.expected_success(202, resp.status)
2252 return rest_client.ResponseBody(resp, body)
2253
2254 def get_snapshot_export_location(self, snapshot_id, export_location_uuid,
2255 version=LATEST_MICROVERSION):
2256 resp, body = self.get(
2257 "snapshots/%(snapshot_id)s/export-locations/%(el_uuid)s" % {
2258 "snapshot_id": snapshot_id, "el_uuid": export_location_uuid},
2259 version=version)
2260 self.expected_success(200, resp.status)
2261 body = json.loads(body)
2262 return rest_client.ResponseBody(resp, body)
2263
2264 def list_snapshot_export_locations(
2265 self, snapshot_id, version=LATEST_MICROVERSION):
2266 resp, body = self.get(
2267 "snapshots/%s/export-locations" % snapshot_id, version=version)
2268 self.expected_success(200, resp.status)
2269 body = json.loads(body)
2270 return rest_client.ResponseBody(resp, body)
2271
2272 ###############
2273
2274 def get_message(self, message_id, version=LATEST_MICROVERSION):
2275 """Show details for a single message."""
2276 url = 'messages/%s' % message_id
2277 resp, body = self.get(url, version=version)
2278 self.expected_success(200, resp.status)
2279 body = json.loads(body)
2280 return rest_client.ResponseBody(resp, body)
2281
2282 def list_messages(self, params=None, version=LATEST_MICROVERSION):
2283 """List all messages."""
2284 url = 'messages'
2285 url += '?%s' % parse.urlencode(params) if params else ''
2286 resp, body = self.get(url, version=version)
2287 self.expected_success(200, resp.status)
2288 body = json.loads(body)
2289 return rest_client.ResponseBody(resp, body)
2290
2291 def delete_message(self, message_id, version=LATEST_MICROVERSION):
2292 """Delete a single message."""
2293 url = 'messages/%s' % message_id
2294 resp, body = self.delete(url, version=version)
2295 self.expected_success(204, resp.status)
2296 return rest_client.ResponseBody(resp, body)
2297
2298 ###############
2299
2300 def create_security_service(self, ss_type="ldap",
2301 version=LATEST_MICROVERSION, **kwargs):
2302 """Creates Security Service.
2303
2304 :param ss_type: ldap, kerberos, active_directory
2305 :param version: microversion string
2306 :param kwargs: name, description, dns_ip, server, ou, domain, user,
2307 :param kwargs: password
2308 """
2309 post_body = {"type": ss_type}
2310 post_body.update(kwargs)
2311 body = json.dumps({"security_service": post_body})
2312 resp, body = self.post("security-services", body, version=version)
2313 self.expected_success(200, resp.status)
2314 body = json.loads(body)
2315 return rest_client.ResponseBody(resp, body)
2316
2317 def update_security_service(self, ss_id, version=LATEST_MICROVERSION,
2318 **kwargs):
2319 """Updates Security Service.
2320
2321 :param ss_id: id of security-service entity
2322 :param version: microversion string
2323 :param kwargs: dns_ip, server, ou, domain, user, password, name,
2324 :param kwargs: description
2325 :param kwargs: for 'active' status can be changed
2326 :param kwargs: only 'name' and 'description' fields
2327 """
2328 body = json.dumps({"security_service": kwargs})
2329 resp, body = self.put("security-services/%s" % ss_id, body,
2330 version=version)
2331 self.expected_success(200, resp.status)
2332 body = json.loads(body)
2333 return rest_client.ResponseBody(resp, body)
2334
2335 def get_security_service(self, ss_id, version=LATEST_MICROVERSION):
2336 resp, body = self.get("security-services/%s" % ss_id, version=version)
2337 self.expected_success(200, resp.status)
2338 body = json.loads(body)
2339 return rest_client.ResponseBody(resp, body)
2340
2341 def list_security_services(self, detailed=False, params=None,
2342 version=LATEST_MICROVERSION):
2343 uri = "security-services"
2344 if detailed:
2345 uri += '/detail'
2346 if params:
2347 uri += "?%s" % parse.urlencode(params)
2348 resp, body = self.get(uri, version=version)
2349 self.expected_success(200, resp.status)
2350 body = json.loads(body)
2351 return rest_client.ResponseBody(resp, body)
2352
2353 def delete_security_service(self, ss_id, version=LATEST_MICROVERSION):
2354 resp, body = self.delete("security-services/%s" % ss_id,
2355 version=version)
2356 self.expected_success(202, resp.status)
2357 return rest_client.ResponseBody(resp, body)
2358
2359 ###############
2360
2361 def create_subnet(
2362 self, share_network_id, availability_zone=None,
2363 neutron_net_id=None, neutron_subnet_id=None,
2364 metadata=None, version=LATEST_MICROVERSION):
2365 body = {'share_network_id': share_network_id}
2366
2367 if availability_zone:
2368 body['availability_zone'] = availability_zone
2369 if neutron_net_id:
2370 body['neutron_net_id'] = neutron_net_id
2371 if neutron_subnet_id:
2372 body['neutron_subnet_id'] = neutron_subnet_id
2373
2374 if utils.is_microversion_ge(version, "2.78") and metadata:
2375 body["metadata"] = metadata
2376 body = json.dumps({"share-network-subnet": body})
2377 url = '/share-networks/%s/subnets' % share_network_id
2378 resp, body = self.post(url, body, version=LATEST_MICROVERSION)
2379 self.expected_success(200, resp.status)
2380 body = json.loads(body)
2381 return rest_client.ResponseBody(resp, body)
2382
2383 def get_subnet(self, share_network_subnet_id, share_network_id):
2384 url = ('share-networks/%(network)s/subnets/%(subnet)s' % {
2385 'network': share_network_id,
2386 'subnet': share_network_subnet_id}
2387 )
2388 resp, body = self.get(url)
2389 self.expected_success(200, resp.status)
2390 body = json.loads(body)
2391 return rest_client.ResponseBody(resp, body)
2392
2393 def delete_subnet(self, share_network_id, share_network_subnet_id):
2394 url = ('share-networks/%(network)s/subnets/%(subnet)s' % {
2395 'network': share_network_id,
2396 'subnet': share_network_subnet_id}
2397 )
2398 resp, body = self.delete(url)
2399 self.expected_success(202, resp.status)
2400 return rest_client.ResponseBody(resp, body)
2401
2402 def subnet_create_check(
2403 self, share_network_id, neutron_net_id=None,
2404 neutron_subnet_id=None, availability_zone=None,
2405 reset=False, version=LATEST_MICROVERSION):
2406 body = {
2407 'share_network_subnet_create_check': {
2408 'neutron_net_id': neutron_net_id,
2409 'neutron_subnet_id': neutron_subnet_id,
2410 'availability_zone': availability_zone,
2411 'reset': reset,
2412 }
2413 }
2414
2415 body = json.dumps(body)
2416 resp, body = self.post(
2417 f'share-networks/{share_network_id}/action',
2418 body, headers=EXPERIMENTAL, extra_headers=True,
2419 version=version)
2420 self.expected_success(202, resp.status)
2421
2422 body = json.loads(body)
2423 return rest_client.ResponseBody(resp, body)
2424
2425 ###############
2426
2427 def share_server_migration_check(
2428 self, share_server_id, host, writable=False,
2429 preserve_snapshots=False, nondisruptive=False,
2430 new_share_network_id=None, version=LATEST_MICROVERSION):
2431 body = {
2432 'migration_check': {
2433 'host': host,
2434 'writable': writable,
2435 'preserve_snapshots': preserve_snapshots,
2436 'nondisruptive': nondisruptive,
2437 'new_share_network_id': new_share_network_id,
2438 }
2439 }
2440
2441 body = json.dumps(body)
2442 resp, body = self.post('share-servers/%s/action' % share_server_id,
2443 body, headers=EXPERIMENTAL, extra_headers=True,
2444 version=version)
2445 self.expected_success(200, resp.status)
2446
2447 body = json.loads(body)
2448 return rest_client.ResponseBody(resp, body)
2449
2450 def share_server_migration_start(self, share_server_id, host,
2451 writable=False, new_share_network_id=None,
2452 preserve_snapshots=False,
2453 nondisruptive=False,
2454 version=LATEST_MICROVERSION):
2455 body = {
2456 'migration_start': {
2457 'host': host,
2458 'writable': writable,
2459 'preserve_snapshots': preserve_snapshots,
2460 'nondisruptive': nondisruptive,
2461 'new_share_network_id': new_share_network_id,
2462 }
2463 }
2464
2465 body = json.dumps(body)
2466 resp, body = self.post('share-servers/%s/action' % share_server_id,
2467 body, headers=EXPERIMENTAL, extra_headers=True,
2468 version=version)
2469 self.expected_success(202, resp.status)
2470
2471 return rest_client.ResponseBody(resp, body)
2472
2473 def share_server_migration_complete(self, share_server_id,
2474 version=LATEST_MICROVERSION):
2475 body = {
2476 'migration_complete': None
2477 }
2478
2479 body = json.dumps(body)
2480 resp, body = self.post('share-servers/%s/action' % share_server_id,
2481 body, headers=EXPERIMENTAL, extra_headers=True,
2482 version=version)
2483 self.expected_success(200, resp.status)
2484
2485 body = json.loads(body)
2486 return rest_client.ResponseBody(resp, body)
2487
2488 def share_server_migration_cancel(self, share_server_id,
2489 version=LATEST_MICROVERSION):
2490 body = {
2491 'migration_cancel': None
2492 }
2493
2494 body = json.dumps(body)
2495 resp, body = self.post('share-servers/%s/action' % share_server_id,
2496 body, headers=EXPERIMENTAL, extra_headers=True,
2497 version=version)
2498 self.expected_success(202, resp.status)
2499
2500 return rest_client.ResponseBody(resp, body)
2501
2502 def share_server_migration_get_progress(self, share_server_id,
2503 version=LATEST_MICROVERSION):
2504 body = {
2505 'migration_get_progress': None
2506 }
2507
2508 body = json.dumps(body)
2509 resp, body = self.post('share-servers/%s/action' % share_server_id,
2510 body, headers=EXPERIMENTAL, extra_headers=True,
2511 version=version)
2512 self.expected_success(200, resp.status)
2513
2514 body = json.loads(body)
2515 return rest_client.ResponseBody(resp, body)
2516
2517 #################
2518
2519 def _update_metadata(self, resource, resource_id, metadata=None,
2520 method="post", parent_resource=None, parent_id=None):
2521 if parent_resource is None:
2522 uri = f'{resource}s/{resource_id}/metadata'
2523 else:
2524 uri = (f'{parent_resource}/{parent_id}'
2525 f'/{resource}s/{resource_id}/metadata')
2526 if metadata is None:
2527 metadata = {}
2528 post_body = {"metadata": metadata}
2529 body = json.dumps(post_body)
2530 if method == "post":
2531 resp, body = self.post(uri, body)
2532 if method == "put":
2533 resp, body = self.put(uri, body)
2534 self.expected_success(200, resp.status)
2535 body = json.loads(body)
2536 return rest_client.ResponseBody(resp, body)
2537
2538 def set_metadata(self, resource_id, metadata=None, resource='share',
2539 parent_resource=None, parent_id=None):
2540 return self._update_metadata(resource, resource_id, metadata,
2541 method="post",
2542 parent_resource=parent_resource,
2543 parent_id=parent_id)
2544
2545 def update_all_metadata(self, resource_id, metadata=None,
2546 resource='share', parent_resource=None,
2547 parent_id=None):
2548 return self._update_metadata(resource, resource_id, metadata,
2549 method="put",
2550 parent_resource=parent_resource,
2551 parent_id=parent_id)
2552
2553 def delete_metadata(self, resource_id, key, resource='share',
2554 parent_resource=None, parent_id=None):
2555 if parent_resource is None:
2556 uri = f'{resource}s/{resource_id}/metadata/{key}'
2557 else:
2558 uri = (f'{parent_resource}/{parent_id}'
2559 f'/{resource}s/{resource_id}/metadata/{key}')
2560 resp, body = self.delete(uri)
2561 self.expected_success(200, resp.status)
2562 return rest_client.ResponseBody(resp, body)
2563
2564 def get_metadata(self, resource_id, resource='share',
2565 parent_resource=None, parent_id=None):
2566 if parent_resource is None:
2567 uri = f'{resource}s/{resource_id}/metadata'
2568 else:
2569 uri = (f'{parent_resource}/{parent_id}'
2570 f'/{resource}s/{resource_id}/metadata')
2571
2572 resp, body = self.get(uri)
2573 self.expected_success(200, resp.status)
2574 body = json.loads(body)
2575 return rest_client.ResponseBody(resp, body)
2576
2577 def get_metadata_item(self, resource_id, key, resource='share',
2578 parent_resource=None, parent_id=None):
2579 if parent_resource is None:
2580 uri = f'{resource}s/{resource_id}/metadata/{key}'
2581 else:
2582 uri = (f'{parent_resource}/{parent_id}'
2583 f'/{resource}s/{resource_id}/metadata/{key}')
2584 resp, body = self.get(uri)
2585 self.expected_success(200, resp.status)
2586 body = json.loads(body)
2587 return rest_client.ResponseBody(resp, body)
2588
2589 #################
2590
2591 def create_resource_lock(self, resource_id, resource_type,
2592 resource_action='delete', lock_reason=None,
2593 version=LATEST_MICROVERSION):
2594 body = {
2595 "resource_lock": {
2596 'resource_id': resource_id,
2597 'resource_type': resource_type,
2598 'resource_action': resource_action,
2599 'lock_reason': lock_reason,
2600 },
2601 }
2602 body = json.dumps(body)
2603 resp, body = self.post("resource-locks", body, version=version)
2604 self.expected_success(200, resp.status)
2605 body = json.loads(body)
2606 return rest_client.ResponseBody(resp, body)
2607
2608 def get_resource_lock(self, lock_id, version=LATEST_MICROVERSION):
2609 resp, body = self.get("resource-locks/%s" % lock_id, version=version)
2610
2611 self.expected_success(200, resp.status)
2612 body = json.loads(body)
2613 return rest_client.ResponseBody(resp, body)
2614
2615 def list_resource_locks(self, filters=None, version=LATEST_MICROVERSION):
2616 uri = (
2617 "resource-locks?%s" % parse.urlencode(filters)
2618 if filters else "resource-locks"
2619 )
2620
2621 resp, body = self.get(uri, version=version)
2622
2623 self.expected_success(200, resp.status)
2624 body = json.loads(body)
2625 return rest_client.ResponseBody(resp, body)
2626
2627 def update_resource_lock(self,
2628 lock_id,
2629 resource_action=None,
2630 lock_reason=None,
2631 version=LATEST_MICROVERSION):
2632 uri = 'resource-locks/%s' % lock_id
2633 post_body = {}
2634 if resource_action:
2635 post_body['resource_action'] = resource_action
2636 if lock_reason:
2637 post_body['lock_reason'] = lock_reason
2638 body = json.dumps({'resource_lock': post_body})
2639
2640 resp, body = self.put(uri, body, version=version)
2641
2642 self.expected_success(200, resp.status)
2643 body = json.loads(body)
2644 return rest_client.ResponseBody(resp, body)
2645
2646 def delete_resource_lock(self, lock_id, version=LATEST_MICROVERSION):
2647 uri = "resource-locks/%s" % lock_id
2648
2649 resp, body = self.delete(uri, version=version)
2650
2651 self.expected_success(204, resp.status)
2652 return rest_client.ResponseBody(resp, body)