Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / snf-astakos.py @ 60c42f9f

History | View | Annotate | Download (7.4 kB)

1 97fc1e06 Stavros Sachtouris
# Copyright 2013 GRNET S.A. All rights reserved.
2 97fc1e06 Stavros Sachtouris
#
3 97fc1e06 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 97fc1e06 Stavros Sachtouris
# without modification, are permitted provided that the following
5 97fc1e06 Stavros Sachtouris
# conditions are met:
6 97fc1e06 Stavros Sachtouris
#
7 97fc1e06 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 97fc1e06 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 97fc1e06 Stavros Sachtouris
#      disclaimer.
10 97fc1e06 Stavros Sachtouris
#
11 97fc1e06 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 97fc1e06 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 97fc1e06 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 97fc1e06 Stavros Sachtouris
#      provided with the distribution.
15 97fc1e06 Stavros Sachtouris
#
16 97fc1e06 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 97fc1e06 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 97fc1e06 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 97fc1e06 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 97fc1e06 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 97fc1e06 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 97fc1e06 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 97fc1e06 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 97fc1e06 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 97fc1e06 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 97fc1e06 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 97fc1e06 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 97fc1e06 Stavros Sachtouris
#
29 97fc1e06 Stavros Sachtouris
# The views and conclusions contained in the software and
30 97fc1e06 Stavros Sachtouris
# documentation are those of the authors and should not be
31 97fc1e06 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 97fc1e06 Stavros Sachtouris
# or implied, of GRNET S.A.command
33 97fc1e06 Stavros Sachtouris
34 97fc1e06 Stavros Sachtouris
from astakosclient import AstakosClient
35 97fc1e06 Stavros Sachtouris
36 97fc1e06 Stavros Sachtouris
from kamaki.cli import command
37 82e32e50 Stavros Sachtouris
from kamaki.cli.errors import CLIBaseUrlError
38 b4f69041 Stavros Sachtouris
from kamaki.cli.commands import (
39 b4f69041 Stavros Sachtouris
    _command_init, errors, _optional_json, addLogSettings)
40 97fc1e06 Stavros Sachtouris
from kamaki.cli.command_tree import CommandTree
41 97fc1e06 Stavros Sachtouris
from kamaki.cli.utils import print_dict
42 683335b1 Stavros Sachtouris
from kamaki.cli.argument import FlagArgument, ValueArgument
43 9b47150e Stavros Sachtouris
from kamaki.cli.logger import get_logger
44 97fc1e06 Stavros Sachtouris
45 97fc1e06 Stavros Sachtouris
snfastakos_cmds = CommandTree('astakos', 'astakosclient CLI')
46 97fc1e06 Stavros Sachtouris
_commands = [snfastakos_cmds]
47 97fc1e06 Stavros Sachtouris
48 97fc1e06 Stavros Sachtouris
49 09b2b38e Stavros Sachtouris
log = get_logger(__name__)
50 97fc1e06 Stavros Sachtouris
51 97fc1e06 Stavros Sachtouris
52 97fc1e06 Stavros Sachtouris
class _astakos_init(_command_init):
53 97fc1e06 Stavros Sachtouris
54 b4f69041 Stavros Sachtouris
    def __init__(self, arguments=dict(), auth_base=None, cloud=None):
55 b4f69041 Stavros Sachtouris
        super(_astakos_init, self).__init__(arguments, auth_base, cloud)
56 683335b1 Stavros Sachtouris
        self['token'] = ValueArgument('Custom token', '--token')
57 683335b1 Stavros Sachtouris
58 97fc1e06 Stavros Sachtouris
    @errors.generic.all
59 97fc1e06 Stavros Sachtouris
    #@errors.user.load
60 b4f69041 Stavros Sachtouris
    @addLogSettings
61 97fc1e06 Stavros Sachtouris
    def _run(self):
62 b4f69041 Stavros Sachtouris
        self.cloud = self.cloud if self.cloud else 'default'
63 b4f69041 Stavros Sachtouris
        self.token = self['token'] or self._custom_token('astakos')\
64 144b3551 Stavros Sachtouris
            or self.config.get_cloud(self.cloud, 'token')
65 82e32e50 Stavros Sachtouris
        if getattr(self, 'auth_base', False):
66 82e32e50 Stavros Sachtouris
            astakos_endpoints = self.auth_base.get_service_endpoints(
67 b4f69041 Stavros Sachtouris
                self._custom_type('astakos') or 'identity',
68 b4f69041 Stavros Sachtouris
                self._custom_version('astakos') or '')
69 82e32e50 Stavros Sachtouris
            base_url = astakos_endpoints['publicURL']
70 82e32e50 Stavros Sachtouris
        else:
71 b4f69041 Stavros Sachtouris
            base_url = self._custom_url('astakos')
72 82e32e50 Stavros Sachtouris
        if not base_url:
73 82e32e50 Stavros Sachtouris
            raise CLIBaseUrlError(service='astakos')
