-# Copyright 2012 GRNET S.A. All rights reserved.
+# Copyright 2012-2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
import json
-class ComputeClientApi(Client):
+class ComputeRestClient(Client):
- def servers_get(self, server_id='', command='', success=200, **kwargs):
- """GET base_url/servers[/server_id][/command] request
-
- :param server_id: integer (as int or str)
-
- :param command: 'ips', 'stats', or ''
+ # NON-cyclades
+ def limits_get(self, success=200, **kwargs):
+ """GET base_url/limits
:param success: success code or list or tupple of accepted success
codes. if server response code is not in this list, a ClientError
:returns: request response
"""
- path = path4url('servers', server_id, command)
+ path = path4url('limits')
return self.get(path, success=success, **kwargs)
- def servers_delete(self, server_id='', command='', success=204, **kwargs):
- """DEL ETE base_url/servers[/server_id][/command] request
+ def servers_get(
+ self,
+ server_id='', detail=False,
+ changes_since=None,
+ image=None,
+ flavor=None,
+ name=None,
+ marker=None,
+ limit=None,
+ status=None,
+ host=None,
+ success=200,
+ **kwargs):
+ """GET base_url/servers/['detail' | <server_id>]
+
+ :param server_id: (int or int str) ignored if detail
- :param server_id: integer (as int or str)
+ :param detail: (boolean)
- :param command: 'ips', 'stats', or ''
+ :param changes-since: A time/date stamp for when the server last
+ changed status
- :param success: success code or list or tupple of accepted success
- codes. if server response code is not in this list, a ClientError
- raises
+ :param image: Name of the image in URL format
+
+ :param flavor: Name of the flavor in URL format
+
+ :param name: Name of the server as a string
+
+ :param marker: UUID of the server at which you want to set a marker
+
+ :param limit: (int) limit of values to return
+
+ :param status: Status of the server (e.g. filter on "ACTIVE")
+
+ :param host: Name of the host as a string
:returns: request response
"""
- path = path4url('servers', server_id, command)
- return self.delete(path, success=success, **kwargs)
+ if not server_id:
+ self.set_param('changes-since', changes_since, iff=changes_since)
+ self.set_param('image', image, iff=image)
+ self.set_param('flavor', flavor, iff=flavor)
+ self.set_param('name', name, iff=name)
+ self.set_param('marker', marker, iff=marker)
+ self.set_param('limit', limit, iff=limit)
+ self.set_param('status', status, iff=status)
+ self.set_param('host', host, iff=host)
+
+ path = path4url('servers', 'detail' if detail else (server_id or ''))
+ return self.get(path, success=success, **kwargs)
def servers_post(
self,
- server_id='',
- command='',
+ security_group=None,
+ user_data=None,
+ availability_zone=None,
json_data=None,
success=202,
**kwargs):
- """POST base_url/servers[/server_id]/[command] request
+ """POST base_url/servers
- :param server_id: integer (as int or str)
+ :param json_data: a json-formated dict that will be send as data
- :param command: 'ips', 'stats', or ''
+ :param security_group: (str)
- :param json_data: a json-formated dict that will be send as data
+ :param user_data: Use to pass configuration information or scripts upon
+ launch. Must be Base64 encoded.
- :param success: success code or list or tupple of accepted success
- codes. if server response code is not in this list, a ClientError
- raises
+ :param availability_zone: (str)
:returns: request response
"""
- data = json_data
+
+ self.set_param('security_group', security_group, iff=security_group)
+ self.set_param('user_data', user_data, iff=user_data)
+ self.set_param(
+ 'availability_zone', availability_zone, iff=availability_zone)
+
if json_data:
- data = json.dumps(json_data)
+ json_data = json.dumps(json_data)
self.set_header('Content-Type', 'application/json')
- self.set_header('Content-Length', len(data))
+ self.set_header('Content-Length', len(json_data))
- path = path4url('servers', server_id, command)
- return self.post(path, data=data, success=success, **kwargs)
+ path = path4url('servers')
+ return self.post(path, data=json_data, success=success, **kwargs)
def servers_put(
self,
- server_id='',
- command='',
- json_data=None,
- success=204,
+ server_id, server_name=None, json_data=None, success=204,
**kwargs):
- """PUT base_url/servers[/server_id]/[command] request
-
- :param server_id: integer (as int or str)
-
- :param command: 'ips', 'stats', or ''
+ """PUT base_url/servers/<server_id>
:param json_data: a json-formated dict that will be send as data
- :param success: success code or list or tupple of accepted success
- codes. if server response code is not in this list, a ClientError
- raises
+ :param success: success code (iterable of) codes
+
+ :raises ClientError: if returned code not in success list
:returns: request response
"""
- data = json_data
- if json_data is not None:
- data = json.dumps(json_data)
+ self.set_param('server', server_name, iff=server_name)
+
+ if json_data:
+ json_data = json.dumps(json_data)
self.set_header('Content-Type', 'application/json')
- self.set_header('Content-Length', len(data))
+ self.set_header('Content-Length', len(json_data))
+ path = path4url('servers', server_id)
+ return self.put(path, data=json_data, success=success, **kwargs)
- path = path4url('servers', server_id, command)
- return self.put(path, data=data, success=success, **kwargs)
+ def servers_delete(self, server_id, success=204, **kwargs):
+ """DEL ETE base_url/servers/<server_id>
- def flavors_get(self, flavor_id='', command='', success=200, **kwargs):
- """GET base_url[/flavor_id][/command]
+ :param json_data: a json-formated dict that will be send as data
- :param flavor_id: integer (str or int)
+ :param success: success code (iterable of) codes
- :param command: flavor service command
+ :raises ClientError: if returned code not in success list
- :param success: success code or list or tupple of accepted success
- codes. if server response code is not in this list, a ClientError
- raises
+ :returns: request response
+ """
+ path = path4url('servers', server_id)
+ return self.delete(path, success=success, **kwargs)
+
+ def servers_metadata_get(self, server_id, key=None, success=200, **kwargs):
+ """GET base_url/servers/<server_id>/metadata[/key]
:returns: request response
"""
- path = path4url('flavors', flavor_id, command)
+ path = path4url('servers', server_id, 'metadata', key or '')
return self.get(path, success=success, **kwargs)
- def images_get(self, image_id='', command='', success=200, **kwargs):
- """GET base_url[/image_id][/command]
+ def servers_metadata_post(
+ self, server_id, json_data=None, success=202, **kwargs):
+ """POST base_url/servers/<server_id>/metadata
- :param image_id: string
+ :returns: request response
+ """
+ if json_data:
+ json_data = json.dumps(json_data)
+ self.set_header('Content-Type', 'application/json')
+ self.set_header('Content-Length', len(json_data))
+ path = path4url('servers', server_id, 'metadata')
+ return self.post(path, data=json_data, success=success, **kwargs)
- :param command: image server command
+ def servers_metadata_put(
+ self, server_id, key=None, json_data=None, success=204, **kwargs):
+ """PUT base_url/servers/<server_id>/metadata[/key]
- :param success: success code or list or tupple of accepted success
- codes. if server response code is not in this list, a ClientError
- raises
+ :returns: request response
+ """
+ if json_data:
+ json_data = json.dumps(json_data)
+ self.set_header('Content-Type', 'application/json')
+ self.set_header('Content-Length', len(json_data))
+ path = path4url('servers', server_id, 'metadata', key or '')
+ return self.put(path, data=json_data, success=success, **kwargs)
+
+ def servers_metadata_delete(self, server_id, key, success=204, **kwargs):
+ """DEL ETE base_url/servers/<server_id>/metadata[/key]
:returns: request response
"""
- path = path4url('images', image_id, command)
- return self.get(path, success=success, **kwargs)
+ path = path4url('servers', server_id, 'metadata', key)
+ return self.delete(path, success=success, **kwargs)
- def images_delete(self, image_id='', command='', success=204, **kwargs):
- """DELETE base_url[/image_id][/command]
+ def servers_action_post(
+ self, server_id, json_data=None, success=202, **kwargs):
+ """POST base_url/servers/<server_id>/action
- :param image_id: string
+ :returns: request response
+ """
+ if json_data:
+ json_data = json.dumps(json_data)
+ self.set_header('Content-Type', 'application/json')
+ self.set_header('Content-Length', len(json_data))
+ path = path4url('servers', server_id, 'action')
+ return self.post(path, data=json_data, success=success, **kwargs)
- :param command: image server command
+ def servers_ips_get(
+ self, server_id,
+ network_id=None, changes_since=None, success=(304, 200),
+ **kwargs):
+ """GET base_url/servers/<server_id>/ips[/network_id]
- :param success: success code or list or tuple of accepted success
- codes. if server response code is not in this list, a ClientError
- raises
+ :param changes_since: time/date stamp in UNIX/epoch time. Checks for
+ changes since a previous request.
:returns: request response
"""
- path = path4url('images', image_id, command)
- return self.delete(path, success=success, **kwargs)
+ self.set_param('changes-since', changes_since, iff=changes_since)
+ path = path4url('servers', server_id, 'ips', network_id or '')
+ return self.get(path, success=success, **kwargs)
- def images_post(
+ def images_get(
self,
image_id='',
- command='',
- json_data=None,
- success=201,
+ detail=False,
+ changes_since=None,
+ server_name=None,
+ name=None,
+ status=None,
+ marker=None,
+ limit=None,
+ type=None,
+ success=200,
**kwargs):
- """POST base_url/images[/image_id]/[command] request
+ """GET base_url[/image_id][/command]
- :param image_id: string
+ :param image_id: (str) ignored if detail
- :param command: image server command
+ :param detail: (bool)
- :param json_data: (dict) will be send as data
+ --- Parameters ---
+ :param changes_since: when the image last changed status
- :param success: success code or list or tuple of accepted success
- codes. if server response code is not in this list, a ClientError
- raises
+ :param server_name: Name of the server in URL format
+
+ :param name: Name of the image
+
+ :param status: Status of the image (e.g. filter on "ACTIVE")
+
+ :param marker: UUID of the image at which you want to set a marker
+
+ :param limit: Integer value for the limit of values to return
+
+ :param type: Type of image (e.g. BASE, SERVER, or ALL)
+
+ :returns: request response
+ """
+ if not image_id:
+ self.set_param('changes-since', changes_since, iff=changes_since)
+ self.set_param('name', name, iff=name)
+ self.set_param('status', status, iff=status)
+ self.set_param('marker', marker, iff=marker)
+ self.set_param('limit', limit, iff=limit)
+ self.set_param('type', type, iff=type)
+ self.set_param('server', server_name, iff=server_name)
+
+ path = path4url('images', 'detail' if detail else (image_id or ''))
+ return self.get(path, success=success, **kwargs)
+
+ def images_delete(self, image_id='', success=204, **kwargs):
+ """DEL ETE base_url/images/<image_id>
+
+ :returns: request response
+ """
+ path = path4url('images', image_id)
+ return self.delete(path, success=success, **kwargs)
+
+ def images_metadata_get(self, image_id, key=None, success=200, **kwargs):
+ """GET base_url/<image_id>/metadata[/key]
+
+ :returns: request response
+ """
+ path = path4url('images', image_id, 'metadata', key or '')
+ return self.get(path, success=success, **kwargs)
+
+ def images_metadata_post(
+ self, image_id, json_data=None, success=201, **kwargs):
+ """POST base_url/images/<image_id>/metadata
+
+ :returns: request response
+ """
+ if json_data is not None:
+ json_data = json.dumps(json_data)
+ self.set_header('Content-Type', 'application/json')
+ self.set_header('Content-Length', len(json_data))
+
+ path = path4url('images', image_id, 'metadata')
+ return self.post(path, data=json_data, success=success, **kwargs)
+
+ def images_metadata_put(
+ self, image_id, key=None, json_data=None, success=201, **kwargs):
+ """PUT base_url/images/<image_id>/metadata
:returns: request response
"""
- data = json_data
if json_data is not None:
- data = json.dumps(json_data)
+ json_data = json.dumps(json_data)
self.set_header('Content-Type', 'application/json')
- self.set_header('Content-Length', len(data))
+ self.set_header('Content-Length', len(json_data))
- path = path4url('images', image_id, command)
- return self.post(path, data=data, success=success, **kwargs)
+ path = path4url('images', image_id, 'metadata')
+ return self.put(path, data=json_data, success=success, **kwargs)
- def images_put(
+ def images_metadata_delete(self, image_id, key, success=204, **kwargs):
+ """DEL ETE base_url/images/<image_id>/metadata/key
+
+ :returns: request response
+ """
+ path = path4url('images', image_id, 'metadata', key)
+ return self.delete(path, success=success, **kwargs)
+
+ def flavors_get(
self,
- image_id='',
- command='',
- json_data=None,
- success=201,
+ flavor_id='',
+ detail=False,
+ changes_since=None,
+ minDisk=None,
+ minRam=None,
+ marker=None,
+ limit=None,
+ success=200,
**kwargs):
- """PUT base_url/images[/image_id]/[command] request
+ """GET base_url[/flavor_id][/command]
- :param image_id: string
+ :param flavor_id: ignored if detail
- :param command: image server command
+ :param detail: (bool)
- :param json_data: (dict) will be send as data
+ --- Parameters ---
- :param success: success code or list or tuple of accepted success
- codes. if server response code is not in this list, a ClientError
- raises
+ :param changes_since: when the flavor last changed
+
+ :param minDisk: minimum disk space in GB filter
+
+ :param minRam: minimum RAM filter
+
+ :param marker: UUID of the flavor at which to set a marker
+
+ :param limit: limit the number of returned values
:returns: request response
"""
- data = json_data
+ if not flavor_id:
+ self.set_param('changes-since', changes_since, iff=changes_since)
+ self.set_param('minDisk', minDisk, iff=minDisk)
+ self.set_param('minRam', minRam, iff=minRam)
+ self.set_param('marker', marker, iff=marker)
+ self.set_param('limit', limit, iff=limit)
+
+ path = path4url('flavors', 'detail' if detail else (flavor_id or ''))
+ return self.get(path, success=success, **kwargs)
+
+ def floating_ip_pools_get(self, success=200, **kwargs):
+ path = path4url('os-floating-ip-pools')
+ return self.get(path, success=success, **kwargs)
+
+ def floating_ips_get(self, ip='', success=200, **kwargs):
+ path = path4url('os-floating-ips', ip or '')
+ return self.get(path, success=success, **kwargs)
+
+ def floating_ips_post(self, json_data, ip='', success=201, **kwargs):
+ path = path4url('os-floating-ips', ip or '')
if json_data is not None:
- data = json.dumps(json_data)
+ json_data = json.dumps(json_data)
self.set_header('Content-Type', 'application/json')
- self.set_header('Content-Length', len(data))
+ self.set_header('Content-Length', len(json_data))
+ return self.post(path, data=json_data, success=success, **kwargs)
- path = path4url('images', image_id, command)
- return self.put(path, data=data, success=success, **kwargs)
+ def floating_ips_delete(self, ip='', success=204, **kwargs):
+ path = path4url('os-floating-ips', ip or '')
+ return self.delete(path, success=success, **kwargs)