Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / astakos.py @ 7806f19d

History | View | Annotate | Download (6.2 kB)

1 e3f01d64 Stavros Sachtouris
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2 7493ccb6 Stavros Sachtouris
#
3 7493ccb6 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 7493ccb6 Stavros Sachtouris
# without modification, are permitted provided that the following
5 7493ccb6 Stavros Sachtouris
# conditions are met:
6 7493ccb6 Stavros Sachtouris
#
7 7493ccb6 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 7493ccb6 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 7493ccb6 Stavros Sachtouris
#      disclaimer.
10 7493ccb6 Stavros Sachtouris
#
11 7493ccb6 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 7493ccb6 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 7493ccb6 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 7493ccb6 Stavros Sachtouris
#      provided with the distribution.
15 7493ccb6 Stavros Sachtouris
#
16 7493ccb6 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 7493ccb6 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 7493ccb6 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 7493ccb6 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 7493ccb6 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 7493ccb6 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 7493ccb6 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 7493ccb6 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 7493ccb6 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 7493ccb6 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 7493ccb6 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 7493ccb6 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 7493ccb6 Stavros Sachtouris
#
29 7493ccb6 Stavros Sachtouris
# The views and conclusions contained in the software and
30 7493ccb6 Stavros Sachtouris
# documentation are those of the authors and should not be
31 7493ccb6 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 7493ccb6 Stavros Sachtouris
# or implied, of GRNET S.A.command
33 7493ccb6 Stavros Sachtouris
34 c2be5c96 Stavros Sachtouris
from kamaki.cli import command
35 8cec3671 Stavros Sachtouris
from kamaki.clients.astakos import AstakosClient
36 b4f69041 Stavros Sachtouris
from kamaki.cli.commands import (
37 b4f69041 Stavros Sachtouris
    _command_init, errors, _optional_json, addLogSettings)
38 be99b6ad Stavros Sachtouris
from kamaki.cli.command_tree import CommandTree
39 fe4940bc Stavros Sachtouris
from kamaki.cli.errors import CLIBaseUrlError, CLIError
40 fe4940bc Stavros Sachtouris
from kamaki.cli.utils import print_dict, ask_user
41 7493ccb6 Stavros Sachtouris
42 4018326d Stavros Sachtouris
user_cmds = CommandTree('user', 'Astakos API commands')
43 4018326d Stavros Sachtouris
_commands = [user_cmds]
44 234954d1 Stavros Sachtouris
45 234954d1 Stavros Sachtouris
46 4018326d Stavros Sachtouris
class _user_init(_command_init):
47 36526b3c Stavros Sachtouris
48 dc897a7e Stavros Sachtouris
    def _write_main_token(self, token):
49 dc897a7e Stavros Sachtouris
        tokens = self.config.get_cloud(self.cloud, 'token').split()
50 dc897a7e Stavros Sachtouris
        if token in tokens:
51 dc897a7e Stavros Sachtouris
            tokens.remove(token)
52 dc897a7e Stavros Sachtouris
        tokens.insert(0, token)
53 dc897a7e Stavros Sachtouris
        self.config.set_cloud(self.cloud, 'token', ' '.join(tokens))
54 dc897a7e Stavros Sachtouris
        self.config.write()
55 dc897a7e Stavros Sachtouris
56 a03ade9e Stavros Sachtouris
    @errors.generic.all
57 4018326d Stavros Sachtouris
    @errors.user.load
58 b4f69041 Stavros Sachtouris
    @addLogSettings
59 436bd910 Stavros Sachtouris
    def _run(self):
60 b4f69041 Stavros Sachtouris
        if getattr(self, 'cloud', False):
61 b4f69041 Stavros Sachtouris
            base_url = self._custom_url('astakos')
62 b4f69041 Stavros Sachtouris
            if base_url:
63 b4f69041 Stavros Sachtouris
                token = self._custom_token('astakos')\
64 144b3551 Stavros Sachtouris
                    or self.config.get_cloud(self.cloud, 'token')
65 dc897a7e Stavros Sachtouris
                token = token.split()[0] if ' ' in token else token
66 b4f69041 Stavros Sachtouris
                self.client = AstakosClient(base_url=base_url, token=token)
