blob: 93b28a20a0e69016d8579118ae9376c6b888cc78 [file] [log] [blame]
Rohit Karajgidd47d7e2012-07-31 04:11:01 -07001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
Rohit Karajgidd47d7e2012-07-31 04:11:01 -07004# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053018import json
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053019import time
Matthew Treinish26dd0fa2012-12-04 17:14:37 -050020import urllib
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053021
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070022from tempest.common.rest_client import RestClient
23from tempest import exceptions
24
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053025
Matthew Treinish9854d5b2012-09-20 10:22:13 -040026class VolumesClientJSON(RestClient):
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070027 """
28 Client class to send CRUD Volume API requests to a Cinder endpoint
29 """
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053030
chris fattarsi5098fa22012-04-17 13:27:00 -070031 def __init__(self, config, username, password, auth_url, tenant_name=None):
Matthew Treinish9854d5b2012-09-20 10:22:13 -040032 super(VolumesClientJSON, self).__init__(config, username, password,
Zhongyue Luo79d8d362012-09-25 13:49:27 +080033 auth_url, tenant_name)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070034
35 self.service = self.config.volume.catalog_type
36 self.build_interval = self.config.volume.build_interval
37 self.build_timeout = self.config.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})
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070084 resp, body = self.post('volumes', post_body, self.headers)
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})
91 resp, body = self.put('volumes/%s' % volume_id, put_body,
92 self.headers)
93 body = json.loads(body)
94 return resp, body['volume']
95
rajalakshmi-ganesanddd9e0e2012-03-21 00:49:22 +053096 def delete_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -050097 """Deletes the Specified Volume."""
Rohit Karajgidd47d7e2012-07-31 04:11:01 -070098 return self.delete("volumes/%s" % str(volume_id))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +053099
Ryan Hsua67f4632013-08-29 16:03:06 -0700100 def upload_volume(self, volume_id, image_name, disk_format):
Giulio Fidente884e9da2013-06-21 17:25:42 +0200101 """Uploads a volume in Glance."""
102 post_body = {
103 'image_name': image_name,
Ryan Hsua67f4632013-08-29 16:03:06 -0700104 'disk_format': disk_format
Giulio Fidente884e9da2013-06-21 17:25:42 +0200105 }
106 post_body = json.dumps({'os-volume_upload_image': post_body})
107 url = 'volumes/%s/action' % (volume_id)
108 resp, body = self.post(url, post_body, self.headers)
109 body = json.loads(body)
110 return resp, body['os-volume_upload_image']
111
Rohit Karajgia42fe442012-09-21 03:08:33 -0700112 def attach_volume(self, volume_id, instance_uuid, mountpoint):
Sean Daguef237ccb2013-01-04 15:19:14 -0500113 """Attaches a volume to a given instance on a given mountpoint."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700114 post_body = {
Zhongyue Luo30a563f2012-09-30 23:43:50 +0900115 'instance_uuid': instance_uuid,
116 'mountpoint': mountpoint,
117 }
Rohit Karajgia42fe442012-09-21 03:08:33 -0700118 post_body = json.dumps({'os-attach': post_body})
119 url = 'volumes/%s/action' % (volume_id)
120 resp, body = self.post(url, post_body, self.headers)
121 return resp, body
122
123 def detach_volume(self, volume_id):
Sean Daguef237ccb2013-01-04 15:19:14 -0500124 """Detaches a volume from an instance."""
Rohit Karajgia42fe442012-09-21 03:08:33 -0700125 post_body = {}
126 post_body = json.dumps({'os-detach': post_body})
127 url = 'volumes/%s/action' % (volume_id)
128 resp, body = self.post(url, post_body, self.headers)
129 return resp, body
130
zhangyanzi6b632432013-10-24 19:08:50 +0800131 def reserve_volume(self, volume_id):
132 """Reserves a volume."""
133 post_body = {}
134 post_body = json.dumps({'os-reserve': post_body})
135 url = 'volumes/%s/action' % (volume_id)
136 resp, body = self.post(url, post_body, self.headers)
137 return resp, body
138
139 def unreserve_volume(self, volume_id):
140 """Restore a reserved volume ."""
141 post_body = {}
142 post_body = json.dumps({'os-unreserve': post_body})
143 url = 'volumes/%s/action' % (volume_id)
144 resp, body = self.post(url, post_body, self.headers)
145 return resp, body
146
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530147 def wait_for_volume_status(self, volume_id, status):
Sean Daguef237ccb2013-01-04 15:19:14 -0500148 """Waits for a Volume to reach a given status."""
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530149 resp, body = self.get_volume(volume_id)
Rohit Karajgidd47d7e2012-07-31 04:11:01 -0700150 volume_name = body['display_name']
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530151 volume_status = body['status']
152 start = int(time.time())
153
154 while volume_status != status:
155 time.sleep(self.build_interval)
156 resp, body = self.get_volume(volume_id)
157 volume_status = body['status']
158 if volume_status == 'error':
rajalakshmi-ganesane3bb58f2012-05-16 12:01:15 +0530159 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530160
161 if int(time.time()) - start >= self.build_timeout:
Zhongyue Luo79d8d362012-09-25 13:49:27 +0800162 message = ('Volume %s failed to reach %s status within '
163 'the required time (%s s).' %
164 (volume_name, status, self.build_timeout))
rajalakshmi-ganesanb4465572012-03-22 01:22:50 +0530165 raise exceptions.TimeoutException(message)
David Kranz6aceb4a2012-06-05 14:05:45 -0400166
167 def is_resource_deleted(self, id):
168 try:
Attila Fazekasf53172c2013-01-26 01:04:42 +0100169 self.get_volume(id)
David Kranz6aceb4a2012-06-05 14:05:45 -0400170 except exceptions.NotFound:
171 return True
172 return False
wanghao5b981752013-10-22 11:41:41 +0800173
174 def extend_volume(self, volume_id, extend_size):
175 """Extend a volume."""
176 post_body = {
177 'new_size': extend_size
178 }
179 post_body = json.dumps({'os-extend': post_body})
180 url = 'volumes/%s/action' % (volume_id)
181 resp, body = self.post(url, post_body, self.headers)
182 return resp, body
wanghaoaa1f2f92013-10-10 11:30:37 +0800183
184 def reset_volume_status(self, volume_id, status):
185 """Reset the Specified Volume's Status."""
186 post_body = json.dumps({'os-reset_status': {"status": status}})
187 resp, body = self.post('volumes/%s/action' % volume_id, post_body,
188 self.headers)
189 return resp, body
190
191 def volume_begin_detaching(self, volume_id):
192 """Volume Begin Detaching."""
193 post_body = json.dumps({'os-begin_detaching': {}})
194 resp, body = self.post('volumes/%s/action' % volume_id, post_body,
195 self.headers)
196 return resp, body
197
198 def volume_roll_detaching(self, volume_id):
199 """Volume Roll Detaching."""
200 post_body = json.dumps({'os-roll_detaching': {}})
201 resp, body = self.post('volumes/%s/action' % volume_id, post_body,
202 self.headers)
203 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})
213 resp, body = self.post('os-volume-transfer',
214 post_body,
215 self.headers)
216 body = json.loads(body)
217 return resp, body['transfer']
218
219 def get_volume_transfer(self, transfer_id):
220 """Returns the details of a volume transfer."""
221 url = "os-volume-transfer/%s" % str(transfer_id)
222 resp, body = self.get(url, self.headers)
223 body = json.loads(body)
224 return resp, body['transfer']
225
226 def list_volume_transfers(self, params=None):
227 """List all the volume transfers created."""
228 url = 'os-volume-transfer'
229 if params:
230 url += '?%s' % urllib.urlencode(params)
231 resp, body = self.get(url)
232 body = json.loads(body)
233 return resp, body['transfers']
234
235 def delete_volume_transfer(self, transfer_id):
236 """Delete a volume transfer."""
237 return self.delete("os-volume-transfer/%s" % str(transfer_id))
238
239 def accept_volume_transfer(self, transfer_id, transfer_auth_key):
240 """Accept a volume transfer."""
241 post_body = {
242 'auth_key': transfer_auth_key,
243 }
244 url = 'os-volume-transfer/%s/accept' % transfer_id
245 post_body = json.dumps({'accept': post_body})
246 resp, body = self.post(url, post_body, self.headers)
247 body = json.loads(body)
248 return resp, body['transfer']