Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / snf-astakos.py @ 556e6916

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 97fc1e06 Stavros Sachtouris
class _astakos_init(_command_init):
50 97fc1e06 Stavros Sachtouris
51 b4f69041 Stavros Sachtouris
    def __init__(self, arguments=dict(), auth_base=None, cloud=None):
52 b4f69041 Stavros Sachtouris
        super(_astakos_init, self).__init__(arguments, auth_base, cloud)
53 683335b1 Stavros Sachtouris
        self['token'] = ValueArgument('Custom token', '--token')
54 683335b1 Stavros Sachtouris
55 97fc1e06 Stavros Sachtouris
    @errors.generic.all
56 97fc1e06 Stavros Sachtouris
    #@errors.user.load
57 b4f69041 Stavros Sachtouris
    @addLogSettings
58 97fc1e06 Stavros Sachtouris
    def _run(self):
59 b4f69041 Stavros Sachtouris
        self.cloud = self.cloud if self.cloud else 'default'
60 b4f69041 Stavros Sachtouris
        self.token = self['token'] or self._custom_token('astakos')\
61 144b3551 Stavros Sachtouris
            or self.config.get_cloud(self.cloud, 'token')
62 82e32e50 Stavros Sachtouris
        if getattr(self, 'auth_base', False):
63 82e32e50 Stavros Sachtouris
            astakos_endpoints = self.auth_base.get_service_endpoints(
64 b4f69041 Stavros Sachtouris
                self._custom_type('astakos') or 'identity',
65 b4f69041 Stavros Sachtouris
                self._custom_version('astakos') or '')
66 82e32e50 Stavros Sachtouris
            base_url = astakos_endpoints['publicURL']
67 82e32e50 Stavros Sachtouris
        else:
68 b4f69041 Stavros Sachtouris
            base_url = self._custom_url('astakos')
69 82e32e50 Stavros Sachtouris
        if not base_url:
70 82e32e50 Stavros Sachtouris
            raise CLIBaseUrlError(service='astakos')
71 556e6916 Stavros Sachtouris
        self.client = AstakosClient(
72 556e6916 Stavros Sachtouris
            base_url, logger=get_logger('kamaki.clients'))
73 97fc1e06 Stavros Sachtouris
74 97fc1e06 Stavros Sachtouris
    def main(self):
75 97fc1e06 Stavros Sachtouris
        self._run()
76 97fc1e06 Stavros Sachtouris
77 97fc1e06 Stavros Sachtouris
78 97fc1e06 Stavros Sachtouris
@command(snfastakos_cmds)
79 97fc1e06 Stavros Sachtouris
class astakos_authenticate(_astakos_init, _optional_json):
80 97fc1e06 Stavros Sachtouris
    """Authenticate a user
81 97fc1e06 Stavros Sachtouris
    Get user information (e.g. unique account name) from token
82 97fc1e06 Stavros Sachtouris
    Token should be set in settings:
83 144b3551 Stavros Sachtouris
    *  check if a token is set    /config get cloud.default.token
84 144b3551 Stavros Sachtouris
    *  permanently set a token    /config set cloud.default.token <token>
85 97fc1e06 Stavros Sachtouris
    Token can also be provided as a parameter
86 144b3551 Stavros Sachtouris
    (To use a named cloud, use its name instead of "default")
87 97fc1e06 Stavros Sachtouris
    """
88 97fc1e06 Stavros Sachtouris
89 97fc1e06 Stavros Sachtouris
    arguments = dict(
90 97fc1e06 Stavros Sachtouris
        usage=FlagArgument('also return usage information', ('--with-usage'))
91 97fc1e06 Stavros Sachtouris
    )
92 97fc1e06 Stavros Sachtouris
93 683335b1 Stavros Sachtouris
    def _run(self):
94 97fc1e06 Stavros Sachtouris
        self._print(
95 e1c18867 Stavros Sachtouris
            self.client.get_user_info(self.token, self['usage']), print_dict)
96 97fc1e06 Stavros Sachtouris
97 683335b1 Stavros Sachtouris
    def main(self):
