root / kamaki / cli / commands / snf-astakos.py @ 09b2b38e
History | View | Annotate | Download (7.1 kB)
1 | 97fc1e06 | Stavros Sachtouris | # Copyright 2013 GRNET S.A. All rights reserved.
|
---|---|---|---|
2 | 97fc1e06 | Stavros Sachtouris | #
|
3 | 97fc1e06 | Stavros Sachtouris | # Redistribution and use in source and binary forms, with or
|
4 | 97fc1e06 | Stavros Sachtouris | # without modification, are permitted provided that the following
|
5 | 97fc1e06 | Stavros Sachtouris | # conditions are met:
|
6 | 97fc1e06 | Stavros Sachtouris | #
|
7 | 97fc1e06 | Stavros Sachtouris | # 1. Redistributions of source code must retain the above
|
8 | 97fc1e06 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
9 | 97fc1e06 | Stavros Sachtouris | # disclaimer.
|
10 | 97fc1e06 | Stavros Sachtouris | #
|
11 | 97fc1e06 | Stavros Sachtouris | # 2. Redistributions in binary form must reproduce the above
|
12 | 97fc1e06 | Stavros Sachtouris | # copyright notice, this list of conditions and the following
|
13 | 97fc1e06 | Stavros Sachtouris | # disclaimer in the documentation and/or other materials
|
14 | 97fc1e06 | Stavros Sachtouris | # provided with the distribution.
|
15 | 97fc1e06 | Stavros Sachtouris | #
|
16 | 97fc1e06 | Stavros Sachtouris | # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 | 97fc1e06 | Stavros Sachtouris | # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 | 97fc1e06 | Stavros Sachtouris | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 | 97fc1e06 | Stavros Sachtouris | # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 | 97fc1e06 | Stavros Sachtouris | # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 | 97fc1e06 | Stavros Sachtouris | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 | 97fc1e06 | Stavros Sachtouris | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 | 97fc1e06 | Stavros Sachtouris | # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 | 97fc1e06 | Stavros Sachtouris | # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 | 97fc1e06 | Stavros Sachtouris | # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 | 97fc1e06 | Stavros Sachtouris | # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 | 97fc1e06 | Stavros Sachtouris | # POSSIBILITY OF SUCH DAMAGE.
|
28 | 97fc1e06 | Stavros Sachtouris | #
|
29 | 97fc1e06 | Stavros Sachtouris | # The views and conclusions contained in the software and
|
30 | 97fc1e06 | Stavros Sachtouris | # documentation are those of the authors and should not be
|
31 | 97fc1e06 | Stavros Sachtouris | # interpreted as representing official policies, either expressed
|
32 | 97fc1e06 | Stavros Sachtouris | # or implied, of GRNET S.A.command
|
33 | 97fc1e06 | Stavros Sachtouris | |
34 | 97fc1e06 | Stavros Sachtouris | from astakosclient import AstakosClient |
35 | 97fc1e06 | Stavros Sachtouris | |
36 | 97fc1e06 | Stavros Sachtouris | from kamaki.cli import command |
37 | 683335b1 | Stavros Sachtouris | from kamaki.cli.errors import CLISyntaxError |
38 | e1c18867 | Stavros Sachtouris | from kamaki.cli.commands import _command_init, _optional_json |
39 | 97fc1e06 | Stavros Sachtouris | from kamaki.cli.command_tree import CommandTree |
40 | 97fc1e06 | Stavros Sachtouris | from kamaki.cli.utils import print_dict |
41 | 683335b1 | Stavros Sachtouris | from kamaki.cli.argument import FlagArgument, ValueArgument |
42 | 09b2b38e | Stavros Sachtouris | from kamaki.cli.logger import add_file_logger, get_logger |
43 | 97fc1e06 | Stavros Sachtouris | |
44 | 97fc1e06 | Stavros Sachtouris | snfastakos_cmds = CommandTree('astakos', 'astakosclient CLI') |
45 | 97fc1e06 | Stavros Sachtouris | _commands = [snfastakos_cmds] |
46 | 97fc1e06 | Stavros Sachtouris | |
47 | 97fc1e06 | Stavros Sachtouris | |
48 | 09b2b38e | Stavros Sachtouris | log = get_logger(__name__) |
49 | 97fc1e06 | Stavros Sachtouris | |
50 | 97fc1e06 | Stavros Sachtouris | |
51 | 97fc1e06 | Stavros Sachtouris | class _astakos_init(_command_init): |
52 | 97fc1e06 | Stavros Sachtouris | |
53 | 683335b1 | Stavros Sachtouris | def __init__(self, arguments=dict()): |
54 | 683335b1 | Stavros Sachtouris | super(_astakos_init, self).__init__(arguments) |
55 | 683335b1 | Stavros Sachtouris | self['token'] = ValueArgument('Custom token', '--token') |
56 | 683335b1 | Stavros Sachtouris | |
57 | 97fc1e06 | Stavros Sachtouris | def _run(self): |
58 | 683335b1 | Stavros Sachtouris | self.token = self['token']\ |
59 | 683335b1 | Stavros Sachtouris | or self.config.get('astakos', 'token')\ |
60 | 97fc1e06 | Stavros Sachtouris | or self.config.get('user', 'token')\ |
61 | 97fc1e06 | Stavros Sachtouris | or self.config.get('global', 'token') |
62 | 97fc1e06 | Stavros Sachtouris | base_url = self.config.get('astakos', 'url')\ |
63 | 97fc1e06 | Stavros Sachtouris | or self.config.get('user', 'url')\ |
64 | 97fc1e06 | Stavros Sachtouris | or self.config.get('global', 'url') |
65 | 09b2b38e | Stavros Sachtouris | self.client = AstakosClient(
|
66 | 09b2b38e | Stavros Sachtouris | base_url, logger=add_file_logger('astakosclient'))
|
67 | 97fc1e06 | Stavros Sachtouris | self._set_log_params()
|
68 | 97fc1e06 | Stavros Sachtouris | self._update_max_threads()
|
69 | 97fc1e06 | Stavros Sachtouris | |
70 | 97fc1e06 | Stavros Sachtouris | def main(self): |
71 | 97fc1e06 | Stavros Sachtouris | self._run()
|
72 | 97fc1e06 | Stavros Sachtouris | |
73 | 97fc1e06 | Stavros Sachtouris | |
74 | 97fc1e06 | Stavros Sachtouris | @command(snfastakos_cmds)
|
75 | 97fc1e06 | Stavros Sachtouris | class astakos_authenticate(_astakos_init, _optional_json): |
76 | 97fc1e06 | Stavros Sachtouris | """Authenticate a user
|
77 | 97fc1e06 | Stavros Sachtouris | Get user information (e.g. unique account name) from token
|
78 | 97fc1e06 | Stavros Sachtouris | Token should be set in settings:
|
79 | 97fc1e06 | Stavros Sachtouris | * check if a token is set /config get token
|
80 | 97fc1e06 | Stavros Sachtouris | * permanently set a token /config set token <token>
|
81 | 97fc1e06 | Stavros Sachtouris | Token can also be provided as a parameter
|
82 | 97fc1e06 | Stavros Sachtouris | """
|
83 | 97fc1e06 | Stavros Sachtouris | |
84 | 97fc1e06 | Stavros Sachtouris | arguments = dict(
|
85 | 97fc1e06 | Stavros Sachtouris | usage=FlagArgument('also return usage information', ('--with-usage')) |
86 | 97fc1e06 | Stavros Sachtouris | ) |
87 | 97fc1e06 | Stavros Sachtouris | |
88 | 683335b1 | Stavros Sachtouris | def _run(self): |
89 | 97fc1e06 | Stavros Sachtouris | self._print(
|
90 | e1c18867 | Stavros Sachtouris | self.client.get_user_info(self.token, self['usage']), print_dict) |
91 | 97fc1e06 | Stavros Sachtouris | |
92 | 683335b1 | Stavros Sachtouris | def main(self): |
93 | 683335b1 | Stavros Sachtouris | super(self.__class__, self)._run() |
94 | 683335b1 | Stavros Sachtouris | self._run()
|
95 | 683335b1 | Stavros Sachtouris | |
96 | 683335b1 | Stavros Sachtouris | |
97 | 683335b1 | Stavros Sachtouris | @command(snfastakos_cmds)
|
98 | 683335b1 | Stavros Sachtouris | class astakos_username(_astakos_init, _optional_json): |
99 | 683335b1 | Stavros Sachtouris | """Get username(s) from uuid(s)"""
|
100 | 683335b1 | Stavros Sachtouris | |
101 | 683335b1 | Stavros Sachtouris | arguments = dict(
|
102 | 683335b1 | Stavros Sachtouris | service_token=ValueArgument( |
103 | 683335b1 | Stavros Sachtouris | 'Use service token instead', '--service-token') |
104 | 683335b1 | Stavros Sachtouris | ) |
105 | 683335b1 | Stavros Sachtouris | |
106 | 683335b1 | Stavros Sachtouris | def _run(self, uuids): |
107 | 683335b1 | Stavros Sachtouris | assert uuids and isinstance(uuids, list), 'No valid uuids' |
108 | 683335b1 | Stavros Sachtouris | if 1 == len(uuids): |
109 | 683335b1 | Stavros Sachtouris | self._print(self.client.get_username(self.token, uuids[0])) |
110 | 683335b1 | Stavros Sachtouris | else:
|
111 | 683335b1 | Stavros Sachtouris | self._print(
|
112 | 683335b1 | Stavros Sachtouris | self.client.get_username(self.token, uuids), print_dict) |
113 | 683335b1 | Stavros Sachtouris | |
114 | 683335b1 | Stavros Sachtouris | def main(self, uuid, *more_uuids): |
115 | 683335b1 | Stavros Sachtouris | super(self.__class__, self)._run() |
116 | 683335b1 | Stavros Sachtouris | self._run([uuid] + list(more_uuids)) |
117 | 683335b1 | Stavros Sachtouris | |
118 | 683335b1 | Stavros Sachtouris | |
119 | 683335b1 | Stavros Sachtouris | @command(snfastakos_cmds)
|
120 | 683335b1 | Stavros Sachtouris | class astakos_uuid(_astakos_init, _optional_json): |
121 | 683335b1 | Stavros Sachtouris | """Get uuid(s) from username(s)"""
|
122 | 683335b1 | Stavros Sachtouris | |
123 | 683335b1 | Stavros Sachtouris | def _run(self, usernames): |
124 | 683335b1 | Stavros Sachtouris | assert usernames and isinstance(usernames, list), 'No valid usernames' |
125 | 683335b1 | Stavros Sachtouris | if 1 == len(usernames): |
126 | 683335b1 | Stavros Sachtouris | self._print(self.client.get_uuid(self.token, usernames[0])) |
127 | 683335b1 | Stavros Sachtouris | else:
|
128 | 683335b1 | Stavros Sachtouris | self._print(
|
129 | 683335b1 | Stavros Sachtouris | self.client.get_uuids(self.token, usernames), print_dict) |
130 | 683335b1 | Stavros Sachtouris | |
131 | 683335b1 | Stavros Sachtouris | def main(self, usernames, *more_usernames): |
132 | 683335b1 | Stavros Sachtouris | super(self.__class__, self)._run() |
133 | 683335b1 | Stavros Sachtouris | self._run([usernames] + list(more_usernames)) |
134 | 683335b1 | Stavros Sachtouris | |
135 | 683335b1 | Stavros Sachtouris | |
136 | 683335b1 | Stavros Sachtouris | @command(snfastakos_cmds)
|
137 | 683335b1 | Stavros Sachtouris | class astakos_quotas(_astakos_init, _optional_json): |
138 | 683335b1 | Stavros Sachtouris | """Get user (or service) quotas"""
|
139 | 683335b1 | Stavros Sachtouris | |
140 | 683335b1 | Stavros Sachtouris | def _run(self): |
141 | 683335b1 | Stavros Sachtouris | self._print(self.client.get_quotas(self.token), print_dict) |
142 | 683335b1 | Stavros Sachtouris | |
143 | 683335b1 | Stavros Sachtouris | def main(self): |
144 | 683335b1 | Stavros Sachtouris | super(self.__class__, self)._run() |
145 | 683335b1 | Stavros Sachtouris | self._run()
|
146 | 683335b1 | Stavros Sachtouris | |
147 | 683335b1 | Stavros Sachtouris | |
148 | 683335b1 | Stavros Sachtouris | @command(snfastakos_cmds)
|
149 | 683335b1 | Stavros Sachtouris | class astakos_services(_astakos_init): |
150 | 683335b1 | Stavros Sachtouris | """Astakos operations filtered by services"""
|
151 | 683335b1 | Stavros Sachtouris | |
152 | 683335b1 | Stavros Sachtouris | |
153 | 683335b1 | Stavros Sachtouris | @command(snfastakos_cmds)
|
154 | e1c18867 | Stavros Sachtouris | class astakos_services_list(_astakos_init, _optional_json): |
155 | 683335b1 | Stavros Sachtouris | """List available services"""
|
156 | 683335b1 | Stavros Sachtouris | |
157 | 683335b1 | Stavros Sachtouris | def _run(self): |
158 | 683335b1 | Stavros Sachtouris | self._print(self.client.get_services()) |
159 | 683335b1 | Stavros Sachtouris | |
160 | 683335b1 | Stavros Sachtouris | def main(self): |
161 | 683335b1 | Stavros Sachtouris | super(self.__class__, self)._run() |
162 | 683335b1 | Stavros Sachtouris | self._run()
|
163 | 683335b1 | Stavros Sachtouris | |
164 | 683335b1 | Stavros Sachtouris | |
165 | 683335b1 | Stavros Sachtouris | @command(snfastakos_cmds)
|
166 | 683335b1 | Stavros Sachtouris | class astakos_services_username(_astakos_init, _optional_json): |
167 | 683335b1 | Stavros Sachtouris | """Get service username(s) from uuid(s)"""
|
168 | 683335b1 | Stavros Sachtouris | |
169 | 683335b1 | Stavros Sachtouris | def _run(self, stoken, uuids): |
170 | 683335b1 | Stavros Sachtouris | assert uuids and isinstance(uuids, list), 'No valid uuids' |
171 | 683335b1 | Stavros Sachtouris | if 1 == len(uuids): |
172 | 683335b1 | Stavros Sachtouris | self._print(self.client.service_get_username(stoken, uuids[0])) |
173 | 683335b1 | Stavros Sachtouris | else:
|
174 | 683335b1 | Stavros Sachtouris | self._print(
|
175 | 683335b1 | Stavros Sachtouris | self.client.service_get_usernames(stoken, uuids), print_dict)
|
176 | 683335b1 | Stavros Sachtouris | |
177 | 683335b1 | Stavros Sachtouris | def main(self, service_token, uuid, *more_uuids): |
178 | 683335b1 | Stavros Sachtouris | super(self.__class__, self)._run() |
179 | 683335b1 | Stavros Sachtouris | self._run(service_token, [uuid] + list(more_uuids)) |
180 | 683335b1 | Stavros Sachtouris | |
181 | 683335b1 | Stavros Sachtouris | |
182 | 683335b1 | Stavros Sachtouris | @command(snfastakos_cmds)
|
183 | 683335b1 | Stavros Sachtouris | class astakos_services_uuid(_astakos_init, _optional_json): |
184 | 683335b1 | Stavros Sachtouris | """Get service uuid(s) from username(s)"""
|
185 | 683335b1 | Stavros Sachtouris | |
186 | 683335b1 | Stavros Sachtouris | def _run(self, stoken, usernames): |
187 | 683335b1 | Stavros Sachtouris | assert usernames and isinstance(usernames, list), 'No valid usernames' |
188 | 683335b1 | Stavros Sachtouris | if 1 == len(usernames): |
189 | 683335b1 | Stavros Sachtouris | self._print(self.client.service_get_uuid(self.token, usernames[0])) |
190 | 683335b1 | Stavros Sachtouris | else:
|
191 | 683335b1 | Stavros Sachtouris | self._print(
|
192 | 683335b1 | Stavros Sachtouris | self.client.service_get_uuids(self.token, usernames), |
193 | 683335b1 | Stavros Sachtouris | print_dict) |
194 | 683335b1 | Stavros Sachtouris | |
195 | 683335b1 | Stavros Sachtouris | def main(self, service_token, usernames, *more_usernames): |
196 | 683335b1 | Stavros Sachtouris | super(self.__class__, self)._run() |
197 | 683335b1 | Stavros Sachtouris | self._run(service_token, [usernames] + list(more_usernames)) |
198 | 683335b1 | Stavros Sachtouris | |
199 | 683335b1 | Stavros Sachtouris | |
200 | 683335b1 | Stavros Sachtouris | @command(snfastakos_cmds)
|
201 | 683335b1 | Stavros Sachtouris | class astakos_services_quotas(_astakos_init, _optional_json): |
202 | 683335b1 | Stavros Sachtouris | """Get user (or service) quotas"""
|
203 | 683335b1 | Stavros Sachtouris | |
204 | 683335b1 | Stavros Sachtouris | arguments = dict(
|
205 | 683335b1 | Stavros Sachtouris | uuid=ValueArgument('A user unique id to get quotas for', '--uuid') |
206 | 683335b1 | Stavros Sachtouris | ) |
207 | 683335b1 | Stavros Sachtouris | |
208 | 683335b1 | Stavros Sachtouris | def _run(self, stoken): |
209 | 683335b1 | Stavros Sachtouris | self._print(self.client.service_get_quotas(stoken, self['uuid'])) |
210 | 683335b1 | Stavros Sachtouris | |
211 | 683335b1 | Stavros Sachtouris | def main(self, service_token): |
212 | 97fc1e06 | Stavros Sachtouris | super(self.__class__, self)._run() |
213 | 683335b1 | Stavros Sachtouris | self._run(service_token) |