Revision c788a761 kamaki/cli/commands/cyclades.py

b/kamaki/cli/commands/cyclades.py
39 39
from kamaki.cli import command
40 40
from kamaki.cli.command_tree import CommandTree
41 41
from kamaki.cli.utils import remove_from_items, filter_dicts_by_dict
42
from kamaki.cli.errors import raiseCLIError, CLISyntaxError, CLIBaseUrlError
42
from kamaki.cli.errors import (
43
    raiseCLIError, CLISyntaxError, CLIBaseUrlError, CLIInvalidArgument)
43 44
from kamaki.clients.cyclades import CycladesClient, ClientError
44 45
from kamaki.cli.argument import FlagArgument, ValueArgument, KeyValueArgument
45 46
from kamaki.cli.argument import ProgressBarArgument, DateArgument, IntArgument
......
116 117
            timeout=timeout)
117 118

  
118 119

  
120
class _firewall_wait(_service_wait):
121

  
122
    def _wait(self, server_id, current_status, timeout=60):
123
        super(_firewall_wait, self)._wait(
124
            'Firewall of server',
125
            server_id, self.client.wait_firewall, current_status,
126
            timeout=timeout)
127

  
128

  
119 129
class _init_cyclades(_command_init):
120 130
    @errors.generic.all
121 131
    @addLogSettings
......
552 562

  
553 563

  
554 564
@command(server_cmds)
555
class server_firewall_set(_init_cyclades, _optional_output_cmd):
565
class server_firewall_set(
566
        _init_cyclades, _optional_output_cmd, _firewall_wait):
556 567
    """Set the firewall profile on virtual server public network
557 568
    Values for profile:
558 569
    - DISABLED: Shutdown firewall
......
560 571
    - PROTECTED: Firewall in secure mode
561 572
    """
562 573

  
574
    arguments = dict(
575
        wait=FlagArgument('Wait server firewall to build', ('-w', '--wait')),
576
        timeout=IntArgument(
577
            'Set wait timeout in seconds (default: 60)', '--timeout',
578
            default=60)
579
    )
580

  
563 581
    @errors.generic.all
564 582
    @errors.cyclades.connection
565 583
    @errors.cyclades.server_id
566 584
    @errors.cyclades.firewall
567 585
    def _run(self, server_id, profile):
568
        self._optional_output(self.client.set_firewall_profile(
569
            server_id=int(server_id), profile=('%s' % profile).upper()))
586
        if self['timeout'] and not self['wait']:
587
            raise CLIInvalidArgument('Invalid use of --timeout', details=[
588
                'Timeout is used only along with -w/--wait'])
589
        old_profile = self.client.get_firewall_profile(server_id)
590
        if old_profile.lower() == profile.lower():
591
            self.error('Firewall of server %s: allready in status %s' % (
592
                server_id, old_profile))
593
        else:
594
            self._optional_output(self.client.set_firewall_profile(
595
                server_id=int(server_id), profile=('%s' % profile).upper()))
596
            if self['wait']:
597
                self._wait(server_id, old_profile, timeout=self['timeout'])
570 598

  
571 599
    def main(self, server_id, profile):
572 600
        super(self.__class__, self)._run()

Also available in: Unified diff