1 # Copyright 2012-2013 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
34 from kamaki.clients import Client
35 from kamaki.clients.utils import path4url
39 class ComputeRestClient(Client):
42 def limits_get(self, success=200, **kwargs):
43 """GET base_url/limits
45 :param success: success code or list or tupple of accepted success
46 codes. if server response code is not in this list, a ClientError
49 :returns: request response
51 path = path4url('limits')
52 return self.get(path, success=success, **kwargs)
56 server_id='', detail=False,
67 """GET base_url/servers/['detail'|<server_id>]
69 :param server_id: (int or int str) ignored if detail
71 :param detail: (boolean)
75 :param changes-since: A time/date stamp for when the server last
78 :param image: Name of the image in URL format
80 :param flavor: Name of the flavor in URL format
82 :param name: Name of the server as a string
84 :param marker: UUID of the server at which you want to set a marker
86 :param limit: (int) limit of values to return
88 :param status: Status of the server (e.g. filter on "ACTIVE")
90 :param host: Name of the host as a string
92 :returns: request response
95 self.set_param('changes-since', changes_since, iff=changes_since)
96 self.set_param('image', image, iff=image)
97 self.set_param('flavor', flavor, iff=flavor)
98 self.set_param('name', name, iff=name)
99 self.set_param('marker', marker, iff=marker)
100 self.set_param('limit', limit, iff=limit)
101 self.set_param('status', status, iff=status)
102 self.set_param('host', host, iff=host)
104 path = path4url('servers', 'detail' if detail else (server_id or ''))
105 return self.get(path, success=success, **kwargs)
111 availability_zone=None,
121 """POST base_url/servers
123 :param json_data: a json-formated dict that will be send as data
127 :param security_group: (str)
129 :param user_data: Use to pass configuration information or scripts upon
130 launch. Must be Base64 encoded.
132 :param availability_zone: (str)
134 :param server: Server
136 :param imageRef: ID or full URL.
138 :param flavorRef: ID or full URL.
140 :param name: (str) The name of the new server
142 :param metadata: (dict) Metadata key: value pairs. max size of the key
143 and value is 255 bytes each.
145 :param personality: (str) File path and contents (text only) to inject
146 into the server at launch. The maximum size of the file path data
147 is 255 bytes. The maximum limit refers to the number of bytes in
148 the decoded data and not the number of characters in the encoded
151 :returns: request response
154 self.set_param(security_group, security_group, iff=security_group)
155 self.set_param(user_data, user_data, iff=user_data)
157 availability_zone, availability_zone, iff=availability_zone)
158 self.set_param(server, server, iff=server)
159 self.set_param(imageRef, imageRef, iff=imageRef)
160 self.set_param(flavorRef, flavorRef, iff=flavorRef)
161 self.set_param(name, name, iff=name)
162 if metadata: # don't json.dump None
163 self.set_param(metadata, json.dumps(metadata))
164 self.set_param(personality, personality, iff=personality)
167 json_data = json.dumps(json_data)
168 self.set_header('Content-Type', 'application/json')
169 self.set_header('Content-Length', len(json_data))
171 path = path4url('servers')
172 return self.post(path, data=json_data, success=success, **kwargs)
176 server_id, server_name=None, json_data=None, success=204,
178 """PUT base_url/servers/<server_id>
180 :param json_data: a json-formated dict that will be send as data
182 :param success: success code (iterable of) codes
184 :raises ClientError: if returned code not in success list
186 :returns: request response
188 self.set_param('server', server_name, iff=server_name)
191 json_data = json.dumps(json_data)
192 self.set_header('Content-Type', 'application/json')
193 self.set_header('Content-Length', len(json_data))
194 path = path4url('servers', server_id)
195 return self.put(path, data=json_data, success=success, **kwargs)
197 def servers_delete(self, server_id, success=204, **kwargs):
198 """DEL ETE base_url/servers/<server_id>
200 :param json_data: a json-formated dict that will be send as data
202 :param success: success code (iterable of) codes
204 :raises ClientError: if returned code not in success list
206 :returns: request response
208 path = path4url('servers', server_id)
209 return self.delete(path, success=success, **kwargs)
211 def servers_metadata_get(self, server_id, key=None, success=200, **kwargs):
212 """GET base_url/servers/<server_id>/metadata[/key]
214 :returns: request response
216 path = path4url('servers', server_id, 'metadata', key or '')
217 return self.get(path, success=success, **kwargs)
219 def servers_metadata_post(
220 self, server_id, json_data=None, success=202, **kwargs):
221 """POST base_url/servers/<server_id>/metadata
223 :returns: request response
226 json_data = json.dumps(json_data)
227 self.set_header('Content-Type', 'application/json')
228 self.set_header('Content-Length', len(json_data))
229 path = path4url('servers', server_id, 'metadata')
230 return self.post(path, data=json_data, success=success, **kwargs)
232 def servers_metadata_put(
233 self, server_id, key=None, json_data=None, success=204, **kwargs):
234 """PUT base_url/servers/<server_id>/metadata[/key]
236 :returns: request response
239 json_data = json.dumps(json_data)
240 self.set_header('Content-Type', 'application/json')
241 self.set_header('Content-Length', len(json_data))
242 path = path4url('servers', server_id, 'metadata', key or '')
243 return self.put(path, data=json_data, success=success, **kwargs)
245 def servers_metadata_delete(self, server_id, key, success=204, **kwargs):
246 """DEL ETE base_url/servers/<server_id>/metadata[/key]
248 :returns: request response
250 path = path4url('servers', server_id, 'metadata', key)
251 return self.delete(path, success=success, **kwargs)
253 def servers_actions_post(
254 self, server_id, json_data=None, success=202, **kwargs):
255 """POST base_url/servers/<server_id>/action
257 :returns: request response
260 json_data = json.dumps(json_data)
261 self.set_header('Content-Type', 'application/json')
262 self.set_header('Content-Length', len(json_data))
263 path = path4url('servers', server_id, 'action')
264 return self.post(path, data=json_data, success=success, **kwargs)
268 network_id=None, changes_since=None, success=(304, 200),
270 """GET base_url/servers/<server_id>/ips[/network_id]
272 :param changes_since: time/date stamp in UNIX/epoch time. Checks for
273 changes since a previous request.
275 :returns: request response
277 self.set_param('changes-since', changes_since, iff=changes_since)
278 path = path4url('servers', server_id, 'ips', network_id or '')
279 return self.get(path, success=success, **kwargs)
294 """GET base_url[/image_id][/command]
296 :param image_id: (str) ignored if detail
298 :param detail: (bool)
301 :param changes_since: when the image last changed status
303 :param server_name: Name of the server in URL format
305 :param name: Name of the image
307 :param status: Status of the image (e.g. filter on "ACTIVE")
309 :param marker: UUID of the image at which you want to set a marker
311 :param limit: Integer value for the limit of values to return
313 :param type: Type of image (e.g. BASE, SERVER, or ALL)
315 :returns: request response
318 self.set_param('changes-since', changes_since, iff=changes_since)
319 self.set_param('name', name, iff=name)
320 self.set_param('status', status, iff=status)
321 self.set_param('marker', marker, iff=marker)
322 self.set_param('limit', limit, iff=limit)
323 self.set_param('type', type, iff=type)
324 self.set_param('server', server_name, iff=server_name)
326 path = path4url('images', 'detail' if detail else (image_id or ''))
327 return self.get(path, success=success, **kwargs)
329 def images_delete(self, image_id='', success=204, **kwargs):
330 """DEL ETE base_url/images/<image_id>
332 :returns: request response
334 path = path4url('images', image_id)
335 return self.delete(path, success=success, **kwargs)
337 def images_metadata_get(self, image_id, key=None, success=200, **kwargs):
338 """GET base_url/<image_id>/metadata[/key]
340 :returns: request response
342 path = path4url('images', image_id, 'metadata', key or '')
343 return self.get(path, success=success, **kwargs)
345 def images_metadata_post(
346 self, image_id, json_data=None, success=201, **kwargs):
347 """POST base_url/images/<image_id>/metadata
349 :returns: request response
351 if json_data is not None:
352 json_data = json.dumps(json_data)
353 self.set_header('Content-Type', 'application/json')
354 self.set_header('Content-Length', len(json_data))
356 path = path4url('images', image_id, 'metadata')
357 return self.post(path, data=json_data, success=success, **kwargs)
359 def images_metadata_put(
360 self, image_id, key=None, json_data=None, success=201, **kwargs):
361 """PUT base_url/images/<image_id>/metadata
363 :returns: request response
365 if json_data is not None:
366 json_data = json.dumps(json_data)
367 self.set_header('Content-Type', 'application/json')
368 self.set_header('Content-Length', len(json_data))
370 path = path4url('images', image_id, 'metadata')
371 return self.put(path, data=json_data, success=success, **kwargs)
373 def images_metadata_delete(self, image_id, key, success=204, **kwargs):
374 """DEL ETE base_url/images/<image_id>/metadata/key
376 :returns: request response
378 path = path4url('images', image_id, 'metadata', key)
379 return self.delete(path, success=success, **kwargs)
392 """GET base_url[/flavor_id][/command]
394 :param flavor_id: ignored if detail
396 :param detail: (bool)
400 :param changes_since: when the flavor last changed
402 :param minDisk: minimum disk space in GB filter
404 :param minRam: minimum RAM filter
406 :param marker: UUID of the flavor at which to set a marker
408 :param limit: limit the number of returned values
410 :returns: request response
413 self.set_param('changes-since', changes_since, iff=changes_since)
414 self.set_param('minDisk', minDisk, iff=minDisk)
415 self.set_param('minRam', minRam, iff=minRam)
416 self.set_param('marker', marker, iff=marker)
417 self.set_param('limit', limit, iff=limit)
419 path = path4url('flavors', 'detail' if detail else (flavor_id or ''))
420 return self.get(path, success=success, **kwargs)
422 def floating_ip_pools_get(self, tenant_id, success=200, **kwargs):
423 path = path4url('os-floating-ip-pools')
424 return self.get(path, success=success, **kwargs)
426 def floating_ips_get(self, tenant_id, ip='', success=200, **kwargs):
427 path = path4url('os-floating-ips', ip or '')
428 return self.get(path, success=success, **kwargs)
430 def floating_ips_post(
431 self, tenant_id, json_data, ip='', success=201, **kwargs):
432 path = path4url('os-floating-ips', ip or '')
433 if json_data is not None:
434 json_data = json.dumps(json_data)
435 self.set_header('Content-Type', 'application/json')
436 self.set_header('Content-Length', len(json_data))
437 return self.post(path, data=json_data, success=success, **kwargs)
439 def floating_ips_delete(self, tenant_id, ip='', success=204, **kwargs):
440 path = path4url('os-floating-ips', ip or '')
441 return self.delete(path, success=success, **kwargs)