Revision 606fe15f

b/kamaki/cli/argument.py
51 51
    """
52 52

  
53 53
    def __init__(self, arity, help=None, parsed_name=None, default=None):
54
        self.arity = int(arity)
55

  
54 56
        if help is not None:
55 57
            self.help = help
56 58
        if parsed_name is not None:
57 59
            self.parsed_name = parsed_name
58 60
        if default is not None:
59 61
            self.default = default
60
        self.arity = arity
61 62

  
62 63
    @property
63 64
    def parsed_name(self):
b/kamaki/clients/compute.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
from kamaki.clients import Client, ClientError
34
from kamaki.clients import ClientError
35
from kamaki.clients.compute_rest_api import ComputeClientApi
35 36
from kamaki.clients.utils import path4url
36 37
import json
37 38

  
38 39

  
39
class ComputeClient(Client):
40
class ComputeClient(ComputeClientApi):
40 41
    """OpenStack Compute API 1.1 client"""
41 42

  
42
    """
43
    def _raise_for_status(self, r):
44
        try:
45
            d = r.json
46
            key = d.keys()[0]
47
            val = d[key]
48
            message = '%s: %s' % (key, val.get('message', ''))
49
            details = val.get('details', '')
50
        except AttributeError:
51
            message = 'Request responded with error code %s' %\
52
                unicode(r.status_code)
53
            details = '%s %s' %\
54
                (unicode(r.request.method), unicode(r.request.url))
55
        raise ClientError(message, r.status_code, details)
56
    """
57

  
58
    def servers_get(self, server_id='', command='', **kwargs):
59
        """GET base_url/servers[/server_id][/command] request
60
        @param server_id or ''
61
        @param command: can be 'ips', 'stats', or ''
62
        """
63
        path = path4url('servers', server_id, command)
64
        success = kwargs.pop('success', 200)
65
        return self.get(path, success=success, **kwargs)
66

  
67
    def servers_delete(self, server_id='', command='', **kwargs):
68
        """DEL ETE base_url/servers[/server_id][/command] request
69
        @param server_id or ''
70
        @param command: can be 'ips', 'stats', or ''
71
        """
72
        path = path4url('servers', server_id, command)
73
        success = kwargs.pop('success', 204)
74
        return self.delete(path, success=success, **kwargs)
75

  
76
    def servers_post(self, server_id='', command='', json_data=None, **kwargs):
77
        """POST base_url/servers[/server_id]/[command] request
78
        @param server_id or ''
79
        @param command: can be 'action' or ''
80
        @param json_data: a json valid dict that will be send as data
81
        """
82
        data = json_data
83
        if json_data is not None:
84
            data = json.dumps(json_data)
85
            self.set_header('Content-Type', 'application/json')
86
            self.set_header('Content-Length', len(data))
87

  
88
        path = path4url('servers', server_id, command)
89
        success = kwargs.pop('success', 202)
90
        return self.post(path, data=data, success=success, **kwargs)
91

  
92
    def servers_put(self, server_id='', command='', json_data=None, **kwargs):
93
        """PUT base_url/servers[/server_id]/[command] request
94
        @param server_id or ''
95
        @param command: can be 'action' or ''
96
        @param json_data: a json valid dict that will be send as data
97
        """
98
        data = json_data
99
        if json_data is not None:
100
            data = json.dumps(json_data)
101
            self.set_header('Content-Type', 'application/json')
102
            self.set_header('Content-Length', len(data))
103

  
104
        path = path4url('servers', server_id, command)
105
        success = kwargs.pop('success', 204)
106
        return self.put(path, data=data, success=success, **kwargs)
107

  
108 43
    def list_servers(self, detail=False):
109 44
        """List servers, returned detailed output if detailed is True"""
110 45
        detail = 'detail' if detail else ''
b/kamaki/clients/compute_rest_api.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
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.
15
#
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.
28
#
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.
33

  
34
from kamaki.clients import Client
35
from kamaki.clients.utils import path4url
36
import json
37

  
38

  
39
class ComputeClientApi(Client):
40

  
41
    def servers_get(self, server_id='', command='', success=200, **kwargs):
42
        """GET base_url/servers[/server_id][/command] request
43

  
44
        :param server_id: integer (as int or str)
45

  
46
        :param command: 'ips', 'stats', or ''
47

  
48
        :param success: success code or list or tupple of accepted success
49
            codes. if server response code is not in this list, a ClientError
50
            raises
51

  
52
        :returns: request response
53
        """
54
        path = path4url('servers', server_id, command)
55
        return self.get(path, success=success, **kwargs)
56

  
57
    def servers_delete(self, server_id='', command='', success=204, **kwargs):
58
        """DEL ETE base_url/servers[/server_id][/command] request
59

  
60
        :param server_id: integer (as int or str)
61

  
62
        :param command: 'ips', 'stats', or ''
63

  
64
        :param success: success code or list or tupple of accepted success
65
            codes. if server response code is not in this list, a ClientError
66
            raises
67

  
68
        :returns: request response
69
        """
70
        path = path4url('servers', server_id, command)
71
        return self.delete(path, success=success, **kwargs)
72

  
73
    def servers_post(self,
74
        server_id='', command='', json_data=None, success=202, **kwargs):
75
        """POST base_url/servers[/server_id]/[command] request
76

  
77
        :param server_id: integer (as int or str)
78

  
79
        :param command: 'ips', 'stats', or ''
80

  
81
        :param json_data: a json-formated dict that will be send as data
82

  
83
        :param success: success code or list or tupple of accepted success
84
            codes. if server response code is not in this list, a ClientError
85
            raises
86

  
87
        :returns: request response
88
        """
89
        data = json_data
90
        if json_data is not None:
91
            data = json.dumps(json_data)
92
            self.set_header('Content-Type', 'application/json')
93
            self.set_header('Content-Length', len(data))
94

  
95
        path = path4url('servers', server_id, command)
96
        return self.post(path, data=data, success=success, **kwargs)
97

  
98
    def servers_put(self,
99
        server_id='', command='', json_data=None, success=204, **kwargs):
100
        """PUT base_url/servers[/server_id]/[command] request
101

  
102
        :param server_id: integer (as int or str)
103

  
104
        :param command: 'ips', 'stats', or ''
105

  
106
        :param json_data: a json-formated dict that will be send as data
107

  
108
        :param success: success code or list or tupple of accepted success
109
            codes. if server response code is not in this list, a ClientError
110
            raises
111

  
112
        :returns: request response
113
        """
114
        data = json_data
115
        if json_data is not None:
116
            data = json.dumps(json_data)
117
            self.set_header('Content-Type', 'application/json')
118
            self.set_header('Content-Length', len(data))
119

  
120
        path = path4url('servers', server_id, command)
121
        return self.put(path, data=data, success=success, **kwargs)

Also available in: Unified diff