Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / network.py @ 4e25b350

History | View | Annotate | Download (23.4 kB)

1
# Copyright 2011-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 io import StringIO
35
from pydoc import pager
36

    
37
from kamaki.cli import command
38
from kamaki.cli.command_tree import CommandTree
39
from kamaki.cli.errors import (
40
    CLIBaseUrlError, CLIInvalidArgument, raiseCLIError)
41
from kamaki.clients.cyclades import CycladesNetworkClient, ClientError
42
from kamaki.cli.argument import (
43
    FlagArgument, ValueArgument, RepeatableArgument, IntArgument)
44
from kamaki.cli.commands import _command_init, errors, addLogSettings
45
from kamaki.cli.commands import (
46
    _optional_output_cmd, _optional_json, _name_filter, _id_filter)
47
from kamaki.cli.commands.cyclades import _service_wait
48

    
49

    
50
network_cmds = CommandTree('network', 'Networking API network commands')
51
port_cmds = CommandTree('port', 'Networking API network commands')
52
subnet_cmds = CommandTree('subnet', 'Networking API network commands')
53
ip_cmds = CommandTree('ip', 'Networking API floatingip commands')
54
_commands = [network_cmds, port_cmds, subnet_cmds, ip_cmds]
55

    
56

    
57
about_authentication = '\nUser Authentication:\
58
    \n  to check authentication: [kamaki] ]user authenticate\
59
    \n  to set authentication token: \
60
    [kamaki] config set cloud.<CLOUD>.token <TOKEN>'
61

    
62

    
63
class _port_wait(_service_wait):
64

    
65
    def _wait(self, port_id, current_status, timeout=60):
66
        super(_port_wait, self)._wait(
67
            'Port', port_id, self.client.wait_port, current_status,
68
            timeout=timeout)
69

    
70

    
71
class _init_network(_command_init):
72
    @errors.generic.all
73
    @addLogSettings
74
    def _run(self, service='network'):
75
        if getattr(self, 'cloud', None):
76
            base_url = self._custom_url(service) or self._custom_url(
77
                'network')
78
            if base_url:
79
                token = self._custom_token(service) or self._custom_token(
80
                    'network') or self.config.get_cloud('token')
81
                self.client = CycladesNetworkClient(
82
                  base_url=base_url, token=token)
83
                return
84
        else:
85
            self.cloud = 'default'
86
        if getattr(self, 'auth_base', False):
87
            network_endpoints = self.auth_base.get_service_endpoints(
88
                self._custom_type('network') or 'network',
89
                self._custom_version('network') or '')
90
            base_url = network_endpoints['publicURL']
91
            token = self.auth_base.token
92
            self.client = CycladesNetworkClient(base_url=base_url, token=token)
93
        else:
94
            raise CLIBaseUrlError(service='network')
95

    
96
    def _filter_by_user_id(self, nets):
97
        return [net for net in nets if net['user_id'] == self['user_id']] if (
98
            self['user_id']) else nets
99

    
100
    def main(self):
101
        self._run()
102

    
103

    
104
@command(network_cmds)
105
class network_list(_init_network, _optional_json, _name_filter, _id_filter):
106
    """List networks
107
    Use filtering arguments (e.g., --name-like) to manage long server lists
108
    """
109

    
110
    arguments = dict(
111
        detail=FlagArgument('show detailed output', ('-l', '--details')),
112
        more=FlagArgument(
113
            'output results in pages (-n to set items per page, default 10)',
114
            '--more'),
115
        user_id=ValueArgument(
116
            'show only networks belonging to user with this id', '--user-id')
117
    )
118

    
119
    @errors.generic.all
120
    @errors.cyclades.connection
121
    def _run(self):
122
        detail = bool(self['detail'] or self['user_id'])
123
        nets = self.client.list_networks(detail=detail)
124
        nets = self._filter_by_user_id(nets)
125
        nets = self._filter_by_name(nets)
126
        nets = self._filter_by_id(nets)
127
        if detail and not self['detail']:
128
            nets = [dict(
129
                id=n['id'], name=n['name'], links=n['links']) for n in nets]
130
        kwargs = dict()
131
        if self['more']:
132
            kwargs['out'] = StringIO()