67 b4f69041 Stavros Sachtouris
                return
68 b4f69041 Stavros Sachtouris
        else:
69 b4f69041 Stavros Sachtouris
            self.cloud = 'default'
70 8cec3671 Stavros Sachtouris
        if getattr(self, 'auth_base', False):
71 8cec3671 Stavros Sachtouris
            self.client = self.auth_base
72 b4f69041 Stavros Sachtouris
            return
73 b4f69041 Stavros Sachtouris
        raise CLIBaseUrlError(service='astakos')
74 7493ccb6 Stavros Sachtouris
75 436bd910 Stavros Sachtouris
    def main(self):
76 5fdccdec Stavros Sachtouris
        self._run()
77 436bd910 Stavros Sachtouris
78 234954d1 Stavros Sachtouris
79 4018326d Stavros Sachtouris
@command(user_cmds)
80 545c6c29 Stavros Sachtouris
class user_authenticate(_user_init, _optional_json):
81 0663d8d6 Stavros Sachtouris
    """Authenticate a user
82 0663d8d6 Stavros Sachtouris
    Get user information (e.g. unique account name) from token
83 0663d8d6 Stavros Sachtouris
    Token should be set in settings:
84 023d7ab0 Stavros Sachtouris
    *  check if a token is set    /config whoami cloud.default.token
85 144b3551 Stavros Sachtouris
    *  permanently set a token    /config set cloud.default.token <token>
86 0663d8d6 Stavros Sachtouris
    Token can also be provided as a parameter
87 144b3551 Stavros Sachtouris
    (In case of another named cloud, use its name instead of default)
88 0663d8d6 Stavros Sachtouris
    """
89 7493ccb6 Stavros Sachtouris
90 f5c28bfa Stavros Sachtouris
    @staticmethod
91 f5c28bfa Stavros Sachtouris
    def _print_access(r):
92 f5c28bfa Stavros Sachtouris
        print_dict(r['access'])
93 f5c28bfa Stavros Sachtouris
94 a03ade9e Stavros Sachtouris
    @errors.generic.all
95 4018326d Stavros Sachtouris
    @errors.user.authenticate
96 436bd910 Stavros Sachtouris
    def _run(self, custom_token=None):
97 a7533bf3 Stavros Sachtouris
        token_bu = self.client.token
98 a7533bf3 Stavros Sachtouris
        try:
99 a7533bf3 Stavros Sachtouris
            r = self.client.authenticate(custom_token)
100 fe4940bc Stavros Sachtouris
            if (token_bu != self.client.token and
101 fe4940bc Stavros Sachtouris
                    ask_user('Permanently save token as cloud.%s.token ?' % (
102 fe4940bc Stavros Sachtouris
                        self.cloud))):
103 dc897a7e Stavros Sachtouris
                self._write_main_token(self.client.token)
104 a7533bf3 Stavros Sachtouris
        except Exception:
105 a7533bf3 Stavros Sachtouris
            #recover old token
106 a7533bf3 Stavros Sachtouris
            self.client.token = token_bu
107 a7533bf3 Stavros Sachtouris
            raise
108 f5c28bfa Stavros Sachtouris
        self._print(r, self._print_access)
109 436bd910 Stavros Sachtouris
110 436bd910 Stavros Sachtouris
    def main(self, custom_token=None):
111 9a8861d1 Stavros Sachtouris
        super(self.__class__, self)._run()
112 436bd910 Stavros Sachtouris
        self._run(custom_token)
113 9a8861d1 Stavros Sachtouris
114 9a8861d1 Stavros Sachtouris
115 9a8861d1 Stavros Sachtouris
@command(user_cmds)
116 9a8861d1 Stavros Sachtouris
class user_list(_user_init, _optional_json):
117 258a45f3 Stavros Sachtouris
    """List all authenticated users"""
118 9a8861d1 Stavros Sachtouris
119 9a8861d1 Stavros Sachtouris
    @errors.generic.all
120 9a8861d1 Stavros Sachtouris
    def _run(self, custom_token=None):
121 9a8861d1 Stavros Sachtouris
        self._print(self.client.list_users())
