blob: b55a0373e436c7417f6baebded19e39c44d6e01c [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Rohit Karajgidd47d7e2012-07-31 04:11:01 -07002# 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
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053016import json
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053017import time
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050018import urllib
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053019
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000020from tempest.common import rest_client
Matthew Treinish684d8992014-01-30 16:27:40 +000021from tempest import config
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070022from tempest import exceptions
23
Matthew Treinish684d8992014-01-30 16:27:40 +000024CONF = config.CONF
25
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053026
Yuiko Takada4d41c2f2014-03-07 11:58:31 +000027class VolumesClientJSON(rest_client.RestClient):
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070028 """
29 Client class to send CRUD Volume API requests to a Cinder endpoint
30 """
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053031
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000032 def __init__(self, auth_provider):
33 super(VolumesClientJSON, self).__init__(auth_provider)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070034
Matthew Treinish684d8992014-01-30 16:27:40 +000035 self.service = CONF.volume.catalog_type
36 self.build_interval = CONF.volume.build_interval
37 self.build_timeout = CONF.volume.build_timeout
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053038
anju tiwari789449a2013-08-29 16:56:17 +053039 def get_attachment_from_volume(self, volume):
40 """Return the element 'attachment' from input volumes."""
41 return volume['attachments'][0]
42
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053043 def list_volumes(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050044 """List all the volumes created."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070045 url = 'volumes'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050046 if params:
47 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053048
chris fattarsi5098fa22012-04-17 13:27:00 -070049 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053050 body = json.loads(body)
51 return resp, body['volumes']
52
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053053 def list_volumes_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050054 """List the details of all volumes."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070055 url = 'volumes/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050056 if params:
57 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053058
chris fattarsi5098fa22012-04-17 13:27:00 -070059 resp, body = self.get(url)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053060 body = json.loads(body)
61 return resp, body['volumes']
62
Attila Fazekasb8aa7592013-01-26 01:25:45 +010063 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050064 """Returns the details of a single volume."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070065 url = "volumes/%s" % str(volume_id)
Attila Fazekasb8aa7592013-01-26 01:25:45 +010066 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053067 body = json.loads(body)
68 return resp, body['volume']
69
Jerry Cai9733d0e2014-03-19 15:50:49 +080070 def create_volume(self, size=None, **kwargs):
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053071 """
72 Creates a new Volume.
Jerry Cai9733d0e2014-03-19 15:50:49 +080073 size: Size of volume in GB.
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053074 Following optional keyword arguments are accepted:
75 display_name: Optional Volume Name.
76 metadata: A dictionary of values to be used as metadata.
Rohan Rhishikesh Kanadec316f0a2012-12-04 05:44:39 -080077 volume_type: Optional Name of volume_type for the volume
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010078 snapshot_id: When specified the volume is created from this snapshot
Giulio Fidente36836c42013-04-05 15:43:51 +020079 imageRef: When specified the volume is created from this image
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053080 """
Jerry Cai9733d0e2014-03-19 15:50:49 +080081 # for bug #1293885:
82 # If no size specified, read volume size from CONF
83 if size is None:
84 size = CONF.volume.volume_size
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010085 post_body = {'size': size}
86 post_body.update(kwargs)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053087 post_body = json.dumps({'volume': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020088 resp, body = self.post('volumes', post_body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053089 body = json.loads(body)
90 return resp, body['volume']
91
QingXin Meng611768a2013-09-18 00:51:33 -070092 def update_volume(self, volume_id, **kwargs):
93 """Updates the Specified Volume."""
94 put_body = json.dumps({'volume': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020095 resp, body = self.put('volumes/%s' % volume_id, put_body)
QingXin Meng611768a2013-09-18 00:51:33 -070096 body = json.loads(body)
97 return resp, body['volume']
98
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053099 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500100 """Deletes the Specified Volume."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700101 return self.delete("volumes/%s" % str(volume_id))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530102
Ryan Hsua67f4632013-08-29 16:03:06 -0700103 def upload_volume(self, volume_id, image_name, disk_format):
Giulio Fidente884e9da2013-06-21 17:25:42 +0200104 """Uploads a volume in Glance."""
105 post_body = {
106 'image_name': image_name,
Ryan Hsua67f4632013-08-29 16:03:06 -0700107 'disk_format': disk_format
Giulio Fidente884e9da2013-06-21 17:25:42 +0200108 }
109 post_body = json.dumps({'os-volume_upload_image': post_body})
110 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200111 resp, body = self.post(url, post_body)
Giulio Fidente884e9da2013-06-21 17:25:42 +0200112 body = json.loads(body)
113 return resp, body['os-volume_upload_image']
114
Rohit Karajgia42fe442012-09-21 03:08:33 -0700115 def attach_volume(self, volume_id, instance_uuid, mountpoint):
Sean Daguef237ccb2013-01-04 15:19:14 -0500116 """Attaches a volume to a given instance on a given mountpoint."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700117 post_body = {
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900118 'instance_uuid': instance_uuid,
119 'mountpoint': mountpoint,
120 }
Rohit Karajgia42fe442012-09-21 03:08:33 -0700121 post_body = json.dumps({'os-attach': post_body})
122 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200123 resp, body = self.post(url, post_body)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700124 return resp, body
125
126 def detach_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500127 """Detaches a volume from an instance."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700128 post_body = {}
129 post_body = json.dumps({'os-detach': post_body})
130 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200131 resp, body = self.post(url, post_body)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700132 return resp, body
133
zhangyanzi6b632432013-10-24 19:08:50 +0800134 def reserve_volume(self, volume_id):
135 """Reserves a volume."""
136 post_body = {}
137 post_body = json.dumps({'os-reserve': post_body})
138 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200139 resp, body = self.post(url, post_body)
zhangyanzi6b632432013-10-24 19:08:50 +0800140 return resp, body
141
142 def unreserve_volume(self, volume_id):
143 """Restore a reserved volume ."""
144 post_body = {}
145 post_body = json.dumps({'os-unreserve': post_body})
146 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200147 resp, body = self.post(url, post_body)
zhangyanzi6b632432013-10-24 19:08:50 +0800148 return resp, body
149
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530150 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500151 """Waits for a Volume to reach a given status."""
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530152 resp, body = self.get_volume(volume_id)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700153 volume_name = body['display_name']
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530154 volume_status = body['status']
155 start = int(time.time())
156
157 while volume_status != status:
158 time.sleep(self.build_interval)
159 resp, body = self.get_volume(volume_id)
160 volume_status = body['status']
161 if volume_status == 'error':
rajalakshmi-ganesane3bb58f2012-05-16 12:01:15 +0530162 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530163
164 if int(time.time()) - start >= self.build_timeout:
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800165 message = ('Volume %s failed to reach %s status within '
166 'the required time (%s s).' %
167 (volume_name, status, self.build_timeout))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530168 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400169
170 def is_resource_deleted(self, id):
171 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100172 self.get_volume(id)
David Kranz6aceb4a2012-06-05 14:05:45 -0400173 except exceptions.NotFound:
174 return True
175 return False
wanghao5b981752013-10-22 11:41:41 +0800176
177 def extend_volume(self, volume_id, extend_size):
178 """Extend a volume."""
179 post_body = {
180 'new_size': extend_size
181 }
182 post_body = json.dumps({'os-extend': post_body})
183 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200184 resp, body = self.post(url, post_body)
wanghao5b981752013-10-22 11:41:41 +0800185 return resp, body
wanghaoaa1f2f92013-10-10 11:30:37 +0800186
187 def reset_volume_status(self, volume_id, status):
188 """Reset the Specified Volume's Status."""
189 post_body = json.dumps({'os-reset_status': {"status": status}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200190 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800191 return resp, body
192
193 def volume_begin_detaching(self, volume_id):
194 """Volume Begin Detaching."""
195 post_body = json.dumps({'os-begin_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200196 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800197 return resp, body
198
199 def volume_roll_detaching(self, volume_id):
200 """Volume Roll Detaching."""
201 post_body = json.dumps({'os-roll_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200202 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800203 return resp, body
wingwjcbd82dc2013-10-22 16:38:39 +0800204
205 def create_volume_transfer(self, vol_id, display_name=None):
206 """Create a volume transfer."""
207 post_body = {
208 'volume_id': vol_id
209 }
210 if display_name:
211 post_body['name'] = display_name
212 post_body = json.dumps({'transfer': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200213 resp, body = self.post('os-volume-transfer', post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800214 body = json.loads(body)
215 return resp, body['transfer']
216
217 def get_volume_transfer(self, transfer_id):
218 """Returns the details of a volume transfer."""
219 url = "os-volume-transfer/%s" % str(transfer_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200220 resp, body = self.get(url)
wingwjcbd82dc2013-10-22 16:38:39 +0800221 body = json.loads(body)
222 return resp, body['transfer']
223
224 def list_volume_transfers(self, params=None):
225 """List all the volume transfers created."""
226 url = 'os-volume-transfer'
227 if params:
228 url += '?%s' % urllib.urlencode(params)
229 resp, body = self.get(url)
230 body = json.loads(body)
231 return resp, body['transfers']
232
233 def delete_volume_transfer(self, transfer_id):
234 """Delete a volume transfer."""
235 return self.delete("os-volume-transfer/%s" % str(transfer_id))
236
237 def accept_volume_transfer(self, transfer_id, transfer_auth_key):
238 """Accept a volume transfer."""
239 post_body = {
240 'auth_key': transfer_auth_key,
241 }
242 url = 'os-volume-transfer/%s/accept' % transfer_id
243 post_body = json.dumps({'accept': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200244 resp, body = self.post(url, post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800245 body = json.loads(body)
246 return resp, body['transfer']
zhangyanziaa180072013-11-21 12:31:26 +0800247
248 def update_volume_readonly(self, volume_id, readonly):
249 """Update the Specified Volume readonly."""
250 post_body = {
251 'readonly': readonly
252 }
253 post_body = json.dumps({'os-update_readonly_flag': post_body})
254 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200255 resp, body = self.post(url, post_body)
zhangyanziaa180072013-11-21 12:31:26 +0800256 return resp, body
wanghao9d3d6cb2013-11-12 15:10:10 +0800257
258 def force_delete_volume(self, volume_id):
259 """Force Delete Volume."""
260 post_body = json.dumps({'os-force_delete': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200261 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghao9d3d6cb2013-11-12 15:10:10 +0800262 return resp, body
huangtianhua0ff41682013-12-16 14:49:31 +0800263
264 def create_volume_metadata(self, volume_id, metadata):
265 """Create metadata for the volume."""
266 put_body = json.dumps({'metadata': metadata})
267 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200268 resp, body = self.post(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800269 body = json.loads(body)
270 return resp, body['metadata']
271
272 def get_volume_metadata(self, volume_id):
273 """Get metadata of the volume."""
274 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200275 resp, body = self.get(url)
huangtianhua0ff41682013-12-16 14:49:31 +0800276 body = json.loads(body)
277 return resp, body['metadata']
278
279 def update_volume_metadata(self, volume_id, metadata):
280 """Update metadata for the volume."""
281 put_body = json.dumps({'metadata': metadata})
282 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200283 resp, body = self.put(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800284 body = json.loads(body)
285 return resp, body['metadata']
286
287 def update_volume_metadata_item(self, volume_id, id, meta_item):
288 """Update metadata item for the volume."""
289 put_body = json.dumps({'meta': meta_item})
290 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200291 resp, body = self.put(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800292 body = json.loads(body)
293 return resp, body['meta']
294
295 def delete_volume_metadata_item(self, volume_id, id):
296 """Delete metadata item for the volume."""
297 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200298 resp, body = self.delete(url)
huangtianhua0ff41682013-12-16 14:49:31 +0800299 return resp, body