blob: 2183c569a12e9010e7bade2d69063178edb973f2 [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
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070020from tempest.common.rest_client import RestClient
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
Matthew Treinish9854d5b2012-09-20 10:22:13 -040027class VolumesClientJSON(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
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053070 def create_volume(self, size, **kwargs):
71 """
72 Creates a new Volume.
73 size(Required): Size of volume in GB.
74 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 """
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010081 post_body = {'size': size}
82 post_body.update(kwargs)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053083 post_body = json.dumps({'volume': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020084 resp, body = self.post('volumes', post_body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053085 body = json.loads(body)
86 return resp, body['volume']
87
QingXin Meng611768a2013-09-18 00:51:33 -070088 def update_volume(self, volume_id, **kwargs):
89 """Updates the Specified Volume."""
90 put_body = json.dumps({'volume': kwargs})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020091 resp, body = self.put('volumes/%s' % volume_id, put_body)
QingXin Meng611768a2013-09-18 00:51:33 -070092 body = json.loads(body)
93 return resp, body['volume']
94
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053095 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050096 """Deletes the Specified Volume."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070097 return self.delete("volumes/%s" % str(volume_id))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053098
Ryan Hsua67f4632013-08-29 16:03:06 -070099 def upload_volume(self, volume_id, image_name, disk_format):
Giulio Fidente884e9da2013-06-21 17:25:42 +0200100 """Uploads a volume in Glance."""
101 post_body = {
102 'image_name': image_name,
Ryan Hsua67f4632013-08-29 16:03:06 -0700103 'disk_format': disk_format
Giulio Fidente884e9da2013-06-21 17:25:42 +0200104 }
105 post_body = json.dumps({'os-volume_upload_image': post_body})
106 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200107 resp, body = self.post(url, post_body)
Giulio Fidente884e9da2013-06-21 17:25:42 +0200108 body = json.loads(body)
109 return resp, body['os-volume_upload_image']
110
Rohit Karajgia42fe442012-09-21 03:08:33 -0700111 def attach_volume(self, volume_id, instance_uuid, mountpoint):
Sean Daguef237ccb2013-01-04 15:19:14 -0500112 """Attaches a volume to a given instance on a given mountpoint."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700113 post_body = {
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900114 'instance_uuid': instance_uuid,
115 'mountpoint': mountpoint,
116 }
Rohit Karajgia42fe442012-09-21 03:08:33 -0700117 post_body = json.dumps({'os-attach': post_body})
118 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200119 resp, body = self.post(url, post_body)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700120 return resp, body
121
122 def detach_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500123 """Detaches a volume from an instance."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700124 post_body = {}
125 post_body = json.dumps({'os-detach': post_body})
126 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200127 resp, body = self.post(url, post_body)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700128 return resp, body
129
zhangyanzi6b632432013-10-24 19:08:50 +0800130 def reserve_volume(self, volume_id):
131 """Reserves a volume."""
132 post_body = {}
133 post_body = json.dumps({'os-reserve': post_body})
134 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200135 resp, body = self.post(url, post_body)
zhangyanzi6b632432013-10-24 19:08:50 +0800136 return resp, body
137
138 def unreserve_volume(self, volume_id):
139 """Restore a reserved volume ."""
140 post_body = {}
141 post_body = json.dumps({'os-unreserve': post_body})
142 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200143 resp, body = self.post(url, post_body)
zhangyanzi6b632432013-10-24 19:08:50 +0800144 return resp, body
145
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530146 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500147 """Waits for a Volume to reach a given status."""
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530148 resp, body = self.get_volume(volume_id)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700149 volume_name = body['display_name']
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530150 volume_status = body['status']
151 start = int(time.time())
152
153 while volume_status != status:
154 time.sleep(self.build_interval)
155 resp, body = self.get_volume(volume_id)
156 volume_status = body['status']
157 if volume_status == 'error':
rajalakshmi-ganesane3bb58f2012-05-16 12:01:15 +0530158 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530159
160 if int(time.time()) - start >= self.build_timeout:
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800161 message = ('Volume %s failed to reach %s status within '
162 'the required time (%s s).' %
163 (volume_name, status, self.build_timeout))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530164 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400165
166 def is_resource_deleted(self, id):
167 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100168 self.get_volume(id)
David Kranz6aceb4a2012-06-05 14:05:45 -0400169 except exceptions.NotFound:
170 return True
171 return False
wanghao5b981752013-10-22 11:41:41 +0800172
173 def extend_volume(self, volume_id, extend_size):
174 """Extend a volume."""
175 post_body = {
176 'new_size': extend_size
177 }
178 post_body = json.dumps({'os-extend': post_body})
179 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200180 resp, body = self.post(url, post_body)
wanghao5b981752013-10-22 11:41:41 +0800181 return resp, body
wanghaoaa1f2f92013-10-10 11:30:37 +0800182
183 def reset_volume_status(self, volume_id, status):
184 """Reset the Specified Volume's Status."""
185 post_body = json.dumps({'os-reset_status': {"status": status}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200186 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800187 return resp, body
188
189 def volume_begin_detaching(self, volume_id):
190 """Volume Begin Detaching."""
191 post_body = json.dumps({'os-begin_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200192 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800193 return resp, body
194
195 def volume_roll_detaching(self, volume_id):
196 """Volume Roll Detaching."""
197 post_body = json.dumps({'os-roll_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200198 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghaoaa1f2f92013-10-10 11:30:37 +0800199 return resp, body
wingwjcbd82dc2013-10-22 16:38:39 +0800200
201 def create_volume_transfer(self, vol_id, display_name=None):
202 """Create a volume transfer."""
203 post_body = {
204 'volume_id': vol_id
205 }
206 if display_name:
207 post_body['name'] = display_name
208 post_body = json.dumps({'transfer': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200209 resp, body = self.post('os-volume-transfer', post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800210 body = json.loads(body)
211 return resp, body['transfer']
212
213 def get_volume_transfer(self, transfer_id):
214 """Returns the details of a volume transfer."""
215 url = "os-volume-transfer/%s" % str(transfer_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200216 resp, body = self.get(url)
wingwjcbd82dc2013-10-22 16:38:39 +0800217 body = json.loads(body)
218 return resp, body['transfer']
219
220 def list_volume_transfers(self, params=None):
221 """List all the volume transfers created."""
222 url = 'os-volume-transfer'
223 if params:
224 url += '?%s' % urllib.urlencode(params)
225 resp, body = self.get(url)
226 body = json.loads(body)
227 return resp, body['transfers']
228
229 def delete_volume_transfer(self, transfer_id):
230 """Delete a volume transfer."""
231 return self.delete("os-volume-transfer/%s" % str(transfer_id))
232
233 def accept_volume_transfer(self, transfer_id, transfer_auth_key):
234 """Accept a volume transfer."""
235 post_body = {
236 'auth_key': transfer_auth_key,
237 }
238 url = 'os-volume-transfer/%s/accept' % transfer_id
239 post_body = json.dumps({'accept': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200240 resp, body = self.post(url, post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800241 body = json.loads(body)
242 return resp, body['transfer']
zhangyanziaa180072013-11-21 12:31:26 +0800243
244 def update_volume_readonly(self, volume_id, readonly):
245 """Update the Specified Volume readonly."""
246 post_body = {
247 'readonly': readonly
248 }
249 post_body = json.dumps({'os-update_readonly_flag': post_body})
250 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200251 resp, body = self.post(url, post_body)
zhangyanziaa180072013-11-21 12:31:26 +0800252 return resp, body
wanghao9d3d6cb2013-11-12 15:10:10 +0800253
254 def force_delete_volume(self, volume_id):
255 """Force Delete Volume."""
256 post_body = json.dumps({'os-force_delete': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200257 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
wanghao9d3d6cb2013-11-12 15:10:10 +0800258 return resp, body
huangtianhua0ff41682013-12-16 14:49:31 +0800259
260 def create_volume_metadata(self, volume_id, metadata):
261 """Create metadata for the volume."""
262 put_body = json.dumps({'metadata': metadata})
263 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200264 resp, body = self.post(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800265 body = json.loads(body)
266 return resp, body['metadata']
267
268 def get_volume_metadata(self, volume_id):
269 """Get metadata of the volume."""
270 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200271 resp, body = self.get(url)
huangtianhua0ff41682013-12-16 14:49:31 +0800272 body = json.loads(body)
273 return resp, body['metadata']
274
275 def update_volume_metadata(self, volume_id, metadata):
276 """Update metadata for the volume."""
277 put_body = json.dumps({'metadata': metadata})
278 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200279 resp, body = self.put(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800280 body = json.loads(body)
281 return resp, body['metadata']
282
283 def update_volume_metadata_item(self, volume_id, id, meta_item):
284 """Update metadata item for the volume."""
285 put_body = json.dumps({'meta': meta_item})
286 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200287 resp, body = self.put(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800288 body = json.loads(body)
289 return resp, body['meta']
290
291 def delete_volume_metadata_item(self, volume_id, id):
292 """Delete metadata item for the volume."""
293 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200294 resp, body = self.delete(url)
huangtianhua0ff41682013-12-16 14:49:31 +0800295 return resp, body