Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / astakos.py @ b91111b9

History | View | Annotate | Download (4.3 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 b91111b9 Stavros Sachtouris
from kamaki.cli.argument import ValueArgument
36 8cec3671 Stavros Sachtouris
from kamaki.clients.astakos import AstakosClient
37 b4f69041 Stavros Sachtouris
from kamaki.cli.commands import (
38 b4f69041 Stavros Sachtouris
    _command_init, errors, _optional_json, addLogSettings)
39 be99b6ad Stavros Sachtouris
from kamaki.cli.command_tree import CommandTree
40 8cec3671 Stavros Sachtouris
from kamaki.cli.errors import CLIBaseUrlError
41 f5c28bfa Stavros Sachtouris
from kamaki.cli.utils import print_dict
42 7493ccb6 Stavros Sachtouris
43 4018326d Stavros Sachtouris
user_cmds = CommandTree('user', 'Astakos API commands')
44 4018326d Stavros Sachtouris
_commands = [user_cmds]
45 234954d1 Stavros Sachtouris
46 234954d1 Stavros Sachtouris
47 4018326d Stavros Sachtouris
class _user_init(_command_init):
48 36526b3c Stavros Sachtouris
49 a03ade9e Stavros Sachtouris
    @errors.generic.all
50 4018326d Stavros Sachtouris
    @errors.user.load
51 b4f69041 Stavros Sachtouris
    @addLogSettings
52 436bd910 Stavros Sachtouris
    def _run(self):
53 b4f69041 Stavros Sachtouris
        if getattr(self, 'cloud', False):
54 b4f69041 Stavros Sachtouris
            base_url = self._custom_url('astakos')
55 b4f69041 Stavros Sachtouris
            if base_url:
56 b4f69041 Stavros Sachtouris
                token = self._custom_token('astakos')\
57 144b3551 Stavros Sachtouris
                    or self.config.get_cloud(self.cloud, 'token')
58 b4f69041 Stavros Sachtouris
                self.client = AstakosClient(base_url=base_url, token=token)
59 b4f69041 Stavros Sachtouris
                return
60 b4f69041 Stavros Sachtouris
        else:
61 b4f69041 Stavros Sachtouris
            self.cloud = 'default'
62 8cec3671 Stavros Sachtouris
        if getattr(self, 'auth_base', False):
63 8cec3671 Stavros Sachtouris
            self.client = self.auth_base
64 b4f69041 Stavros Sachtouris
            return
65 b4f69041 Stavros Sachtouris
        raise CLIBaseUrlError(service='astakos')
66 7493ccb6 Stavros Sachtouris
67 436bd910 Stavros Sachtouris
    def main(self):
68 5fdccdec Stavros Sachtouris
        self._run()
69 436bd910 Stavros Sachtouris
70 234954d1 Stavros Sachtouris
71 4018326d Stavros Sachtouris
@command(user_cmds)
72 545c6c29 Stavros Sachtouris
class user_authenticate(_user_init, _optional_json):
73 0663d8d6 Stavros Sachtouris
    """Authenticate a user
74 0663d8d6 Stavros Sachtouris
    Get user information (e.g. unique account name) from token
75 0663d8d6 Stavros Sachtouris
    Token should be set in settings:
76 144b3551 Stavros Sachtouris
    *  check if a token is set    /config get cloud.default.token
77 144b3551 Stavros Sachtouris
    *  permanently set a token    /config set cloud.default.token <token>
78 0663d8d6 Stavros Sachtouris
    Token can also be provided as a parameter
79 144b3551 Stavros Sachtouris
    (In case of another named cloud, use its name instead of default)
80 0663d8d6 Stavros Sachtouris
    """
81 7493ccb6 Stavros Sachtouris
82 f5c28bfa Stavros Sachtouris
    @staticmethod
83 f5c28bfa Stavros Sachtouris
    def _print_access(r):
84 f5c28bfa Stavros Sachtouris
        print_dict(r['access'])
85 f5c28bfa Stavros Sachtouris
86 a03ade9e Stavros Sachtouris
    @errors.generic.all
87 4018326d Stavros Sachtouris
    @errors.user.authenticate
88 436bd910 Stavros Sachtouris
    def _run(self, custom_token=None):
89 a7533bf3 Stavros Sachtouris
        token_bu = self.client.token
90 a7533bf3 Stavros Sachtouris
        try:
91 a7533bf3 Stavros Sachtouris
            r = self.client.authenticate(custom_token)
92 a7533bf3 Stavros Sachtouris
        except Exception:
93 a7533bf3 Stavros Sachtouris
            #recover old token
94 a7533bf3 Stavros Sachtouris
            self.client.token = token_bu
95 a7533bf3 Stavros Sachtouris
            raise
96 f5c28bfa Stavros Sachtouris
        self._print(r, self._print_access)
97 436bd910 Stavros Sachtouris
98 436bd910 Stavros Sachtouris
    def main(self, custom_token=None):
99 9a8861d1 Stavros Sachtouris
        super(self.__class__, self)._run()
100 436bd910 Stavros Sachtouris
        self._run(custom_token)
101 9a8861d1 Stavros Sachtouris
102 9a8861d1 Stavros Sachtouris
103 9a8861d1 Stavros Sachtouris
@command(user_cmds)
104 9a8861d1 Stavros Sachtouris
class user_list(_user_init, _optional_json):
105 9a8861d1 Stavros Sachtouris
    """Get service endpoints"""
106 9a8861d1 Stavros Sachtouris
107 9a8861d1 Stavros Sachtouris
    @errors.generic.all
108 9a8861d1 Stavros Sachtouris
    def _run(self, custom_token=None):
109 9a8861d1 Stavros Sachtouris
        self._print(self.client.list_users())
110 9a8861d1 Stavros Sachtouris
111 9a8861d1 Stavros Sachtouris
    def main(self):
112 9a8861d1 Stavros Sachtouris
        super(self.__class__, self)._run()
113 9a8861d1 Stavros Sachtouris
        self._run()
114 b91111b9 Stavros Sachtouris
115 b91111b9 Stavros Sachtouris
116 b91111b9 Stavros Sachtouris
@command(user_cmds)
117 b91111b9 Stavros Sachtouris
class user_info(_user_init, _optional_json):
118 b91111b9 Stavros Sachtouris
    """Get info for current or selected user"""
119 b91111b9 Stavros Sachtouris
120 b91111b9 Stavros Sachtouris
    arguments = dict(
121 b91111b9 Stavros Sachtouris
        token=ValueArgument('Use this  instead of current token', ('--token'))
122 b91111b9 Stavros Sachtouris
    )
123 b91111b9 Stavros Sachtouris
124 b91111b9 Stavros Sachtouris
    @errors.generic.all
125 b91111b9 Stavros Sachtouris
    def _run(self):
126 b91111b9 Stavros Sachtouris
        self._print(self.client.user_info(self['token']), print_dict)
127 b91111b9 Stavros Sachtouris
128 b91111b9 Stavros Sachtouris
    def main(self):
129 b91111b9 Stavros Sachtouris
        super(self.__class__, self)._run()
130 b91111b9 Stavros Sachtouris
        self._run()