Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / snf-astakos.py @ 82e32e50

History | View | Annotate | Download (7.5 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 _command_init, errors, _optional_json
39
from kamaki.cli.command_tree import CommandTree
40
from kamaki.cli.utils import print_dict
41
from kamaki.cli.argument import FlagArgument, ValueArgument
42
from kamaki.cli.logger import add_stream_logger
43

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

    
47

    
48
log = add_stream_logger(__name__)
49

    
50

    
51
class _astakos_init(_command_init):
52

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

    
57
    @errors.generic.all
58
    #@errors.user.load
59
    def _run(self):
60
        self.token = self['token']\
61
            or self.config.get('astakos', 'token')\
62
            or self.config.get('user', 'token')\
63
            or self.config.get('global', 'token')
64
        if getattr(self, 'auth_base', False):
65
            astakos_endpoints = self.auth_base.get_service_endpoints(
66
                self.config.get('astakos', 'type'),
67
                self.config.get('astakos', 'version'))
68
            base_url = astakos_endpoints['publicURL']
69
        else:
70
            base_url = self.config.get('astakos', 'url')
71
        if not base_url:
72
            raise CLIBaseUrlError(service='astakos')
73
        self.client = AstakosClient(base_url, logger=log)
74
        self._set_log_params()
75
        self._update_max_threads()
76

    
77
    def main(self):
78
        self._run()
79

    
80

    
81
@command(snfastakos_cmds)
82
class astakos_authenticate(_astakos_init, _optional_json):
83
    """Authenticate a user
84
    Get user information (e.g. unique account name) from token
85
    Token should be set in settings:
86
    *  check if a token is set    /config get token
87
    *  permanently set a token    /config set token <token>
88
    Token can also be provided as a parameter
89
    """
90

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

    
95
    @errors.generic.all
96
    #@errors.user.authenticate
97
    def _run(self):
98
        print('KAMAKI LOG: call get_user_info(%s, %s)' % (
99
            self.token, self['usage']))
100
        self._print(
101
            self.client.get_user_info(self.token, self['usage']),
102
            print_dict)
103

    
104
    def main(self):
105
        super(self.__class__, self)._run()
106
        self._run()
107

    
108

    
109
@command(snfastakos_cmds)
110
class astakos_username(_astakos_init, _optional_json):
111
    """Get username(s) from uuid(s)"""
112

    
113
    arguments = dict(
114
        service_token=ValueArgument(
115
            'Use service token instead', '--service-token')
116
    )
117

    
118
    def _run(self, uuids):
119
        assert uuids and isinstance(uuids, list), 'No valid uuids'
120
        if 1 == len(uuids):
121
            self._print(self.client.get_username(self.token, uuids[0]))
122
        else:
123
            self._print(
124
                self.client.get_username(self.token, uuids), print_dict)
125

    
126
    def main(self, uuid, *more_uuids):
127
        super(self.__class__, self)._run()
128
        self._run([uuid] + list(more_uuids))
129

    
130

    
131
@command(snfastakos_cmds)
132
class astakos_uuid(_astakos_init, _optional_json):
133
    """Get uuid(s) from username(s)"""
134

    
135
    def _run(self, usernames):
136
        assert usernames and isinstance(usernames, list), 'No valid usernames'
137
        if 1 == len(usernames):
138
            self._print(self.client.get_uuid(self.token, usernames[0]))
139
        else:
140
            self._print(
141
                self.client.get_uuids(self.token, usernames), print_dict)
142

    
143
    def main(self, usernames, *more_usernames):
144
        super(self.__class__, self)._run()
145
        self._run([usernames] + list(more_usernames))
146

    
147

    
148
@command(snfastakos_cmds)
149
class astakos_quotas(_astakos_init, _optional_json):
150
    """Get user (or service) quotas"""
151

    
152
    def _run(self):
153
            self._print(self.client.get_quotas(self.token), print_dict)
154

    
155
    def main(self):
156
        super(self.__class__, self)._run()
157
        self._run()
158

    
159

    
160
@command(snfastakos_cmds)
161
class astakos_services(_astakos_init):
162
    """Astakos operations filtered by services"""
163

    
164

    
165
@command(snfastakos_cmds)
166
class astakos_services_list(_astakos_init):
167
    """List available services"""
168

    
169
    def _run(self):
170
        self._print(self.client.get_services())
171

    
172
    def main(self):
173
        super(self.__class__, self)._run()
174
        self._run()
175

    
176

    
177
@command(snfastakos_cmds)
178
class astakos_services_username(_astakos_init, _optional_json):
179
    """Get service username(s) from uuid(s)"""
180

    
181
    def _run(self, stoken, uuids):
182
        assert uuids and isinstance(uuids, list), 'No valid uuids'
183
        if 1 == len(uuids):
184
            self._print(self.client.service_get_username(stoken, uuids[0]))
185
        else:
186
            self._print(
187
                self.client.service_get_usernames(stoken, uuids), print_dict)
188

    
189
    def main(self, service_token, uuid, *more_uuids):
190
        super(self.__class__, self)._run()
191
        self._run(service_token, [uuid] + list(more_uuids))
192

    
193

    
194
@command(snfastakos_cmds)
195
class astakos_services_uuid(_astakos_init, _optional_json):
196
    """Get service uuid(s) from username(s)"""
197

    
198
    def _run(self, stoken, usernames):
199
        assert usernames and isinstance(usernames, list), 'No valid usernames'
200
        if 1 == len(usernames):
201
            self._print(self.client.service_get_uuid(self.token, usernames[0]))
202
        else:
203
            self._print(
204
                self.client.service_get_uuids(self.token, usernames),
205
                print_dict)
206

    
207
    def main(self, service_token, usernames, *more_usernames):
208
        super(self.__class__, self)._run()
209
        self._run(service_token, [usernames] + list(more_usernames))
210

    
211

    
212
@command(snfastakos_cmds)
213
class astakos_services_quotas(_astakos_init, _optional_json):
214
    """Get user (or service) quotas"""
215

    
216
    arguments = dict(
217
        uuid=ValueArgument('A user unique id to get quotas for', '--uuid')
218
    )
219

    
220
    def _run(self, stoken):
221
        self._print(self.client.service_get_quotas(stoken, self['uuid']))
222

    
223
    def main(self, service_token):
224
        super(self.__class__, self)._run()
225
        self._run(service_token)