74 97fc1e06 Stavros Sachtouris
        self.client = AstakosClient(base_url, logger=log)
75 97fc1e06 Stavros Sachtouris
76 97fc1e06 Stavros Sachtouris
    def main(self):
77 97fc1e06 Stavros Sachtouris
        self._run()
78 97fc1e06 Stavros Sachtouris
79 97fc1e06 Stavros Sachtouris
80 97fc1e06 Stavros Sachtouris
@command(snfastakos_cmds)
81 97fc1e06 Stavros Sachtouris
class astakos_authenticate(_astakos_init, _optional_json):
82 97fc1e06 Stavros Sachtouris
    """Authenticate a user
83 97fc1e06 Stavros Sachtouris
    Get user information (e.g. unique account name) from token
84 97fc1e06 Stavros Sachtouris
    Token should be set in settings:
85 144b3551 Stavros Sachtouris
    *  check if a token is set    /config get cloud.default.token
86 144b3551 Stavros Sachtouris
    *  permanently set a token    /config set cloud.default.token <token>
87 97fc1e06 Stavros Sachtouris
    Token can also be provided as a parameter
88 144b3551 Stavros Sachtouris
    (To use a named cloud, use its name instead of "default")
89 97fc1e06 Stavros Sachtouris
    """
90 97fc1e06 Stavros Sachtouris
91 97fc1e06 Stavros Sachtouris
    arguments = dict(
92 97fc1e06 Stavros Sachtouris
        usage=FlagArgument('also return usage information', ('--with-usage'))
93 97fc1e06 Stavros Sachtouris
    )
94 97fc1e06 Stavros Sachtouris
95 683335b1 Stavros Sachtouris
    def _run(self):
96 97fc1e06 Stavros Sachtouris
        self._print(
97 e1c18867 Stavros Sachtouris
            self.client.get_user_info(self.token, self['usage']), print_dict)
98 97fc1e06 Stavros Sachtouris
99 683335b1 Stavros Sachtouris
    def main(self):
100 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
101 683335b1 Stavros Sachtouris
        self._run()
102 683335b1 Stavros Sachtouris
103 683335b1 Stavros Sachtouris
104 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
105 683335b1 Stavros Sachtouris
class astakos_username(_astakos_init, _optional_json):
106 683335b1 Stavros Sachtouris
    """Get username(s) from uuid(s)"""
107 683335b1 Stavros Sachtouris
108 683335b1 Stavros Sachtouris
    arguments = dict(
109 683335b1 Stavros Sachtouris
        service_token=ValueArgument(
110 683335b1 Stavros Sachtouris
            'Use service token instead', '--service-token')
111 683335b1 Stavros Sachtouris
    )
112 683335b1 Stavros Sachtouris
113 683335b1 Stavros Sachtouris
    def _run(self, uuids):
114 683335b1 Stavros Sachtouris
        assert uuids and isinstance(uuids, list), 'No valid uuids'
115 683335b1 Stavros Sachtouris
        if 1 == len(uuids):
116 683335b1 Stavros Sachtouris
            self._print(self.client.get_username(self.token, uuids[0]))
117 683335b1 Stavros Sachtouris
        else:
118 683335b1 Stavros Sachtouris
            self._print(
119 683335b1 Stavros Sachtouris
                self.client.get_username(self.token, uuids), print_dict)
120 683335b1 Stavros Sachtouris
121 683335b1 Stavros Sachtouris
    def main(self, uuid, *more_uuids):
122 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
123 683335b1 Stavros Sachtouris
        self._run([uuid] + list(more_uuids))
124 683335b1 Stavros Sachtouris
125 683335b1 Stavros Sachtouris
126 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
127 683335b1 Stavros Sachtouris
class astakos_uuid(_astakos_init, _optional_json):
128 683335b1 Stavros Sachtouris
    """Get uuid(s) from username(s)"""
129 683335b1 Stavros Sachtouris
130 683335b1 Stavros Sachtouris
    def _run(self, usernames):
131 683335b1 Stavros Sachtouris
        assert usernames and isinstance(usernames, list), 'No valid usernames'
132 683335b1 Stavros Sachtouris
        if 1 == len(usernames):
133 683335b1 Stavros Sachtouris
            self._print(self.client.get_uuid(self.token, usernames[0]))
134 683335b1 Stavros Sachtouris
        else:
135 683335b1 Stavros Sachtouris
            self._print(
136 683335b1 Stavros Sachtouris
                self.client.get_uuids(self.token, usernames), print_dict)
137 683335b1 Stavros Sachtouris
138 683335b1 Stavros Sachtouris
    def main(self, usernames, *more_usernames):
139 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
140 683335b1 Stavros Sachtouris
        self._run([usernames] + list(more_usernames))
141 683335b1 Stavros Sachtouris
142 683335b1 Stavros Sachtouris
143 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
144 683335b1 Stavros Sachtouris
class astakos_quotas(_astakos_init, _optional_json):
145 683335b1 Stavros Sachtouris
    """Get user (or service) quotas"""
