From a382ea56f65a4c85b0c17f217355d521ee578185 Mon Sep 17 00:00:00 2001 From: Stavros Sachtouris Date: Thu, 3 Jan 2013 18:23:10 +0200 Subject: [PATCH] Add since arg in server_list --- kamaki/cli/commands/cyclades_cli.py | 13 +++++++++++-- kamaki/clients/cyclades.py | 12 ++++++++++++ kamaki/clients/cyclades_rest_api.py | 24 ++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/kamaki/cli/commands/cyclades_cli.py b/kamaki/cli/commands/cyclades_cli.py index db223ec..68e157b 100644 --- a/kamaki/cli/commands/cyclades_cli.py +++ b/kamaki/cli/commands/cyclades_cli.py @@ -69,7 +69,10 @@ class server_list(_init_cyclades): """List servers""" arguments = dict( - detail=FlagArgument('show detailed output', '-l') + detail=FlagArgument('show detailed output', '-l'), + since=ValueArgument( + 'show only items since date (\' d/m/Y H:M:S \')', + '--since') ) def _info_print(self, server): @@ -98,8 +101,14 @@ class server_list(_init_cyclades): def main(self): super(self.__class__, self).main() try: - servers = self.client.list_servers(self['detail']) + servers = self.client.list_servers(self['detail'], self['since']) self._print(servers) + except ClientError as ce: + if ce.status == 400 and 'changes-since' in ('%s' % ce): + raiseCLIError(None, + 'Incorrect date format for --since', + details=['Accepted date format: d/m/y']) + raiseCLIError(ce) except Exception as err: raiseCLIError(err) diff --git a/kamaki/clients/cyclades.py b/kamaki/clients/cyclades.py index 84374e9..6e8604b 100644 --- a/kamaki/clients/cyclades.py +++ b/kamaki/clients/cyclades.py @@ -93,6 +93,18 @@ class CycladesClient(CycladesClientApi): r = self.servers_post(server_id, 'action', json_data=req, success=202) r.release() + def list_servers(self, detail=False, changes_since=None): + """ + :param detail: (bool) append full server details to each item if true + + :param changes_since: (date) + + :returns: list of server ids and names + """ + detail = 'detail' if detail else '' + r = self.servers_get(command=detail, changes_since=changes_since) + return r.json['servers']['values'] + def list_server_nics(self, server_id): """ :param server_id: integer (str or int) diff --git a/kamaki/clients/cyclades_rest_api.py b/kamaki/clients/cyclades_rest_api.py index f316806..e814f6a 100644 --- a/kamaki/clients/cyclades_rest_api.py +++ b/kamaki/clients/cyclades_rest_api.py @@ -39,6 +39,30 @@ import json class CycladesClientApi(ComputeClient): """GRNet Cyclades REST API Client""" + def servers_get(self, + server_id='', + command='', + success=200, + changes_since=None, + **kwargs): + """GET base_url/servers[/server_id][/command] request + + :param server_id: integer (as int or str) + + :param command: 'ips', 'stats', or '' + + :param success: success code or list or tupple of accepted success + codes. if server response code is not in this list, a ClientError + raises + + :param changes_since: (date) + + :returns: request response + """ + path = path4url('servers', server_id, command) + self.set_param('changes-since', changes_since, changes_since) + return self.get(path, success=success, **kwargs) + def networks_get(self, network_id='', command='', success=(200, 203), **kwargs): """GET base_url/networks[/network_id][/command] request -- 1.7.10.4