blob: 6c9749756f9f9e14b8bd6a39305e10a2bc99e9ba [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
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +080027class BaseVolumesClientJSON(rest_client.RestClient):
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070028 """
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +080029 Base client class to send CRUD Volume API requests to a Cinder endpoint
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070030 """
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053031
Andrea Frittoli8bbdb162014-01-06 11:06:13 +000032 def __init__(self, auth_provider):
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +080033 super(BaseVolumesClientJSON, 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:
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +080075 display_name: Optional Volume Name(only for V1).
76 name: Optional Volume Name(only for V2).
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053077 metadata: A dictionary of values to be used as metadata.
Rohan Rhishikesh Kanadec316f0a2012-12-04 05:44:39 -080078 volume_type: Optional Name of volume_type for the volume
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010079 snapshot_id: When specified the volume is created from this snapshot
Giulio Fidente36836c42013-04-05 15:43:51 +020080 imageRef: When specified the volume is created from this image
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053081 """
Jerry Cai9733d0e2014-03-19 15:50:49 +080082 # for bug #1293885:
83 # If no size specified, read volume size from CONF
84 if size is None:
85 size = CONF.volume.volume_size
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010086 post_body = {'size': size}
87 post_body.update(kwargs)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053088 post_body = json.dumps({'volume': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020089 resp, body = self.post('volumes', post_body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053090 body = json.loads(body)
91 return resp, body['volume']
92
QingXin Meng611768a2013-09-18 00:51:33 -070093 def update_volume(self, volume_id, **kwargs):
94 """Updates the Specified Volume."""
95 put_body = json.dumps({'volume': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020096 resp, body = self.put('volumes/%s' % volume_id, put_body)
QingXin Meng611768a2013-09-18 00:51:33 -070097 body = json.loads(body)
98 return resp, body['volume']
99
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +0530100 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500101 """Deletes the Specified Volume."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700102 return self.delete("volumes/%s" % str(volume_id))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530103
Ryan Hsua67f4632013-08-29 16:03:06 -0700104 def upload_volume(self, volume_id, image_name, disk_format):
Giulio Fidente884e9da2013-06-21 17:25:42 +0200105 """Uploads a volume in Glance."""
106 post_body = {
107 'image_name': image_name,
Ryan Hsua67f4632013-08-29 16:03:06 -0700108 'disk_format': disk_format
Giulio Fidente884e9da2013-06-21 17:25:42 +0200109 }
110 post_body = json.dumps({'os-volume_upload_image': post_body})
111 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200112 resp, body = self.post(url, post_body)
Giulio Fidente884e9da2013-06-21 17:25:42 +0200113 body = json.loads(body)
114 return resp, body['os-volume_upload_image']
115
Rohit Karajgia42fe442012-09-21 03:08:33 -0700116 def attach_volume(self, volume_id, instance_uuid, mountpoint):
Sean Daguef237ccb2013-01-04 15:19:14 -0500117 """Attaches a volume to a given instance on a given mountpoint."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700118 post_body = {
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900119 'instance_uuid': instance_uuid,
120 'mountpoint': mountpoint,
121 }
Rohit Karajgia42fe442012-09-21 03:08:33 -0700122 post_body = json.dumps({'os-attach': post_body})
123 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200124 resp, body = self.post(url, post_body)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700125 return resp, body
126
127 def detach_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500128 """Detaches a volume from an instance."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700129 post_body = {}
130 post_body = json.dumps({'os-detach': post_body})
131 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200132 resp, body = self.post(url, post_body)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700133 return resp, body
134
zhangyanzi6b632432013-10-24 19:08:50 +0800135 def reserve_volume(self, volume_id):
136 """Reserves a volume."""
137 post_body = {}
138 post_body = json.dumps({'os-reserve': post_body})
139 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200140 resp, body = self.post(url, post_body)
zhangyanzi6b632432013-10-24 19:08:50 +0800141 return resp, body
142
143 def unreserve_volume(self, volume_id):
144 """Restore a reserved volume ."""
145 post_body = {}
146 post_body = json.dumps({'os-unreserve': post_body})
147 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200148 resp, body = self.post(url, post_body)
zhangyanzi6b632432013-10-24 19:08:50 +0800149 return resp, body
150
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530151 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500152 """Waits for a Volume to reach a given status."""
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530153 resp, body = self.get_volume(volume_id)
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:
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +0800165 message = 'Volume %s failed to reach %s status within '\
166 'the required time (%s s).' % (volume_id,
167 status,
168 self.build_timeout)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530169 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400170
171 def is_resource_deleted(self, id):
172 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100173 self.get_volume(id)
David Kranz6aceb4a2012-06-05 14:05:45 -0400174 except exceptions.NotFound:
175 return True
176 return False
wanghao5b981752013-10-22 11:41:41 +0800177
178 def extend_volume(self, volume_id, extend_size):
179 """Extend a volume."""
180 post_body = {
181 'new_size': extend_size
182 }
183 post_body = json.dumps({'os-extend': post_body})
184 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200185 resp, body = self.post(url, post_body)
wanghao5b981752013-10-22 11:41:41 +0800186 return resp, body
wanghaoaa1f2f92013-10-10 11:30:37 +0800187
188 def reset_volume_status(self, volume_id, status):
189 """Reset the Specified Volume's Status."""
190 post_body = json.dumps({'os-reset_status': {"status": status}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200191 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800192 return resp, body
193
194 def volume_begin_detaching(self, volume_id):
195 """Volume Begin Detaching."""
196 post_body = json.dumps({'os-begin_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200197 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800198 return resp, body
199
200 def volume_roll_detaching(self, volume_id):
201 """Volume Roll Detaching."""
202 post_body = json.dumps({'os-roll_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200203 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800204 return resp, body
wingwjcbd82dc2013-10-22 16:38:39 +0800205
206 def create_volume_transfer(self, vol_id, display_name=None):
207 """Create a volume transfer."""
208 post_body = {
209 'volume_id': vol_id
210 }
211 if display_name:
212 post_body['name'] = display_name
213 post_body = json.dumps({'transfer': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200214 resp, body = self.post('os-volume-transfer', post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800215 body = json.loads(body)
216 return resp, body['transfer']
217
218 def get_volume_transfer(self, transfer_id):
219 """Returns the details of a volume transfer."""
220 url = "os-volume-transfer/%s" % str(transfer_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200221 resp, body = self.get(url)
wingwjcbd82dc2013-10-22 16:38:39 +0800222 body = json.loads(body)
223 return resp, body['transfer']
224
225 def list_volume_transfers(self, params=None):
226 """List all the volume transfers created."""
227 url = 'os-volume-transfer'
228 if params:
229 url += '?%s' % urllib.urlencode(params)
230 resp, body = self.get(url)
231 body = json.loads(body)
232 return resp, body['transfers']
233
234 def delete_volume_transfer(self, transfer_id):
235 """Delete a volume transfer."""
236 return self.delete("os-volume-transfer/%s" % str(transfer_id))
237
238 def accept_volume_transfer(self, transfer_id, transfer_auth_key):
239 """Accept a volume transfer."""
240 post_body = {
241 'auth_key': transfer_auth_key,
242 }
243 url = 'os-volume-transfer/%s/accept' % transfer_id
244 post_body = json.dumps({'accept': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200245 resp, body = self.post(url, post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800246 body = json.loads(body)
247 return resp, body['transfer']
zhangyanziaa180072013-11-21 12:31:26 +0800248
249 def update_volume_readonly(self, volume_id, readonly):
250 """Update the Specified Volume readonly."""
251 post_body = {
252 'readonly': readonly
253 }
254 post_body = json.dumps({'os-update_readonly_flag': post_body})
255 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200256 resp, body = self.post(url, post_body)
zhangyanziaa180072013-11-21 12:31:26 +0800257 return resp, body
wanghao9d3d6cb2013-11-12 15:10:10 +0800258
259 def force_delete_volume(self, volume_id):
260 """Force Delete Volume."""
261 post_body = json.dumps({'os-force_delete': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200262 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghao9d3d6cb2013-11-12 15:10:10 +0800263 return resp, body
huangtianhua0ff41682013-12-16 14:49:31 +0800264
265 def create_volume_metadata(self, volume_id, metadata):
266 """Create metadata for the volume."""
267 put_body = json.dumps({'metadata': metadata})
268 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200269 resp, body = self.post(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800270 body = json.loads(body)
271 return resp, body['metadata']
272
273 def get_volume_metadata(self, volume_id):
274 """Get metadata of the volume."""
275 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200276 resp, body = self.get(url)
huangtianhua0ff41682013-12-16 14:49:31 +0800277 body = json.loads(body)
278 return resp, body['metadata']
279
280 def update_volume_metadata(self, volume_id, metadata):
281 """Update metadata for the volume."""
282 put_body = json.dumps({'metadata': metadata})
283 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200284 resp, body = self.put(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800285 body = json.loads(body)
286 return resp, body['metadata']
287
288 def update_volume_metadata_item(self, volume_id, id, meta_item):
289 """Update metadata item for the volume."""
290 put_body = json.dumps({'meta': meta_item})
291 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200292 resp, body = self.put(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800293 body = json.loads(body)
294 return resp, body['meta']
295
296 def delete_volume_metadata_item(self, volume_id, id):
297 """Delete metadata item for the volume."""
298 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200299 resp, body = self.delete(url)
huangtianhua0ff41682013-12-16 14:49:31 +0800300 return resp, body
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +0800301
302
303class VolumesClientJSON(BaseVolumesClientJSON):
304 """
305 Client class to send CRUD Volume V1 API requests to a Cinder endpoint
306 """