root / kamaki / cli / commands / snf-astakos.py @ d1d0cec9
History | View | Annotate | Download (10.5 kB)
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, AstakosClientException |
35 |
|
36 |
from kamaki.cli import command |
37 |
from kamaki.cli.errors import CLIBaseUrlError |
38 |
from kamaki.cli.commands import ( |
39 |
_command_init, errors, _optional_json, addLogSettings) |
40 |
from kamaki.cli.command_tree import CommandTree |
41 |
from kamaki.cli.utils import print_dict, format_size |
42 |
from kamaki.cli.argument import FlagArgument, ValueArgument |
43 |
from kamaki.cli.logger import get_logger |
44 |
|
45 |
snfastakos_cmds = CommandTree('astakos', 'astakosclient CLI') |
46 |
_commands = [snfastakos_cmds] |
47 |
|
48 |
|
49 |
def astakoserror(foo): |
50 |
def _raise(self, *args, **kwargs): |
51 |
try:
|
52 |
return foo(self, *args, **kwargs) |
53 |
except AstakosClientException as ace: |
54 |
try:
|
55 |
ace.details = ['%s' % ace.details]
|
56 |
except Exception: |
57 |
pass
|
58 |
finally:
|
59 |
raise ace
|
60 |
return _raise
|
61 |
|
62 |
|
63 |
class _astakos_init(_command_init): |
64 |
|
65 |
def __init__(self, arguments=dict(), auth_base=None, cloud=None): |
66 |
super(_astakos_init, self).__init__(arguments, auth_base, cloud) |
67 |
self['token'] = ValueArgument('Custom token', '--token') |
68 |
|
69 |
@errors.generic.all
|
70 |
@astakoserror
|
71 |
@addLogSettings
|
72 |
def _run(self): |
73 |
self.cloud = self.cloud if self.cloud else 'default' |
74 |
self.token = self['token'] or self._custom_token('astakos')\ |
75 |
or self.config.get_cloud(self.cloud, 'token') |
76 |
if getattr(self, 'auth_base', False): |
77 |
astakos_endpoints = self.auth_base.get_service_endpoints(
|
78 |
self._custom_type('astakos') or 'identity', |
79 |
self._custom_version('astakos') or '') |
80 |
base_url = astakos_endpoints['SNF:uiURL']
|
81 |
base_url = ''.join(base_url.split('/ui')) |
82 |
else:
|
83 |
base_url = self._custom_url('astakos') |
84 |
if not base_url: |
85 |
raise CLIBaseUrlError(service='astakos') |
86 |
self.client = AstakosClient(
|
87 |
base_url, logger=get_logger('kamaki.clients'))
|
88 |
|
89 |
def main(self): |
90 |
self._run()
|
91 |
|
92 |
|
93 |
@command(snfastakos_cmds)
|
94 |
class astakos_user_info(_astakos_init, _optional_json): |
95 |
"""Authenticate a user
|
96 |
Get user information (e.g. unique account name) from token
|
97 |
Token should be set in settings:
|
98 |
* check if a token is set /config get cloud.default.token
|
99 |
* permanently set a token /config set cloud.default.token <token>
|
100 |
Token can also be provided as a parameter
|
101 |
(To use a named cloud, use its name instead of "default")
|
102 |
"""
|
103 |
|
104 |
arguments = dict(
|
105 |
usage=FlagArgument('also return usage information', ('--with-usage')) |
106 |
) |
107 |
|
108 |
@errors.generic.all
|
109 |
@astakoserror
|
110 |
def _run(self): |
111 |
self._print(
|
112 |
self.client.get_user_info(self.token, self['usage']), print_dict) |
113 |
|
114 |
def main(self): |
115 |
super(self.__class__, self)._run() |
116 |
self._run()
|
117 |
|
118 |
|
119 |
@command(snfastakos_cmds)
|
120 |
class astakos_user_name(_astakos_init, _optional_json): |
121 |
"""Get username(s) from uuid(s)"""
|
122 |
|
123 |
arguments = dict(
|
124 |
service_token=ValueArgument( |
125 |
'Use service token instead', '--service-token') |
126 |
) |
127 |
|
128 |
@errors.generic.all
|
129 |
@astakoserror
|
130 |
def _run(self, uuids): |
131 |
assert uuids and isinstance(uuids, list), 'No valid uuids' |
132 |
if 1 == len(uuids): |
133 |
self._print(self.client.get_username(self.token, uuids[0])) |
134 |
else:
|
135 |
self._print(
|
136 |
self.client.get_username(self.token, uuids), print_dict) |
137 |
|
138 |
def main(self, uuid, *more_uuids): |
139 |
super(self.__class__, self)._run() |
140 |
self._run([uuid] + list(more_uuids)) |
141 |
|
142 |
|
143 |
@command(snfastakos_cmds)
|
144 |
class astakos_user_uuid(_astakos_init, _optional_json): |
145 |
"""Get uuid(s) from username(s)"""
|
146 |
|
147 |
@errors.generic.all
|
148 |
@astakoserror
|
149 |
def _run(self, usernames): |
150 |
assert usernames and isinstance(usernames, list), 'No valid usernames' |
151 |
if 1 == len(usernames): |
152 |
self._print(self.client.get_uuid(self.token, usernames[0])) |
153 |
else:
|
154 |
self._print(
|
155 |
self.client.get_uuids(self.token, usernames), print_dict) |
156 |
|
157 |
def main(self, usernames, *more_usernames): |
158 |
super(self.__class__, self)._run() |
159 |
self._run([usernames] + list(more_usernames)) |
160 |
|
161 |
|
162 |
@command(snfastakos_cmds)
|
163 |
class astakos_quotas(_astakos_init, _optional_json): |
164 |
"""Get user (or service) quotas"""
|
165 |
|
166 |
@staticmethod
|
167 |
def _print_with_format(d): |
168 |
""" Print d with size formating when needed
|
169 |
:param d: (dict) {system: {<service>: {usage: ..., limit: ..., }, ...}}
|
170 |
"""
|
171 |
newd = dict()
|
172 |
for k, service in d['system'].items(): |
173 |
newd[k] = dict(service)
|
174 |
for term in ('usage', 'limit'): |
175 |
if term in service: |
176 |
newd[k][term] = format_size(service[term]) |
177 |
print_dict(newd) |
178 |
|
179 |
@errors.generic.all
|
180 |
@astakoserror
|
181 |
def _run(self): |
182 |
self._print(
|
183 |
self.client.get_quotas(self.token), self._print_with_format) |
184 |
|
185 |
def main(self): |
186 |
super(self.__class__, self)._run() |
187 |
self._run()
|
188 |
|
189 |
|
190 |
@command(snfastakos_cmds)
|
191 |
class astakos_services(_astakos_init): |
192 |
"""Astakos operations filtered by services"""
|
193 |
|
194 |
|
195 |
@command(snfastakos_cmds)
|
196 |
class astakos_services_list(_astakos_init, _optional_json): |
197 |
"""List available services"""
|
198 |
|
199 |
@errors.generic.all
|
200 |
@astakoserror
|
201 |
def _run(self): |
202 |
self._print(self.client.get_services()) |
203 |
|
204 |
def main(self): |
205 |
super(self.__class__, self)._run() |
206 |
self._run()
|
207 |
|
208 |
|
209 |
@command(snfastakos_cmds)
|
210 |
class astakos_services_username(_astakos_init, _optional_json): |
211 |
"""Get service username(s) from uuid(s)"""
|
212 |
|
213 |
@errors.generic.all
|
214 |
@astakoserror
|
215 |
def _run(self, stoken, uuids): |
216 |
assert uuids and isinstance(uuids, list), 'No valid uuids' |
217 |
if 1 == len(uuids): |
218 |
self._print(self.client.service_get_username(stoken, uuids[0])) |
219 |
else:
|
220 |
self._print(
|
221 |
self.client.service_get_usernames(stoken, uuids), print_dict)
|
222 |
|
223 |
def main(self, service_token, uuid, *more_uuids): |
224 |
super(self.__class__, self)._run() |
225 |
self._run(service_token, [uuid] + list(more_uuids)) |
226 |
|
227 |
|
228 |
@command(snfastakos_cmds)
|
229 |
class astakos_services_uuid(_astakos_init, _optional_json): |
230 |
"""Get service uuid(s) from username(s)"""
|
231 |
|
232 |
@errors.generic.all
|
233 |
@astakoserror
|
234 |
def _run(self, stoken, usernames): |
235 |
assert usernames and isinstance(usernames, list), 'No valid usernames' |
236 |
if 1 == len(usernames): |
237 |
self._print(self.client.service_get_uuid(self.token, usernames[0])) |
238 |
else:
|
239 |
self._print(
|
240 |
self.client.service_get_uuids(self.token, usernames), |
241 |
print_dict) |
242 |
|
243 |
def main(self, service_token, usernames, *more_usernames): |
244 |
super(self.__class__, self)._run() |
245 |
self._run(service_token, [usernames] + list(more_usernames)) |
246 |
|
247 |
|
248 |
@command(snfastakos_cmds)
|
249 |
class astakos_services_quotas(_astakos_init, _optional_json): |
250 |
"""Get user (or service) quotas"""
|
251 |
|
252 |
arguments = dict(
|
253 |
uuid=ValueArgument('A user unique id to get quotas for', '--uuid') |
254 |
) |
255 |
|
256 |
@errors.generic.all
|
257 |
@astakoserror
|
258 |
def _run(self, stoken): |
259 |
self._print(self.client.service_get_quotas(stoken, self['uuid'])) |
260 |
|
261 |
def main(self, service_token): |
262 |
super(self.__class__, self)._run() |
263 |
self._run(service_token)
|
264 |
|
265 |
# resources
|
266 |
# feedback
|
267 |
# endpoints
|
268 |
|
269 |
# XXX issue_commission, issue_one_commission
|
270 |
# get_pending_commissions
|
271 |
# get_commission_info
|
272 |
# commission_action
|
273 |
# accept_commission
|
274 |
# reject_commission
|
275 |
# resolve_commissions
|
276 |
|
277 |
|
278 |
@command(snfastakos_cmds)
|
279 |
class astakos_resources(_astakos_init, _optional_json): |
280 |
"""List user resources"""
|
281 |
|
282 |
@errors.generic.all
|
283 |
@astakoserror
|
284 |
def _run(self): |
285 |
self._print(self.client.get_resources(), print_dict) |
286 |
|
287 |
def main(self): |
288 |
super(self.__class__, self)._run() |
289 |
self._run()
|
290 |
|
291 |
|
292 |
@command(snfastakos_cmds)
|
293 |
class astakos_feedback(_astakos_init): |
294 |
"""Send feedback to astakos server"""
|
295 |
|
296 |
arguments = dict(
|
297 |
token=ValueArgument('Use this token instead', '--token') |
298 |
) |
299 |
|
300 |
@errors.generic.all
|
301 |
@astakoserror
|
302 |
def _run(self, msg, more_info=None): |
303 |
self.client.send_feedback(
|
304 |
self['token'] or self.token, msg, more_info or '') |
305 |
|
306 |
def main(self, message, more_info=None): |
307 |
super(self.__class__, self)._run() |
308 |
self._run(message, more_info)
|
309 |
|
310 |
|
311 |
@command(snfastakos_cmds)
|
312 |
class astakos_endpoints(_astakos_init, _optional_json): |
313 |
"""Get endpoints service endpoints"""
|
314 |
|
315 |
arguments = dict(
|
316 |
token=ValueArgument('Use this token instead', '--token'), |
317 |
uuid=ValueArgument('User uuid', '--uuid') |
318 |
) |
319 |
|
320 |
@errors.generic.all
|
321 |
@astakoserror
|
322 |
def _run(self): |
323 |
self._print(
|
324 |
self.client.get_endpoints(
|
325 |
self['token'] or self.token, self['uuid']), |
326 |
print_dict) |
327 |
|
328 |
def main(self): |
329 |
super(self.__class__, self)._run() |
330 |
self._run()
|
331 |
|
332 |
|
333 |
@command(snfastakos_cmds)
|
334 |
class astakos_test(_astakos_init): |
335 |
"""Test an astakos command"""
|
336 |
|
337 |
@errors.generic.all
|
338 |
@astakoserror
|
339 |
def _run(self, *args): |
340 |
pass
|
341 |
|
342 |
def main(self, *args): |
343 |
super(self.__class__, self)._run() |
344 |
self._run(*args)
|