Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 1 | # Copyright (C) 2015 EMC Corporation. |
| 2 | # Copyright (C) 2016 Pure Storage, Inc. |
| 3 | # All Rights Reserved. |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | # not use this file except in compliance with the License. You may obtain |
| 7 | # a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations |
| 15 | # under the License. |
| 16 | |
haixin | 7e1a072 | 2020-09-30 14:37:22 +0800 | [diff] [blame] | 17 | import http.client as http_client |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 18 | import time |
| 19 | |
| 20 | from oslo_serialization import jsonutils as json |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 21 | from tempest.lib.common import rest_client |
| 22 | from tempest.lib import exceptions as lib_exc |
| 23 | |
Luigi Toscano | f1432e1 | 2020-07-30 12:22:37 +0200 | [diff] [blame] | 24 | from cinder_tempest_plugin import exceptions as volume_exc |
| 25 | |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 26 | |
| 27 | class ConsistencyGroupsClient(rest_client.RestClient): |
| 28 | """Client class to send CRUD Volume ConsistencyGroup API requests""" |
| 29 | |
| 30 | def __init__(self, auth_provider, service, region, **kwargs): |
| 31 | super(ConsistencyGroupsClient, self).__init__( |
| 32 | auth_provider, service, region, **kwargs) |
| 33 | |
| 34 | def create_consistencygroup(self, volume_types, **kwargs): |
| 35 | """Creates a consistency group.""" |
| 36 | post_body = {'volume_types': volume_types} |
| 37 | if kwargs.get('availability_zone'): |
| 38 | post_body['availability_zone'] = kwargs.get('availability_zone') |
| 39 | if kwargs.get('name'): |
| 40 | post_body['name'] = kwargs.get('name') |
| 41 | if kwargs.get('description'): |
| 42 | post_body['description'] = kwargs.get('description') |
| 43 | post_body = json.dumps({'consistencygroup': post_body}) |
| 44 | resp, body = self.post('consistencygroups', post_body) |
| 45 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 46 | self.expected_success(http_client.ACCEPTED, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 47 | return rest_client.ResponseBody(resp, body) |
| 48 | |
| 49 | def create_consistencygroup_from_src(self, **kwargs): |
| 50 | """Creates a consistency group from source.""" |
| 51 | post_body = {} |
| 52 | if kwargs.get('cgsnapshot_id'): |
| 53 | post_body['cgsnapshot_id'] = kwargs.get('cgsnapshot_id') |
| 54 | if kwargs.get('source_cgid'): |
| 55 | post_body['source_cgid'] = kwargs.get('source_cgid') |
| 56 | if kwargs.get('name'): |
| 57 | post_body['name'] = kwargs.get('name') |
| 58 | if kwargs.get('description'): |
| 59 | post_body['description'] = kwargs.get('description') |
| 60 | post_body = json.dumps({'consistencygroup-from-src': post_body}) |
| 61 | resp, body = self.post('consistencygroups/create_from_src', post_body) |
| 62 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 63 | self.expected_success(http_client.ACCEPTED, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 64 | return rest_client.ResponseBody(resp, body) |
| 65 | |
| 66 | def delete_consistencygroup(self, cg_id): |
| 67 | """Delete a consistency group.""" |
| 68 | post_body = {'force': True} |
| 69 | post_body = json.dumps({'consistencygroup': post_body}) |
| 70 | resp, body = self.post('consistencygroups/%s/delete' % cg_id, |
| 71 | post_body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 72 | self.expected_success(http_client.ACCEPTED, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 73 | return rest_client.ResponseBody(resp, body) |
| 74 | |
| 75 | def show_consistencygroup(self, cg_id): |
| 76 | """Returns the details of a single consistency group.""" |
| 77 | url = "consistencygroups/%s" % str(cg_id) |
| 78 | resp, body = self.get(url) |
| 79 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 80 | self.expected_success(http_client.OK, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 81 | return rest_client.ResponseBody(resp, body) |
| 82 | |
| 83 | def list_consistencygroups(self, detail=False): |
| 84 | """Information for all the tenant's consistency groups.""" |
| 85 | url = "consistencygroups" |
| 86 | if detail: |
| 87 | url += "/detail" |
| 88 | resp, body = self.get(url) |
| 89 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 90 | self.expected_success(http_client.OK, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 91 | return rest_client.ResponseBody(resp, body) |
| 92 | |
| 93 | def create_cgsnapshot(self, consistencygroup_id, **kwargs): |
| 94 | """Creates a consistency group snapshot.""" |
| 95 | post_body = {'consistencygroup_id': consistencygroup_id} |
| 96 | if kwargs.get('name'): |
| 97 | post_body['name'] = kwargs.get('name') |
| 98 | if kwargs.get('description'): |
| 99 | post_body['description'] = kwargs.get('description') |
| 100 | post_body = json.dumps({'cgsnapshot': post_body}) |
| 101 | resp, body = self.post('cgsnapshots', post_body) |
| 102 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 103 | self.expected_success(http_client.ACCEPTED, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 104 | return rest_client.ResponseBody(resp, body) |
| 105 | |
| 106 | def delete_cgsnapshot(self, cgsnapshot_id): |
| 107 | """Delete a consistency group snapshot.""" |
| 108 | resp, body = self.delete('cgsnapshots/%s' % (str(cgsnapshot_id))) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 109 | self.expected_success(http_client.ACCEPTED, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 110 | return rest_client.ResponseBody(resp, body) |
| 111 | |
| 112 | def show_cgsnapshot(self, cgsnapshot_id): |
| 113 | """Returns the details of a single consistency group snapshot.""" |
| 114 | url = "cgsnapshots/%s" % str(cgsnapshot_id) |
| 115 | resp, body = self.get(url) |
| 116 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 117 | self.expected_success(http_client.OK, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 118 | return rest_client.ResponseBody(resp, body) |
| 119 | |
| 120 | def list_cgsnapshots(self, detail=False): |
| 121 | """Information for all the tenant's consistency group snapshotss.""" |
| 122 | url = "cgsnapshots" |
| 123 | if detail: |
| 124 | url += "/detail" |
| 125 | resp, body = self.get(url) |
| 126 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 127 | self.expected_success(http_client.OK, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 128 | return rest_client.ResponseBody(resp, body) |
| 129 | |
| 130 | def wait_for_consistencygroup_status(self, cg_id, status): |
| 131 | """Waits for a consistency group to reach a given status.""" |
| 132 | body = self.show_consistencygroup(cg_id)['consistencygroup'] |
| 133 | cg_status = body['status'] |
| 134 | start = int(time.time()) |
| 135 | |
| 136 | while cg_status != status: |
| 137 | time.sleep(self.build_interval) |
| 138 | body = self.show_consistencygroup(cg_id)['consistencygroup'] |
| 139 | cg_status = body['status'] |
| 140 | if cg_status == 'error': |
Luigi Toscano | f1432e1 | 2020-07-30 12:22:37 +0200 | [diff] [blame] | 141 | raise volume_exc.ConsistencyGroupException(cg_id=cg_id) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 142 | |
| 143 | if int(time.time()) - start >= self.build_timeout: |
| 144 | message = ('Consistency group %s failed to reach %s status ' |
| 145 | '(current %s) within the required time (%s s).' % |
| 146 | (cg_id, status, cg_status, |
| 147 | self.build_timeout)) |
Luigi Toscano | f1432e1 | 2020-07-30 12:22:37 +0200 | [diff] [blame] | 148 | raise lib_exc.TimeoutException(message) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 149 | |
| 150 | def wait_for_consistencygroup_deletion(self, cg_id): |
| 151 | """Waits for consistency group deletion""" |
| 152 | start_time = int(time.time()) |
| 153 | while True: |
| 154 | try: |
| 155 | self.show_consistencygroup(cg_id) |
| 156 | except lib_exc.NotFound: |
| 157 | return |
| 158 | if int(time.time()) - start_time >= self.build_timeout: |
Luigi Toscano | f1432e1 | 2020-07-30 12:22:37 +0200 | [diff] [blame] | 159 | raise lib_exc.TimeoutException |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 160 | time.sleep(self.build_interval) |
| 161 | |
| 162 | def wait_for_cgsnapshot_status(self, cgsnapshot_id, status): |
| 163 | """Waits for a consistency group snapshot to reach a given status.""" |
| 164 | body = self.show_cgsnapshot(cgsnapshot_id)['cgsnapshot'] |
| 165 | cgsnapshot_status = body['status'] |
| 166 | start = int(time.time()) |
| 167 | |
| 168 | while cgsnapshot_status != status: |
| 169 | time.sleep(self.build_interval) |
| 170 | body = self.show_cgsnapshot(cgsnapshot_id)['cgsnapshot'] |
| 171 | cgsnapshot_status = body['status'] |
| 172 | if cgsnapshot_status == 'error': |
Luigi Toscano | f1432e1 | 2020-07-30 12:22:37 +0200 | [diff] [blame] | 173 | raise volume_exc.ConsistencyGroupSnapshotException( |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 174 | cgsnapshot_id=cgsnapshot_id) |
| 175 | |
| 176 | if int(time.time()) - start >= self.build_timeout: |
| 177 | message = ('Consistency group snapshot %s failed to reach ' |
| 178 | '%s status (current %s) within the required time ' |
| 179 | '(%s s).' % |
| 180 | (cgsnapshot_id, status, cgsnapshot_status, |
| 181 | self.build_timeout)) |
Luigi Toscano | f1432e1 | 2020-07-30 12:22:37 +0200 | [diff] [blame] | 182 | raise lib_exc.TimeoutException(message) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 183 | |
| 184 | def wait_for_cgsnapshot_deletion(self, cgsnapshot_id): |
| 185 | """Waits for consistency group snapshot deletion""" |
| 186 | start_time = int(time.time()) |
| 187 | while True: |
| 188 | try: |
| 189 | self.show_cgsnapshot(cgsnapshot_id) |
| 190 | except lib_exc.NotFound: |
| 191 | return |
| 192 | if int(time.time()) - start_time >= self.build_timeout: |
Luigi Toscano | f1432e1 | 2020-07-30 12:22:37 +0200 | [diff] [blame] | 193 | raise lib_exc.TimeoutException |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 194 | time.sleep(self.build_interval) |