root / kamaki / clients / compute / rest_api.py @ fda88be1
History | View | Annotate | Download (14.7 kB)
1 |
# Copyright 2012-2013 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 ComputeRestClient(Client): |
40 |
|
41 |
# NON-cyclades
|
42 |
def limits_get(self, success=200, **kwargs): |
43 |
"""GET base_url/limits
|
44 |
|
45 |
:param success: success code or list or tupple of accepted success
|
46 |
codes. if server response code is not in this list, a ClientError
|
47 |
raises
|
48 |
|
49 |
:returns: request response
|
50 |
"""
|
51 |
path = path4url('limits')
|
52 |
return self.get(path, success=success, **kwargs) |
53 |
|
54 |
def servers_get( |
55 |
self,
|
56 |
server_id='', detail=False, |
57 |
changes_since=None,
|
58 |
image=None,
|
59 |
flavor=None,
|
60 |
name=None,
|
61 |
marker=None,
|
62 |
limit=None,
|
63 |
status=None,
|
64 |
host=None,
|
65 |
success=200,
|
66 |
**kwargs): |
67 |
"""GET base_url/servers/['detail'|<server_id>]
|
68 |
|
69 |
:param server_id: (int or int str) ignored if detail
|
70 |
|
71 |
:param detail: (boolean)
|
72 |
|
73 |
--- Parameters ---
|
74 |
|
75 |
:param changes-since: A time/date stamp for when the server last
|
76 |
changed status
|
77 |
|
78 |
:param image: Name of the image in URL format
|
79 |
|
80 |
:param flavor: Name of the flavor in URL format
|
81 |
|
82 |
:param name: Name of the server as a string
|
83 |
|
84 |
:param marker: UUID of the server at which you want to set a marker
|
85 |
|
86 |
:param limit: (int) limit of values to return
|
87 |
|
88 |
:param status: Status of the server (e.g. filter on "ACTIVE")
|
89 |
|
90 |
:param host: Name of the host as a string
|
91 |
|
92 |
:returns: request response
|
93 |
"""
|
94 |
if not server_id: |
95 |
self.set_param('changes-since', changes_since, iff=changes_since) |
96 |
self.set_param('image', image, iff=image) |
97 |
self.set_param('flavor', flavor, iff=flavor) |
98 |
self.set_param('name', name, iff=name) |
99 |
self.set_param('marker', marker, iff=marker) |
100 |
self.set_param('limit', limit, iff=limit) |
101 |
self.set_param('status', status, iff=status) |
102 |
self.set_param('host', host, iff=host) |
103 |
|
104 |
path = path4url('servers', 'detail' if detail else server_id) |
105 |
return self.get(path, success=success, **kwargs) |
106 |
|
107 |
def servers_post( |
108 |
self,
|
109 |
security_group=None,
|
110 |
user_data=None,
|
111 |
availability_zone=None,
|
112 |
server=None,
|
113 |
imageRef=None,
|
114 |
flavorRef=None,
|
115 |
name=None,
|
116 |
metadata=None,
|
117 |
personality=None,
|
118 |
json_data=None,
|
119 |
success=202,
|
120 |
**kwargs): |
121 |
"""POST base_url/servers
|
122 |
|
123 |
:param json_data: a json-formated dict that will be send as data
|
124 |
|
125 |
--- Parameters
|
126 |
|
127 |
:param security_group: (str)
|
128 |
|
129 |
:param user_data: Use to pass configuration information or scripts upon
|
130 |
launch. Must be Base64 encoded.
|
131 |
|
132 |
:param availability_zone: (str)
|
133 |
|
134 |
:param server: Server
|
135 |
|
136 |
:param imageRef: ID or full URL.
|
137 |
|
138 |
:param flavorRef: ID or full URL.
|
139 |
|
140 |
:param name: (str) The name of the new server
|
141 |
|
142 |
:param metadata: (dict) Metadata key: value pairs. max size of the key
|
143 |
and value is 255 bytes each.
|
144 |
|
145 |
:param personality: (str) File path and contents (text only) to inject
|
146 |
into the server at launch. The maximum size of the file path data
|
147 |
is 255 bytes. The maximum limit refers to the number of bytes in
|
148 |
the decoded data and not the number of characters in the encoded
|
149 |
data.
|
150 |
|
151 |
:returns: request response
|
152 |
"""
|
153 |
|
154 |
self.set_param(security_group, security_group, iff=security_group)
|
155 |
self.set_param(user_data, user_data, iff=user_data)
|
156 |
self.set_param(
|
157 |
availability_zone, availability_zone, iff=availability_zone) |
158 |
self.set_param(server, server, iff=server)
|
159 |
self.set_param(imageRef, imageRef, iff=imageRef)
|
160 |
self.set_param(flavorRef, flavorRef, iff=flavorRef)
|
161 |
self.set_param(name, name, iff=name)
|
162 |
if metadata: # don't json.dump None |
163 |
self.set_param(metadata, json.dumps(metadata))
|
164 |
self.set_param(personality, personality, iff=personality)
|
165 |
|
166 |
if json_data:
|
167 |
json_data = json.dumps(json_data) |
168 |
self.set_header('Content-Type', 'application/json') |
169 |
self.set_header('Content-Length', len(json_data)) |
170 |
|
171 |
path = path4url('servers')
|
172 |
return self.post(path, data=json_data, success=success, **kwargs) |
173 |
|
174 |
def servers_put(self, server_id, json_data=None, success=204, **kwargs): |
175 |
"""PUT base_url/servers/<server_id>
|
176 |
|
177 |
:param json_data: a json-formated dict that will be send as data
|
178 |
|
179 |
:param success: success code (iterable of) codes
|
180 |
|
181 |
:raises ClientError: if returned code not in success list
|
182 |
|
183 |
:returns: request response
|
184 |
"""
|
185 |
if json_data:
|
186 |
json_data = json.dumps(json_data) |
187 |
self.set_header('Content-Type', 'application/json') |
188 |
self.set_header('Content-Length', len(json_data)) |
189 |
path = path4url('servers', server_id)
|
190 |
return self.put(path, data=json_data, success=success, **kwargs) |
191 |
|
192 |
def servers_delete(self, server_id, success=204, **kwargs): |
193 |
"""DEL ETE base_url/servers/<server_id>
|
194 |
|
195 |
:param json_data: a json-formated dict that will be send as data
|
196 |
|
197 |
:param success: success code (iterable of) codes
|
198 |
|
199 |
:raises ClientError: if returned code not in success list
|
200 |
|
201 |
:returns: request response
|
202 |
"""
|
203 |
path = path4url('servers', server_id)
|
204 |
return self.delete(path, success=success, **kwargs) |
205 |
|
206 |
"""
|
207 |
def servers_get
|
208 |
def servers_post
|
209 |
def servers_put
|
210 |
def servers_delete
|
211 |
def servers_metadata_get
|
212 |
def servers_metadata_post
|
213 |
def servers_metadata_put
|
214 |
def servers_metadata_delete
|
215 |
def servers_actions_post
|
216 |
def servers_ips_get
|
217 |
"""
|
218 |
|
219 |
"""
|
220 |
def servers_get(self, server_id='', command='', success=200, **kwargs):
|
221 |
""GET base_url/servers[/server_id][/command] request
|
222 |
|
223 |
:param server_id: integer (as int or str)
|
224 |
|
225 |
:param command: 'ips', 'stats', or ''
|
226 |
|
227 |
:param success: success code or list or tupple of accepted success
|
228 |
codes. if server response code is not in this list, a ClientError
|
229 |
raises
|
230 |
|
231 |
:returns: request response
|
232 |
""
|
233 |
path = path4url('servers', server_id, command)
|
234 |
return self.get(path, success=success, **kwargs)
|
235 |
"""
|
236 |
|
237 |
def servers_delete(self, server_id='', command='', success=204, **kwargs): |
238 |
"""DEL ETE base_url/servers[/server_id][/command] request
|
239 |
|
240 |
:param server_id: integer (as int or str)
|
241 |
|
242 |
:param command: 'ips', 'stats', or ''
|
243 |
|
244 |
:param success: success code or list or tupple of accepted success
|
245 |
codes. if server response code is not in this list, a ClientError
|
246 |
raises
|
247 |
|
248 |
:returns: request response
|
249 |
"""
|
250 |
path = path4url('servers', server_id, command)
|
251 |
return self.delete(path, success=success, **kwargs) |
252 |
|
253 |
"""
|
254 |
def servers_post(
|
255 |
self,
|
256 |
server_id='',
|
257 |
command='',
|
258 |
json_data=None,
|
259 |
success=202,
|
260 |
**kwargs):
|
261 |
""POST base_url/servers[/server_id]/[command] request
|
262 |
|
263 |
:param server_id: integer (as int or str)
|
264 |
|
265 |
:param command: 'ips', 'stats', or ''
|
266 |
|
267 |
:param json_data: a json-formated dict that will be send as data
|
268 |
|
269 |
:param success: success code or list or tupple of accepted success
|
270 |
codes. if server response code is not in this list, a ClientError
|
271 |
raises
|
272 |
|
273 |
:returns: request response
|
274 |
""
|
275 |
data = json_data
|
276 |
if json_data:
|
277 |
data = json.dumps(json_data)
|
278 |
self.set_header('Content-Type', 'application/json')
|
279 |
self.set_header('Content-Length', len(data))
|
280 |
|
281 |
path = path4url('servers', server_id, command)
|
282 |
return self.post(path, data=data, success=success, **kwargs)
|
283 |
|
284 |
def servers_put(
|
285 |
self,
|
286 |
server_id='',
|
287 |
command='',
|
288 |
json_data=None,
|
289 |
success=204,
|
290 |
**kwargs):
|
291 |
""PUT base_url/servers[/server_id]/[command] request
|
292 |
|
293 |
:param server_id: integer (as int or str)
|
294 |
|
295 |
:param command: 'ips', 'stats', or ''
|
296 |
|
297 |
:param json_data: a json-formated dict that will be send as data
|
298 |
|
299 |
:param success: success code or list or tupple of accepted success
|
300 |
codes. if server response code is not in this list, a ClientError
|
301 |
raises
|
302 |
|
303 |
:returns: request response
|
304 |
""
|
305 |
data = json_data
|
306 |
if json_data is not None:
|
307 |
data = json.dumps(json_data)
|
308 |
self.set_header('Content-Type', 'application/json')
|
309 |
self.set_header('Content-Length', len(data))
|
310 |
|
311 |
path = path4url('servers', server_id, command)
|
312 |
return self.put(path, data=data, success=success, **kwargs)
|
313 |
"""
|
314 |
|
315 |
def flavors_get(self, flavor_id='', command='', success=200, **kwargs): |
316 |
"""GET base_url[/flavor_id][/command]
|
317 |
|
318 |
:param flavor_id: integer (str or int)
|
319 |
|
320 |
:param command: flavor service command
|
321 |
|
322 |
:param success: success code or list or tupple of accepted success
|
323 |
codes. if server response code is not in this list, a ClientError
|
324 |
raises
|
325 |
|
326 |
:returns: request response
|
327 |
"""
|
328 |
path = path4url('flavors', flavor_id, command)
|
329 |
return self.get(path, success=success, **kwargs) |
330 |
|
331 |
def images_get(self, image_id='', command='', success=200, **kwargs): |
332 |
"""GET base_url[/image_id][/command]
|
333 |
|
334 |
:param image_id: string
|
335 |
|
336 |
:param command: image server command
|
337 |
|
338 |
:param success: success code or list or tupple of accepted success
|
339 |
codes. if server response code is not in this list, a ClientError
|
340 |
raises
|
341 |
|
342 |
:returns: request response
|
343 |
"""
|
344 |
path = path4url('images', image_id, command)
|
345 |
return self.get(path, success=success, **kwargs) |
346 |
|
347 |
def images_delete(self, image_id='', command='', success=204, **kwargs): |
348 |
"""DELETE base_url[/image_id][/command]
|
349 |
|
350 |
:param image_id: string
|
351 |
|
352 |
:param command: image server command
|
353 |
|
354 |
:param success: success code or list or tuple of accepted success
|
355 |
codes. if server response code is not in this list, a ClientError
|
356 |
raises
|
357 |
|
358 |
:returns: request response
|
359 |
"""
|
360 |
path = path4url('images', image_id, command)
|
361 |
return self.delete(path, success=success, **kwargs) |
362 |
|
363 |
def images_post( |
364 |
self,
|
365 |
image_id='',
|
366 |
command='',
|
367 |
json_data=None,
|
368 |
success=201,
|
369 |
**kwargs): |
370 |
"""POST base_url/images[/image_id]/[command] request
|
371 |
|
372 |
:param image_id: string
|
373 |
|
374 |
:param command: image server command
|
375 |
|
376 |
:param json_data: (dict) will be send as data
|
377 |
|
378 |
:param success: success code or list or tuple of accepted success
|
379 |
codes. if server response code is not in this list, a ClientError
|
380 |
raises
|
381 |
|
382 |
:returns: request response
|
383 |
"""
|
384 |
data = json_data |
385 |
if json_data is not None: |
386 |
data = json.dumps(json_data) |
387 |
self.set_header('Content-Type', 'application/json') |
388 |
self.set_header('Content-Length', len(data)) |
389 |
|
390 |
path = path4url('images', image_id, command)
|
391 |
return self.post(path, data=data, success=success, **kwargs) |
392 |
|
393 |
def images_put( |
394 |
self,
|
395 |
image_id='',
|
396 |
command='',
|
397 |
json_data=None,
|
398 |
success=201,
|
399 |
**kwargs): |
400 |
"""PUT base_url/images[/image_id]/[command] request
|
401 |
|
402 |
:param image_id: string
|
403 |
|
404 |
:param command: image server command
|
405 |
|
406 |
:param json_data: (dict) will be send as data
|
407 |
|
408 |
:param success: success code or list or tuple of accepted success
|
409 |
codes. if server response code is not in this list, a ClientError
|
410 |
raises
|
411 |
|
412 |
:returns: request response
|
413 |
"""
|
414 |
data = json_data |
415 |
if json_data is not None: |
416 |
data = json.dumps(json_data) |
417 |
self.set_header('Content-Type', 'application/json') |
418 |
self.set_header('Content-Length', len(data)) |
419 |
|
420 |
path = path4url('images', image_id, command)
|
421 |
return self.put(path, data=data, success=success, **kwargs) |
422 |
|
423 |
def floating_ip_pools_get(self, tenant_id, success=200, **kwargs): |
424 |
path = path4url(tenant_id, 'os-floating-ip-pools')
|
425 |
return self.get(path, success=success, **kwargs) |
426 |
|
427 |
def floating_ips_get(self, tenant_id, ip='', success=200, **kwargs): |
428 |
path = path4url(tenant_id, 'os-floating-ips', ip or '') |
429 |
return self.get(path, success=success, **kwargs) |
430 |
|
431 |
def floating_ips_post( |
432 |
self, tenant_id, json_data, ip='', success=201, **kwargs): |
433 |
path = path4url(tenant_id, 'os-floating-ips', ip or '') |
434 |
if json_data is not None: |
435 |
json_data = json.dumps(json_data) |
436 |
self.set_header('Content-Type', 'application/json') |
437 |
self.set_header('Content-Length', len(json_data)) |
438 |
return self.post(path, data=json_data, success=success, **kwargs) |
439 |
|
440 |
def floating_ips_delete(self, tenant_id, ip='', success=204, **kwargs): |
441 |
path = path4url(tenant_id, 'os-floating-ips', ip or '') |
442 |
return self.delete(path, success=success, **kwargs) |