__init__.py 16.8 KB
Newer Older
1
# Copyright 2011-2013 GRNET S.A. All rights reserved.
Giorgos Verigakis's avatar
Giorgos Verigakis committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

34
35
from time import sleep

36
from kamaki.clients.cyclades.rest_api import CycladesRestClient
37
from kamaki.clients.network import NetworkClient
38
from kamaki.clients.utils import path4url
39
from kamaki.clients import ClientError
Giorgos Verigakis's avatar
Giorgos Verigakis committed
40

41

42
class CycladesClient(CycladesRestClient):
43
    """Synnefo Cyclades Compute API client"""
44

45
46
47
48
49
50
51
52
53
    def create_server(
            self, name, flavor_id, image_id,
            metadata=None, personality=None):
        """Submit request to create a new server

        :param name: (str)

        :param flavor_id: integer id denoting a preset hardware configuration

54
        :param image_id: (str) id denoting the OS image to run on virt. server
55
56
57
58

        :param metadata: (dict) vm metadata updated by os/users image metadata

        :param personality: a list of (file path, file contents) tuples,
59
            describing files to be injected into virtual server upon creation
60

61
        :returns: a dict with the new virtual server details
62
63
64
65
66
67
68
69
70
71
72

        :raises ClientError: wraps request errors
        """
        image = self.get_image_details(image_id)
        metadata = metadata or dict()
        for key in ('os', 'users'):
            try:
                metadata[key] = image['metadata'][key]
            except KeyError:
                pass

73
74
75
        return super(CycladesClient, self).create_server(
            name, flavor_id, image_id,
            metadata=metadata, personality=personality)
76

Giorgos Verigakis's avatar
Giorgos Verigakis committed
77
    def start_server(self, server_id):
78
79
80
        """Submit a startup request

        :param server_id: integer (str or int)
81
82

        :returns: (dict) response headers
83
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
84
        req = {'start': {}}
85
        r = self.servers_action_post(server_id, json_data=req, success=202)
86
        return r.headers
87

Giorgos Verigakis's avatar
Giorgos Verigakis committed
88
    def shutdown_server(self, server_id):
89
90
91
        """Submit a shutdown request

        :param server_id: integer (str or int)
92
93

        :returns: (dict) response headers
94
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
95
        req = {'shutdown': {}}
96
        r = self.servers_action_post(server_id, json_data=req, success=202)
97
        return r.headers
98

Giorgos Verigakis's avatar
Giorgos Verigakis committed
99
    def get_server_console(self, server_id):
100
101
102
        """
        :param server_id: integer (str or int)

103
        :returns: (dict) info to set a VNC connection to virtual server
104
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
105
        req = {'console': {'type': 'vnc'}}
106
        r = self.servers_action_post(server_id, json_data=req, success=200)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
107
        return r.json['console']
108
109

    def get_firewall_profile(self, server_id):
110
111
112
113
114
115
116
        """
        :param server_id: integer (str or int)

        :returns: (str) ENABLED | DISABLED | PROTECTED

        :raises ClientError: 520 No Firewall Profile
        """
117
118
        r = self.get_server_details(server_id)
        try:
119
            return r['attachments'][0]['firewallProfile']
120
        except KeyError:
121
            raise ClientError(
122
                'No Firewall Profile',
123
                details='Server %s is missing a firewall profile' % server_id)
124

Giorgos Verigakis's avatar
Giorgos Verigakis committed
125
126
    def set_firewall_profile(self, server_id, profile):
        """Set the firewall profile for the public interface of a server
127
128
129
130

        :param server_id: integer (str or int)

        :param profile: (str) ENABLED | DISABLED | PROTECTED
131
132

        :returns: (dict) response headers
Giorgos Verigakis's avatar
Giorgos Verigakis committed
133
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
134
        req = {'firewallProfile': {'profile': profile}}
135
        r = self.servers_action_post(server_id, json_data=req, success=202)
136
        return r.headers
137

138
    def list_server_nics(self, server_id):
139
140
141
142
143
        """
        :param server_id: integer (str or int)

        :returns: (dict) network interface connections
        """