122 9a8861d1 Stavros Sachtouris
123 9a8861d1 Stavros Sachtouris
    def main(self):
124 9a8861d1 Stavros Sachtouris
        super(self.__class__, self)._run()
125 9a8861d1 Stavros Sachtouris
        self._run()
126 b91111b9 Stavros Sachtouris
127 b91111b9 Stavros Sachtouris
128 b91111b9 Stavros Sachtouris
@command(user_cmds)
129 49fac78e Stavros Sachtouris
class user_whoami(_user_init, _optional_json):
130 49fac78e Stavros Sachtouris
    """Get current session user information"""
131 b91111b9 Stavros Sachtouris
132 b91111b9 Stavros Sachtouris
    @errors.generic.all
133 b91111b9 Stavros Sachtouris
    def _run(self):
134 fe4940bc Stavros Sachtouris
        self._print(self.client.user_info(), print_dict)
135 b91111b9 Stavros Sachtouris
136 b91111b9 Stavros Sachtouris
    def main(self):
137 b91111b9 Stavros Sachtouris
        super(self.__class__, self)._run()
138 b91111b9 Stavros Sachtouris
        self._run()
139 fe4940bc Stavros Sachtouris
140 fe4940bc Stavros Sachtouris
141 fe4940bc Stavros Sachtouris
@command(user_cmds)
142 fe4940bc Stavros Sachtouris
class user_set(_user_init, _optional_json):
143 fe4940bc Stavros Sachtouris
    """Set session user by id
144 fe4940bc Stavros Sachtouris
    To enrich your options, authenticate more users:
145 fe4940bc Stavros Sachtouris
    /user authenticate <other user token>
146 fe4940bc Stavros Sachtouris
    To list authenticated users
147 fe4940bc Stavros Sachtouris
    /user list
148 fe4940bc Stavros Sachtouris
    To get the current session user
149 023d7ab0 Stavros Sachtouris
    /user whoami
150 fe4940bc Stavros Sachtouris
    """
151 fe4940bc Stavros Sachtouris
152 fe4940bc Stavros Sachtouris
    @errors.generic.all
153 fe4940bc Stavros Sachtouris
    def _run(self, uuid):
154 fe4940bc Stavros Sachtouris
        for user in self.client.list_users():
155 fe4940bc Stavros Sachtouris
            if user.get('id', None) in (uuid,):
156 fe4940bc Stavros Sachtouris
                ntoken = user['auth_token']
157 fe4940bc Stavros Sachtouris
                if ntoken == self.client.token:
158 fe4940bc Stavros Sachtouris
                    print('%s (%s) is already the session user' % (
159 fe4940bc Stavros Sachtouris
                        self.client.user_term('name'),
160 fe4940bc Stavros Sachtouris
                        self.client.user_term('id')))
161 fe4940bc Stavros Sachtouris
                    return
162 fe4940bc Stavros Sachtouris
                self.client.token = user['auth_token']
163 fe4940bc Stavros Sachtouris
                print('Session user set to %s (%s)' % (
164 fe4940bc Stavros Sachtouris
                        self.client.user_term('name'),
165 fe4940bc Stavros Sachtouris
                        self.client.user_term('id')))
166 dc897a7e Stavros Sachtouris
                if ask_user('Permanently make %s the main user?' % (
167 dc897a7e Stavros Sachtouris
                        self.client.user_term('name'))):
168 dc897a7e Stavros Sachtouris
                    self._write_main_token(self.client.token)
169 fe4940bc Stavros Sachtouris
                return
170 fe4940bc Stavros Sachtouris
        raise CLIError(
171 fe4940bc Stavros Sachtouris
            'User with UUID %s not authenticated in current session' % uuid,
172 fe4940bc Stavros Sachtouris
            details=[
173 fe4940bc Stavros Sachtouris
                'To authenticate a user', '  /user authenticate <t0k3n>'])
174 fe4940bc Stavros Sachtouris
175 fe4940bc Stavros Sachtouris
    def main(self, uuid):
176 fe4940bc Stavros Sachtouris
        super(self.__class__, self)._run()
177 fe4940bc Stavros Sachtouris
        self._run(uuid)