133
            kwargs['title'] = ()
134
        self._print(nets, **kwargs)
135
        if self['more']:
136
            pager(kwargs['out'].getvalue())
137

    
138
    def main(self):
139
        super(self.__class__, self)._run()
140
        self._run()
141

    
142

    
143
@command(network_cmds)
144
class network_info(_init_network, _optional_json):
145
    """Get details about a network"""
146

    
147
    @errors.generic.all
148
    @errors.cyclades.connection
149
    @errors.cyclades.network_id
150
    def _run(self, network_id):
151
        net = self.client.get_network_details(network_id)
152
        self._print(net, self.print_dict)
153

    
154
    def main(self, network_id):
155
        super(self.__class__, self)._run()
156
        self._run(network_id=network_id)
157

    
158

    
159
class NetworkTypeArgument(ValueArgument):
160

    
161
    types = ('MAC_FILTERED', 'CUSTOM', 'IP_LESS_ROUTED', 'PHYSICAL_VLAN')
162

    
163
    @property
164
    def value(self):
165
        return getattr(self, '_value', self.types[0])
166

    
167
    @value.setter
168
    def value(self, new_value):
169
        if new_value and new_value.upper() in self.types:
170
            self._value = new_value.upper()
171
        elif new_value:
172
            raise CLIInvalidArgument(
173
                'Invalid network type %s' % new_value, details=[
174
                    'Valid types: %s' % ', '.join(self.types), ])
175

    
176

    
177
@command(network_cmds)
178
class network_create(_init_network, _optional_json):
179
    """Create a new network (default type: MAC_FILTERED)"""
180

    
181
    arguments = dict(
182
        name=ValueArgument('Network name', '--name'),
183
        shared=FlagArgument(
184
            'Make network shared (special privileges required)', '--shared'),
185
        network_type=NetworkTypeArgument(
186
            'Valid network types: %s' % (', '.join(NetworkTypeArgument.types)),
187
            '--type')
188
    )
189

    
190
    @errors.generic.all
191
    @errors.cyclades.connection
192
    @errors.cyclades.network_type
193
    def _run(self, network_type):
194
        net = self.client.create_network(
195
            network_type, name=self['name'], shared=self['shared'])
196
        self._print(net, self.print_dict)
197

    
198
    def main(self):
199
        super(self.__class__, self)._run()
200
        self._run(network_type=self['network_type'])
201

    
202

    
203
@command(network_cmds)
204
class network_delete(_init_network, _optional_output_cmd):
205
    """Delete a network"""
206

    
207
    @errors.generic.all
208
    @errors.cyclades.connection
209
    @errors.cyclades.network_id
210
    def _run(self, network_id):
211
        r = self.client.delete_network(network_id)
212
        self._optional_output(r)
213

    
214
    def main(self, network_id):
215
        super(self.__class__, self)._run()
216
        self._run(network_id=network_id)
217

    
218

    
219
@command(network_cmds)
220
class network_modify(_init_network, _optional_json):
221
    """Modify network attributes"""
222

    
223
    arguments = dict(new_name=ValueArgument('Rename the network', '--name'))
224
    required = ['new_name', ]
225

    
226
    @errors.generic.all
227
    @errors.cyclades.connection
228
    @errors.cyclades.network_id
229
    def _run(self, network_id):
230
        r = self.client.update_network(network_id, name=self['new_name'])
231
        self._print(r, self.print_dict)
232

    
233
    def main(self, network_id):
234
        super(self.__class__, self)._run()
235
        self._run(network_id=network_id)
236

    
237

    
238
@command(subnet_cmds)
239
class subnet_list(_init_network, _optional_json, _name_filter, _id_filter):
240
    """List subnets
241
    Use filtering arguments (e.g., --name-like) to manage long server lists
242
    """
243

    
244
    arguments = dict(
245
        detail=FlagArgument('show detailed output', ('-l', '--details')),
246
        more=FlagArgument(
247
            'output results in pages (-n to set items per page, default 10)',
248
            '--more')
249
    )
250

    
251
    @errors.generic.all
252
    @errors.cyclades.connection
253
    def _run(self):
254
        nets = self.client.list_subnets()
