Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / compute / rest_api.py @ 65a8b1da

History | View | Annotate | Download (16.2 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(
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
    """
254
    def servers_actions_post
255
    def servers_ips_get
256
    """
257

    
258
    """
259
    def servers_get(self, server_id='', command='', success=200, **kwargs):
260
        ""GET base_url/servers[/server_id][/command] request
261

262
        :param server_id: integer (as int or str)
263

264
        :param command: 'ips', 'stats', or ''
265

266
        :param success: success code or list or tupple of accepted success
267
            codes. if server response code is not in this list, a ClientError
268
            raises
269

270
        :returns: request response
271
        ""
272
        path = path4url('servers', server_id, command)
273
    ""
274

275
    def servers_delete(self, server_id='', command='', success=204, **kwargs):
276
        ""DEL ETE base_url/servers[/server_id][/command] request
277

278
        :param server_id: integer (as int or str)
279

280
        :param command: 'ips', 'stats', or ''
281

282
        :param success: success code or list or tupple of accepted success
283
            codes. if server response code is not in this list, a ClientError
284
            raises
285

286
        :returns: request response
287
        ""
288
        path = path4url('servers', server_id, command)
289
        return self.delete(path, success=success, **kwargs)
290

291
    def servers_post(
292
            self,
293
            server_id='',
294
            command='',
295
            json_data=None,
296
            success=202,
297
            **kwargs):
298
        ""POST base_url/servers[/server_id]/[command] request
299

300
        :param server_id: integer (as int or str)
301

302
        :param command: 'ips', 'stats', or ''
303

304
        :param json_data: a json-formated dict that will be send as data
305

306
        :param success: success code or list or tupple of accepted success
307
            codes. if server response code is not in this list, a ClientError
308
            raises
309

310
        :returns: request response
311
        ""
312
        data = json_data
313
        if json_data:
314
            data = json.dumps(json_data)
315
            self.set_header('Content-Type', 'application/json')
316
            self.set_header('Content-Length', len(data))
317

318
        path = path4url('servers', server_id, command)
319
        return self.post(path, data=data, success=success, **kwargs)
320

321
    def servers_put(
322
            self,
323
            server_id='',
324
            command='',
325
            json_data=None,
326
            success=204,
327
            **kwargs):
328
        ""PUT base_url/servers[/server_id]/[command] request
329

330
        :param server_id: integer (as int or str)
331

332
        :param command: 'ips', 'stats', or ''
333

334
        :param json_data: a json-formated dict that will be send as data
335

336
        :param success: success code or list or tupple of accepted success
337
            codes. if server response code is not in this list, a ClientError
338
            raises
339

340
        :returns: request response
341
        ""
342
        data = json_data
343
        if json_data is not None:
344
            data = json.dumps(json_data)
345
            self.set_header('Content-Type', 'application/json')
346
            self.set_header('Content-Length', len(data))
347

348
        path = path4url('servers', server_id, command)
349
        return self.put(path, data=data, success=success, **kwargs)
350
    """
351

    
352
    def flavors_get(self, flavor_id='', command='', success=200, **kwargs):
353
        """GET base_url[/flavor_id][/command]
354

355
        :param flavor_id: integer (str or int)
356

357
        :param command: flavor service command
358

359
        :param success: success code or list or tupple of accepted success
360
            codes. if server response code is not in this list, a ClientError
361
            raises
362

363
        :returns: request response
364
        """
365
        path = path4url('flavors', flavor_id, command)
366
        return self.get(path, success=success, **kwargs)
367

    
368
    def images_get(self, image_id='', command='', success=200, **kwargs):
369
        """GET base_url[/image_id][/command]
370

371
        :param image_id: string
372

373
        :param command: image server command
374

375
        :param success: success code or list or tupple of accepted success
376
            codes. if server response code is not in this list, a ClientError
377
            raises
378

379
        :returns: request response
380
        """
381
        path = path4url('images', image_id, command)
382
        return self.get(path, success=success, **kwargs)
383

    
384
    def images_delete(self, image_id='', command='', success=204, **kwargs):
385
        """DELETE base_url[/image_id][/command]
386

387
        :param image_id: string
388

389
        :param command: image server command
390

391
        :param success: success code or list or tuple of accepted success
392
            codes. if server response code is not in this list, a ClientError
393
            raises
394

395
        :returns: request response
396
        """
397
        path = path4url('images', image_id, command)
398
        return self.delete(path, success=success, **kwargs)
399

    
400
    def images_post(
401
            self,
402
            image_id='',
403
            command='',
404
            json_data=None,
405
            success=201,
406
            **kwargs):
407
        """POST base_url/images[/image_id]/[command] request
408

409
        :param image_id: string
410

411
        :param command: image server command
412

413
        :param json_data: (dict) will be send as data
414

415
        :param success: success code or list or tuple of accepted success
416
            codes. if server response code is not in this list, a ClientError
417
            raises
418

419
        :returns: request response
420
        """
421
        data = json_data
422
        if json_data is not None:
423
            data = json.dumps(json_data)
424
            self.set_header('Content-Type', 'application/json')
425
            self.set_header('Content-Length', len(data))
426

    
427
        path = path4url('images', image_id, command)
428
        return self.post(path, data=data, success=success, **kwargs)
429

    
430
    def images_put(
431
            self,
432
            image_id='',
433
            command='',
434
            json_data=None,
435
            success=201,
436
            **kwargs):
437
        """PUT base_url/images[/image_id]/[command] request
438

439
        :param image_id: string
440

441
        :param command: image server command
442

443
        :param json_data: (dict) will be send as data
444

445
        :param success: success code or list or tuple of accepted success
446
            codes. if server response code is not in this list, a ClientError
447
            raises
448

449
        :returns: request response
450
        """
451
        data = json_data
452
        if json_data is not None:
453
            data = json.dumps(json_data)
454
            self.set_header('Content-Type', 'application/json')
455
            self.set_header('Content-Length', len(data))
456

    
457
        path = path4url('images', image_id, command)
458
        return self.put(path, data=data, success=success, **kwargs)
459

    
460
    def floating_ip_pools_get(self, tenant_id, success=200, **kwargs):
461
        path = path4url(tenant_id, 'os-floating-ip-pools')
462
        return self.get(path, success=success, **kwargs)
463

    
464
    def floating_ips_get(self, tenant_id, ip='', success=200, **kwargs):
465
        path = path4url(tenant_id, 'os-floating-ips', ip or '')
466
        return self.get(path, success=success, **kwargs)
467

    
468
    def floating_ips_post(
469
            self, tenant_id, json_data, ip='', success=201, **kwargs):
470
        path = path4url(tenant_id, 'os-floating-ips', ip or '')
471
        if json_data is not None:
472
            json_data = json.dumps(json_data)
473
            self.set_header('Content-Type', 'application/json')
474
            self.set_header('Content-Length', len(json_data))
475
        return self.post(path, data=json_data, success=success, **kwargs)
476

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