Merge branch 'feature-expose-astakosclient' into develop
authorStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 28 May 2013 08:45:36 +0000 (11:45 +0300)
committerStavros Sachtouris <saxtouri@admin.grnet.gr>
Tue, 28 May 2013 08:45:36 +0000 (11:45 +0300)
Changelog
kamaki/cli/commands/snf-astakos.py [new file with mode: 0644]

index 2d905de..addd81f 100644 (file)
--- a/Changelog
+++ b/Changelog
@@ -80,4 +80,7 @@ Features:
 - Expand runtime args of image register for managing metadata and metada file
     dumps and loads [#3797]
 - Add server-firewall-get command to get a VMs firewall profile
+- Implement an optional astakosclient cli exposed as "astakos", with the following methods:
+    authenticate, uuid, username, quotas, service uuid/username/quotas
+
 
diff --git a/kamaki/cli/commands/snf-astakos.py b/kamaki/cli/commands/snf-astakos.py
new file mode 100644 (file)
index 0000000..fa8d3f4
--- /dev/null
@@ -0,0 +1,219 @@
+# Copyright 2013 GRNET S.A. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or
+# without modification, are permitted provided that the following
+# conditions are met:
+#
+#   1. Redistributions of source code must retain the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above
+#      copyright notice, this list of conditions and the following
+#      disclaimer in the documentation and/or other materials
+#      provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
+# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
+# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+# The views and conclusions contained in the software and
+# documentation are those of the authors and should not be
+# interpreted as representing official policies, either expressed
+# or implied, of GRNET S.A.command
+
+from astakosclient import AstakosClient
+
+from kamaki.cli import command
+from kamaki.cli.errors import CLISyntaxError
+from kamaki.cli.commands import _command_init, errors, _optional_json
+from kamaki.cli.command_tree import CommandTree
+from kamaki.cli.utils import print_dict
+from kamaki.cli.argument import FlagArgument, ValueArgument
+from kamaki.cli.logger import add_stream_logger
+
+snfastakos_cmds = CommandTree('astakos', 'astakosclient CLI')
+_commands = [snfastakos_cmds]
+
+
+log = add_stream_logger(__name__)
+
+
+class _astakos_init(_command_init):
+
+    def __init__(self, arguments=dict()):
+        super(_astakos_init, self).__init__(arguments)
+        self['token'] = ValueArgument('Custom token', '--token')
+
+    @errors.generic.all
+    #@errors.user.load
+    def _run(self):
+        self.token = self['token']\
+            or self.config.get('astakos', 'token')\
+            or self.config.get('user', 'token')\
+            or self.config.get('global', 'token')
+        base_url = self.config.get('astakos', 'url')\
+            or self.config.get('user', 'url')\
+            or self.config.get('global', 'url')
+        self.client = AstakosClient(base_url, logger=log)
+        self._set_log_params()
+        self._update_max_threads()
+
+    def main(self):
+        self._run()
+
+
+@command(snfastakos_cmds)
+class astakos_authenticate(_astakos_init, _optional_json):
+    """Authenticate a user
+    Get user information (e.g. unique account name) from token
+    Token should be set in settings:
+    *  check if a token is set    /config get token
+    *  permanently set a token    /config set token <token>
+    Token can also be provided as a parameter
+    """
+
+    arguments = dict(
+        usage=FlagArgument('also return usage information', ('--with-usage'))
+    )
+
+    @errors.generic.all
+    #@errors.user.authenticate
+    def _run(self):
+        print('KAMAKI LOG: call get_user_info(%s, %s)' % (
+            self.token, self['usage']))
+        self._print(
+            self.client.get_user_info(self.token, self['usage']),
+            print_dict)
+
+    def main(self):
+        super(self.__class__, self)._run()
+        self._run()
+
+
+@command(snfastakos_cmds)
+class astakos_username(_astakos_init, _optional_json):
+    """Get username(s) from uuid(s)"""
+
+    arguments = dict(
+        service_token=ValueArgument(
+            'Use service token instead', '--service-token')
+    )
+
+    def _run(self, uuids):
+        assert uuids and isinstance(uuids, list), 'No valid uuids'
+        if 1 == len(uuids):
+            self._print(self.client.get_username(self.token, uuids[0]))
+        else:
+            self._print(
+                self.client.get_username(self.token, uuids), print_dict)
+
+    def main(self, uuid, *more_uuids):
+        super(self.__class__, self)._run()
+        self._run([uuid] + list(more_uuids))
+
+
+@command(snfastakos_cmds)
+class astakos_uuid(_astakos_init, _optional_json):
+    """Get uuid(s) from username(s)"""
+
+    def _run(self, usernames):
+        assert usernames and isinstance(usernames, list), 'No valid usernames'
+        if 1 == len(usernames):
+            self._print(self.client.get_uuid(self.token, usernames[0]))
+        else:
+            self._print(
+                self.client.get_uuids(self.token, usernames), print_dict)
+
+    def main(self, usernames, *more_usernames):
+        super(self.__class__, self)._run()
+        self._run([usernames] + list(more_usernames))
+
+
+@command(snfastakos_cmds)
+class astakos_quotas(_astakos_init, _optional_json):
+    """Get user (or service) quotas"""
+
+    def _run(self):
+            self._print(self.client.get_quotas(self.token), print_dict)
+
+    def main(self):
+        super(self.__class__, self)._run()
+        self._run()
+
+
+@command(snfastakos_cmds)
+class astakos_services(_astakos_init):
+    """Astakos operations filtered by services"""
+
+
+@command(snfastakos_cmds)
+class astakos_services_list(_astakos_init):
+    """List available services"""
+
+    def _run(self):
+        self._print(self.client.get_services())
+
+    def main(self):
+        super(self.__class__, self)._run()
+        self._run()
+
+
+@command(snfastakos_cmds)
+class astakos_services_username(_astakos_init, _optional_json):
+    """Get service username(s) from uuid(s)"""
+
+    def _run(self, stoken, uuids):
+        assert uuids and isinstance(uuids, list), 'No valid uuids'
+        if 1 == len(uuids):
+            self._print(self.client.service_get_username(stoken, uuids[0]))
+        else:
+            self._print(
+                self.client.service_get_usernames(stoken, uuids), print_dict)
+
+    def main(self, service_token, uuid, *more_uuids):
+        super(self.__class__, self)._run()
+        self._run(service_token, [uuid] + list(more_uuids))
+
+
+@command(snfastakos_cmds)
+class astakos_services_uuid(_astakos_init, _optional_json):
+    """Get service uuid(s) from username(s)"""
+
+    def _run(self, stoken, usernames):
+        assert usernames and isinstance(usernames, list), 'No valid usernames'
+        if 1 == len(usernames):
+            self._print(self.client.service_get_uuid(self.token, usernames[0]))
+        else:
+            self._print(
+                self.client.service_get_uuids(self.token, usernames),
+                print_dict)
+
+    def main(self, service_token, usernames, *more_usernames):
+        super(self.__class__, self)._run()
+        self._run(service_token, [usernames] + list(more_usernames))
+
+
+@command(snfastakos_cmds)
+class astakos_services_quotas(_astakos_init, _optional_json):
+    """Get user (or service) quotas"""
+
+    arguments = dict(
+        uuid=ValueArgument('A user unique id to get quotas for', '--uuid')
+    )
+
+    def _run(self, stoken):
+        self._print(self.client.service_get_quotas(stoken, self['uuid']))
+
+    def main(self, service_token):
+        super(self.__class__, self)._run()
+        self._run(service_token)