144
        r = self.servers_ips_get(server_id)
145
        return r.json['attachments']
146

Giorgos Verigakis's avatar
Giorgos Verigakis committed
147
    def get_server_stats(self, server_id):
148
149
150
151
152
        """
        :param server_id: integer (str or int)

        :returns: (dict) auto-generated graphs of statistics (urls)
        """
153
        r = self.servers_stats_get(server_id)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
154
        return r.json['stats']
155

Giorgos Verigakis's avatar
Giorgos Verigakis committed
156
    def list_networks(self, detail=False):
157
158
159
160
161
        """
        :param detail: (bool)

        :returns: (list) id,name if not detail else full info per network
        """
162
163
        detail = 'detail' if detail else ''
        r = self.networks_get(command=detail)
164
        return r.json['networks']
Giorgos Verigakis's avatar
Giorgos Verigakis committed
165

166
    def list_network_nics(self, network_id):
167
168
169
170
171
        """
        :param network_id: integer (str or int)

        :returns: (list)
        """
172
        r = self.networks_get(network_id=network_id)
173
        return r.json['network']['attachments']
174

175
176
    def create_network(
            self, name,
177
            cidr=None, gateway=None, type=None, dhcp=False):
178
179
180
181
182
183
184
        """
        :param name: (str)

        :param cidr: (str)

        :param geteway: (str)

185
186
        :param type: (str) if None, will use MAC_FILTERED as default
            Valid values: CUSTOM, IP_LESS_ROUTED, MAC_FILTERED, PHYSICAL_VLAN
187

188
        :param dhcp: (bool)
189
190

        :returns: (dict) network detailed info
191
192
193
        """
        net = dict(name=name)
        if cidr:
194
            net['cidr'] = cidr
195
        if gateway:
196
            net['gateway'] = gateway
197
        net['type'] = type or 'MAC_FILTERED'
198
        net['dhcp'] = True if dhcp else False
199
        req = dict(network=net)
200
        r = self.networks_post(json_data=req, success=202)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
201
        return r.json['network']
Giorgos Verigakis's avatar
Giorgos Verigakis committed
202
203

    def get_network_details(self, network_id):
204
205
206
207
208
        """
        :param network_id: integer (str or int)

        :returns: (dict)
        """
209
        r = self.networks_get(network_id=network_id)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
210
        return r.json['network']
Giorgos Verigakis's avatar
Giorgos Verigakis committed
211
212

    def update_network_name(self, network_id, new_name):
213
214
215
216
        """
        :param network_id: integer (str or int)

        :param new_name: (str)
217
218

        :returns: (dict) response headers
219
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
220
        req = {'network': {'name': new_name}}
221
222
        r = self.networks_put(network_id=network_id, json_data=req)
        return r.headers
Giorgos Verigakis's avatar
Giorgos Verigakis committed
223
224

    def delete_network(self, network_id):
225
226
227
        """
        :param network_id: integer (str or int)

228
229
        :returns: (dict) response headers

230
231
        :raises ClientError: 421 Network in use
        """
232
        try:
233
234
            r = self.networks_delete(network_id)
            return r.headers
235
236
        except ClientError as err:
            if err.status == 421:
237
                err.details = [
238
                    'Network may be still connected to at least one server']
239
            raise
Giorgos Verigakis's avatar
Giorgos Verigakis committed
240
241

    def connect_server(self, server_id, network_id):
242
243
244
245
246
        """ Connect a server to a network

        :param server_id: integer (str or int)

        :param network_id: integer (str or int)
247
248

        :returns: (dict) response headers
249
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
250
        req = {'add': {'serverRef': server_id}}
251
252
        r = self.networks_post(network_id, 'action', json_data=req)
        return r.headers
Giorgos Verigakis's avatar
Giorgos Verigakis committed
253

254
    def disconnect_server(self, server_id, nic_id):
255
256
257
258
        """
        :param server_id: integer (str or int)

        :param nic_id: (str)
259
260

        :returns: (int) the number of nics disconnected
261
        """
262
        vm_nets = self.list_server_nics(server_id)
263
        num_of_disconnections = 0