98 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
99 683335b1 Stavros Sachtouris
        self._run()
100 683335b1 Stavros Sachtouris
101 683335b1 Stavros Sachtouris
102 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
103 683335b1 Stavros Sachtouris
class astakos_username(_astakos_init, _optional_json):
104 683335b1 Stavros Sachtouris
    """Get username(s) from uuid(s)"""
105 683335b1 Stavros Sachtouris
106 683335b1 Stavros Sachtouris
    arguments = dict(
107 683335b1 Stavros Sachtouris
        service_token=ValueArgument(
108 683335b1 Stavros Sachtouris
            'Use service token instead', '--service-token')
109 683335b1 Stavros Sachtouris
    )
110 683335b1 Stavros Sachtouris
111 683335b1 Stavros Sachtouris
    def _run(self, uuids):
112 683335b1 Stavros Sachtouris
        assert uuids and isinstance(uuids, list), 'No valid uuids'
113 683335b1 Stavros Sachtouris
        if 1 == len(uuids):
114 683335b1 Stavros Sachtouris
            self._print(self.client.get_username(self.token, uuids[0]))
115 683335b1 Stavros Sachtouris
        else:
116 683335b1 Stavros Sachtouris
            self._print(
117 683335b1 Stavros Sachtouris
                self.client.get_username(self.token, uuids), print_dict)
118 683335b1 Stavros Sachtouris
119 683335b1 Stavros Sachtouris
    def main(self, uuid, *more_uuids):
120 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
121 683335b1 Stavros Sachtouris
        self._run([uuid] + list(more_uuids))
122 683335b1 Stavros Sachtouris
123 683335b1 Stavros Sachtouris
124 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
125 683335b1 Stavros Sachtouris
class astakos_uuid(_astakos_init, _optional_json):
126 683335b1 Stavros Sachtouris
    """Get uuid(s) from username(s)"""
127 683335b1 Stavros Sachtouris
128 683335b1 Stavros Sachtouris
    def _run(self, usernames):
129 683335b1 Stavros Sachtouris
        assert usernames and isinstance(usernames, list), 'No valid usernames'
130 683335b1 Stavros Sachtouris
        if 1 == len(usernames):
131 683335b1 Stavros Sachtouris
            self._print(self.client.get_uuid(self.token, usernames[0]))
132 683335b1 Stavros Sachtouris
        else:
133 683335b1 Stavros Sachtouris
            self._print(
134 683335b1 Stavros Sachtouris
                self.client.get_uuids(self.token, usernames), print_dict)
135 683335b1 Stavros Sachtouris
136 683335b1 Stavros Sachtouris
    def main(self, usernames, *more_usernames):
137 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
138 683335b1 Stavros Sachtouris
        self._run([usernames] + list(more_usernames))
139 683335b1 Stavros Sachtouris
140 683335b1 Stavros Sachtouris
141 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
142 683335b1 Stavros Sachtouris
class astakos_quotas(_astakos_init, _optional_json):
143 683335b1 Stavros Sachtouris
    """Get user (or service) quotas"""
144 683335b1 Stavros Sachtouris
145 683335b1 Stavros Sachtouris
    def _run(self):
146 683335b1 Stavros Sachtouris
            self._print(self.client.get_quotas(self.token), print_dict)
147 683335b1 Stavros Sachtouris
148 683335b1 Stavros Sachtouris
    def main(self):
149 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
150 683335b1 Stavros Sachtouris
        self._run()
151 683335b1 Stavros Sachtouris
152 683335b1 Stavros Sachtouris
153 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
154 683335b1 Stavros Sachtouris
class astakos_services(_astakos_init):
155 683335b1 Stavros Sachtouris
    """Astakos operations filtered by services"""
156 683335b1 Stavros Sachtouris
157 683335b1 Stavros Sachtouris
158 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
159 e1c18867 Stavros Sachtouris
class astakos_services_list(_astakos_init, _optional_json):
160 683335b1 Stavros Sachtouris
    """List available services"""
161 683335b1 Stavros Sachtouris
162 683335b1 Stavros Sachtouris
    def _run(self):
