blob: cea99ed316339dfc516eb19026b93b09845191b4 [file] [log] [blame]
Patrick East9b434d12016-08-12 17:23:19 -07001# 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
haixin7e1a0722020-09-30 14:37:22 +080017import http.client as http_client
Patrick East9b434d12016-08-12 17:23:19 -070018import time
19
20from oslo_serialization import jsonutils as json
Patrick East9b434d12016-08-12 17:23:19 -070021from tempest.lib.common import rest_client
22from tempest.lib import exceptions as lib_exc
23
Luigi Toscanof1432e12020-07-30 12:22:37 +020024from cinder_tempest_plugin import exceptions as volume_exc
25
Patrick East9b434d12016-08-12 17:23:19 -070026
27class 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)
poojajadhav496ea792017-01-23 21:05:34 +053046 self.expected_success(http_client.ACCEPTED, resp.status)
Patrick East9b434d12016-08-12 17:23:19 -070047 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)
poojajadhav496ea792017-01-23 21:05:34 +053063 self.expected_success(http_client.ACCEPTED, resp.status)
Patrick East9b434d12016-08-12 17:23:19 -070064 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)
poojajadhav496ea792017-01-23 21:05:34 +053072 self.expected_success(http_client.ACCEPTED, resp.status)
Patrick East9b434d12016-08-12 17:23:19 -070073 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)
poojajadhav496ea792017-01-23 21:05:34 +053080 self.expected_success(http_client.OK, resp.status)
Patrick East9b434d12016-08-12 17:23:19 -070081 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)
poojajadhav496ea792017-01-23 21:05:34 +053090 self.expected_success(http_client.OK, resp.status)
Patrick East9b434d12016-08-12 17:23:19 -070091 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)
poojajadhav496ea792017-01-23 21:05:34 +0530103 self.expected_success(http_client.ACCEPTED, resp.status)
Patrick East9b434d12016-08-12 17:23:19 -0700104 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)))
poojajadhav496ea792017-01-23 21:05:34 +0530109 self.expected_success(http_client.ACCEPTED, resp.status)
Patrick East9b434d12016-08-12 17:23:19 -0700110 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)
poojajadhav496ea792017-01-23 21:05:34 +0530117 self.expected_success(http_client.OK, resp.status)
Patrick East9b434d12016-08-12 17:23:19 -0700118 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)
poojajadhav496ea792017-01-23 21:05:34 +0530127 self.expected_success(http_client.OK, resp.status)
Patrick East9b434d12016-08-12 17:23:19 -0700128 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 Toscanof1432e12020-07-30 12:22:37 +0200141 raise volume_exc.ConsistencyGroupException(cg_id=cg_id)
Patrick East9b434d12016-08-12 17:23:19 -0700142
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 Toscanof1432e12020-07-30 12:22:37 +0200148 raise lib_exc.TimeoutException(message)
Patrick East9b434d12016-08-12 17:23:19 -0700149
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 Toscanof1432e12020-07-30 12:22:37 +0200159 raise lib_exc.TimeoutException
Patrick East9b434d12016-08-12 17:23:19 -0700160 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 Toscanof1432e12020-07-30 12:22:37 +0200173 raise volume_exc.ConsistencyGroupSnapshotException(
Patrick East9b434d12016-08-12 17:23:19 -0700174 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 Toscanof1432e12020-07-30 12:22:37 +0200182 raise lib_exc.TimeoutException(message)
Patrick East9b434d12016-08-12 17:23:19 -0700183
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 Toscanof1432e12020-07-30 12:22:37 +0200193 raise lib_exc.TimeoutException
Patrick East9b434d12016-08-12 17:23:19 -0700194 time.sleep(self.build_interval)