Update wait to use --status
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 20 Dec 2013 12:58:29 +0000 (14:58 +0200)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Fri, 20 Dec 2013 13:39:25 +0000 (15:39 +0200)
kamaki/cli/argument/__init__.py
kamaki/cli/commands/cyclades.py
kamaki/cli/commands/network.py

index 384e3c4..f2dca9b 100644 (file)
@@ -32,7 +32,8 @@
 # or implied, of GRNET S.A.
 
 from kamaki.cli.config import Config
-from kamaki.cli.errors import CLISyntaxError, raiseCLIError
+from kamaki.cli.errors import (
+    CLISyntaxError, raiseCLIError, CLIInvalidArgument)
 from kamaki.cli.utils import split_input, to_bytes
 
 from datetime import datetime as dtm
@@ -375,6 +376,30 @@ class KeyValueArgument(Argument):
                 raiseCLIError(e, 'KeyValueArgument Syntax Error')
 
 
+class StatusArgument(ValueArgument):
+    """Initialize with valid_states=['list', 'of', 'states']
+    First state is the default"""
+
+    def __init__(self, *args, **kwargs):
+        self.valid_states = kwargs.pop('valid_states', ['BUILD', ])
+        super(StatusArgument, self).__init__(*args, **kwargs)
+
+    @property
+    def value(self):
+        return getattr(self, '_value', None)
+
+    @value.setter
+    def value(self, new_status):
+        if new_status:
+            new_status = new_status.upper()
+            if new_status not in self.valid_states:
+                raise CLIInvalidArgument(
+                    'Invalid argument %s' % new_status, details=[
+                    'Usage: '
+                    '%s=[%s]' % (self.lvalue, '|'.join(self.valid_states))])
+            self._value = new_status
+
+
 class ProgressBarArgument(FlagArgument):
     """Manage a progress bar"""
 
index 303e4a5..6f72ad1 100644 (file)
@@ -44,7 +44,7 @@ from kamaki.cli.errors import (
 from kamaki.clients.cyclades import CycladesClient
 from kamaki.cli.argument import (
     FlagArgument, ValueArgument, KeyValueArgument, RepeatableArgument,
-    ProgressBarArgument, DateArgument, IntArgument)
+    ProgressBarArgument, DateArgument, IntArgument, StatusArgument)
 from kamaki.cli.commands import _command_init, errors, addLogSettings
 from kamaki.cli.commands import (
     _optional_output_cmd, _optional_json, _name_filter, _id_filter)
@@ -69,6 +69,8 @@ howto_personality = [
     '  [mode=]MODE: permission in octal (e.g., 0777)',
     'e.g., -p /tmp/my.file,owner=root,mode=0777']
 
+server_states = ('BUILD', 'ACTIVE', 'STOPPED', 'REBOOT')
+
 
 class _service_wait(object):
 
@@ -277,9 +279,9 @@ class server_info(_init_cyclades, _optional_json):
     @errors.cyclades.connection
     @errors.cyclades.server_id
     def _run(self, server_id):
-        vm = self.client.get_server_nics(server_id)
         if self['nics']:
