1 # Copyright 2013 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.command
34 from astakosclient import AstakosClient
36 from kamaki.cli import command
37 from kamaki.cli.errors import CLISyntaxError
38 from kamaki.cli.commands import _command_init, _optional_json
39 from kamaki.cli.command_tree import CommandTree
40 from kamaki.cli.utils import print_dict
41 from kamaki.cli.argument import FlagArgument, ValueArgument
42 from kamaki.cli.logger import add_file_logger, get_logger
44 snfastakos_cmds = CommandTree('astakos', 'astakosclient CLI')
45 _commands = [snfastakos_cmds]
48 log = get_logger(__name__)
51 class _astakos_init(_command_init):
53 def __init__(self, arguments=dict()):
54 super(_astakos_init, self).__init__(arguments)
55 self['token'] = ValueArgument('Custom token', '--token')
58 self.token = self['token']\
59 or self.config.get('astakos', 'token')\
60 or self.config.get('user', 'token')\
61 or self.config.get('global', 'token')
62 base_url = self.config.get('astakos', 'url')\
63 or self.config.get('user', 'url')\
64 or self.config.get('global', 'url')
65 self.client = AstakosClient(
66 base_url, logger=add_file_logger('astakosclient'))
67 self._set_log_params()
68 self._update_max_threads()
74 @command(snfastakos_cmds)
75 class astakos_authenticate(_astakos_init, _optional_json):
76 """Authenticate a user
77 Get user information (e.g. unique account name) from token
78 Token should be set in settings:
79 * check if a token is set /config get token
80 * permanently set a token /config set token <token>
81 Token can also be provided as a parameter
85 usage=FlagArgument('also return usage information', ('--with-usage'))
90 self.client.get_user_info(self.token, self['usage']), print_dict)
93 super(self.__class__, self)._run()
97 @command(snfastakos_cmds)
98 class astakos_username(_astakos_init, _optional_json):
99 """Get username(s) from uuid(s)"""
102 service_token=ValueArgument(
103 'Use service token instead', '--service-token')
106 def _run(self, uuids):
107 assert uuids and isinstance(uuids, list), 'No valid uuids'
109 self._print(self.client.get_username(self.token, uuids[0]))
112 self.client.get_username(self.token, uuids), print_dict)
114 def main(self, uuid, *more_uuids):
115 super(self.__class__, self)._run()
116 self._run([uuid] + list(more_uuids))
119 @command(snfastakos_cmds)
120 class astakos_uuid(_astakos_init, _optional_json):
121 """Get uuid(s) from username(s)"""
123 def _run(self, usernames):
124 assert usernames and isinstance(usernames, list), 'No valid usernames'
125 if 1 == len(usernames):
126 self._print(self.client.get_uuid(self.token, usernames[0]))
129 self.client.get_uuids(self.token, usernames), print_dict)
131 def main(self, usernames, *more_usernames):
132 super(self.__class__, self)._run()
133 self._run([usernames] + list(more_usernames))
136 @command(snfastakos_cmds)
137 class astakos_quotas(_astakos_init, _optional_json):
138 """Get user (or service) quotas"""
141 self._print(self.client.get_quotas(self.token), print_dict)
144 super(self.__class__, self)._run()
148 @command(snfastakos_cmds)
149 class astakos_services(_astakos_init):
150 """Astakos operations filtered by services"""
153 @command(snfastakos_cmds)
154 class astakos_services_list(_astakos_init, _optional_json):
155 """List available services"""
158 self._print(self.client.get_services())
161 super(self.__class__, self)._run()
165 @command(snfastakos_cmds)
166 class astakos_services_username(_astakos_init, _optional_json):
167 """Get service username(s) from uuid(s)"""
169 def _run(self, stoken, uuids):
170 assert uuids and isinstance(uuids, list), 'No valid uuids'
172 self._print(self.client.service_get_username(stoken, uuids[0]))
175 self.client.service_get_usernames(stoken, uuids), print_dict)
177 def main(self, service_token, uuid, *more_uuids):
178 super(self.__class__, self)._run()
179 self._run(service_token, [uuid] + list(more_uuids))
182 @command(snfastakos_cmds)
183 class astakos_services_uuid(_astakos_init, _optional_json):
184 """Get service uuid(s) from username(s)"""
186 def _run(self, stoken, usernames):
187 assert usernames and isinstance(usernames, list), 'No valid usernames'
188 if 1 == len(usernames):
189 self._print(self.client.service_get_uuid(self.token, usernames[0]))
192 self.client.service_get_uuids(self.token, usernames),
195 def main(self, service_token, usernames, *more_usernames):
196 super(self.__class__, self)._run()
197 self._run(service_token, [usernames] + list(more_usernames))
200 @command(snfastakos_cmds)
201 class astakos_services_quotas(_astakos_init, _optional_json):
202 """Get user (or service) quotas"""
205 uuid=ValueArgument('A user unique id to get quotas for', '--uuid')
208 def _run(self, stoken):
209 self._print(self.client.service_get_quotas(stoken, self['uuid']))
211 def main(self, service_token):
212 super(self.__class__, self)._run()
213 self._run(service_token)