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 | |
| 17 | import time |
| 18 | |
| 19 | from oslo_serialization import jsonutils as json |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 20 | from six.moves import http_client |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 21 | from tempest import exceptions |
| 22 | from tempest.lib.common import rest_client |
| 23 | from tempest.lib import exceptions as lib_exc |
| 24 | |
| 25 | |
| 26 | class ConsistencyGroupsClient(rest_client.RestClient): |
| 27 | """Client class to send CRUD Volume ConsistencyGroup API requests""" |
| 28 | |
| 29 | def __init__(self, auth_provider, service, region, **kwargs): |
| 30 | super(ConsistencyGroupsClient, self).__init__( |
| 31 | auth_provider, service, region, **kwargs) |
| 32 | |
| 33 | def create_consistencygroup(self, volume_types, **kwargs): |
| 34 | """Creates a consistency group.""" |
| 35 | post_body = {'volume_types': volume_types} |
| 36 | if kwargs.get('availability_zone'): |
| 37 | post_body['availability_zone'] = kwargs.get('availability_zone') |
| 38 | if kwargs.get('name'): |
| 39 | post_body['name'] = kwargs.get('name') |
| 40 | if kwargs.get('description'): |
| 41 | post_body['description'] = kwargs.get('description') |
| 42 | post_body = json.dumps({'consistencygroup': post_body}) |
| 43 | resp, body = self.post('consistencygroups', post_body) |
| 44 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 45 | self.expected_success(http_client.ACCEPTED, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 46 | return rest_client.ResponseBody(resp, body) |
| 47 | |
| 48 | def create_consistencygroup_from_src(self, **kwargs): |
| 49 | """Creates a consistency group from source.""" |
| 50 | post_body = {} |
| 51 | if kwargs.get('cgsnapshot_id'): |
| 52 | post_body['cgsnapshot_id'] = kwargs.get('cgsnapshot_id') |
| 53 | if kwargs.get('source_cgid'): |
| 54 | post_body['source_cgid'] = kwargs.get('source_cgid') |
| 55 | if kwargs.get('name'): |
| 56 | post_body['name'] = kwargs.get('name') |
| 57 | if kwargs.get('description'): |
| 58 | post_body['description'] = kwargs.get('description') |
| 59 | post_body = json.dumps({'consistencygroup-from-src': post_body}) |
| 60 | resp, body = self.post('consistencygroups/create_from_src', post_body) |
| 61 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 62 | self.expected_success(http_client.ACCEPTED, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 63 | return rest_client.ResponseBody(resp, body) |
| 64 | |
| 65 | def delete_consistencygroup(self, cg_id): |
| 66 | """Delete a consistency group.""" |
| 67 | post_body = {'force': True} |
| 68 | post_body = json.dumps({'consistencygroup': post_body}) |
| 69 | resp, body = self.post('consistencygroups/%s/delete' % cg_id, |
| 70 | post_body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 71 | self.expected_success(http_client.ACCEPTED, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 72 | return rest_client.ResponseBody(resp, body) |
| 73 | |
| 74 | def show_consistencygroup(self, cg_id): |
| 75 | """Returns the details of a single consistency group.""" |
| 76 | url = "consistencygroups/%s" % str(cg_id) |
| 77 | resp, body = self.get(url) |
| 78 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 79 | self.expected_success(http_client.OK, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 80 | return rest_client.ResponseBody(resp, body) |
| 81 | |
| 82 | def list_consistencygroups(self, detail=False): |
| 83 | """Information for all the tenant's consistency groups.""" |
| 84 | url = "consistencygroups" |
| 85 | if detail: |
| 86 | url += "/detail" |
| 87 | resp, body = self.get(url) |
| 88 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 89 | self.expected_success(http_client.OK, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 90 | return rest_client.ResponseBody(resp, body) |
| 91 | |
| 92 | def create_cgsnapshot(self, consistencygroup_id, **kwargs): |
| 93 | """Creates a consistency group snapshot.""" |
| 94 | post_body = {'consistencygroup_id': consistencygroup_id} |
| 95 | if kwargs.get('name'): |
| 96 | post_body['name'] = kwargs.get('name') |
| 97 | if kwargs.get('description'): |
| 98 | post_body['description'] = kwargs.get('description') |
| 99 | post_body = json.dumps({'cgsnapshot': post_body}) |
| 100 | resp, body = self.post('cgsnapshots', post_body) |
| 101 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 102 | self.expected_success(http_client.ACCEPTED, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 103 | return rest_client.ResponseBody(resp, body) |
| 104 | |
| 105 | def delete_cgsnapshot(self, cgsnapshot_id): |
| 106 | """Delete a consistency group snapshot.""" |
| 107 | resp, body = self.delete('cgsnapshots/%s' % (str(cgsnapshot_id))) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 108 | self.expected_success(http_client.ACCEPTED, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 109 | return rest_client.ResponseBody(resp, body) |
| 110 | |
| 111 | def show_cgsnapshot(self, cgsnapshot_id): |
| 112 | """Returns the details of a single consistency group snapshot.""" |
| 113 | url = "cgsnapshots/%s" % str(cgsnapshot_id) |
| 114 | resp, body = self.get(url) |
| 115 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 116 | self.expected_success(http_client.OK, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 117 | return rest_client.ResponseBody(resp, body) |
| 118 | |
| 119 | def list_cgsnapshots(self, detail=False): |
| 120 | """Information for all the tenant's consistency group snapshotss.""" |
| 121 | url = "cgsnapshots" |
| 122 | if detail: |
| 123 | url += "/detail" |
| 124 | resp, body = self.get(url) |
| 125 | body = json.loads(body) |
poojajadhav | 496ea79 | 2017-01-23 21:05:34 +0530 | [diff] [blame] | 126 | self.expected_success(http_client.OK, resp.status) |
Patrick East | 9b434d1 | 2016-08-12 17:23:19 -0700 | [diff] [blame] | 127 | return rest_client.ResponseBody(resp, body) |
| 128 | |
| 129 | def wait_for_consistencygroup_status(self, cg_id, status): |
| 130 | """Waits for a consistency group to reach a given status.""" |
| 131 | body = self.show_consistencygroup(cg_id)['consistencygroup'] |
| 132 | cg_status = body['status'] |
| 133 | start = int(time.time()) |
| 134 | |
| 135 | while cg_status != status: |
| 136 | time.sleep(self.build_interval) |
| 137 | body = self.show_consistencygroup(cg_id)['consistencygroup'] |
| 138 | cg_status = body['status'] |
| 139 | if cg_status == 'error': |
| 140 | raise exceptions.ConsistencyGroupException(cg_id=cg_id) |
| 141 | |
| 142 | if int(time.time()) - start >= self.build_timeout: |
| 143 | message = ('Consistency group %s failed to reach %s status ' |
| 144 | '(current %s) within the required time (%s s).' % |
| 145 | (cg_id, status, cg_status, |
| 146 | self.build_timeout)) |
| 147 | raise exceptions.TimeoutException(message) |
| 148 | |
| 149 | def wait_for_consistencygroup_deletion(self, cg_id): |
| 150 | """Waits for consistency group deletion""" |
| 151 | start_time = int(time.time()) |
| 152 | while True: |
| 153 | try: |
| 154 | self.show_consistencygroup(cg_id) |
| 155 | except lib_exc.NotFound: |
| 156 | return |
| 157 | if int(time.time()) - start_time >= self.build_timeout: |
| 158 | raise exceptions.TimeoutException |
| 159 | time.sleep(self.build_interval) |
| 160 | |
| 161 | def wait_for_cgsnapshot_status(self, cgsnapshot_id, status): |
| 162 | """Waits for a consistency group snapshot to reach a given status.""" |
| 163 | body = self.show_cgsnapshot(cgsnapshot_id)['cgsnapshot'] |
| 164 | cgsnapshot_status = body['status'] |
| 165 | start = int(time.time()) |
| 166 | |
| 167 | while cgsnapshot_status != status: |
| 168 | time.sleep(self.build_interval) |
| 169 | body = self.show_cgsnapshot(cgsnapshot_id)['cgsnapshot'] |
| 170 | cgsnapshot_status = body['status'] |
| 171 | if cgsnapshot_status == 'error': |
| 172 | raise exceptions.ConsistencyGroupSnapshotException( |
| 173 | cgsnapshot_id=cgsnapshot_id) |
| 174 | |
| 175 | if int(time.time()) - start >= self.build_timeout: |
| 176 | message = ('Consistency group snapshot %s failed to reach ' |
| 177 | '%s status (current %s) within the required time ' |
| 178 | '(%s s).' % |
| 179 | (cgsnapshot_id, status, cgsnapshot_status, |
| 180 | self.build_timeout)) |
| 181 | raise exceptions.TimeoutException(message) |
| 182 | |
| 183 | def wait_for_cgsnapshot_deletion(self, cgsnapshot_id): |
| 184 | """Waits for consistency group snapshot deletion""" |
| 185 | start_time = int(time.time()) |
| 186 | while True: |
| 187 | try: |
| 188 | self.show_cgsnapshot(cgsnapshot_id) |
| 189 | except lib_exc.NotFound: |
| 190 | return |
| 191 | if int(time.time()) - start_time >= self.build_timeout: |
| 192 | raise exceptions.TimeoutException |
| 193 | time.sleep(self.build_interval) |