Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / snf-astakos.py @ 97fc1e06

History | View | Annotate | Download (3.4 kB)

1 97fc1e06 Stavros Sachtouris
# Copyright 2013 GRNET S.A. All rights reserved.
2 97fc1e06 Stavros Sachtouris
#
3 97fc1e06 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 97fc1e06 Stavros Sachtouris
# without modification, are permitted provided that the following
5 97fc1e06 Stavros Sachtouris
# conditions are met:
6 97fc1e06 Stavros Sachtouris
#
7 97fc1e06 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 97fc1e06 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 97fc1e06 Stavros Sachtouris
#      disclaimer.
10 97fc1e06 Stavros Sachtouris
#
11 97fc1e06 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 97fc1e06 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 97fc1e06 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 97fc1e06 Stavros Sachtouris
#      provided with the distribution.
15 97fc1e06 Stavros Sachtouris
#
16 97fc1e06 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 97fc1e06 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 97fc1e06 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 97fc1e06 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 97fc1e06 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 97fc1e06 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 97fc1e06 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 97fc1e06 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 97fc1e06 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 97fc1e06 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 97fc1e06 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 97fc1e06 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 97fc1e06 Stavros Sachtouris
#
29 97fc1e06 Stavros Sachtouris
# The views and conclusions contained in the software and
30 97fc1e06 Stavros Sachtouris
# documentation are those of the authors and should not be
31 97fc1e06 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 97fc1e06 Stavros Sachtouris
# or implied, of GRNET S.A.command
33 97fc1e06 Stavros Sachtouris
34 97fc1e06 Stavros Sachtouris
from astakosclient import AstakosClient
35 97fc1e06 Stavros Sachtouris
36 97fc1e06 Stavros Sachtouris
from kamaki.cli import command
37 97fc1e06 Stavros Sachtouris
from kamaki.cli.commands import _command_init, errors, _optional_json
38 97fc1e06 Stavros Sachtouris
from kamaki.cli.command_tree import CommandTree
39 97fc1e06 Stavros Sachtouris
from kamaki.cli.utils import print_dict
40 97fc1e06 Stavros Sachtouris
from kamaki.cli.argument import FlagArgument
41 97fc1e06 Stavros Sachtouris
from kamaki.cli.logger import add_stream_logger
42 97fc1e06 Stavros Sachtouris
43 97fc1e06 Stavros Sachtouris
snfastakos_cmds = CommandTree('astakos', 'astakosclient CLI')
44 97fc1e06 Stavros Sachtouris
_commands = [snfastakos_cmds]
45 97fc1e06 Stavros Sachtouris
46 97fc1e06 Stavros Sachtouris
47 97fc1e06 Stavros Sachtouris
log = add_stream_logger(__name__)
48 97fc1e06 Stavros Sachtouris
49 97fc1e06 Stavros Sachtouris
50 97fc1e06 Stavros Sachtouris
class _astakos_init(_command_init):
51 97fc1e06 Stavros Sachtouris
52 97fc1e06 Stavros Sachtouris
    @errors.generic.all
53 97fc1e06 Stavros Sachtouris
    #@errors.user.load
54 97fc1e06 Stavros Sachtouris
    def _run(self):
55 97fc1e06 Stavros Sachtouris
        self.token = self.config.get('astakos', 'token')\
56 97fc1e06 Stavros Sachtouris
            or self.config.get('user', 'token')\
57 97fc1e06 Stavros Sachtouris
            or self.config.get('global', 'token')
58 97fc1e06 Stavros Sachtouris
        base_url = self.config.get('astakos', 'url')\
59 97fc1e06 Stavros Sachtouris
            or self.config.get('user', 'url')\
60 97fc1e06 Stavros Sachtouris
            or self.config.get('global', 'url')
61 97fc1e06 Stavros Sachtouris
        self.client = AstakosClient(base_url, logger=log)
62 97fc1e06 Stavros Sachtouris
        self._set_log_params()
63 97fc1e06 Stavros Sachtouris
        self._update_max_threads()
64 97fc1e06 Stavros Sachtouris
65 97fc1e06 Stavros Sachtouris
    def main(self):
66 97fc1e06 Stavros Sachtouris
        self._run()
67 97fc1e06 Stavros Sachtouris
68 97fc1e06 Stavros Sachtouris
69 97fc1e06 Stavros Sachtouris
@command(snfastakos_cmds)
70 97fc1e06 Stavros Sachtouris
class astakos_authenticate(_astakos_init, _optional_json):
71 97fc1e06 Stavros Sachtouris
    """Authenticate a user
72 97fc1e06 Stavros Sachtouris
    Get user information (e.g. unique account name) from token
73 97fc1e06 Stavros Sachtouris
    Token should be set in settings:
74 97fc1e06 Stavros Sachtouris
    *  check if a token is set    /config get token
75 97fc1e06 Stavros Sachtouris
    *  permanently set a token    /config set token <token>
76 97fc1e06 Stavros Sachtouris
    Token can also be provided as a parameter
77 97fc1e06 Stavros Sachtouris
    """
78 97fc1e06 Stavros Sachtouris
79 97fc1e06 Stavros Sachtouris
    arguments = dict(
80 97fc1e06 Stavros Sachtouris
        usage=FlagArgument('also return usage information', ('--with-usage'))
81 97fc1e06 Stavros Sachtouris
    )
82 97fc1e06 Stavros Sachtouris
83 97fc1e06 Stavros Sachtouris
    #@errors.generic.all
84 97fc1e06 Stavros Sachtouris
    #@errors.user.authenticate
85 97fc1e06 Stavros Sachtouris
    def _run(self, token=None):
86 97fc1e06 Stavros Sachtouris
        print('KAMAKI LOG: call get_user_info(%s, %s)' % (
87 97fc1e06 Stavros Sachtouris
            token or self.token, self['usage']))
88 97fc1e06 Stavros Sachtouris
        self._print(
89 97fc1e06 Stavros Sachtouris
            self.client.get_user_info(token or self.token, self['usage']),
90 97fc1e06 Stavros Sachtouris
            print_dict)
91 97fc1e06 Stavros Sachtouris
92 97fc1e06 Stavros Sachtouris
    def main(self, custom_token=None):
93 97fc1e06 Stavros Sachtouris
        super(self.__class__, self)._run()
94 97fc1e06 Stavros Sachtouris
        self._run(custom_token)