Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / compute / rest_api.py @ 03033b54

History | View | Annotate | Download (9 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 ComputeRestClient(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(
74
            self,
75
            server_id='',
76
            command='',
77
            json_data=None,
78
            success=202,
79
            **kwargs):
80
        """POST base_url/servers[/server_id]/[command] request
81

82
        :param server_id: integer (as int or str)
83

84
        :param command: 'ips', 'stats', or ''
85

86
        :param json_data: a json-formated dict that will be send as data
87

88
        :param success: success code or list or tupple of accepted success
89
            codes. if server response code is not in this list, a ClientError
90
            raises
91

92
        :returns: request response
93
        """
94
        data = json_data
95
        if json_data:
96
            data = json.dumps(json_data)
97
            self.set_header('Content-Type', 'application/json')
98
            self.set_header('Content-Length', len(data))
99

    
100
        path = path4url('servers', server_id, command)
101
        return self.post(path, data=data, success=success, **kwargs)
102

    
103
    def servers_put(
104
            self,
105
            server_id='',
106
            command='',
107
            json_data=None,
108
            success=204,
109
            **kwargs):
110
        """PUT base_url/servers[/server_id]/[command] request
111

112
        :param server_id: integer (as int or str)
113

114
        :param command: 'ips', 'stats', or ''
115

116
        :param json_data: a json-formated dict that will be send as data
117

118
        :param success: success code or list or tupple of accepted success
119
            codes. if server response code is not in this list, a ClientError
120
            raises
121

122
        :returns: request response
123
        """
124
        data = json_data
125
        if json_data is not None:
126
            data = json.dumps(json_data)
127
            self.set_header('Content-Type', 'application/json')
128
            self.set_header('Content-Length', len(data))
129

    
130
        path = path4url('servers', server_id, command)
131
        return self.put(path, data=data, success=success, **kwargs)
132

    
133
    def flavors_get(self, flavor_id='', command='', success=200, **kwargs):
134
        """GET base_url[/flavor_id][/command]
135

136
        :param flavor_id: integer (str or int)
137

138
        :param command: flavor service command
139

140
        :param success: success code or list or tupple of accepted success
141
            codes. if server response code is not in this list, a ClientError
142
            raises
143

144
        :returns: request response
145
        """
146
        path = path4url('flavors', flavor_id, command)
147
        return self.get(path, success=success, **kwargs)
148

    
149
    def images_get(self, image_id='', command='', success=200, **kwargs):
150
        """GET base_url[/image_id][/command]
151

152
        :param image_id: string
153

154
        :param command: image server command
155

156
        :param success: success code or list or tupple of accepted success
157
            codes. if server response code is not in this list, a ClientError
158
            raises
159

160
        :returns: request response
161
        """
162
        path = path4url('images', image_id, command)
163
        return self.get(path, success=success, **kwargs)
164

    
165
    def images_delete(self, image_id='', command='', success=204, **kwargs):
166
        """DELETE base_url[/image_id][/command]
167

168
        :param image_id: string
169

170
        :param command: image server command
171

172
        :param success: success code or list or tuple of accepted success
173
            codes. if server response code is not in this list, a ClientError
174
            raises
175

176
        :returns: request response
177
        """
178
        path = path4url('images', image_id, command)
179
        return self.delete(path, success=success, **kwargs)
180

    
181
    def images_post(
182
            self,
183
            image_id='',
184
            command='',
185
            json_data=None,
186
            success=201,
187
            **kwargs):
188
        """POST base_url/images[/image_id]/[command] request
189

190
        :param image_id: string
191

192
        :param command: image server command
193

194
        :param json_data: (dict) will be send as data
195

196
        :param success: success code or list or tuple of accepted success
197
            codes. if server response code is not in this list, a ClientError
198
            raises
199

200
        :returns: request response
201
        """
202
        data = json_data
203
        if json_data is not None:
204
            data = json.dumps(json_data)
205
            self.set_header('Content-Type', 'application/json')
206
            self.set_header('Content-Length', len(data))
207

    
208
        path = path4url('images', image_id, command)
209
        return self.post(path, data=data, success=success, **kwargs)
210

    
211
    def images_put(
212
            self,
213
            image_id='',
214
            command='',
215
            json_data=None,
216
            success=201,
217
            **kwargs):
218
        """PUT base_url/images[/image_id]/[command] request
219

220
        :param image_id: string
221

222
        :param command: image server command
223

224
        :param json_data: (dict) will be send as data
225

226
        :param success: success code or list or tuple of accepted success
227
            codes. if server response code is not in this list, a ClientError
228
            raises
229

230
        :returns: request response
231
        """
232
        data = json_data
233
        if json_data is not None:
234
            data = json.dumps(json_data)
235
            self.set_header('Content-Type', 'application/json')
236
            self.set_header('Content-Length', len(data))
237

    
238
        path = path4url('images', image_id, command)
239
        return self.put(path, data=data, success=success, **kwargs)
240

    
241
    def floating_ip_pools_get(self, tenant_id, success=200, **kwargs):
242
        path = path4url(tenant_id, 'os-floating-ip-pools')
243
        return self.get(path, success=success, **kwargs)
244

    
245
    def floating_ips_get(self, tenant_id, ip='', success=200, **kwargs):
246
        path = path4url(tenant_id, 'os-floating-ips', ip or '')
247
        return self.get(path, success=success, **kwargs)
248

    
249
    def floating_ips_post(
250
            self, tenant_id, json_data, ip='', success=201, **kwargs):
251
        path = path4url(tenant_id, 'os-floating-ips', ip or '')
252
        if json_data is not None:
253
            json_data = json.dumps(json_data)
254
            self.set_header('Content-Type', 'application/json')
255
            self.set_header('Content-Length', len(json_data))
256
        return self.post(path, data=json_data, success=success, **kwargs)
257

    
258
    def floating_ips_delete(self, tenant_id, ip='', success=204, **kwargs):
259
        path = path4url(tenant_id, 'os-floating-ips', ip or '')
260
        return self.delete(path, success=success, **kwargs)