Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / snf-astakos.py @ 683335b1

History | View | Annotate | Download (7.2 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()):
54
        super(_astakos_init, self).__init__(arguments)
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
        base_url = self.config.get('astakos', 'url')\
65
            or self.config.get('user', 'url')\
66
            or self.config.get('global', 'url')
67
        self.client = AstakosClient(base_url, logger=log)
68
        self._set_log_params()
69
        self._update_max_threads()
70

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

    
74

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

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

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

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

    
102

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

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

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

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

    
124

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

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

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

    
141

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

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

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

    
153

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

    
158

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

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

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

    
170

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

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

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

    
187

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

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

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

    
205

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

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

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

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