-            self._print(vm.get('attachments', []))
+            self._print(
+                self.client.get_server_nics(server_id), self.print_dict)
         elif self['network_id']:
             self._print(
                 self.client.get_server_network_nics(
@@ -288,6 +290,7 @@ class server_info(_init_cyclades, _optional_json):
             self._print(
                 self.client.get_server_stats(server_id), self.print_dict)
         else:
+            vm = self.client.get_server_details(server_id)
             uuids = self._uuids2usernames([vm['user_id'], vm['tenant_id']])
             vm['user_id'] += ' (%s)' % uuids[vm['user_id']]
             vm['tenant_id'] += ' (%s)' % uuids[vm['tenant_id']]
@@ -295,7 +298,7 @@ class server_info(_init_cyclades, _optional_json):
 
     def main(self, server_id):
         super(self.__class__, self)._run()
-        choose_one = ('nics', 'vnc', 'stats')
+        choose_one = ('nics', 'stats', 'diagnostics')
         count = len([a for a in choose_one if self[a]])
         if count > 1:
             raise CLIInvalidArgument('Invalid argument compination', details=[
@@ -773,11 +776,16 @@ class server_stats(_init_cyclades, _optional_json):
 
 @command(server_cmds)
 class server_wait(_init_cyclades, _server_wait):
-    """Wait for server to finish (BUILD, STOPPED, REBOOT, ACTIVE)"""
+    """Wait for server to change its status (default: BUILD)"""
 
     arguments = dict(
         timeout=IntArgument(
-            'Wait limit in seconds (default: 60)', '--timeout', default=60)
+            'Wait limit in seconds (default: 60)', '--timeout', default=60),
+        server_status=StatusArgument(
+            'Status to wait for (%s, default: %s)' % (
+                ', '.join(server_states), server_states[0]),
+            '--status',
+            valid_states=server_states)
     )
 
     @errors.generic.all
@@ -793,9 +801,9 @@ class server_wait(_init_cyclades, _server_wait):
                 'status is already %s' % (
                     server_id, current_status, r['status']))
 
-    def main(self, server_id, current_status='BUILD'):
+    def main(self, server_id):
         super(self.__class__, self)._run()
-        self._run(server_id=server_id, current_status=current_status)
+        self._run(server_id=server_id, current_status=self['server_status'])
 
 
 @command(flavor_cmds)
index b46954c..cb53ce5 100644 (file)
@@ -40,7 +40,8 @@ from kamaki.cli.errors import (
     CLIBaseUrlError, CLIInvalidArgument, raiseCLIError)
 from kamaki.clients.cyclades import CycladesNetworkClient, ClientError
 from kamaki.cli.argument import (
-    FlagArgument, ValueArgument, RepeatableArgument, IntArgument)
+    FlagArgument, ValueArgument, RepeatableArgument, IntArgument,
+    StatusArgument)
 from kamaki.cli.commands import _command_init, errors, addLogSettings
 from kamaki.cli.commands import (
     _optional_output_cmd, _optional_json, _name_filter, _id_filter)
@@ -59,6 +60,8 @@ about_authentication = '\nUser Authentication:\
     \n  to set authentication token: \
     [kamaki] config set cloud.<CLOUD>.token <TOKEN>'
 
+port_states = ('BUILD', 'ACTIVE', 'DOWN', 'ERROR')
+
 
 class _port_wait(_service_wait):
 
@@ -484,25 +487,6 @@ class port_modify(_init_network, _optional_json):
         self._run(port_id=port_id)
 
 
-class PortStatusArgument(ValueArgument):
-
-    valid = ('BUILD', 'ACTIVE', 'DOWN', 'ERROR')
-
-    @property
-    def value(self):
-        return getattr(self, '_value', None)
-
-    @value.setter
-    def value(self, new_status):
-        if new_status:
-            new_status = new_status.upper()
-            if new_status in self.valid:
-                raise CLIInvalidArgument(
-                    'Invalid argument %s' % new_status, details=[
-                    'Status valid values: %s'] % ', '.join(self.valid))
-            self._value = new_status
-
-
 class _port_create(_init_network, _optional_json, _port_wait):
 
     def connect(self, network_id, device_id):
@@ -557,32 +541,34 @@ class port_create(_port_create):
 
 @command(port_cmds)
 class port_wait(_init_network, _port_wait):
-    """Wait for port to finish [ACTIVE, DOWN, BUILD, ERROR]"""
+    """Wait for port to finish (default: BUILD)"""
 
     arguments = dict(
-        current_status=PortStatusArgument(
-            'Wait while in this status', '--status'),
+        port_status=StatusArgument(
+            'Wait while in this status (%s, default: %s)' % (
+                ', '.join(port_states), port_states[0]),
+            '--status',
+            valid_states=port_states),
         timeout=IntArgument(
             'Wait limit in seconds (default: 60)', '--timeout', default=60)
     )
 
     @errors.generic.all
     @errors.cyclades.connection
-    def _run(self, port_id, current_status):
+    def _run(self, port_id, port_status):
         port = self.client.get_port_details(port_id)
-        if port['status'].lower() == current_status.lower():
-            self._wait(port_id, current_status, timeout=self['timeout'])
+        if port['status'].lower() == port_status.lower():
+            self._wait(port_id, port_status, timeout=self['timeout'])
         else:
             self.error(
                 'Port %s: Cannot wait for status %s, '
                 'status is already %s' % (
-                    port_id, current_status, port['status']))
+                    port_id, port_status, port['status']))
 
     def main(self, port_id):
         super(self.__class__, self)._run()
-        current_status = self['current_status'] or self.arguments[
-            'current_status'].valid[0]
-        self._run(port_id=port_id, current_status=current_status)
+        port_status = self['port_status'] or port_states[0]
+        self._run(port_id=port_id, port_status=port_status)
 
 
 @command(ip_cmds)