Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 1 | # Copyright (C) 2017 Dell Inc. or its subsidiaries. |
| 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 | |
songwenping | 99d6e00 | 2021-01-05 03:07:46 +0000 | [diff] [blame^] | 16 | from urllib import parse as urllib |
| 17 | |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 18 | from oslo_serialization import jsonutils as json |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 19 | |
zhufl | da8d258 | 2018-09-29 15:58:04 +0800 | [diff] [blame] | 20 | from tempest.lib.api_schema.response.volume import groups as schema |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 21 | from tempest.lib.common import rest_client |
| 22 | from tempest.lib import exceptions as lib_exc |
zhufl | 0273652 | 2017-06-19 13:57:28 +0800 | [diff] [blame] | 23 | from tempest.lib.services.volume import base_client |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 24 | |
| 25 | |
| 26 | class GroupsClient(base_client.BaseClient): |
| 27 | """Client class to send CRUD Volume Group API requests""" |
zhufl | da8d258 | 2018-09-29 15:58:04 +0800 | [diff] [blame] | 28 | api_version = 'v3' |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 29 | |
| 30 | def create_group(self, **kwargs): |
| 31 | """Creates a group. |
| 32 | |
| 33 | group_type and volume_types are required parameters in kwargs. |
| 34 | For a full list of available parameters, please refer to the official |
| 35 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 36 | https://docs.openstack.org/api-ref/block-storage/v3/#create-group |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 37 | """ |
| 38 | post_body = json.dumps({'group': kwargs}) |
| 39 | resp, body = self.post('groups', post_body) |
| 40 | body = json.loads(body) |
zhufl | da8d258 | 2018-09-29 15:58:04 +0800 | [diff] [blame] | 41 | self.validate_response(schema.create_group, resp, body) |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 42 | return rest_client.ResponseBody(resp, body) |
| 43 | |
| 44 | def delete_group(self, group_id, delete_volumes=True): |
| 45 | """Deletes a group. |
| 46 | |
| 47 | For a full list of available parameters, please refer to the official |
| 48 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 49 | https://docs.openstack.org/api-ref/block-storage/v3/#delete-group |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 50 | """ |
| 51 | post_body = {'delete-volumes': delete_volumes} |
| 52 | post_body = json.dumps({'delete': post_body}) |
| 53 | resp, body = self.post('groups/%s/action' % group_id, |
| 54 | post_body) |
zhufl | da8d258 | 2018-09-29 15:58:04 +0800 | [diff] [blame] | 55 | self.validate_response(schema.delete_group, resp, body) |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 56 | return rest_client.ResponseBody(resp, body) |
| 57 | |
| 58 | def show_group(self, group_id): |
| 59 | """Returns the details of a single group. |
| 60 | |
| 61 | For a full list of available parameters, please refer to the official |
| 62 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 63 | https://docs.openstack.org/api-ref/block-storage/v3/#show-group-details |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 64 | """ |
| 65 | url = "groups/%s" % str(group_id) |
| 66 | resp, body = self.get(url) |
| 67 | body = json.loads(body) |
zhufl | da8d258 | 2018-09-29 15:58:04 +0800 | [diff] [blame] | 68 | self.validate_response(schema.show_group, resp, body) |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 69 | return rest_client.ResponseBody(resp, body) |
| 70 | |
| 71 | def list_groups(self, detail=False, **params): |
| 72 | """Lists information for all the tenant's groups. |
| 73 | |
| 74 | For a full list of available parameters, please refer to the official |
| 75 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 76 | https://docs.openstack.org/api-ref/block-storage/v3/#list-groups |
| 77 | https://docs.openstack.org/api-ref/block-storage/v3/#list-groups-with-details |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 78 | """ |
| 79 | url = "groups" |
zhufl | da8d258 | 2018-09-29 15:58:04 +0800 | [diff] [blame] | 80 | schema_list_groups = schema.list_groups_no_detail |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 81 | if detail: |
| 82 | url += "/detail" |
zhufl | da8d258 | 2018-09-29 15:58:04 +0800 | [diff] [blame] | 83 | schema_list_groups = schema.list_groups_with_detail |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 84 | if params: |
| 85 | url += '?%s' % urllib.urlencode(params) |
| 86 | resp, body = self.get(url) |
| 87 | body = json.loads(body) |
zhufl | da8d258 | 2018-09-29 15:58:04 +0800 | [diff] [blame] | 88 | self.validate_response(schema_list_groups, resp, body) |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 89 | return rest_client.ResponseBody(resp, body) |
| 90 | |
xing-yang | d56edc3 | 2017-07-01 14:00:06 -0700 | [diff] [blame] | 91 | def create_group_from_source(self, **kwargs): |
| 92 | """Creates a group from source. |
| 93 | |
| 94 | For a full list of available parameters, please refer to the official |
| 95 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 96 | https://docs.openstack.org/api-ref/block-storage/v3/#create-group-from-source |
xing-yang | d56edc3 | 2017-07-01 14:00:06 -0700 | [diff] [blame] | 97 | """ |
| 98 | post_body = json.dumps({'create-from-src': kwargs}) |
| 99 | resp, body = self.post('groups/action', post_body) |
| 100 | body = json.loads(body) |
zhufl | da8d258 | 2018-09-29 15:58:04 +0800 | [diff] [blame] | 101 | self.validate_response(schema.create_group_from_source, resp, body) |
xing-yang | d56edc3 | 2017-07-01 14:00:06 -0700 | [diff] [blame] | 102 | return rest_client.ResponseBody(resp, body) |
| 103 | |
xing-yang | 9ee9860 | 2017-07-01 14:12:54 -0700 | [diff] [blame] | 104 | def update_group(self, group_id, **kwargs): |
| 105 | """Updates the specified group. |
| 106 | |
| 107 | For a full list of available parameters, please refer to the official |
| 108 | API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 109 | https://docs.openstack.org/api-ref/block-storage/v3/#update-group |
xing-yang | 9ee9860 | 2017-07-01 14:12:54 -0700 | [diff] [blame] | 110 | """ |
| 111 | put_body = json.dumps({'group': kwargs}) |
| 112 | resp, body = self.put('groups/%s' % group_id, put_body) |
zhufl | da8d258 | 2018-09-29 15:58:04 +0800 | [diff] [blame] | 113 | self.validate_response(schema.update_group, resp, body) |
xing-yang | 9ee9860 | 2017-07-01 14:12:54 -0700 | [diff] [blame] | 114 | return rest_client.ResponseBody(resp, body) |
| 115 | |
jeremy.zhang | cca9974 | 2017-08-20 01:08:24 +0800 | [diff] [blame] | 116 | def reset_group_status(self, group_id, status_to_set): |
| 117 | """Resets group status. |
| 118 | |
| 119 | For more information, please refer to the official API reference: |
Andreas Jaeger | bf30ae7 | 2019-07-22 19:22:57 +0200 | [diff] [blame] | 120 | https://docs.openstack.org/api-ref/block-storage/v3/#reset-group-status |
jeremy.zhang | cca9974 | 2017-08-20 01:08:24 +0800 | [diff] [blame] | 121 | """ |
| 122 | post_body = json.dumps({'reset_status': {'status': status_to_set}}) |
| 123 | resp, body = self.post('groups/%s/action' % group_id, post_body) |
zhufl | da8d258 | 2018-09-29 15:58:04 +0800 | [diff] [blame] | 124 | self.validate_response(schema.reset_group_status, resp, body) |
jeremy.zhang | cca9974 | 2017-08-20 01:08:24 +0800 | [diff] [blame] | 125 | return rest_client.ResponseBody(resp, body) |
| 126 | |
Xing Yang | 0ddf83e | 2015-11-17 22:15:25 -0500 | [diff] [blame] | 127 | def is_resource_deleted(self, id): |
| 128 | try: |
| 129 | self.show_group(id) |
| 130 | except lib_exc.NotFound: |
| 131 | return True |
| 132 | return False |
| 133 | |
| 134 | @property |
| 135 | def resource_type(self): |
| 136 | """Returns the primary type of resource this client works with.""" |
| 137 | return 'group' |