blob: f19718e4b8759b82fd8aae91759b0b0a59321200 [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
Matthew Treinish684d8992014-01-30 16:27:40 +000020from tempest import config
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070021from tempest import exceptions
Ken'ichi Ohmichia39d0be2014-12-17 08:46:11 +000022from tempest.services.volume.json import base
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070023
Matthew Treinish684d8992014-01-30 16:27:40 +000024CONF = config.CONF
25
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053026
Ken'ichi Ohmichia39d0be2014-12-17 08:46:11 +000027class BaseVolumesClientJSON(base.VolumeClient):
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
Ken'ichi Ohmichia39d0be2014-12-17 08:46:11 +000032 create_resp = 200
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053033
anju tiwari789449a2013-08-29 16:56:17 +053034 def get_attachment_from_volume(self, volume):
35 """Return the element 'attachment' from input volumes."""
36 return volume['attachments'][0]
37
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053038 def list_volumes(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050039 """List all the volumes created."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070040 url = 'volumes'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050041 if params:
42 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053043
chris fattarsi5098fa22012-04-17 13:27:00 -070044 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053045 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000046 self.expected_success(200, resp.status)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053047 return resp, body['volumes']
48
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053049 def list_volumes_with_detail(self, params=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050050 """List the details of all volumes."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070051 url = 'volumes/detail'
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050052 if params:
53 url += '?%s' % urllib.urlencode(params)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053054
chris fattarsi5098fa22012-04-17 13:27:00 -070055 resp, body = self.get(url)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053056 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000057 self.expected_success(200, resp.status)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053058 return resp, body['volumes']
59
Attila Fazekasb8aa7592013-01-26 01:25:45 +010060 def get_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050061 """Returns the details of a single volume."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070062 url = "volumes/%s" % str(volume_id)
Attila Fazekasb8aa7592013-01-26 01:25:45 +010063 resp, body = self.get(url)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053064 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000065 self.expected_success(200, resp.status)
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053066 return resp, body['volume']
67
Jerry Cai9733d0e2014-03-19 15:50:49 +080068 def create_volume(self, size=None, **kwargs):
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053069 """
70 Creates a new Volume.
Jerry Cai9733d0e2014-03-19 15:50:49 +080071 size: Size of volume in GB.
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053072 Following optional keyword arguments are accepted:
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +080073 display_name: Optional Volume Name(only for V1).
74 name: Optional Volume Name(only for V2).
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053075 metadata: A dictionary of values to be used as metadata.
Rohan Rhishikesh Kanadec316f0a2012-12-04 05:44:39 -080076 volume_type: Optional Name of volume_type for the volume
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010077 snapshot_id: When specified the volume is created from this snapshot
Giulio Fidente36836c42013-04-05 15:43:51 +020078 imageRef: When specified the volume is created from this image
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053079 """
Jerry Cai9733d0e2014-03-19 15:50:49 +080080 # for bug #1293885:
81 # If no size specified, read volume size from CONF
82 if size is None:
83 size = CONF.volume.volume_size
Attila Fazekas36b1fcf2013-01-31 16:41:04 +010084 post_body = {'size': size}
85 post_body.update(kwargs)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053086 post_body = json.dumps({'volume': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +020087 resp, body = self.post('volumes', post_body)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053088 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000089 self.expected_success(self.create_resp, resp.status)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053090 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)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +000097 self.expected_success(200, resp.status)
QingXin Meng611768a2013-09-18 00:51:33 -070098 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."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000102 resp, body = self.delete("volumes/%s" % str(volume_id))
103 self.expected_success(202, resp.status)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530104
Ryan Hsua67f4632013-08-29 16:03:06 -0700105 def upload_volume(self, volume_id, image_name, disk_format):
Giulio Fidente884e9da2013-06-21 17:25:42 +0200106 """Uploads a volume in Glance."""
107 post_body = {
108 'image_name': image_name,
Ryan Hsua67f4632013-08-29 16:03:06 -0700109 'disk_format': disk_format
Giulio Fidente884e9da2013-06-21 17:25:42 +0200110 }
111 post_body = json.dumps({'os-volume_upload_image': post_body})
112 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200113 resp, body = self.post(url, post_body)
Giulio Fidente884e9da2013-06-21 17:25:42 +0200114 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000115 self.expected_success(202, resp.status)
Giulio Fidente884e9da2013-06-21 17:25:42 +0200116 return resp, body['os-volume_upload_image']
117
Rohit Karajgia42fe442012-09-21 03:08:33 -0700118 def attach_volume(self, volume_id, instance_uuid, mountpoint):
Sean Daguef237ccb2013-01-04 15:19:14 -0500119 """Attaches a volume to a given instance on a given mountpoint."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700120 post_body = {
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900121 'instance_uuid': instance_uuid,
122 'mountpoint': mountpoint,
123 }
Rohit Karajgia42fe442012-09-21 03:08:33 -0700124 post_body = json.dumps({'os-attach': post_body})
125 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200126 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000127 self.expected_success(202, resp.status)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700128 return resp, body
129
130 def detach_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500131 """Detaches a volume from an instance."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700132 post_body = {}
133 post_body = json.dumps({'os-detach': post_body})
134 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200135 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000136 self.expected_success(202, resp.status)
Rohit Karajgia42fe442012-09-21 03:08:33 -0700137 return resp, body
138
zhangyanzi6b632432013-10-24 19:08:50 +0800139 def reserve_volume(self, volume_id):
140 """Reserves a volume."""
141 post_body = {}
142 post_body = json.dumps({'os-reserve': post_body})
143 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200144 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000145 self.expected_success(202, resp.status)
zhangyanzi6b632432013-10-24 19:08:50 +0800146 return resp, body
147
148 def unreserve_volume(self, volume_id):
149 """Restore a reserved volume ."""
150 post_body = {}
151 post_body = json.dumps({'os-unreserve': post_body})
152 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200153 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000154 self.expected_success(202, resp.status)
zhangyanzi6b632432013-10-24 19:08:50 +0800155 return resp, body
156
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530157 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500158 """Waits for a Volume to reach a given status."""
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530159 resp, body = self.get_volume(volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530160 volume_status = body['status']
161 start = int(time.time())
162
163 while volume_status != status:
164 time.sleep(self.build_interval)
165 resp, body = self.get_volume(volume_id)
166 volume_status = body['status']
167 if volume_status == 'error':
rajalakshmi-ganesane3bb58f2012-05-16 12:01:15 +0530168 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530169
170 if int(time.time()) - start >= self.build_timeout:
Martin Pavlasek1102c3a2014-10-20 17:17:55 +0200171 message = ('Volume %s failed to reach %s status (current: %s) '
172 'within the required time '
173 '(%s s).' % (volume_id,
174 status,
175 volume_status,
176 self.build_timeout))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530177 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400178
179 def is_resource_deleted(self, id):
180 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100181 self.get_volume(id)
David Kranz6aceb4a2012-06-05 14:05:45 -0400182 except exceptions.NotFound:
183 return True
184 return False
wanghao5b981752013-10-22 11:41:41 +0800185
Matt Riedemannd2b96512014-10-13 10:18:16 -0700186 @property
187 def resource_type(self):
188 """Returns the primary type of resource this client works with."""
189 return 'volume'
190
wanghao5b981752013-10-22 11:41:41 +0800191 def extend_volume(self, volume_id, extend_size):
192 """Extend a volume."""
193 post_body = {
194 'new_size': extend_size
195 }
196 post_body = json.dumps({'os-extend': post_body})
197 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200198 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000199 self.expected_success(202, resp.status)
wanghao5b981752013-10-22 11:41:41 +0800200 return resp, body
wanghaoaa1f2f92013-10-10 11:30:37 +0800201
202 def reset_volume_status(self, volume_id, status):
203 """Reset the Specified Volume's Status."""
204 post_body = json.dumps({'os-reset_status': {"status": status}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200205 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000206 self.expected_success(202, resp.status)
wanghaoaa1f2f92013-10-10 11:30:37 +0800207 return resp, body
208
209 def volume_begin_detaching(self, volume_id):
210 """Volume Begin Detaching."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000211 # ref cinder/api/contrib/volume_actions.py#L158
wanghaoaa1f2f92013-10-10 11:30:37 +0800212 post_body = json.dumps({'os-begin_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200213 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000214 self.expected_success(202, resp.status)
wanghaoaa1f2f92013-10-10 11:30:37 +0800215 return resp, body
216
217 def volume_roll_detaching(self, volume_id):
218 """Volume Roll Detaching."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000219 # cinder/api/contrib/volume_actions.py#L170
wanghaoaa1f2f92013-10-10 11:30:37 +0800220 post_body = json.dumps({'os-roll_detaching': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200221 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000222 self.expected_success(202, resp.status)
wanghaoaa1f2f92013-10-10 11:30:37 +0800223 return resp, body
wingwjcbd82dc2013-10-22 16:38:39 +0800224
225 def create_volume_transfer(self, vol_id, display_name=None):
226 """Create a volume transfer."""
227 post_body = {
228 'volume_id': vol_id
229 }
230 if display_name:
231 post_body['name'] = display_name
232 post_body = json.dumps({'transfer': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200233 resp, body = self.post('os-volume-transfer', post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800234 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000235 self.expected_success(202, resp.status)
wingwjcbd82dc2013-10-22 16:38:39 +0800236 return resp, body['transfer']
237
238 def get_volume_transfer(self, transfer_id):
239 """Returns the details of a volume transfer."""
240 url = "os-volume-transfer/%s" % str(transfer_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200241 resp, body = self.get(url)
wingwjcbd82dc2013-10-22 16:38:39 +0800242 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000243 self.expected_success(200, resp.status)
wingwjcbd82dc2013-10-22 16:38:39 +0800244 return resp, body['transfer']
245
246 def list_volume_transfers(self, params=None):
247 """List all the volume transfers created."""
248 url = 'os-volume-transfer'
249 if params:
250 url += '?%s' % urllib.urlencode(params)
251 resp, body = self.get(url)
252 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000253 self.expected_success(200, resp.status)
wingwjcbd82dc2013-10-22 16:38:39 +0800254 return resp, body['transfers']
255
256 def delete_volume_transfer(self, transfer_id):
257 """Delete a volume transfer."""
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000258 resp, body = self.delete("os-volume-transfer/%s" % str(transfer_id))
259 self.expected_success(202, resp.status)
wingwjcbd82dc2013-10-22 16:38:39 +0800260
261 def accept_volume_transfer(self, transfer_id, transfer_auth_key):
262 """Accept a volume transfer."""
263 post_body = {
264 'auth_key': transfer_auth_key,
265 }
266 url = 'os-volume-transfer/%s/accept' % transfer_id
267 post_body = json.dumps({'accept': post_body})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200268 resp, body = self.post(url, post_body)
wingwjcbd82dc2013-10-22 16:38:39 +0800269 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000270 self.expected_success(202, resp.status)
wingwjcbd82dc2013-10-22 16:38:39 +0800271 return resp, body['transfer']
zhangyanziaa180072013-11-21 12:31:26 +0800272
273 def update_volume_readonly(self, volume_id, readonly):
274 """Update the Specified Volume readonly."""
275 post_body = {
276 'readonly': readonly
277 }
278 post_body = json.dumps({'os-update_readonly_flag': post_body})
279 url = 'volumes/%s/action' % (volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200280 resp, body = self.post(url, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000281 self.expected_success(202, resp.status)
zhangyanziaa180072013-11-21 12:31:26 +0800282 return resp, body
wanghao9d3d6cb2013-11-12 15:10:10 +0800283
284 def force_delete_volume(self, volume_id):
285 """Force Delete Volume."""
286 post_body = json.dumps({'os-force_delete': {}})
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200287 resp, body = self.post('volumes/%s/action' % volume_id, post_body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000288 self.expected_success(202, resp.status)
wanghao9d3d6cb2013-11-12 15:10:10 +0800289 return resp, body
huangtianhua0ff41682013-12-16 14:49:31 +0800290
291 def create_volume_metadata(self, volume_id, metadata):
292 """Create metadata for the volume."""
293 put_body = json.dumps({'metadata': metadata})
294 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200295 resp, body = self.post(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800296 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000297 self.expected_success(200, resp.status)
huangtianhua0ff41682013-12-16 14:49:31 +0800298 return resp, body['metadata']
299
300 def get_volume_metadata(self, volume_id):
301 """Get metadata of the volume."""
302 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200303 resp, body = self.get(url)
huangtianhua0ff41682013-12-16 14:49:31 +0800304 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000305 self.expected_success(200, resp.status)
huangtianhua0ff41682013-12-16 14:49:31 +0800306 return resp, body['metadata']
307
308 def update_volume_metadata(self, volume_id, metadata):
309 """Update metadata for the volume."""
310 put_body = json.dumps({'metadata': metadata})
311 url = "volumes/%s/metadata" % str(volume_id)
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200312 resp, body = self.put(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800313 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000314 self.expected_success(200, resp.status)
huangtianhua0ff41682013-12-16 14:49:31 +0800315 return resp, body['metadata']
316
317 def update_volume_metadata_item(self, volume_id, id, meta_item):
318 """Update metadata item for the volume."""
319 put_body = json.dumps({'meta': meta_item})
320 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200321 resp, body = self.put(url, put_body)
huangtianhua0ff41682013-12-16 14:49:31 +0800322 body = json.loads(body)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000323 self.expected_success(200, resp.status)
huangtianhua0ff41682013-12-16 14:49:31 +0800324 return resp, body['meta']
325
326 def delete_volume_metadata_item(self, volume_id, id):
327 """Delete metadata item for the volume."""
328 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
Valeriy Ponomaryov88686d82014-02-16 12:24:51 +0200329 resp, body = self.delete(url)
Swapnil Kulkarnid9df38c2014-08-16 18:06:52 +0000330 self.expected_success(200, resp.status)
Zhi Kun Liu6e6cf832014-05-08 17:25:22 +0800331
332
333class VolumesClientJSON(BaseVolumesClientJSON):
334 """
335 Client class to send CRUD Volume V1 API requests to a Cinder endpoint
336 """