blob: 05242128d3f5ec14d7cd8f3bbb9a575cbb7b9069 [file] [log] [blame]
Zhi Kun Liu4be2f602014-02-08 11:52:05 +08001# Copyright 2012 OpenStack Foundation
2# 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
16import json
17import time
18import urllib
19
20from tempest.common.rest_client import RestClient
21from tempest import config
22from tempest import exceptions
23
24CONF = config.CONF
25
26
27class VolumesClientJSON(RestClient):
28 """
29 Client class to send CRUD Volume API requests to a Cinder endpoint
30 """
31
32 def __init__(self, auth_provider):
33 super(VolumesClientJSON, self).__init__(auth_provider)
34
35 self.service = CONF.volume.catalog_type
36 self.build_interval = CONF.volume.build_interval
37 self.build_timeout = CONF.volume.build_timeout
38
39 def get_attachment_from_volume(self, volume):
40 """Return the element 'attachment' from input volumes."""
41 return volume['attachments'][0]
42
43 def list_volumes(self, params=None):
44 """List all the volumes created."""
45 url = 'volumes'
46 if params:
47 url += '?%s' % urllib.urlencode(params)
48
49 resp, body = self.get(url)
50 body = json.loads(body)
51 return resp, body['volumes']
52
53 def list_volumes_with_detail(self, params=None):
54 """List the details of all volumes."""
55 url = 'volumes/detail'
56 if params:
57 url += '?%s' % urllib.urlencode(params)
58
59 resp, body = self.get(url)
60 body = json.loads(body)
61 return resp, body['volumes']
62
63 def get_volume(self, volume_id):
64 """Returns the details of a single volume."""
65 url = "volumes/%s" % str(volume_id)
66 resp, body = self.get(url)
67 body = json.loads(body)
68 return resp, body['volume']
69
70 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.
77 volume_type: Optional Name of volume_type for the volume
78 snapshot_id: When specified the volume is created from this snapshot
79 imageRef: When specified the volume is created from this image
80 """
81 post_body = {'size': size}
82 post_body.update(kwargs)
83 post_body = json.dumps({'volume': post_body})
84 resp, body = self.post('volumes', post_body, self.headers)
85 body = json.loads(body)
86 return resp, body['volume']
87
88 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
96 def delete_volume(self, volume_id):
97 """Deletes the Specified Volume."""
98 return self.delete("volumes/%s" % str(volume_id))
99
100 def upload_volume(self, volume_id, image_name, disk_format):
101 """Uploads a volume in Glance."""
102 post_body = {
103 'image_name': image_name,
104 'disk_format': disk_format
105 }
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
112 def attach_volume(self, volume_id, instance_uuid, mountpoint):
113 """Attaches a volume to a given instance on a given mountpoint."""
114 post_body = {
115 'instance_uuid': instance_uuid,
116 'mountpoint': mountpoint,
117 }
118 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):
124 """Detaches a volume from an instance."""
125 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
131 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
147 def wait_for_volume_status(self, volume_id, status):
148 """Waits for a Volume to reach a given status."""
149 resp, body = self.get_volume(volume_id)
150 volume_name = body['display_name']
151 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':
159 raise exceptions.VolumeBuildErrorException(volume_id=volume_id)
160
161 if int(time.time()) - start >= self.build_timeout:
162 message = ('Volume %s failed to reach %s status within '
163 'the required time (%s s).' %
164 (volume_name, status, self.build_timeout))
165 raise exceptions.TimeoutException(message)
166
167 def is_resource_deleted(self, id):
168 try:
169 self.get_volume(id)
170 except exceptions.NotFound:
171 return True
172 return False
173
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
183
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
204
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']
249
250 def update_volume_readonly(self, volume_id, readonly):
251 """Update the Specified Volume readonly."""
252 post_body = {
253 'readonly': readonly
254 }
255 post_body = json.dumps({'os-update_readonly_flag': post_body})
256 url = 'volumes/%s/action' % (volume_id)
257 resp, body = self.post(url, post_body, self.headers)
258 return resp, body
259
260 def force_delete_volume(self, volume_id):
261 """Force Delete Volume."""
262 post_body = json.dumps({'os-force_delete': {}})
263 resp, body = self.post('volumes/%s/action' % volume_id, post_body,
264 self.headers)
265 return resp, body
266
267 def create_volume_metadata(self, volume_id, metadata):
268 """Create metadata for the volume."""
269 put_body = json.dumps({'metadata': metadata})
270 url = "volumes/%s/metadata" % str(volume_id)
271 resp, body = self.post(url, put_body, self.headers)
272 body = json.loads(body)
273 return resp, body['metadata']
274
275 def get_volume_metadata(self, volume_id):
276 """Get metadata of the volume."""
277 url = "volumes/%s/metadata" % str(volume_id)
278 resp, body = self.get(url, self.headers)
279 body = json.loads(body)
280 return resp, body['metadata']
281
282 def update_volume_metadata(self, volume_id, metadata):
283 """Update metadata for the volume."""
284 put_body = json.dumps({'metadata': metadata})
285 url = "volumes/%s/metadata" % str(volume_id)
286 resp, body = self.put(url, put_body, self.headers)
287 body = json.loads(body)
288 return resp, body['metadata']
289
290 def update_volume_metadata_item(self, volume_id, id, meta_item):
291 """Update metadata item for the volume."""
292 put_body = json.dumps({'meta': meta_item})
293 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
294 resp, body = self.put(url, put_body, self.headers)
295 body = json.loads(body)
296 return resp, body['meta']
297
298 def delete_volume_metadata_item(self, volume_id, id):
299 """Delete metadata item for the volume."""
300 url = "volumes/%s/metadata/%s" % (str(volume_id), str(id))
301 resp, body = self.delete(url, self.headers)
302 return resp, body