Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (15.5 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 or ''))
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(
175
            self,
176
            server_id, server_name=None, json_data=None, success=204,
177
            **kwargs):
178
        """PUT base_url/servers/<server_id>
179

180
        :param json_data: a json-formated dict that will be send as data
181

182
        :param success: success code (iterable of) codes
183

184
        :raises ClientError: if returned code not in success list
185

186
        :returns: request response
187
        """
188
        self.set_param('server', server_name, iff=server_name)
189

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

    
197
    def servers_delete(self, server_id, success=204, **kwargs):
198
        """DEL ETE base_url/servers/<server_id>
199

200
        :param json_data: a json-formated dict that will be send as data
201

202
        :param success: success code (iterable of) codes
203

204
        :raises ClientError: if returned code not in success list
205

206
        :returns: request response
207
        """
208
        path = path4url('servers', server_id)
209
        return self.delete(path, success=success, **kwargs)
210

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

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

    
219
    def servers_metadata_post(
220
            self, server_id, json_data=None, success=202, **kwargs):
221
        """POST base_url/servers/<server_id>/metadata
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, 'metadata')
230
        return self.post(path, data=json_data, success=success, **kwargs)
231

    
232
    def servers_metadata_put(
233
            self, server_id, key=None, json_data=None, success=204, **kwargs):
234
        """PUT base_url/servers/<server_id>/metadata[/key]
235

236
        :returns: request response
237
        """
238
        if json_data:
239
            json_data = json.dumps(json_data)
240
            self.set_header('Content-Type', 'application/json')
241
            self.set_header('Content-Length', len(json_data))
242
        path = path4url('servers', server_id, 'metadata', key or '')
243
        return self.put(path, data=json_data, success=success, **kwargs)
244

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

248
        :returns: request response
249
        """
250
        path = path4url('servers', server_id, 'metadata', key)
251
        return self.delete(path, success=success, **kwargs)
252

    
253
    def servers_actions_post(
254
            self, server_id, json_data=None, success=202, **kwargs):
255
        """POST base_url/servers/<server_id>/action
256

257
        :returns: request response
258
        """
259
        if json_data:
260
            json_data = json.dumps(json_data)
261
            self.set_header('Content-Type', 'application/json')
262
            self.set_header('Content-Length', len(json_data))
263
        path = path4url('servers', server_id, 'action')
264
        return self.post(path, data=json_data, success=success, **kwargs)
265

    
266
    def servers_ips_get(
267
            self, server_id,
268
            network_id=None, changes_since=None, success=(304, 200),
269
            **kwargs):
270
        """GET base_url/servers/<server_id>/ips[/network_id]
271

272
        :param changes_since: time/date stamp in UNIX/epoch time. Checks for
273
            changes since a previous request.
274

275
        :returns: request response
276
        """
277
        self.set_param('changes-since', changes_since, iff=changes_since)
278
        path = path4url('servers', server_id, 'ips', network_id or '')
279
        return self.get(path, success=success, **kwargs)
280

    
281
    def images_get(
282
            self,
283
            image_id='',
284
            detail=False,
285
            changes_since=None,
286
            server_name=None,
287
            name=None,
288
            status=None,
289
            marker=None,
290
            limit=None,
291
            type=None,
292
            success=200,
293
            **kwargs):
294
        """GET base_url[/image_id][/command]
295

296
        :param image_id: (str) ignored if detail
297

298
        :param detail: (bool)
299

300
        --- Parameters ---
301
        :param changes_since: when the image last changed status
302

303
        :param server_name: Name of the server in URL format
304

305
        :param name: Name of the image
306

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

309
        :param marker: UUID of the image at which you want to set a marker
310

311
        :param limit: Integer value for the limit of values to return
312

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

315
        :returns: request response
316
        """
317
        if not image_id:
318
            self.set_param('changes-since', changes_since, iff=changes_since)
319
            self.set_param('name', name, iff=name)
320
            self.set_param('status', status, iff=status)
321
            self.set_param('marker', marker, iff=marker)
322
            self.set_param('limit', limit, iff=limit)
323
            self.set_param('type', type, iff=type)
324
            self.set_param('server', server_name, iff=server_name)
