root / kamaki / clients / compute_rest_api.py @ b482315a
History | View | Annotate | Download (7.8 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) |
122 |
|
123 |
def flavors_get(self, flavor_id='', command='', success=200, **kwargs): |
124 |
"""GET base_url[/flavor_id][/command]
|
125 |
|
126 |
:param flavor_id: integer (str or int)
|
127 |
|
128 |
:param command: flavor service command
|
129 |
|
130 |
:param success: success code or list or tupple of accepted success
|
131 |
codes. if server response code is not in this list, a ClientError
|
132 |
raises
|
133 |
|
134 |
:returns: request response
|
135 |
"""
|
136 |
path = path4url('flavors', flavor_id, command)
|
137 |
return self.get(path, success=success, **kwargs) |
138 |
|
139 |
def images_get(self, image_id='', command='', success=200, **kwargs): |
140 |
"""GET base_url[/image_id][/command]
|
141 |
|
142 |
:param image_id: string
|
143 |
|
144 |
:param command: image server command
|
145 |
|
146 |
:param success: success code or list or tupple of accepted success
|
147 |
codes. if server response code is not in this list, a ClientError
|
148 |
raises
|
149 |
|
150 |
:returns: request response
|
151 |
"""
|
152 |
path = path4url('images', image_id, command)
|
153 |
return self.get(path, success=success, **kwargs) |
154 |
|
155 |
def images_delete(self, image_id='', command='', success=204, **kwargs): |
156 |
"""DELETE base_url[/image_id][/command]
|
157 |
|
158 |
:param image_id: string
|
159 |
|
160 |
:param command: image server command
|
161 |
|
162 |
:param success: success code or list or tuple of accepted success
|
163 |
codes. if server response code is not in this list, a ClientError
|
164 |
raises
|
165 |
|
166 |
:returns: request response
|
167 |
"""
|
168 |
path = path4url('images', image_id, command)
|
169 |
return self.delete(path, success=success, **kwargs) |
170 |
|
171 |
def images_post(self, |
172 |
image_id='', command='', json_data=None, success=201, **kwargs): |
173 |
"""POST base_url/images[/image_id]/[command] request
|
174 |
|
175 |
:param image_id: string
|
176 |
|
177 |
:param command: image server command
|
178 |
|
179 |
:param json_data: (dict) will be send as data
|
180 |
|
181 |
:param success: success code or list or tuple of accepted success
|
182 |
codes. if server response code is not in this list, a ClientError
|
183 |
raises
|
184 |
|
185 |
:returns: request response
|
186 |
"""
|
187 |
data = json_data |
188 |
if json_data is not None: |
189 |
data = json.dumps(json_data) |
190 |
self.set_header('Content-Type', 'application/json') |
191 |
self.set_header('Content-Length', len(data)) |
192 |
|
193 |
path = path4url('images', image_id, command)
|
194 |
return self.post(path, data=data, success=success, **kwargs) |
195 |
|
196 |
def images_put(self, |
197 |
image_id='', command='', json_data=None, success=201, **kwargs): |
198 |
"""PUT base_url/images[/image_id]/[command] request
|
199 |
|
200 |
:param image_id: string
|
201 |
|
202 |
:param command: image server command
|
203 |
|
204 |
:param json_data: (dict) will be send as data
|
205 |
|
206 |
:param success: success code or list or tuple of accepted success
|
207 |
codes. if server response code is not in this list, a ClientError
|
208 |
raises
|
209 |
|
210 |
:returns: request response
|
211 |
"""
|
212 |
data = json_data |
213 |
if json_data is not None: |
214 |
data = json.dumps(json_data) |
215 |
self.set_header('Content-Type', 'application/json') |
216 |
self.set_header('Content-Length', len(data)) |
217 |
|
218 |
path = path4url('images', image_id, command)
|
219 |
return self.put(path, data=data, success=success, **kwargs) |