255
        nets = self._filter_by_name(nets)
256
        nets = self._filter_by_id(nets)
257
        if not self['detail']:
258
            nets = [dict(
259
                id=n['id'], name=n['name'], links=n['links']) for n in nets]
260
        kwargs = dict()
261
        if self['more']:
262
            kwargs['out'] = StringIO()
263
            kwargs['title'] = ()
264
        self._print(nets, **kwargs)
265
        if self['more']:
266
            pager(kwargs['out'].getvalue())
267

    
268
    def main(self):
269
        super(self.__class__, self)._run()
270
        self._run()
271

    
272

    
273
@command(subnet_cmds)
274
class subnet_info(_init_network, _optional_json):
275
    """Get details about a subnet"""
276

    
277
    @errors.generic.all
278
    @errors.cyclades.connection
279
    def _run(self, subnet_id):
280
        net = self.client.get_subnet_details(subnet_id)
281
        self._print(net, self.print_dict)
282

    
283
    def main(self, subnet_id):
284
        super(self.__class__, self)._run()
285
        self._run(subnet_id=subnet_id)
286

    
287

    
288
class AllocationPoolArgument(RepeatableArgument):
289

    
290
    @property
291
    def value(self):
292
        return super(AllocationPoolArgument, self).value or []
293

    
294
    @value.setter
295
    def value(self, new_pools):
296
        if not new_pools:
297
            return
298
        new_list = []
299
        for pool in new_pools:
300
            start, comma, end = pool.partition(',')
301
            if not (start and comma and end):
302
                raise CLIInvalidArgument(
303
                    'Invalid allocation pool argument %s' % pool, details=[
304
                    'Allocation values must be of the form:',
305
                    '  <start address>,<end address>'])
306
            new_list.append(dict(start=start, end=end))
307
        self._value = new_list
308

    
309

    
310
@command(subnet_cmds)
311
class subnet_create(_init_network, _optional_json):
312
    """Create a new subnet"""
313

    
314
    arguments = dict(
315
        name=ValueArgument('Subnet name', '--name'),
316
        allocation_pools=AllocationPoolArgument(
317
            'start_address,end_address of allocation pool (can be repeated)'
318
            ' e.g., --alloc-pool=123.45.67.1,123.45.67.8',
319
            '--alloc-pool'),
320
        gateway=ValueArgument('Gateway IP', '--gateway'),
321
        subnet_id=ValueArgument('The id for the subnet', '--id'),
322
        ipv6=FlagArgument('If set, IP version is set to 6, else 4', '--ipv6'),
323
        enable_dhcp=FlagArgument('Enable dhcp (default: off)', '--with-dhcp'),
324
        network_id=ValueArgument('Set the network ID', '--network-id'),
325
        cidr=ValueArgument('Set the CIDR', '--cidr')
326
    )
327
    required = ('network_id', 'cidr')
328

    
329
    @errors.generic.all
330
    @errors.cyclades.connection
331
    @errors.cyclades.network_id
332
    def _run(self, network_id, cidr):
333
        net = self.client.create_subnet(
334
            network_id, cidr,
335
            self['name'], self['allocation_pools'], self['gateway'],
336
            self['subnet_id'], self['ipv6'], self['enable_dhcp'])
337
        self._print(net, self.print_dict)
338

    
339
    def main(self):
340
        super(self.__class__, self)._run()
341
        self._run(network_id=self['network_id'], cidr=self['cidr'])
342

    
343

    
344
# @command(subnet_cmds)
345
# class subnet_delete(_init_network, _optional_output_cmd):
346
#     """Delete a subnet"""
347

    
348
#     @errors.generic.all
349
#     @errors.cyclades.connection
350
#     def _run(self, subnet_id):
351
#         r = self.client.delete_subnet(subnet_id)
352
#         self._optional_output(r)
353

    
354
#     def main(self, subnet_id):
355
#         super(self.__class__, self)._run()
356
#         self._run(subnet_id=subnet_id)
357

    
358

    
359
@command(subnet_cmds)
360
class subnet_modify(_init_network, _optional_json):
361
    """Modify the attributes of a subnet"""
362

    
363
    arguments = dict(
364
        new_name=ValueArgument('New name of the subnet', '--name')
365
    )
