Revision 606fe15f kamaki/clients/compute.py
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 '' |
Also available in: Unified diff