163 683335b1 Stavros Sachtouris
        self._print(self.client.get_services())
164 683335b1 Stavros Sachtouris
165 683335b1 Stavros Sachtouris
    def main(self):
166 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
167 683335b1 Stavros Sachtouris
        self._run()
168 683335b1 Stavros Sachtouris
169 683335b1 Stavros Sachtouris
170 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
171 683335b1 Stavros Sachtouris
class astakos_services_username(_astakos_init, _optional_json):
172 683335b1 Stavros Sachtouris
    """Get service username(s) from uuid(s)"""
173 683335b1 Stavros Sachtouris
174 683335b1 Stavros Sachtouris
    def _run(self, stoken, uuids):
175 683335b1 Stavros Sachtouris
        assert uuids and isinstance(uuids, list), 'No valid uuids'
176 683335b1 Stavros Sachtouris
        if 1 == len(uuids):
177 683335b1 Stavros Sachtouris
            self._print(self.client.service_get_username(stoken, uuids[0]))
178 683335b1 Stavros Sachtouris
        else:
179 683335b1 Stavros Sachtouris
            self._print(
180 683335b1 Stavros Sachtouris
                self.client.service_get_usernames(stoken, uuids), print_dict)
181 683335b1 Stavros Sachtouris
182 683335b1 Stavros Sachtouris
    def main(self, service_token, uuid, *more_uuids):
183 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
184 683335b1 Stavros Sachtouris
        self._run(service_token, [uuid] + list(more_uuids))
185 683335b1 Stavros Sachtouris
186 683335b1 Stavros Sachtouris
187 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
188 683335b1 Stavros Sachtouris
class astakos_services_uuid(_astakos_init, _optional_json):
189 683335b1 Stavros Sachtouris
    """Get service uuid(s) from username(s)"""
190 683335b1 Stavros Sachtouris
191 683335b1 Stavros Sachtouris
    def _run(self, stoken, usernames):
192 683335b1 Stavros Sachtouris
        assert usernames and isinstance(usernames, list), 'No valid usernames'
193 683335b1 Stavros Sachtouris
        if 1 == len(usernames):
194 683335b1 Stavros Sachtouris
            self._print(self.client.service_get_uuid(self.token, usernames[0]))
195 683335b1 Stavros Sachtouris
        else:
196 683335b1 Stavros Sachtouris
            self._print(
197 683335b1 Stavros Sachtouris
                self.client.service_get_uuids(self.token, usernames),
198 683335b1 Stavros Sachtouris
                print_dict)
199 683335b1 Stavros Sachtouris
200 683335b1 Stavros Sachtouris
    def main(self, service_token, usernames, *more_usernames):
201 683335b1 Stavros Sachtouris
        super(self.__class__, self)._run()
202 683335b1 Stavros Sachtouris
        self._run(service_token, [usernames] + list(more_usernames))
203 683335b1 Stavros Sachtouris
204 683335b1 Stavros Sachtouris
205 683335b1 Stavros Sachtouris
@command(snfastakos_cmds)
206 683335b1 Stavros Sachtouris
class astakos_services_quotas(_astakos_init, _optional_json):
207 683335b1 Stavros Sachtouris
    """Get user (or service) quotas"""
208 683335b1 Stavros Sachtouris
209 683335b1 Stavros Sachtouris
    arguments = dict(
210 683335b1 Stavros Sachtouris
        uuid=ValueArgument('A user unique id to get quotas for', '--uuid')
211 683335b1 Stavros Sachtouris
    )
212 683335b1 Stavros Sachtouris
213 683335b1 Stavros Sachtouris
    def _run(self, stoken):
214 683335b1 Stavros Sachtouris
        self._print(self.client.service_get_quotas(stoken, self['uuid']))
215 683335b1 Stavros Sachtouris
216 683335b1 Stavros Sachtouris
    def main(self, service_token):
217 97fc1e06 Stavros Sachtouris
        super(self.__class__, self)._run()
218 683335b1 Stavros Sachtouris
        self._run(service_token)