366
    required = ['new_name']
367

    
368
    @errors.generic.all
369
    @errors.cyclades.connection
370
    def _run(self, subnet_id):
371
        r = self.client.get_subnet_details(subnet_id)
372
        r = self.client.update_subnet(
373
            subnet_id, r['network_id'], name=self['new_name'])
374
        self._print(r, self.print_dict)
375

    
376
    def main(self, subnet_id):
377
        super(self.__class__, self)._run()
378
        self._run(subnet_id=subnet_id)
379

    
380

    
381
@command(port_cmds)
382
class port_list(_init_network, _optional_json, _name_filter, _id_filter):
383
    """List all ports"""
384

    
385
    arguments = dict(
386
        detail=FlagArgument('show detailed output', ('-l', '--details')),
387
        more=FlagArgument(
388
            'output results in pages (-n to set items per page, default 10)',
389
            '--more'),
390
        user_id=ValueArgument(
391
            'show only networks belonging to user with this id', '--user-id')
392
    )
393

    
394
    @errors.generic.all
395
    @errors.cyclades.connection
396
    def _run(self):
397
        detail = bool(self['detail'] or self['user_id'])
398
        ports = self.client.list_ports(detail=detail)
399
        ports = self._filter_by_user_id(ports)
400
        ports = self._filter_by_name(ports)
401
        ports = self._filter_by_id(ports)
402
        if detail and not self['detail']:
403
            ports = [dict(
404
                id=p['id'], name=p['name'], links=p['links']) for p in ports]
405
        kwargs = dict()
406
        if self['more']:
407
            kwargs['out'] = StringIO()
408
            kwargs['title'] = ()
409
        self._print(ports, **kwargs)
410
        if self['more']:
411
            pager(kwargs['out'].getvalue())
412

    
413
    def main(self):
414
        super(self.__class__, self)._run()
415
        self._run()
416

    
417

    
418
@command(port_cmds)
419
class port_info(_init_network, _optional_json):
420
    """Get details about a port"""
421

    
422
    @errors.generic.all
423
    @errors.cyclades.connection
424
    def _run(self, port_id):
425
        port = self.client.get_port_details(port_id)
426
        self._print(port, self.print_dict)
427

    
428
    def main(self, port_id):
429
        super(self.__class__, self)._run()
430
        self._run(port_id=port_id)
431

    
432

    
433
@command(port_cmds)
434
class port_delete(_init_network, _optional_output_cmd, _port_wait):
435
    """Delete a port (== disconnect server from network)"""
436

    
437
    arguments = dict(
438
        wait=FlagArgument('Wait port to be established', ('-w', '--wait'))
439
    )
440

    
441
    @errors.generic.all
442
    @errors.cyclades.connection
443
    def _run(self, port_id):
444
        if self['wait']:
445
            status = self.client.get_port_details(port_id)['status']
446
        r = self.client.delete_port(port_id)
447
        if self['wait']:
448
            try:
449
                self._wait(port_id, status)
450
            except ClientError as ce:
451
                if ce.status not in (404, ):
452
                    raise
453
                self.error('Port %s is deleted' % port_id)
454
        self._optional_output(r)
455

    
456
    def main(self, port_id):
457
        super(self.__class__, self)._run()
458
        self._run(port_id=port_id)
459

    
460

    
461
@command(port_cmds)
462
class port_modify(_init_network, _optional_json):
463
    """Modify the attributes of a port"""
464

    
465
    arguments = dict(new_name=ValueArgument('New name of the port', '--name'))
466
    required = ['new_name', ]
467

    
468
    @errors.generic.all
469
    @errors.cyclades.connection
470
    def _run(self, port_id):
471
        r = self.client.get_port_details(port_id)
472
        r = self.client.update_port(
473
            port_id, r['network_id'], name=self['new_name'])
474
        self._print(r, self.print_dict)
475

    
476
    def main(self, port_id):
477
        super(self.__class__, self)._run()
478
        self._run(port_id=port_id)
479

    
480

    
481
class _port_create(_init_network, _optional_json, _port_wait):
482

    
483
    def connect(self, network_id, device_id):
