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