325

    
326
        path = path4url('images', 'detail' if detail else (image_id or ''))
327
        return self.get(path, success=success, **kwargs)
328

    
329
    def images_delete(self, image_id='', success=204, **kwargs):
330
        """DEL ETE base_url/images/<image_id>
331

332
        :returns: request response
333
        """
334
        path = path4url('images', image_id)
335
        return self.delete(path, success=success, **kwargs)
336

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

340
        :returns: request response
341
        """
342
        path = path4url('images', image_id, 'metadata', key or '')
343
        return self.get(path, success=success, **kwargs)
344

    
345
    def images_metadata_post(
346
            self, image_id, json_data=None, success=201, **kwargs):
347
        """POST base_url/images/<image_id>/metadata
348

349
        :returns: request response
350
        """
351
        if json_data is not None:
352
            json_data = json.dumps(json_data)
353
            self.set_header('Content-Type', 'application/json')
354
            self.set_header('Content-Length', len(json_data))
355

    
356
        path = path4url('images', image_id, 'metadata')
357
        return self.post(path, data=json_data, success=success, **kwargs)
358

    
359
    def images_metadata_put(
360
            self, image_id, key=None, json_data=None, success=201, **kwargs):
361
        """PUT base_url/images/<image_id>/metadata
362

363
        :returns: request response
364
        """
365
        if json_data is not None:
366
            json_data = json.dumps(json_data)
367
            self.set_header('Content-Type', 'application/json')
368
            self.set_header('Content-Length', len(json_data))
369

    
370
        path = path4url('images', image_id, 'metadata')
371
        return self.put(path, data=json_data, success=success, **kwargs)
372

    
373
    def images_metadata_delete(self, image_id, key, success=204, **kwargs):
374
        """DEL ETE base_url/images/<image_id>/metadata/key
375

376
        :returns: request response
377
        """
378
        path = path4url('images', image_id, 'metadata', key)
379
        return self.put(path, success=success, **kwargs)
380

    
381
    def flavors_get(
382
            self,
383
            flavor_id='',
384
            detail=False,
385
            changes_since=None,
386
            minDisk=None,
387
            minRam=None,
388
            marker=None,
389
            limit=None,
390
            success=200,
391
            **kwargs):
392
        """GET base_url[/flavor_id][/command]
393

394
        :param flavor_id: ignored if detail
395

396
        :param detail: (bool)
397

398
        --- Parameters ---
399

400
        :param changes_since: when the flavor last changed
401

402
        :param minDisk: minimum disk space in GB filter
403

404
        :param minRam: minimum RAM filter
405

406
        :param marker: UUID of the flavor at which to set a marker
407

408
        :param limit: limit the number of returned values
409

410
        :returns: request response
411
        """
412
        if not flavor_id:
413
            self.set_param('changes-since', changes_since, iff=changes_since)
414
            self.set_param('minDisk', minDisk, iff=minDisk)
415
            self.set_param('minRam', minRam, iff=minRam)
416
            self.set_param('marker', marker, iff=marker)
417
            self.set_param('limit', limit, iff=limit)
418

    
419
        path = path4url('flavors', 'detail' if detail else (flavor_id or ''))
420
        return self.get(path, success=success, **kwargs)
421

    
422
    def floating_ip_pools_get(self, tenant_id, success=200, **kwargs):
423
        path = path4url(tenant_id, 'os-floating-ip-pools')
424
        return self.get(path, success=success, **kwargs)
425

    
426
    def floating_ips_get(self, tenant_id, ip='', success=200, **kwargs):
427
        path = path4url(tenant_id, 'os-floating-ips', ip or '')
428
        return self.get(path, success=success, **kwargs)
429

    
430
    def floating_ips_post(
431
            self, tenant_id, json_data, ip='', success=201, **kwargs):
432
        path = path4url(tenant_id, 'os-floating-ips', ip or '')
433
        if json_data is not None:
434
            json_data = json.dumps(json_data)
435
            self.set_header('Content-Type', 'application/json')
436
            self.set_header('Content-Length', len(json_data))
437
        return self.post(path, data=json_data, success=success, **kwargs)
438

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