484
        fixed_ips = [dict(
485
            subnet_id=self['subnet_id'], ip_address=self['ip_address'])] if (
486
                self['subnet_id']) else None
487
        r = self.client.create_port(
488
            network_id, device_id,
489
            name=self['name'],
490
            security_groups=self['security_group_id'],
491
            fixed_ips=fixed_ips)
492
        if self['wait']:
493
            self._wait(r['id'], r['status'])
494
            r = self.client.get_port_details(r['id'])
495
        self._print([r])
496

    
497

    
498
@command(port_cmds)
499
class port_create(_port_create):
500
    """Create a new port (== connect server to network)"""
501

    
502
    arguments = dict(
503
        name=ValueArgument('A human readable name', '--name'),
504
        security_group_id=RepeatableArgument(
505
            'Add a security group id (can be repeated)',
506
            ('-g', '--security-group')),
507
        subnet_id=ValueArgument(
508
            'Subnet id for fixed ips (used with --ip-address)',
509
            '--subnet-id'),
510
        ip_address=ValueArgument(
511
            'IP address for subnet id (used with --subnet-id', '--ip-address'),
512
        network_id=ValueArgument('Set the network ID', '--network-id'),
513
        device_id=ValueArgument(
514
            'The device is either a virtual server or a virtual router',
515
            '--device-id'),
516
        wait=FlagArgument('Wait port to be established', ('-w', '--wait')),
517
    )
518
    required = ('network_id', 'device_id')
519

    
520
    @errors.generic.all
521
    @errors.cyclades.connection
522
    @errors.cyclades.network_id
523
    @errors.cyclades.server_id
524
    def _run(self, network_id, server_id):
525
        self.connect(network_id, server_id)
526

    
527
    def main(self):
528
        super(self.__class__, self)._run()
529
        self._run(network_id=self['network_id'], server_id=self['device_id'])
530

    
531

    
532
@command(port_cmds)
533
class port_wait(_init_network, _port_wait):
534
    """Wait for port to finish [ACTIVE, DOWN, BUILD, ERROR]"""
535

    
536
    arguments = dict(
537
        timeout=IntArgument(
538
            'Wait limit in seconds (default: 60)', '--timeout', default=60)
539
    )
540

    
541
    @errors.generic.all
542
    @errors.cyclades.connection
543
    def _run(self, port_id, current_status):
544
        port = self.client.get_port_details(port_id)
545
        if port['status'].lower() == current_status.lower():
546
            self._wait(port_id, current_status, timeout=self['timeout'])
547
        else:
548
            self.error(
549
                'Port %s: Cannot wait for status %s, '
550
                'status is already %s' % (
551
                    port_id, current_status, port['status']))
552

    
553
    def main(self, port_id, current_status='BUILD'):
554
        super(self.__class__, self)._run()
555
        self._run(port_id=port_id, current_status=current_status)
556

    
557

    
558
@command(ip_cmds)
559
class ip_list(_init_network, _optional_json):
560
    """List reserved floating IPs"""
561

    
562
    @errors.generic.all
563
    @errors.cyclades.connection
564
    def _run(self):
565
        self._print(self.client.list_floatingips())
566

    
567
    def main(self):
568
        super(self.__class__, self)._run()
569
        self._run()
570

    
571

    
572
@command(ip_cmds)
573
class ip_info(_init_network, _optional_json):
574
    """Get details on a floating IP"""
575

    
576
    @errors.generic.all
577
    @errors.cyclades.connection
578
    def _run(self, ip_id):
579
        self._print(
580
            self.client.get_floatingip_details(ip_id), self.print_dict)
581

    
582
    def main(self, ip_id):
583
        super(self.__class__, self)._run()
584
        self._run(ip_id=ip_id)
585

    
586

    
587
@command(ip_cmds)
588
class ip_create(_init_network, _optional_json):
589
    """Reserve an IP on a network"""
590

    
591
    arguments = dict(
592
        network_id=ValueArgument(
593
            'The network to preserve the IP on', '--network-id'),
594
        ip_address=ValueArgument('Allocate a specific IP address', '--address')
595
    )
596
    required = ('network_id', )
597

    
598
    @errors.generic.all
599
    @errors.cyclades.connection
600
    @errors.cyclades.network_id
