1dfbc55bac6e87dab4fa72371a04b09e262f2925
[kamaki] / kamaki / cli / commands / snf-astakos.py
1 # Copyright 2013 GRNET S.A. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
5 # conditions are met:
6 #
7 #   1. Redistributions of source code must retain the above
8 #      copyright notice, this list of conditions and the following
9 #      disclaimer.
10 #
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.
15 #
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.
28 #
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
33
34 from astakosclient import AstakosClient
35
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
43
44 snfastakos_cmds = CommandTree('astakos', 'astakosclient CLI')
45 _commands = [snfastakos_cmds]
46
47
48 log = get_logger(__name__)
49
50
51 class _astakos_init(_command_init):
52
53     def __init__(self, arguments=dict()):
54         super(_astakos_init, self).__init__(arguments)
55         self['token'] = ValueArgument('Custom token', '--token')
56
57     def _run(self):
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()
69
70     def main(self):
71         self._run()
72
73
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
82     """
83
84     arguments = dict(
85         usage=FlagArgument('also return usage information', ('--with-usage'))
86     )
87
88     def _run(self):
89         self._print(
90             self.client.get_user_info(self.token, self['usage']), print_dict)
91
92     def main(self):
93         super(self.__class__, self)._run()
94         self._run()
95
96
97 @command(snfastakos_cmds)
98 class astakos_username(_astakos_init, _optional_json):
99     """Get username(s) from uuid(s)"""
100
101     arguments = dict(
102         service_token=ValueArgument(
103             'Use service token instead', '--service-token')
104     )
105
106     def _run(self, uuids):
107         assert uuids and isinstance(uuids, list), 'No valid uuids'
108         if 1 == len(uuids):
109             self._print(self.client.get_username(self.token, uuids[0]))
110         else:
111             self._print(
112                 self.client.get_username(self.token, uuids), print_dict)
113
114     def main(self, uuid, *more_uuids):
115         super(self.__class__, self)._run()
116         self._run([uuid] + list(more_uuids))
117
118
119 @command(snfastakos_cmds)
120 class astakos_uuid(_astakos_init, _optional_json):
121     """Get uuid(s) from username(s)"""
122
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]))
127         else:
128             self._print(
129                 self.client.get_uuids(self.token, usernames), print_dict)
130
131     def main(self, usernames, *more_usernames):
132         super(self.__class__, self)._run()
133         self._run([usernames] + list(more_usernames))
134
135
136 @command(snfastakos_cmds)
137 class astakos_quotas(_astakos_init, _optional_json):
138     """Get user (or service) quotas"""
139
140     def _run(self):
141             self._print(self.client.get_quotas(self.token), print_dict)
142
143     def main(self):
144         super(self.__class__, self)._run()
145         self._run()
146
147
148 @command(snfastakos_cmds)
149 class astakos_services(_astakos_init):
150     """Astakos operations filtered by services"""
151
152
153 @command(snfastakos_cmds)
154 class astakos_services_list(_astakos_init, _optional_json):
155     """List available services"""
156
157     def _run(self):
158         self._print(self.client.get_services())
159
160     def main(self):
161         super(self.__class__, self)._run()
162         self._run()
163
164
165 @command(snfastakos_cmds)
166 class astakos_services_username(_astakos_init, _optional_json):
167     """Get service username(s) from uuid(s)"""
168
169     def _run(self, stoken, uuids):
170         assert uuids and isinstance(uuids, list), 'No valid uuids'
171         if 1 == len(uuids):
172             self._print(self.client.service_get_username(stoken, uuids[0]))
173         else:
174             self._print(
175                 self.client.service_get_usernames(stoken, uuids), print_dict)
176
177     def main(self, service_token, uuid, *more_uuids):
178         super(self.__class__, self)._run()
179         self._run(service_token, [uuid] + list(more_uuids))
180
181
182 @command(snfastakos_cmds)
183 class astakos_services_uuid(_astakos_init, _optional_json):
184     """Get service uuid(s) from username(s)"""
185
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]))
190         else:
191             self._print(
192                 self.client.service_get_uuids(self.token, usernames),
193                 print_dict)
194
195     def main(self, service_token, usernames, *more_usernames):
196         super(self.__class__, self)._run()
197         self._run(service_token, [usernames] + list(more_usernames))
198
199
200 @command(snfastakos_cmds)
201 class astakos_services_quotas(_astakos_init, _optional_json):
202     """Get user (or service) quotas"""
203
204     arguments = dict(
205         uuid=ValueArgument('A user unique id to get quotas for', '--uuid')
206     )
207
208     def _run(self, stoken):
209         self._print(self.client.service_get_quotas(stoken, self['uuid']))
210
211     def main(self, service_token):
212         super(self.__class__, self)._run()
213         self._run(service_token)