root / kamaki / clients / compute_rest_api.py @ 606fe15f
History | View | Annotate | Download (4.5 kB)
1 | 606fe15f | Stavros Sachtouris | # Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 606fe15f | Stavros Sachtouris | #
|
3 | 606fe15f | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | 606fe15f | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | 606fe15f | Stavros Sachtouris | # conditions are met:
|
6 | 606fe15f | Stavros Sachtouris | #
|
7 | 606fe15f | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | 606fe15f | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | 606fe15f | Stavros Sachtouris | # disclaimer.
|
10 | 606fe15f | Stavros Sachtouris | #
|
11 | 606fe15f | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | 606fe15f | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | 606fe15f | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | 606fe15f | Stavros Sachtouris | # provided with the distribution.
|
15 | 606fe15f | Stavros Sachtouris | #
|
16 | 606fe15f | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 606fe15f | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 606fe15f | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 606fe15f | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 606fe15f | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 606fe15f | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 606fe15f | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 606fe15f | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 606fe15f | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 606fe15f | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 606fe15f | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 606fe15f | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 606fe15f | Stavros Sachtouris | #
|
29 | 606fe15f | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | 606fe15f | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | 606fe15f | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | 606fe15f | Stavros Sachtouris | # or implied, of GRNET S.A.
|
33 | 606fe15f | Stavros Sachtouris | |
34 | 606fe15f | Stavros Sachtouris | from kamaki.clients import Client |
35 | 606fe15f | Stavros Sachtouris | from kamaki.clients.utils import path4url |
36 | 606fe15f | Stavros Sachtouris | import json |
37 | 606fe15f | Stavros Sachtouris | |
38 | 606fe15f | Stavros Sachtouris | |
39 | 606fe15f | Stavros Sachtouris | class ComputeClientApi(Client): |
40 | 606fe15f | Stavros Sachtouris | |
41 | 606fe15f | Stavros Sachtouris | def servers_get(self, server_id='', command='', success=200, **kwargs): |
42 | 606fe15f | Stavros Sachtouris | """GET base_url/servers[/server_id][/command] request
|
43 | 606fe15f | Stavros Sachtouris |
|
44 | 606fe15f | Stavros Sachtouris | :param server_id: integer (as int or str)
|
45 | 606fe15f | Stavros Sachtouris |
|
46 | 606fe15f | Stavros Sachtouris | :param command: 'ips', 'stats', or ''
|
47 | 606fe15f | Stavros Sachtouris |
|
48 | 606fe15f | Stavros Sachtouris | :param success: success code or list or tupple of accepted success
|
49 | 606fe15f | Stavros Sachtouris | codes. if server response code is not in this list, a ClientError
|
50 | 606fe15f | Stavros Sachtouris | raises
|
51 | 606fe15f | Stavros Sachtouris |
|
52 | 606fe15f | Stavros Sachtouris | :returns: request response
|
53 | 606fe15f | Stavros Sachtouris | """
|
54 | 606fe15f | Stavros Sachtouris | path = path4url('servers', server_id, command)
|
55 | 606fe15f | Stavros Sachtouris | return self.get(path, success=success, **kwargs) |
56 | 606fe15f | Stavros Sachtouris | |
57 | 606fe15f | Stavros Sachtouris | def servers_delete(self, server_id='', command='', success=204, **kwargs): |
58 | 606fe15f | Stavros Sachtouris | """DEL ETE base_url/servers[/server_id][/command] request
|
59 | 606fe15f | Stavros Sachtouris |
|
60 | 606fe15f | Stavros Sachtouris | :param server_id: integer (as int or str)
|
61 | 606fe15f | Stavros Sachtouris |
|
62 | 606fe15f | Stavros Sachtouris | :param command: 'ips', 'stats', or ''
|
63 | 606fe15f | Stavros Sachtouris |
|
64 | 606fe15f | Stavros Sachtouris | :param success: success code or list or tupple of accepted success
|
65 | 606fe15f | Stavros Sachtouris | codes. if server response code is not in this list, a ClientError
|
66 | 606fe15f | Stavros Sachtouris | raises
|
67 | 606fe15f | Stavros Sachtouris |
|
68 | 606fe15f | Stavros Sachtouris | :returns: request response
|
69 | 606fe15f | Stavros Sachtouris | """
|
70 | 606fe15f | Stavros Sachtouris | path = path4url('servers', server_id, command)
|
71 | 606fe15f | Stavros Sachtouris | return self.delete(path, success=success, **kwargs) |
72 | 606fe15f | Stavros Sachtouris | |
73 | 606fe15f | Stavros Sachtouris | def servers_post(self, |
74 | 606fe15f | Stavros Sachtouris | server_id='', command='', json_data=None, success=202, **kwargs): |
75 | 606fe15f | Stavros Sachtouris | """POST base_url/servers[/server_id]/[command] request
|
76 | 606fe15f | Stavros Sachtouris |
|
77 | 606fe15f | Stavros Sachtouris | :param server_id: integer (as int or str)
|
78 | 606fe15f | Stavros Sachtouris |
|
79 | 606fe15f | Stavros Sachtouris | :param command: 'ips', 'stats', or ''
|
80 | 606fe15f | Stavros Sachtouris |
|
81 | 606fe15f | Stavros Sachtouris | :param json_data: a json-formated dict that will be send as data
|
82 | 606fe15f | Stavros Sachtouris |
|
83 | 606fe15f | Stavros Sachtouris | :param success: success code or list or tupple of accepted success
|
84 | 606fe15f | Stavros Sachtouris | codes. if server response code is not in this list, a ClientError
|
85 | 606fe15f | Stavros Sachtouris | raises
|
86 | 606fe15f | Stavros Sachtouris |
|
87 | 606fe15f | Stavros Sachtouris | :returns: request response
|
88 | 606fe15f | Stavros Sachtouris | """
|
89 | 606fe15f | Stavros Sachtouris | data = json_data |
90 | 606fe15f | Stavros Sachtouris | if json_data is not None: |
91 | 606fe15f | Stavros Sachtouris | data = json.dumps(json_data) |
92 | 606fe15f | Stavros Sachtouris | self.set_header('Content-Type', 'application/json') |
93 | 606fe15f | Stavros Sachtouris | self.set_header('Content-Length', len(data)) |
94 | 606fe15f | Stavros Sachtouris | |
95 | 606fe15f | Stavros Sachtouris | path = path4url('servers', server_id, command)
|
96 | 606fe15f | Stavros Sachtouris | return self.post(path, data=data, success=success, **kwargs) |
97 | 606fe15f | Stavros Sachtouris | |
98 | 606fe15f | Stavros Sachtouris | def servers_put(self, |
99 | 606fe15f | Stavros Sachtouris | server_id='', command='', json_data=None, success=204, **kwargs): |
100 | 606fe15f | Stavros Sachtouris | """PUT base_url/servers[/server_id]/[command] request
|
101 | 606fe15f | Stavros Sachtouris |
|
102 | 606fe15f | Stavros Sachtouris | :param server_id: integer (as int or str)
|
103 | 606fe15f | Stavros Sachtouris |
|
104 | 606fe15f | Stavros Sachtouris | :param command: 'ips', 'stats', or ''
|
105 | 606fe15f | Stavros Sachtouris |
|
106 | 606fe15f | Stavros Sachtouris | :param json_data: a json-formated dict that will be send as data
|
107 | 606fe15f | Stavros Sachtouris |
|
108 | 606fe15f | Stavros Sachtouris | :param success: success code or list or tupple of accepted success
|
109 | 606fe15f | Stavros Sachtouris | codes. if server response code is not in this list, a ClientError
|
110 | 606fe15f | Stavros Sachtouris | raises
|
111 | 606fe15f | Stavros Sachtouris |
|
112 | 606fe15f | Stavros Sachtouris | :returns: request response
|
113 | 606fe15f | Stavros Sachtouris | """
|
114 | 606fe15f | Stavros Sachtouris | data = json_data |
115 | 606fe15f | Stavros Sachtouris | if json_data is not None: |
116 | 606fe15f | Stavros Sachtouris | data = json.dumps(json_data) |
117 | 606fe15f | Stavros Sachtouris | self.set_header('Content-Type', 'application/json') |
118 | 606fe15f | Stavros Sachtouris | self.set_header('Content-Length', len(data)) |
119 | 606fe15f | Stavros Sachtouris | |
120 | 606fe15f | Stavros Sachtouris | path = path4url('servers', server_id, command)
|
121 | 606fe15f | Stavros Sachtouris | return self.put(path, data=data, success=success, **kwargs) |