601
    def _run(self, network_id):
602
        self._print(
603
            self.client.create_floatingip(
604
                network_id, floating_ip_address=self['ip_address']),
605
            self.print_dict)
606

    
607
    def main(self):
608
        super(self.__class__, self)._run()
609
        self._run(network_id=self['network_id'])
610

    
611

    
612
@command(ip_cmds)
613
class ip_delete(_init_network, _optional_output_cmd):
614
    """Unreserve an IP (also delete the port, if attached)"""
615

    
616
    def _run(self, ip_id):
617
        self._optional_output(self.client.delete_floatingip(ip_id))
618

    
619
    def main(self, ip_id):
620
        super(self.__class__, self)._run()
621
        self._run(ip_id=ip_id)
622

    
623

    
624
#  Warn users for some importand changes
625

    
626
@command(network_cmds)
627
class network_connect(_port_create):
628
    """Connect a network with a device (server or router)"""
629

    
630
    arguments = dict(
631
        name=ValueArgument('A human readable name for the port', '--name'),
632
        security_group_id=RepeatableArgument(
633
            'Add a security group id (can be repeated)',
634
            ('-g', '--security-group')),
635
        subnet_id=ValueArgument(
636
            'Subnet id for fixed ips (used with --ip-address)',
637
            '--subnet-id'),
638
        ip_address=ValueArgument(
639
            'IP address for subnet id (used with --subnet-id', '--ip-address'),
640
        wait=FlagArgument('Wait network to connect', ('-w', '--wait')),
641
        device_id=RepeatableArgument(
642
            'Connect this device to the network (can be repeated)',
643
            '--device-id')
644
    )
645
    required = ('device_id', )
646

    
647
    @errors.generic.all
648
    @errors.cyclades.connection
649
    @errors.cyclades.network_id
650
    @errors.cyclades.server_id
651
    def _run(self, network_id, server_id):
652
        self.error('Creating a port to connect network %s with device %s' % (
653
            network_id, server_id))
654
        self.connect(network_id, server_id)
655

    
656
    def main(self, network_id):
657
        super(self.__class__, self)._run()
658
        for sid in self['device_id']:
659
            self._run(network_id=network_id, server_id=sid)
660

    
661

    
662
@command(network_cmds)
663
class network_disconnect(_init_network, _port_wait, _optional_json):
664
    """Disconnnect a network from a device"""
665

    
666
    def _cyclades_client(self):
667
        auth = getattr(self, 'auth_base')
668
        endpoints = auth.get_service_endpoints('compute')
669
        URL = endpoints['publicURL']
670
        from kamaki.clients.cyclades import CycladesClient
671
        return CycladesClient(URL, self.client.token)
672

    
673
    arguments = dict(
674
        wait=FlagArgument('Wait network to disconnect', ('-w', '--wait')),
675
        device_id=RepeatableArgument(
676
            'Disconnect device from the network (can be repeated)',
677
            '--device-id')
678
    )
679
    required = ('device_id', )
680

    
681
    @errors.generic.all
682
    @errors.cyclades.connection
683
    @errors.cyclades.network_id
684
    @errors.cyclades.server_id
685
    def _run(self, network_id, server_id):
686
        vm = self._cyclades_client().get_server_details(server_id)
687
        ports = [port for port in vm['attachments'] if (
688
            port['network_id'] in (network_id, ))]
689
        if not ports:
690
            raiseCLIError('Network %s is not connected to device %s' % (
691
                network_id, server_id))
692
        for port in ports:
693
            if self['wait']:
694
                port['status'] = self.client.get_port_details(port['id'])[
695
                    'status']
696
            self.client.delete_port(port['id'])
697
            self.error('Deleting port %s:' % port['id'])
698
            self.print_dict(port)
699
            if self['wait']:
700
                try:
701
                    self._wait(port['id'], port['status'])
702
                except ClientError as ce:
703
                    if ce.status not in (404, ):
704
                        raise
705
                    self.error('Port %s is deleted' % port['id'])
706

    
707
    def main(self, network_id):
708
        super(self.__class__, self)._run()
709
        for sid in self['device_id']:
710
            self._run(network_id=network_id, server_id=sid)