Revision fe4940bc
b/kamaki/cli/commands/astakos.py | ||
---|---|---|
32 | 32 |
# or implied, of GRNET S.A.command |
33 | 33 |
|
34 | 34 |
from kamaki.cli import command |
35 |
from kamaki.cli.argument import ValueArgument |
|
36 | 35 |
from kamaki.clients.astakos import AstakosClient |
37 | 36 |
from kamaki.cli.commands import ( |
38 | 37 |
_command_init, errors, _optional_json, addLogSettings) |
39 | 38 |
from kamaki.cli.command_tree import CommandTree |
40 |
from kamaki.cli.errors import CLIBaseUrlError |
|
41 |
from kamaki.cli.utils import print_dict |
|
39 |
from kamaki.cli.errors import CLIBaseUrlError, CLIError
|
|
40 |
from kamaki.cli.utils import print_dict, ask_user
|
|
42 | 41 |
|
43 | 42 |
user_cmds = CommandTree('user', 'Astakos API commands') |
44 | 43 |
_commands = [user_cmds] |
... | ... | |
89 | 88 |
token_bu = self.client.token |
90 | 89 |
try: |
91 | 90 |
r = self.client.authenticate(custom_token) |
91 |
if (token_bu != self.client.token and |
|
92 |
ask_user('Permanently save token as cloud.%s.token ?' % ( |
|
93 |
self.cloud))): |
|
94 |
self.config.set_cloud( |
|
95 |
self.cloud, 'token', self.client.token) |
|
96 |
self.config.write() |
|
92 | 97 |
except Exception: |
93 | 98 |
#recover old token |
94 | 99 |
self.client.token = token_bu |
... | ... | |
114 | 119 |
|
115 | 120 |
|
116 | 121 |
@command(user_cmds) |
117 |
class user_info(_user_init, _optional_json): |
|
118 |
"""Get info for current or selected user""" |
|
119 |
|
|
120 |
arguments = dict( |
|
121 |
token=ValueArgument('Use this instead of current token', ('--token')) |
|
122 |
) |
|
122 |
class user_get(_user_init, _optional_json): |
|
123 |
"""Get session user""" |
|
123 | 124 |
|
124 | 125 |
@errors.generic.all |
125 | 126 |
def _run(self): |
126 |
self._print(self.client.user_info(self['token']), print_dict)
|
|
127 |
self._print(self.client.user_info(), print_dict) |
|
127 | 128 |
|
128 | 129 |
def main(self): |
129 | 130 |
super(self.__class__, self)._run() |
130 | 131 |
self._run() |
132 |
|
|
133 |
|
|
134 |
@command(user_cmds) |
|
135 |
class user_set(_user_init, _optional_json): |
|
136 |
"""Set session user by id |
|
137 |
To enrich your options, authenticate more users: |
|
138 |
/user authenticate <other user token> |
|
139 |
To list authenticated users |
|
140 |
/user list |
|
141 |
To get the current session user |
|
142 |
/user get |
|
143 |
""" |
|
144 |
|
|
145 |
@errors.generic.all |
|
146 |
def _run(self, uuid): |
|
147 |
for user in self.client.list_users(): |
|
148 |
if user.get('id', None) in (uuid,): |
|
149 |
ntoken = user['auth_token'] |
|
150 |
if ntoken == self.client.token: |
|
151 |
print('%s (%s) is already the session user' % ( |
|
152 |
self.client.user_term('name'), |
|
153 |
self.client.user_term('id'))) |
|
154 |
return |
|
155 |
self.client.token = user['auth_token'] |
|
156 |
print('Session user set to %s (%s)' % ( |
|
157 |
self.client.user_term('name'), |
|
158 |
self.client.user_term('id'))) |
|
159 |
return |
|
160 |
raise CLIError( |
|
161 |
'User with UUID %s not authenticated in current session' % uuid, |
|
162 |
details=[ |
|
163 |
'To authenticate a user', ' /user authenticate <t0k3n>']) |
|
164 |
|
|
165 |
def main(self, uuid): |
|
166 |
super(self.__class__, self)._run() |
|
167 |
self._run(uuid) |
Also available in: Unified diff