Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / snf-astakos.py @ f724cd35

History | View | Annotate | Download (7.3 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 CLISyntaxError
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
        astakos_endpoints = self.auth_base.get_service_endpoints(
65
            self.config.get('astakos', 'type'),
66
            self.config.get('astakos', 'version'))
67
        base_url = astakos_endpoints['publicURL']
68
        self.client = AstakosClient(base_url, logger=log)
69
        self._set_log_params()
70
        self._update_max_threads()
71

    
72
    def main(self):
73
        self._run()
74

    
75

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

    
86
    arguments = dict(
87
        usage=FlagArgument('also return usage information', ('--with-usage'))
88
    )
89

    
90
    @errors.generic.all
91
    #@errors.user.authenticate
92
    def _run(self):
93
        print('KAMAKI LOG: call get_user_info(%s, %s)' % (
94
            self.token, self['usage']))
95
        self._print(
96
            self.client.get_user_info(self.token, self['usage']),
97
            print_dict)
98

    
99
    def main(self):
100
        super(self.__class__, self)._run()
101
        self._run()
102

    
103

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

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

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

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

    
125

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

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

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

    
142

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

    
147
    def _run(self):
148
            self._print(self.client.get_quotas(self.token), print_dict)
149

    
150
    def main(self):
151
        super(self.__class__, self)._run()
152
        self._run()
153

    
154

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

    
159

    
160
@command(snfastakos_cmds)
161
class astakos_services_list(_astakos_init):
162
    """List available services"""
163

    
164
    def _run(self):
165
        self._print(self.client.get_services())
166

    
167
    def main(self):
168
        super(self.__class__, self)._run()
169
        self._run()
170

    
171

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

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

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

    
188

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

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

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

    
206

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

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

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

    
218
    def main(self, service_token):
219
        super(self.__class__, self)._run()
220
        self._run(service_token)