Revision fd981f77 kamaki/cli/commands/network.py

b/kamaki/cli/commands/network.py
37 37
from kamaki.cli import command
38 38
from kamaki.cli.command_tree import CommandTree
39 39
from kamaki.cli.errors import (
40
    CLISyntaxError, CLIBaseUrlError, CLIInvalidArgument)
40
    CLIBaseUrlError, CLIInvalidArgument, raiseCLIError)
41 41
from kamaki.clients.cyclades import CycladesNetworkClient
42 42
from kamaki.cli.argument import (
43 43
    FlagArgument, ValueArgument, RepeatableArgument, IntArgument)
......
447 447
        self._run(port_id=port_id)
448 448

  
449 449

  
450
class _port_create(_init_network):
451

  
452
    @errors.generic.all
453
    @errors.cyclades.connection
454
    @errors.cyclades.network_id
455
    def _run(self, network_id, device_id):
456
        fixed_ips = [dict(
457
            subnet_id=self['subnet_id'], ip_address=self['ip_address'])] if (
458
                self['subnet_id']) else None
459
        r = self.client.create_port(
460
            network_id, device_id,
461
            name=self['name'],
462
            security_groups=self['security_group_id'],
463
            fixed_ips=fixed_ips)
464
        if self['wait']:
465
            self._wait(r['id'], r['status'])
466
        self._print(r, self.print_dict)
467

  
468

  
450 469
@command(port_cmds)
451 470
class port_create(_init_network, _optional_json, _port_wait):
452 471
    """Create a new port (== connect server to network)"""
......
469 488
    )
470 489
    required = ('network_id', 'device_id')
471 490

  
472
    @errors.generic.all
473
    @errors.cyclades.connection
474
    @errors.cyclades.network_id
475
    def _run(self, network_id, device_id):
476
        fixed_ips = [dict(
477
            subnet_id=self['subnet_id'], ip_address=self['ip_address'])] if (
478
                self['subnet_id']) else None
479
        r = self.client.create_port(
480
            network_id, device_id,
481
            name=self['name'],
482
            security_groups=self['security_group_id'],
483
            fixed_ips=fixed_ips)
484
        if self['wait']:
485
            self._wait(r['id'], r['status'])
486
        self._print(r, self.print_dict)
487

  
488 491
    def main(self):
489 492
        super(self.__class__, self)._run()
490 493
        self._run(network_id=self['network_id'], device_id=self['device_id'])
......
585 588
#  Warn users for some importand changes
586 589

  
587 590
@command(network_cmds)
588
class network_connect(_init_network, _optional_output_cmd):
589
    """DEPRECATED, use /port create"""
591
class network_connect(_port_create):
592
    """Connect a network with a device (server or router)"""
590 593

  
591
    def main(self):
592
        raise CLISyntaxError('DEPRECATED, replaced by kamaki port create')
594
    arguments = dict(
595
        name=ValueArgument('A human readable name', '--name'),
596
        security_group_id=RepeatableArgument(
597
            'Add a security group id (can be repeated)',
598
            ('-g', '--security-group')),
599
        subnet_id=ValueArgument(
600
            'Subnet id for fixed ips (used with --ip-address)',
601
            '--subnet-id'),
602
        ip_address=ValueArgument(
603
            'IP address for subnet id (used with --subnet-id', '--ip-address'),
604
        wait=FlagArgument('Wait port to be established', ('-w', '--wait')),
605
    )
606

  
607
    def main(self, network_id, device_id):
608
        super(self.__class__, self)._run()
609
        self._run(network_id=network_id, device_id=device_id)
593 610

  
594 611

  
595 612
@command(network_cmds)
596
class network_disconnect(_init_network):
597
    """DEPRECATED, /use port delete"""
613
class network_disconnect(_init_network, _optional_json):
614
    """Disconnnect a network from a device"""
598 615

  
599
    def main(self):
600
        raise CLISyntaxError('DEPRECATED, replaced by kamaki port delete')
616
    def _cyclades_client(self):
617
        auth = getattr(self, 'auth_base')
618
        endpoints = auth.get_service_endpoints('compute')
619
        URL = endpoints['publicURL']
620
        from kamaki.clients.cyclades import CycladesClient
621
        return CycladesClient(URL, self.client.token)
622

  
623
    @errors.generic.all
624
    @errors.cyclades.connection
625
    @errors.cyclades.network_id
626
    @errors.cyclades.server_id
627
    def _run(self, network_id, device_id):
628
        vm = self._cyclades_client().get_server_details(device_id)
629
        nets = [net for net in vm['attachments'] if net['network_id'] not in (
630
            'network_id', )]
631
        if not nets:
632
            raiseCLIError('Network %s is not connected to device %s' % (
633
                network_id, device_id))
634
        for net in nets:
635
            self.client.port_delete(net['id'])
636
            self.error('Deleted connection:')
637
            self.print_dict(net)
638

  
639
    def main(self, network_id, device_id):
640
        super(self.__class__, self)._run()
641
        self._run(network_id=network_id, device_id=device_id)

Also available in: Unified diff