Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / compute / rest_api.py @ f1e45161

History | View | Annotate | Download (14 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
        :param changes-since: A time/date stamp for when the server last
74
            changed status
75

76
        :param image: Name of the image in URL format
77

78
        :param flavor: Name of the flavor in URL format
79

80
        :param name: Name of the server as a string
81

82
        :param marker: UUID of the server at which you want to set a marker
83

84
        :param limit: (int) limit of values to return
85

86
        :param status: Status of the server (e.g. filter on "ACTIVE")
87

88
        :param host: Name of the host as a string
89

90
        :returns: request response
91
        """
92
        if not server_id:
93
            self.set_param('changes-since', changes_since, iff=changes_since)
94
            self.set_param('image', image, iff=image)
95
            self.set_param('flavor', flavor, iff=flavor)
96
            self.set_param('name', name, iff=name)
97
            self.set_param('marker', marker, iff=marker)
98
            self.set_param('limit', limit, iff=limit)
99
            self.set_param('status', status, iff=status)
100
            self.set_param('host', host, iff=host)
101

    
102
        path = path4url('servers', 'detail' if detail else (server_id or ''))
103
        return self.get(path, success=success, **kwargs)
104

    
105
    def servers_post(
106
            self,
107
            security_group=None,
108
            user_data=None,
109
            availability_zone=None,
110
            json_data=None,
111
            success=202,
112
            **kwargs):
113
        """POST base_url/servers
114

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

117
        :param security_group: (str)
118

119
        :param user_data: Use to pass configuration information or scripts upon
120
            launch. Must be Base64 encoded.
121

122
        :param availability_zone: (str)
123

124
        :returns: request response
125
        """
126

    
127
        self.set_param('security_group', security_group, iff=security_group)
128
        self.set_param('user_data', user_data, iff=user_data)
129
        self.set_param(
130
            'availability_zone', availability_zone, iff=availability_zone)
131

    
132
        if json_data:
133
            json_data = json.dumps(json_data)
134
            self.set_header('Content-Type', 'application/json')
135
            self.set_header('Content-Length', len(json_data))
136

    
137
        path = path4url('servers')
138
        return self.post(path, data=json_data, success=success, **kwargs)
139

    
140
    def servers_put(
141
            self,
142
            server_id, server_name=None, json_data=None, success=204,
143
            **kwargs):
144
        """PUT base_url/servers/<server_id>
145

146
        :param json_data: a json-formated dict that will be send as data
147

148
        :param success: success code (iterable of) codes
149

150
        :raises ClientError: if returned code not in success list
151

152
        :returns: request response
153
        """
154
        self.set_param('server', server_name, iff=server_name)
155

    
156
        if json_data:
157
            json_data = json.dumps(json_data)
158
            self.set_header('Content-Type', 'application/json')
159
            self.set_header('Content-Length', len(json_data))
160
        path = path4url('servers', server_id)
161
        return self.put(path, data=json_data, success=success, **kwargs)
162

    
163
    def servers_delete(self, server_id, success=204, **kwargs):
164
        """DEL ETE base_url/servers/<server_id>
165

166
        :param json_data: a json-formated dict that will be send as data
167

168
        :param success: success code (iterable of) codes
169

170
        :raises ClientError: if returned code not in success list
171

172
        :returns: request response
173
        """
174
        path = path4url('servers', server_id)
175
        return self.delete(path, success=success, **kwargs)
176

    
177
    def servers_metadata_get(self, server_id, key=None, success=200, **kwargs):
178
        """GET base_url/servers/<server_id>/metadata[/key]
179

180
        :returns: request response
181
        """
182
        path = path4url('servers', server_id, 'metadata', key or '')
183
        return self.get(path, success=success, **kwargs)
184

    
185
    def servers_metadata_post(
186
            self, server_id, json_data=None, success=202, **kwargs):
187
        """POST base_url/servers/<server_id>/metadata
188

189
        :returns: request response
190
        """
191
        if json_data:
192
            json_data = json.dumps(json_data)
193
            self.set_header('Content-Type', 'application/json')
194
            self.set_header('Content-Length', len(json_data))
195
        path = path4url('servers', server_id, 'metadata')
196
        return self.post(path, data=json_data, success=success, **kwargs)
197

    
198
    def servers_metadata_put(
199
            self, server_id, key=None, json_data=None, success=204, **kwargs):
200
        """PUT base_url/servers/<server_id>/metadata[/key]
201

202
        :returns: request response
203
        """
204
        if json_data:
205
            json_data = json.dumps(json_data)
206
            self.set_header('Content-Type', 'application/json')
207
            self.set_header('Content-Length', len(json_data))
208
        path = path4url('servers', server_id, 'metadata', key or '')
209
        return self.put(path, data=json_data, success=success, **kwargs)
210

    
211
    def servers_metadata_delete(self, server_id, key, success=204, **kwargs):
212
        """DEL ETE base_url/servers/<server_id>/metadata[/key]
213

214
        :returns: request response
215
        """
216
        path = path4url('servers', server_id, 'metadata', key)
217
        return self.delete(path, success=success, **kwargs)
218

    
219
    def servers_action_post(
220
            self, server_id, json_data=None, success=202, **kwargs):
221
        """POST base_url/servers/<server_id>/action
222

223
        :returns: request response
224
        """
225
        if json_data:
226
            json_data = json.dumps(json_data)
227
            self.set_header('Content-Type', 'application/json')
228
            self.set_header('Content-Length', len(json_data))
229
        path = path4url('servers', server_id, 'action')
230
        return self.post(path, data=json_data, success=success, **kwargs)
231

    
232
    def servers_ips_get(
233
            self, server_id,
234
            network_id=None, changes_since=None, success=(304, 200),
235
            **kwargs):
236
        """GET base_url/servers/<server_id>/ips[/network_id]
237

238
        :param changes_since: time/date stamp in UNIX/epoch time. Checks for
239
            changes since a previous request.
240

241
        :returns: request response
242
        """
243
        self.set_param('changes-since', changes_since, iff=changes_since)
244
        path = path4url('servers', server_id, 'ips', network_id or '')
245
        return self.get(path, success=success, **kwargs)
246

    
247
    def images_get(
248
            self,
249
            image_id='',
250
            detail=False,
251
            changes_since=None,
252
            server_name=None,
253
            name=None,
254
            status=None,
255
            marker=None,
256
            limit=None,
257
            type=None,
258
            success=200,
259
            **kwargs):
260
        """GET base_url[/image_id][/command]
261

262
        :param image_id: (str) ignored if detail
263

264
        :param detail: (bool)
265

266
        --- Parameters ---
267
        :param changes_since: when the image last changed status
268

269
        :param server_name: Name of the server in URL format
270

271
        :param name: Name of the image
272

273
        :param status: Status of the image (e.g. filter on "ACTIVE")
274

275
        :param marker: UUID of the image at which you want to set a marker
276

277
        :param limit: Integer value for the limit of values to return
278

279
        :param type: Type of image (e.g. BASE, SERVER, or ALL)
280

281
        :returns: request response
282
        """
283
        if not image_id:
284
            self.set_param('changes-since', changes_since, iff=changes_since)
285
            self.set_param('name', name, iff=name)
286
            self.set_param('status', status, iff=status)
287
            self.set_param('marker', marker, iff=marker)
288
            self.set_param('limit', limit, iff=limit)
289
            self.set_param('type', type, iff=type)
290
            self.set_param('server', server_name, iff=server_name)
291

    
292
        path = path4url('images', 'detail' if detail else (image_id or ''))
293
        return self.get(path, success=success, **kwargs)
294

    
295
    def images_delete(self, image_id='', success=204, **kwargs):
296
        """DEL ETE base_url/images/<image_id>
297

298
        :returns: request response
299
        """
300
        path = path4url('images', image_id)
301
        return self.delete(path, success=success, **kwargs)
302

    
303
    def images_metadata_get(self, image_id, key=None, success=200, **kwargs):
304
        """GET base_url/<image_id>/metadata[/key]
305

306
        :returns: request response
307
        """
308
        path = path4url('images', image_id, 'metadata', key or '')
309
        return self.get(path, success=success, **kwargs)
310

    
311
    def images_metadata_post(
312
            self, image_id, json_data=None, success=201, **kwargs):
313
        """POST base_url/images/<image_id>/metadata
314

315
        :returns: request response
316
        """
317
        if json_data is not None:
318
            json_data = json.dumps(json_data)
319
            self.set_header('Content-Type', 'application/json')
320
            self.set_header('Content-Length', len(json_data))
321

    
322
        path = path4url('images', image_id, 'metadata')
323
        return self.post(path, data=json_data, success=success, **kwargs)
324

    
325
    def images_metadata_put(
326
            self, image_id, key=None, json_data=None, success=201, **kwargs):
327
        """PUT base_url/images/<image_id>/metadata
328

329
        :returns: request response
330
        """
331
        if json_data is not None:
332
            json_data = json.dumps(json_data)
333
            self.set_header('Content-Type', 'application/json')
334
            self.set_header('Content-Length', len(json_data))
335

    
336
        path = path4url('images', image_id, 'metadata')
337
        return self.put(path, data=json_data, success=success, **kwargs)
338

    
339
    def images_metadata_delete(self, image_id, key, success=204, **kwargs):
340
        """DEL ETE base_url/images/<image_id>/metadata/key
341

342
        :returns: request response
343
        """
344
        path = path4url('images', image_id, 'metadata', key)
345
        return self.delete(path, success=success, **kwargs)
346

    
347
    def flavors_get(
348
            self,
349
            flavor_id='',
350
            detail=False,
351
            changes_since=None,
352
            minDisk=None,
353
            minRam=None,
354
            marker=None,
355
            limit=None,
356
            success=200,
357
            **kwargs):
358
        """GET base_url[/flavor_id][/command]
359

360
        :param flavor_id: ignored if detail
361

362
        :param detail: (bool)
363

364
        --- Parameters ---
365

366
        :param changes_since: when the flavor last changed
367

368
        :param minDisk: minimum disk space in GB filter
369

370
        :param minRam: minimum RAM filter
371

372
        :param marker: UUID of the flavor at which to set a marker
373

374
        :param limit: limit the number of returned values
375

376
        :returns: request response
377
        """
378
        if not flavor_id:
379
            self.set_param('changes-since', changes_since, iff=changes_since)
380
            self.set_param('minDisk', minDisk, iff=minDisk)
381
            self.set_param('minRam', minRam, iff=minRam)
382
            self.set_param('marker', marker, iff=marker)
383
            self.set_param('limit', limit, iff=limit)
384

    
385
        path = path4url('flavors', 'detail' if detail else (flavor_id or ''))
386
        return self.get(path, success=success, **kwargs)
387

    
388
    def floating_ip_pools_get(self, success=200, **kwargs):
389
        path = path4url('os-floating-ip-pools')
390
        return self.get(path, success=success, **kwargs)
391

    
392
    def floating_ips_get(self, ip='', success=200, **kwargs):
393
        path = path4url('os-floating-ips', ip or '')
394
        return self.get(path, success=success, **kwargs)
395

    
396
    def floating_ips_post(self, json_data, ip='', success=200, **kwargs):
397
        path = path4url('os-floating-ips', ip or '')
398
        return self.post(path, json=json_data, success=success, **kwargs)
399

    
400
    def floating_ips_delete(self, ip='', success=204, **kwargs):
401
        path = path4url('os-floating-ips', ip or '')
402
        return self.delete(path, success=success, **kwargs)