root / kamaki / clients / compute_rest_api.py @ 606fe15f
History | View | Annotate | Download (4.5 kB)
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) |