146 683335b1 Stavros Sachtouris
147 683335b1 Stavros Sachtouris
    def _run(self):
148 683335b1 Stavros Sachtouris
            self._print(self.client.get_quotas(self.token), print_dict)
149 683335b1 Stavros Sachtouris
150 683335b1 Stavros Sachtouris
    def main(self):
151 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
152 683335b1 Stavros Sachtouris
        self._run()
153 683335b1 Stavros Sachtouris
154 683335b1 Stavros Sachtouris
155 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
156 683335b1 Stavros Sachtouris
class astakos_services(_astakos_init):
157 683335b1 Stavros Sachtouris
    """Astakos operations filtered by services"""
158 683335b1 Stavros Sachtouris
159 683335b1 Stavros Sachtouris
160 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
161 e1c18867 Stavros Sachtouris
class astakos_services_list(_astakos_init, _optional_json):
162 683335b1 Stavros Sachtouris
    """List available services"""
163 683335b1 Stavros Sachtouris
164 683335b1 Stavros Sachtouris
    def _run(self):
165 683335b1 Stavros Sachtouris
        self._print(self.client.get_services())
166 683335b1 Stavros Sachtouris
167 683335b1 Stavros Sachtouris
    def main(self):
168 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
169 683335b1 Stavros Sachtouris
        self._run()
170 683335b1 Stavros Sachtouris
171 683335b1 Stavros Sachtouris
172 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
173 683335b1 Stavros Sachtouris
class astakos_services_username(_astakos_init, _optional_json):
174 683335b1 Stavros Sachtouris
    """Get service username(s) from uuid(s)"""
175 683335b1 Stavros Sachtouris
176 683335b1 Stavros Sachtouris
    def _run(self, stoken, uuids):
177 683335b1 Stavros Sachtouris
        assert uuids and isinstance(uuids, list), 'No valid uuids'
178 683335b1 Stavros Sachtouris
        if 1 == len(uuids):
179 683335b1 Stavros Sachtouris
            self._print(self.client.service_get_username(stoken, uuids[0]))
180 683335b1 Stavros Sachtouris
        else:
181 683335b1 Stavros Sachtouris
            self._print(
182 683335b1 Stavros Sachtouris
                self.client.service_get_usernames(stoken, uuids), print_dict)
183 683335b1 Stavros Sachtouris
184 683335b1 Stavros Sachtouris
    def main(self, service_token, uuid, *more_uuids):
185 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
186 683335b1 Stavros Sachtouris
        self._run(service_token, [uuid] + list(more_uuids))
187 683335b1 Stavros Sachtouris
188 683335b1 Stavros Sachtouris
189 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
190 683335b1 Stavros Sachtouris
class astakos_services_uuid(_astakos_init, _optional_json):
191 683335b1 Stavros Sachtouris
    """Get service uuid(s) from username(s)"""
192 683335b1 Stavros Sachtouris
193 683335b1 Stavros Sachtouris
    def _run(self, stoken, usernames):
194 683335b1 Stavros Sachtouris
        assert usernames and isinstance(usernames, list), 'No valid usernames'
195 683335b1 Stavros Sachtouris
        if 1 == len(usernames):
196 683335b1 Stavros Sachtouris
            self._print(self.client.service_get_uuid(self.token, usernames[0]))
197 683335b1 Stavros Sachtouris
        else:
198 683335b1 Stavros Sachtouris
            self._print(
199 683335b1 Stavros Sachtouris
                self.client.service_get_uuids(self.token, usernames),
200 683335b1 Stavros Sachtouris
                print_dict)
201 683335b1 Stavros Sachtouris
202 683335b1 Stavros Sachtouris
    def main(self, service_token, usernames, *more_usernames):
203 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
204 683335b1 Stavros Sachtouris
        self._run(service_token, [usernames] + list(more_usernames))
205 683335b1 Stavros Sachtouris
206 683335b1 Stavros Sachtouris
207 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
208 683335b1 Stavros Sachtouris
class astakos_services_quotas(_astakos_init, _optional_json):
209 683335b1 Stavros Sachtouris
    """Get user (or service) quotas"""
210 683335b1 Stavros Sachtouris
211 683335b1 Stavros Sachtouris
    arguments = dict(
212 683335b1 Stavros Sachtouris
        uuid=ValueArgument('A user unique id to get quotas for', '--uuid')
213 683335b1 Stavros Sachtouris
    )
214 683335b1 Stavros Sachtouris
215 683335b1 Stavros Sachtouris
    def _run(self, stoken):
216 683335b1 Stavros Sachtouris
        self._print(self.client.service_get_quotas(stoken, self['uuid']))
217 683335b1 Stavros Sachtouris
218 683335b1 Stavros Sachtouris
    def main(self, service_token):
219 97fc1e06 Stavros Sachtouris
        super(self.__class__, self)._run()
220 683335b1 Stavros Sachtouris
        self._run(service_token)