Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (15.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
    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
    """
267
    def servers_actions_post
268
    def servers_ips_get
269

270
    def servers_get(self, server_id='', command='', success=200, **kwargs):
271
        ""GET base_url/servers[/server_id][/command] request
272

273
        :param server_id: integer (as int or str)
274

275
        :param command: 'ips', 'stats', or ''
276

277
        :param success: success code or list or tupple of accepted success
278
            codes. if server response code is not in this list, a ClientError
279
            raises
280

281
        :returns: request response
282
        ""
283
        path = path4url('servers', server_id, command)
284

285
    def servers_post(
286
            self,
287
            server_id='',
288
            command='',
289
            json_data=None,
290
            success=202,
291
            **kwargs):
292
        ""POST base_url/servers[/server_id]/[command] request
293

294
        :param server_id: integer (as int or str)
295

296
        :param command: 'ips', 'stats', or ''
297

298
        :param json_data: a json-formated dict that will be send as data
299

300
        :param success: success code or list or tupple of accepted success
301
            codes. if server response code is not in this list, a ClientError
302
            raises
303

304
        :returns: request response
305
        ""
306
        data = json_data
307
        if json_data:
308
            data = json.dumps(json_data)
309
            self.set_header('Content-Type', 'application/json')
310
            self.set_header('Content-Length', len(data))
311

312
        path = path4url('servers', server_id, command)
313
        return self.post(path, data=data, success=success, **kwargs)
314
    """
315

    
316
    def flavors_get(self, flavor_id='', command='', success=200, **kwargs):
317
        """GET base_url[/flavor_id][/command]
318

319
        :param flavor_id: integer (str or int)
320

321
        :param command: flavor service command
322

323
        :param success: success code or list or tupple of accepted success
324
            codes. if server response code is not in this list, a ClientError
325
            raises
326

327
        :returns: request response
328
        """
329
        path = path4url('flavors', flavor_id, command)
330
        return self.get(path, success=success, **kwargs)
331

    
332
    def images_get(self, image_id='', command='', success=200, **kwargs):
333
        """GET base_url[/image_id][/command]
334

335
        :param image_id: string
336

337
        :param command: image server command
338

339
        :param success: success code or list or tupple of accepted success
340
            codes. if server response code is not in this list, a ClientError
341
            raises
342

343
        :returns: request response
344
        """
345
        path = path4url('images', image_id, command)
346
        return self.get(path, success=success, **kwargs)
347

    
348
    def images_delete(self, image_id='', command='', success=204, **kwargs):
349
        """DELETE base_url[/image_id][/command]
350

351
        :param image_id: string
352

353
        :param command: image server command
354

355
        :param success: success code or list or tuple of accepted success
356
            codes. if server response code is not in this list, a ClientError
357
            raises
358

359
        :returns: request response
360
        """
361
        path = path4url('images', image_id, command)
362
        return self.delete(path, success=success, **kwargs)
363

    
364
    def images_post(
365
            self,
366
            image_id='',
367
            command='',
368
            json_data=None,
369
            success=201,
370
            **kwargs):
371
        """POST base_url/images[/image_id]/[command] request
372

373
        :param image_id: string
374

375
        :param command: image server command
376

377
        :param json_data: (dict) will be send as data
378

379
        :param success: success code or list or tuple of accepted success
380
            codes. if server response code is not in this list, a ClientError
381
            raises
382

383
        :returns: request response
384
        """
385
        data = json_data
386
        if json_data is not None:
387
            data = json.dumps(json_data)
388
            self.set_header('Content-Type', 'application/json')
389
            self.set_header('Content-Length', len(data))
390

    
391
        path = path4url('images', image_id, command)
392
        return self.post(path, data=data, success=success, **kwargs)
393

    
394
    def images_put(
395
            self,
396
            image_id='',
397
            command='',
398
            json_data=None,
399
            success=201,
400
            **kwargs):
401
        """PUT base_url/images[/image_id]/[command] request
402

403
        :param image_id: string
404

405
        :param command: image server command
406

407
        :param json_data: (dict) will be send as data
408

409
        :param success: success code or list or tuple of accepted success
410
            codes. if server response code is not in this list, a ClientError
411
            raises
412

413
        :returns: request response
414
        """
415
        data = json_data
416
        if json_data is not None:
417
            data = json.dumps(json_data)
418
            self.set_header('Content-Type', 'application/json')
419
            self.set_header('Content-Length', len(data))
420

    
421
        path = path4url('images', image_id, command)
422
        return self.put(path, data=data, success=success, **kwargs)
423

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

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

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

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