264
        for (nic_id, network_id) in [(
265
266
                net['id'],
                net['network_id']) for net in vm_nets if nic_id == net['id']]:
267
            req = {'remove': {'attachment': '%s' % nic_id}}
268
            self.networks_post(network_id, 'action', json_data=req)
269
270
            num_of_disconnections += 1
        return num_of_disconnections
271
272

    def disconnect_network_nics(self, netid):
273
274
275
276
        """
        :param netid: integer (str or int)
        """
        for nic in self.list_network_nics(netid):
277
            req = dict(remove=dict(attachment=nic))
278
            self.networks_post(netid, 'action', json_data=req)
279

280
281
282
283
    def _wait(
            self, item_id, current_status, get_status,
            delay=1, max_wait=100, wait_cb=None):
        """Wait for item while its status is current_status
284
285
286

        :param server_id: integer (str or int)

287
288
289
290
        :param current_status: (str)

        :param get_status: (method(self, item_id)) if called, returns
            (status, progress %) If no way to tell progress, return None
291
292
293

        :param delay: time interval between retries

294
        :param wait_cb: if set a progress bar is used to show progress
295

296
        :returns: (str) the new mode if successful, (bool) False if timed out
297
        """
298
        status, progress = get_status(self, item_id)
299

300
        if wait_cb:
301
            wait_gen = wait_cb(max_wait // delay)
302
            wait_gen.next()
303

304
305
306
307
308
309
310
311
312
        if status != current_status:
            if wait_cb:
                try:
                    wait_gen.next()
                except Exception:
                    pass
            return status
        old_wait = total_wait = 0

313
314
315
316
        while status == current_status and total_wait <= max_wait:
            if wait_cb:
                try:
                    for i in range(total_wait - old_wait):
317
                        wait_gen.next()
318
319
320
                except Exception:
                    break
            old_wait = total_wait
321
            total_wait = progress or total_wait + 1
322
            sleep(delay)
323
            status, progress = get_status(self, item_id)
324

325
        if total_wait < max_wait:
326
327
            if wait_cb:
                try:
328
                    for i in range(max_wait):
329
330
331
                        wait_gen.next()
                except:
                    pass
332
333
334
335
336
337
338
339
340
341
342
343
344
345
        return status if status != current_status else False

    def wait_server(
            self, server_id,
            current_status='BUILD',
            delay=1, max_wait=100, wait_cb=None):
        """Wait for server while its status is current_status

        :param server_id: integer (str or int)

        :param current_status: (str) BUILD|ACTIVE|STOPPED|DELETED|REBOOT

        :param delay: time interval between retries

346
347
        :max_wait: (int) timeout in secconds

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
        :param wait_cb: if set a progressbar is used to show progress

        :returns: (str) the new mode if succesfull, (bool) False if timed out
        """

        def get_status(self, server_id):
            r = self.get_server_details(server_id)
            return r['status'], (r.get('progress', None) if (
                            current_status in ('BUILD', )) else None)

        return self._wait(
            server_id, current_status, get_status, delay, max_wait, wait_cb)

    def wait_network(
            self, net_id,
363
            current_status='PENDING', delay=1, max_wait=100, wait_cb=None):
364
365
366
367
368
369
370
371
        """Wait for network while its status is current_status

        :param net_id: integer (str or int)

        :param current_status: (str) PENDING | ACTIVE | DELETED

        :param delay: time interval between retries

372
373
        :max_wait: (int) timeout in secconds

374
375
376
377
378
379
380
381
382
383
384
        :param wait_cb: if set a progressbar is used to show progress

        :returns: (str) the new mode if succesfull, (bool) False if timed out
        """

        def get_status(self, net_id):
            r = self.get_network_details(net_id)
            return r['status'], None

        return self._wait(
            net_id, current_status, get_status, delay, max_wait, wait_cb)
385

386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
    def wait_firewall(
            self, server_id,
            current_status='DISABLED', delay=1, max_wait=100, wait_cb=None):
        """Wait while the public network firewall status is current_status

        :param server_id: integer (str or int)

        :param current_status: (str) DISABLED | ENABLED | PROTECTED

        :param delay: time interval between retries

        :max_wait: (int) timeout in secconds

        :param wait_cb: if set a progressbar is used to show progress

        :returns: (str) the new mode if succesfull, (bool) False if timed out
        """

        def get_status(self, server_id):
            return self.get_firewall_profile(server_id), None

        return self._wait(
            server_id, current_status, get_status, delay, max_wait, wait_cb)

410
411
412
413
414
415
416
417
418
    def get_floating_ip_pools(self):
        """
        :returns: (dict) {floating_ip_pools:[{name: ...}, ...]}
        """
        r = self.floating_ip_pools_get()
        return r.json

    def get_floating_ips(self):
        """
Stavros Sachtouris's avatar
Stavros Sachtouris committed
419
        :returns: (dict) {floating_ips: [fixed_ip: , id: , ip: , pool: ]}
420
421
422
423
424
425
426
427
428
429
430
        """
        r = self.floating_ips_get()
        return r.json

    def alloc_floating_ip(self, pool=None, address=None):
        """
        :param pool: (str) pool of ips to allocate from

        :param address: (str) ip address to request

        :returns: (dict) {
431
            fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...}
432
433
434
435
        """
        json_data = dict()
        if pool:
            json_data['pool'] = pool
436
437
        if address:
            json_data['address'] = address
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
        r = self.floating_ips_post(json_data)
        return r.json['floating_ip']

    def get_floating_ip(self, fip_id):
        """
        :param fip_id: (str) floating ip id

        :returns: (dict)
            {fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},

        :raises AssertionError: if fip_id is emtpy
        """
        assert fip_id, 'floating ip id is needed for get_floating_ip'
        r = self.floating_ips_get(fip_id)
        return r.json['floating_ip']

    def delete_floating_ip(self, fip_id=None):
        """
        :param fip_id: (str) floating ip id (if None, all ips are deleted)

        :returns: (dict) request headers

        :raises AssertionError: if fip_id is emtpy
        """
        assert fip_id, 'floating ip id is needed for delete_floating_ip'
        r = self.floating_ips_delete(fip_id)
        return r.headers

466
    def attach_floating_ip(self, server_id, address):
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
        """Associate the address ip to server with server_id

        :param server_id: (int)

        :param address: (str) the ip address to assign to server (vm)

        :returns: (dict) request headers

        :raises ValueError: if server_id cannot be converted to int

        :raises ValueError: if server_id is not of a int-convertable type

        :raises AssertionError: if address is emtpy
        """
        server_id = int(server_id)
482
        assert address, 'address is needed for attach_floating_ip'
483
484
        req = dict(addFloatingIp=dict(address=address))
        r = self.servers_action_post(server_id, json_data=req)
485
486
        return r.headers

487
    def detach_floating_ip(self, server_id, address):
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
        """Disassociate an address ip from the server with server_id

        :param server_id: (int)

        :param address: (str) the ip address to assign to server (vm)

        :returns: (dict) request headers

        :raises ValueError: if server_id cannot be converted to int

        :raises ValueError: if server_id is not of a int-convertable type

        :raises AssertionError: if address is emtpy
        """
        server_id = int(server_id)
503
        assert address, 'address is needed for detach_floating_ip'
504
505
        req = dict(removeFloatingIp=dict(address=address))
        r = self.servers_action_post(server_id, json_data=req)
506
        return r.headers
507
508
509
510
511
512
513
514


class CycladesNetworkClient(NetworkClient):
    """Cyclades Network API extentions"""

    network_types = (
        'CUSTOM', 'MAC_FILTERED', 'IP_LESS_ROUTED', 'PHYSICAL_VLAN')

515
516
517
518
519
    def list_networks(self, detail=None):
        path = path4url('networks', 'detail' if detail else '')
        r = self.get(path, success=200)
        return r.json['networks']

520
521
522
523
524
525
526
527
    def create_network(self, type, name=None, shared=None):
        req = dict(network=dict(type=type, admin_state_up=True))
        if name:
            req['network']['name'] = name
        if shared not in (None, ):
            req['network']['shared'] = bool(shared)
        r = self.networks_post(json_data=req, success=201)
        return r.json['network']