Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7 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, _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
    def _run(self):
58
        self.token = self['token']\
59
            or self.config.get('astakos', 'token')\
60
            or self.config.get('user', 'token')\
61
            or self.config.get('global', 'token')
62
        base_url = self.config.get('astakos', 'url')\
63
            or self.config.get('user', 'url')\
64
            or self.config.get('global', 'url')
65
        self.client = AstakosClient(base_url, logger=log)
66
        self._set_log_params()
67
        self._update_max_threads()
68

    
69
    def main(self):
70
        self._run()
71

    
72

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

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

    
87
    def _run(self):
88
        self._print(
89
            self.client.get_user_info(self.token, self['usage']), print_dict)
90

    
91
    def main(self):
92
        super(self.__class__, self)._run()
93
        self._run()
94

    
95

    
96
@command(snfastakos_cmds)
97
class astakos_username(_astakos_init, _optional_json):
98
    """Get username(s) from uuid(s)"""
99

    
100
    arguments = dict(
101
        service_token=ValueArgument(
102
            'Use service token instead', '--service-token')
103
    )
104

    
105
    def _run(self, uuids):
106
        assert uuids and isinstance(uuids, list), 'No valid uuids'
107
        if 1 == len(uuids):
108
            self._print(self.client.get_username(self.token, uuids[0]))
109
        else:
110
            self._print(
111
                self.client.get_username(self.token, uuids), print_dict)
112

    
113
    def main(self, uuid, *more_uuids):
114
        super(self.__class__, self)._run()
115
        self._run([uuid] + list(more_uuids))
116

    
117

    
118
@command(snfastakos_cmds)
119
class astakos_uuid(_astakos_init, _optional_json):
120
    """Get uuid(s) from username(s)"""
121

    
122
    def _run(self, usernames):
123
        assert usernames and isinstance(usernames, list), 'No valid usernames'
124
        if 1 == len(usernames):
125
            self._print(self.client.get_uuid(self.token, usernames[0]))
126
        else:
127
            self._print(
128
                self.client.get_uuids(self.token, usernames), print_dict)
129

    
130
    def main(self, usernames, *more_usernames):
131
        super(self.__class__, self)._run()
132
        self._run([usernames] + list(more_usernames))
133

    
134

    
135
@command(snfastakos_cmds)
136
class astakos_quotas(_astakos_init, _optional_json):
137
    """Get user (or service) quotas"""
138

    
139
    def _run(self):
140
            self._print(self.client.get_quotas(self.token), print_dict)
141

    
142
    def main(self):
143
        super(self.__class__, self)._run()
144
        self._run()
145

    
146

    
147
@command(snfastakos_cmds)
148
class astakos_services(_astakos_init):
149
    """Astakos operations filtered by services"""
150

    
151

    
152
@command(snfastakos_cmds)
153
class astakos_services_list(_astakos_init, _optional_json):
154
    """List available services"""
155

    
156
    def _run(self):
157
        self._print(self.client.get_services())
158

    
159
    def main(self):
160
        super(self.__class__, self)._run()
161
        self._run()
162

    
163

    
164
@command(snfastakos_cmds)
165
class astakos_services_username(_astakos_init, _optional_json):
166
    """Get service username(s) from uuid(s)"""
167

    
168
    def _run(self, stoken, uuids):
169
        assert uuids and isinstance(uuids, list), 'No valid uuids'
170
        if 1 == len(uuids):
171
            self._print(self.client.service_get_username(stoken, uuids[0]))
172
        else:
173
            self._print(
174
                self.client.service_get_usernames(stoken, uuids), print_dict)
175

    
176
    def main(self, service_token, uuid, *more_uuids):
177
        super(self.__class__, self)._run()
178
        self._run(service_token, [uuid] + list(more_uuids))
179

    
180

    
181
@command(snfastakos_cmds)
182
class astakos_services_uuid(_astakos_init, _optional_json):
183
    """Get service uuid(s) from username(s)"""
184

    
185
    def _run(self, stoken, usernames):
186
        assert usernames and isinstance(usernames, list), 'No valid usernames'
187
        if 1 == len(usernames):
188
            self._print(self.client.service_get_uuid(self.token, usernames[0]))
189
        else:
190
            self._print(
191
                self.client.service_get_uuids(self.token, usernames),
192
                print_dict)
193

    
194
    def main(self, service_token, usernames, *more_usernames):
195
        super(self.__class__, self)._run()
196
        self._run(service_token, [usernames] + list(more_usernames))
197

    
198

    
199
@command(snfastakos_cmds)
200
class astakos_services_quotas(_astakos_init, _optional_json):
201
    """Get user (or service) quotas"""
202

    
203
    arguments = dict(
204
        uuid=ValueArgument('A user unique id to get quotas for', '--uuid')
205
    )
206

    
207
    def _run(self, stoken):
208
        self._print(self.client.service_get_quotas(stoken, self['uuid']))
209

    
210
    def main(self, service_token):
211
        super(self.__class__, self)._run()
212
        self._run(service_token)