root / kamaki / cli / commands / astakos.py @ 16d7b9ff
History | View | Annotate | Download (6.2 kB)
1 |
# Copyright 2011-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 kamaki.cli import command |
35 |
from kamaki.clients.astakos import AstakosClient |
36 |
from kamaki.cli.commands import ( |
37 |
_command_init, errors, _optional_json, addLogSettings) |
38 |
from kamaki.cli.command_tree import CommandTree |
39 |
from kamaki.cli.errors import CLIBaseUrlError, CLIError |
40 |
|
41 |
user_cmds = CommandTree('user', 'Astakos API commands') |
42 |
_commands = [user_cmds] |
43 |
|
44 |
|
45 |
class _user_init(_command_init): |
46 |
|
47 |
def _write_main_token(self, token): |
48 |
tokens = self.config.get_cloud(self.cloud, 'token').split() |
49 |
if token in tokens: |
50 |
tokens.remove(token) |
51 |
tokens.insert(0, token)
|
52 |
self.config.set_cloud(self.cloud, 'token', ' '.join(tokens)) |
53 |
self.config.write()
|
54 |
|
55 |
@errors.generic.all
|
56 |
@errors.user.load
|
57 |
@addLogSettings
|
58 |
def _run(self): |
59 |
if getattr(self, 'cloud', False): |
60 |
base_url = self._custom_url('astakos') |
61 |
if base_url:
|
62 |
token = self._custom_token(
|
63 |
'astakos') or self.config.get_cloud(self.cloud, 'token') |
64 |
token = token.split()[0] if ' ' in token else token |
65 |
self.client = AstakosClient(base_url=base_url, token=token)
|
66 |
return
|
67 |
else:
|
68 |
self.cloud = 'default' |
69 |
if getattr(self, 'auth_base', False): |
70 |
self.client = self.auth_base |
71 |
return
|
72 |
raise CLIBaseUrlError(service='astakos') |
73 |
|
74 |
def main(self): |
75 |
self._run()
|
76 |
|
77 |
|
78 |
@command(user_cmds)
|
79 |
class user_authenticate(_user_init, _optional_json): |
80 |
"""Authenticate a user
|
81 |
Get user information (e.g., unique account name) from token
|
82 |
Token should be set in settings:
|
83 |
* check if a token is set /config whoami cloud.default.token
|
84 |
* permanently set a token /config set cloud.default.token <token>
|
85 |
Token can also be provided as a parameter
|
86 |
(In case of another named cloud, use its name instead of default)
|
87 |
"""
|
88 |
|
89 |
@errors.generic.all
|
90 |
@errors.user.authenticate
|
91 |
def _run(self, custom_token=None): |
92 |
token_bu = self.client.token
|
93 |
try:
|
94 |
r = self.client.authenticate(custom_token)
|
95 |
if (token_bu != self.client.token and self.ask_user( |
96 |
'Permanently save token as cloud.%s.token ?' % (
|
97 |
self.cloud))):
|
98 |
self._write_main_token(self.client.token) |
99 |
except Exception: |
100 |
#recover old token
|
101 |
self.client.token = token_bu
|
102 |
raise
|
103 |
|
104 |
def _print_access(r, out): |
105 |
self.print_dict(r['access'], out=out) |
106 |
|
107 |
self._print(r, _print_access)
|
108 |
|
109 |
def main(self, custom_token=None): |
110 |
super(self.__class__, self)._run() |
111 |
self._run(custom_token)
|
112 |
|
113 |
|
114 |
@command(user_cmds)
|
115 |
class user_list(_user_init, _optional_json): |
116 |
"""List all authenticated users"""
|
117 |
|
118 |
@errors.generic.all
|
119 |
def _run(self, custom_token=None): |
120 |
self._print(self.client.list_users()) |
121 |
|
122 |
def main(self): |
123 |
super(self.__class__, self)._run() |
124 |
self._run()
|
125 |
|
126 |
|
127 |
@command(user_cmds)
|
128 |
class user_whoami(_user_init, _optional_json): |
129 |
"""Get current session user information"""
|
130 |
|
131 |
@errors.generic.all
|
132 |
def _run(self): |
133 |
self._print(self.client.user_info(), self.print_dict) |
134 |
|
135 |
def main(self): |
136 |
super(self.__class__, self)._run() |
137 |
self._run()
|
138 |
|
139 |
|
140 |
@command(user_cmds)
|
141 |
class user_set(_user_init, _optional_json): |
142 |
"""Set session user by id
|
143 |
To enrich your options, authenticate more users:
|
144 |
/user authenticate <other user token>
|
145 |
To list authenticated users
|
146 |
/user list
|
147 |
To get the current session user
|
148 |
/user whoami
|
149 |
"""
|
150 |
|
151 |
@errors.generic.all
|
152 |
def _run(self, uuid): |
153 |
for user in self.client.list_users(): |
154 |
if user.get('id', None) in (uuid,): |
155 |
ntoken = user['auth_token']
|
156 |
if ntoken == self.client.token: |
157 |
self.error('%s (%s) is already the session user' % ( |
158 |
self.client.user_term('name'), |
159 |
self.client.user_term('id'))) |
160 |
return
|
161 |
self.client.token = user['auth_token'] |
162 |
self.error('Session user set to %s (%s)' % ( |
163 |
self.client.user_term('name'), |
164 |
self.client.user_term('id'))) |
165 |
if self.ask_user( |
166 |
'Permanently make %s the main user?' % (
|
167 |
self.client.user_term('name'))): |
168 |
self._write_main_token(self.client.token) |
169 |
return
|
170 |
raise CLIError(
|
171 |
'User with UUID %s not authenticated in current session' % uuid,
|
172 |
details=[ |
173 |
'To authenticate a user', ' /user authenticate <t0k3n>']) |
174 |
|
175 |
def main(self, uuid): |
176 |
super(self.__class__, self)._run() |
177 |
self._run(uuid)
|