31 |
31 |
# interpreted as representing official policies, either expressed
|
32 |
32 |
# or implied, of GRNET S.A.command
|
33 |
33 |
|
34 |
|
from kamaki.cli import command
|
35 |
|
from kamaki.clients.astakos import AstakosClient, ClientError
|
36 |
|
from kamaki.cli.utils import print_dict
|
|
34 |
from traceback import print_stack, print_exc
|
|
35 |
|
|
36 |
from kamaki.clients import ClientError
|
37 |
37 |
from kamaki.cli.errors import raiseCLIError
|
|
38 |
from kamaki.cli import command, _debug
|
|
39 |
from kamaki.clients.astakos import AstakosClient
|
|
40 |
from kamaki.cli.utils import print_dict
|
38 |
41 |
from kamaki.cli.commands import _command_init
|
39 |
42 |
from kamaki.cli.command_tree import CommandTree
|
40 |
43 |
|
... | ... | |
43 |
46 |
|
44 |
47 |
|
45 |
48 |
class _astakos_init(_command_init):
|
|
49 |
|
46 |
50 |
def main(self):
|
47 |
51 |
token = self.config.get('astakos', 'token')\
|
48 |
52 |
or self.config.get('global', 'token')
|
... | ... | |
52 |
56 |
raiseCLIError(None, 'Missing astakos server URL')
|
53 |
57 |
self.client = AstakosClient(base_url=base_url, token=token)
|
54 |
58 |
|
|
59 |
_token_details = [
|
|
60 |
'See if token is set: /config get token',
|
|
61 |
'If not, set a token:',
|
|
62 |
'* (permanent): /config set token <token>',
|
|
63 |
'* (temporary): re-run with <token> parameter']
|
|
64 |
|
|
65 |
def _raise(self, error):
|
|
66 |
if _debug:
|
|
67 |
print_stack()
|
|
68 |
print_exc(error)
|
|
69 |
if isinstance(error, ClientError) and error.status == 401:
|
|
70 |
raiseCLIError(error, details=self._token_details)
|
|
71 |
raiseCLIError(error)
|
|
72 |
|
55 |
73 |
|
56 |
74 |
@command(astakos_cmds)
|
57 |
75 |
class astakos_authenticate(_astakos_init):
|
... | ... | |
63 |
81 |
Token can also be provided as a parameter
|
64 |
82 |
"""
|
65 |
83 |
|
|
84 |
def _raise(self, error, some_token=None):
|
|
85 |
if isinstance(error, ClientError) and error.status == 401:
|
|
86 |
some_token = some_token if some_token else self.client.token
|
|
87 |
if some_token:
|
|
88 |
raiseCLIError(error,
|
|
89 |
'Authorization failed for token %s' % some_token,
|
|
90 |
details=self._token_details)
|
|
91 |
else:
|
|
92 |
raiseCLIError(error,
|
|
93 |
'No token provided',
|
|
94 |
details=self._token_details)
|
|
95 |
super(self.__class__, self)._raise(error)
|
|
96 |
|
66 |
97 |
def main(self, custom_token=None):
|
67 |
98 |
super(self.__class__, self).main()
|
68 |
99 |
try:
|
69 |
100 |
reply = self.client.authenticate(custom_token)
|
70 |
|
except ClientError as ce:
|
71 |
|
if (ce.status == 401):
|
72 |
|
raiseCLIError(ce,
|
73 |
|
details=['See if token is set: /config get token',
|
74 |
|
'If not, set a token:',
|
75 |
|
' 1.(permanent): /config set token <token>',
|
76 |
|
' 2.(temporary): rerun with <token> parameter'])
|
77 |
|
raiseCLIError(ce)
|
78 |
|
except Exception as err:
|
79 |
|
raiseCLIError(err)
|
|
101 |
except Exception as e:
|
|
102 |
self._raise(e)
|
80 |
103 |
print_dict(reply)
|