Commit 606fe15f authored by Stavros Sachtouris's avatar Stavros Sachtouris
Browse files

Restructure kamaki.clients.compute

parent 40a9c357
......@@ -51,13 +51,14 @@ class Argument(object):
"""
def __init__(self, arity, help=None, parsed_name=None, default=None):
self.arity = int(arity)
if help is not None:
self.help = help
if parsed_name is not None:
self.parsed_name = parsed_name
if default is not None:
self.default = default
self.arity = arity
@property
def parsed_name(self):
......
......@@ -31,80 +31,15 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from kamaki.clients import Client, ClientError
from kamaki.clients import ClientError
from kamaki.clients.compute_rest_api import ComputeClientApi
from kamaki.clients.utils import path4url
import json
class ComputeClient(Client):
class ComputeClient(ComputeClientApi):
"""OpenStack Compute API 1.1 client"""
"""
def _raise_for_status(self, r):
try:
d = r.json
key = d.keys()[0]
val = d[key]
message = '%s: %s' % (key, val.get('message', ''))
details = val.get('details', '')
except AttributeError:
message = 'Request responded with error code %s' %\
unicode(r.status_code)
details = '%s %s' %\
(unicode(r.request.method), unicode(r.request.url))
raise ClientError(message, r.status_code, details)
"""
def servers_get(self, server_id='', command='', **kwargs):
"""GET base_url/servers[/server_id][/command] request
@param server_id or ''
@param command: can be 'ips', 'stats', or ''
"""
path = path4url('servers', server_id, command)
success = kwargs.pop('success', 200)
return self.get(path, success=success, **kwargs)
def servers_delete(self, server_id='', command='', **kwargs):
"""DEL ETE base_url/servers[/server_id][/command] request
@param server_id or ''
@param command: can be 'ips', 'stats', or ''
"""
path = path4url('servers', server_id, command)
success = kwargs.pop('success', 204)
return self.delete(path, success=success, **kwargs)
def servers_post(self, server_id='', command='', json_data=None, **kwargs):
"""POST base_url/servers[/server_id]/[command] request
@param server_id or ''
@param command: can be 'action' or ''
@param json_data: a json valid dict that will be send as data
"""
data = json_data
if json_data is not None:
data = json.dumps(json_data)
self.set_header('Content-Type', 'application/json')
self.set_header('Content-Length', len(data))
path = path4url('servers', server_id, command)
success = kwargs.pop('success', 202)
return self.post(path, data=data, success=success, **kwargs)
def servers_put(self, server_id='', command='', json_data=None, **kwargs):
"""PUT base_url/servers[/server_id]/[command] request
@param server_id or ''
@param command: can be 'action' or ''
@param json_data: a json valid dict that will be send as data
"""
data = json_data
if json_data is not None:
data = json.dumps(json_data)
self.set_header('Content-Type', 'application/json')
self.set_header('Content-Length', len(data))
path = path4url('servers', server_id, command)
success = kwargs.pop('success', 204)
return self.put(path, data=data, success=success, **kwargs)
def list_servers(self, detail=False):
"""List servers, returned detailed output if detailed is True"""
detail = 'detail' if detail else ''
......
# Copyright 2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from kamaki.clients import Client
from kamaki.clients.utils import path4url
import json
class ComputeClientApi(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 ''
: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, command)
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
:param server_id: integer (as int or str)
:param command: 'ips', 'stats', or ''
: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, command)
return self.delete(path, success=success, **kwargs)
def servers_post(self,
server_id='', command='', json_data=None, success=202, **kwargs):
"""POST base_url/servers[/server_id]/[command] request
:param server_id: integer (as int or str)
:param command: 'ips', 'stats', or ''
: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
:returns: request response
"""
data = json_data
if json_data is not None:
data = json.dumps(json_data)
self.set_header('Content-Type', 'application/json')
self.set_header('Content-Length', len(data))
path = path4url('servers', server_id, command)
return self.post(path, data=data, success=success, **kwargs)
def servers_put(self,
server_id='', command='', 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 ''
: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
:returns: request response
"""
data = json_data
if json_data is not None:
data = json.dumps(json_data)
self.set_header('Content-Type', 'application/json')
self.set_header('Content-Length', len(data))
path = path4url('servers', server_id, command)
return self.put(path, data=data, success=success, **kwargs)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment