Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.4 kB)

1
# Copyright 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 astakosclient import AstakosClient
35

    
36
from kamaki.cli import command
37
from kamaki.cli.errors import CLIBaseUrlError
38
from kamaki.cli.commands import (
39
    _command_init, errors, _optional_json, addLogSettings)
40
from kamaki.cli.command_tree import CommandTree
41
from kamaki.cli.utils import print_dict
42
from kamaki.cli.argument import FlagArgument, ValueArgument
43
from kamaki.cli.logger import get_logger
44

    
45
snfastakos_cmds = CommandTree('astakos', 'astakosclient CLI')
46
_commands = [snfastakos_cmds]
47

    
48

    
49
class _astakos_init(_command_init):
50

    
51
    def __init__(self, arguments=dict(), auth_base=None, cloud=None):
52
        super(_astakos_init, self).__init__(arguments, auth_base, cloud)
53
        self['token'] = ValueArgument('Custom token', '--token')
54

    
55
    @errors.generic.all
56
    #@errors.user.load
57
    @addLogSettings
58
    def _run(self):
59
        self.cloud = self.cloud if self.cloud else 'default'
60
        self.token = self['token'] or self._custom_token('astakos')\
61
            or self.config.get_cloud(self.cloud, 'token')
62
        if getattr(self, 'auth_base', False):
63
            astakos_endpoints = self.auth_base.get_service_endpoints(
64
                self._custom_type('astakos') or 'identity',
65
                self._custom_version('astakos') or '')
66
            base_url = astakos_endpoints['publicURL']
67
        else:
68
            base_url = self._custom_url('astakos')
69
        if not base_url:
70
            raise CLIBaseUrlError(service='astakos')
71
        self.client = AstakosClient(
72
            base_url, logger=get_logger('kamaki.clients'))
73

    
74
    def main(self):
75
        self._run()
76

    
77

    
78
@command(snfastakos_cmds)
79
class astakos_authenticate(_astakos_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 get cloud.default.token
84
    *  permanently set a token    /config set cloud.default.token <token>
85
    Token can also be provided as a parameter
86
    (To use a named cloud, use its name instead of "default")
87
    """
88

    
89
    arguments = dict(
90
        usage=FlagArgument('also return usage information', ('--with-usage'))
91
    )
92

    
93
    def _run(self):
94
        self._print(
95
            self.client.get_user_info(self.token, self['usage']), print_dict)
96

    
97
    def main(self):
98
        super(self.__class__, self)._run()
99
        self._run()
100

    
101

    
102
@command(snfastakos_cmds)
103
class astakos_username(_astakos_init, _optional_json):
104
    """Get username(s) from uuid(s)"""
105

    
106
    arguments = dict(
107
        service_token=ValueArgument(
108
            'Use service token instead', '--service-token')
109
    )
110

    
111
    def _run(self, uuids):
112
        assert uuids and isinstance(uuids, list), 'No valid uuids'
113
        if 1 == len(uuids):
114
            self._print(self.client.get_username(self.token, uuids[0]))
115
        else:
116
            self._print(
117
                self.client.get_username(self.token, uuids), print_dict)
118

    
119
    def main(self, uuid, *more_uuids):
120
        super(self.__class__, self)._run()
121
        self._run([uuid] + list(more_uuids))
122

    
123

    
124
@command(snfastakos_cmds)
125
class astakos_uuid(_astakos_init, _optional_json):
126
    """Get uuid(s) from username(s)"""
127

    
128
    def _run(self, usernames):
129
        assert usernames and isinstance(usernames, list), 'No valid usernames'
130
        if 1 == len(usernames):
131
            self._print(self.client.get_uuid(self.token, usernames[0]))
132
        else:
133
            self._print(
134
                self.client.get_uuids(self.token, usernames), print_dict)
135

    
136
    def main(self, usernames, *more_usernames):
137
        super(self.__class__, self)._run()
138
        self._run([usernames] + list(more_usernames))
139

    
140

    
141
@command(snfastakos_cmds)
142
class astakos_quotas(_astakos_init, _optional_json):
143
    """Get user (or service) quotas"""
144

    
145
    def _run(self):
146
            self._print(self.client.get_quotas(self.token), print_dict)
147

    
148
    def main(self):
149
        super(self.__class__, self)._run()
150
        self._run()
151

    
152

    
153
@command(snfastakos_cmds)
154
class astakos_services(_astakos_init):
155
    """Astakos operations filtered by services"""
156

    
157

    
158
@command(snfastakos_cmds)
159
class astakos_services_list(_astakos_init, _optional_json):
160
    """List available services"""
161

    
162
    def _run(self):
163
        self._print(self.client.get_services())
164

    
165
    def main(self):
166
        super(self.__class__, self)._run()
167
        self._run()
168

    
169

    
170
@command(snfastakos_cmds)
171
class astakos_services_username(_astakos_init, _optional_json):
172
    """Get service username(s) from uuid(s)"""
173

    
174
    def _run(self, stoken, uuids):
175
        assert uuids and isinstance(uuids, list), 'No valid uuids'
176
        if 1 == len(uuids):
177
            self._print(self.client.service_get_username(stoken, uuids[0]))
178
        else:
179
            self._print(
180
                self.client.service_get_usernames(stoken, uuids), print_dict)
181

    
182
    def main(self, service_token, uuid, *more_uuids):
183
        super(self.__class__, self)._run()
184
        self._run(service_token, [uuid] + list(more_uuids))
185

    
186

    
187
@command(snfastakos_cmds)
188
class astakos_services_uuid(_astakos_init, _optional_json):
189
    """Get service uuid(s) from username(s)"""
190

    
191
    def _run(self, stoken, usernames):
192
        assert usernames and isinstance(usernames, list), 'No valid usernames'
193
        if 1 == len(usernames):
194
            self._print(self.client.service_get_uuid(self.token, usernames[0]))
195
        else:
196
            self._print(
197
                self.client.service_get_uuids(self.token, usernames),
198
                print_dict)
199

    
200
    def main(self, service_token, usernames, *more_usernames):
201
        super(self.__class__, self)._run()
202
        self._run(service_token, [usernames] + list(more_usernames))
203

    
204

    
205
@command(snfastakos_cmds)
206
class astakos_services_quotas(_astakos_init, _optional_json):
207
    """Get user (or service) quotas"""
208

    
209
    arguments = dict(
210
        uuid=ValueArgument('A user unique id to get quotas for', '--uuid')
211
    )
212

    
213
    def _run(self, stoken):
214
        self._print(self.client.service_get_quotas(stoken, self['uuid']))
215

    
216
    def main(self, service_token):
217
        super(self.__class__, self)._run()
218
        self._run(service_token)