root / kamaki / cli / commands / astakos_cli.py @ e9e547ed
History | View | Annotate | Download (6 kB)
1 |
# Copyright 2011-2012 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 kamaki.cli import command |
35 |
from kamaki.clients.astakos import AstakosClient |
36 |
from kamaki.cli.utils import print_dict, print_items |
37 |
from kamaki.cli.errors import raiseCLIError, CLISyntaxError |
38 |
from kamaki.cli.commands import _command_init |
39 |
from kamaki.cli.command_tree import CommandTree |
40 |
from kamaki.cli.argument import ValueArgument |
41 |
|
42 |
astakos_cmds = CommandTree('astakos', 'Astakos API commands') |
43 |
_commands = [astakos_cmds] |
44 |
|
45 |
|
46 |
class _astakos_init(_command_init): |
47 |
def main(self): |
48 |
token = self.config.get('astakos', 'token')\ |
49 |
or self.config.get('global', 'token') |
50 |
base_url = self.config.get('astakos', 'url')\ |
51 |
or self.config.get('global', 'url') |
52 |
if base_url is None: |
53 |
raiseCLIError(None, 'Missing astakos server URL') |
54 |
self.client = AstakosClient(base_url=base_url, token=token)
|
55 |
|
56 |
|
57 |
@command(astakos_cmds)
|
58 |
class astakos_authenticate(_astakos_init): |
59 |
"""Authenticate a user, show user information"""
|
60 |
|
61 |
def main(self, token=None): |
62 |
super(self.__class__, self).main() |
63 |
try:
|
64 |
reply = self.client.authenticate(token)
|
65 |
except Exception as err: |
66 |
raiseCLIError(err) |
67 |
print_dict(reply) |
68 |
|
69 |
|
70 |
@command(astakos_cmds)
|
71 |
class astakos_admin_userinfo(_astakos_init): |
72 |
"""Get user info, provided you have admin privileges"""
|
73 |
|
74 |
def __init__(self, arguments={}): |
75 |
super(self.__class__, self).__init__(arguments) |
76 |
self.arguments['email'] = ValueArgument('target user email', '--email') |
77 |
self.arguments['username'] = ValueArgument('target username', |
78 |
'--username')
|
79 |
|
80 |
def main(self, admin_token=None): |
81 |
super(self.__class__, self).main() |
82 |
email = self.get_argument('email') |
83 |
username = self.get_argument('username') |
84 |
try:
|
85 |
if email and username: |
86 |
raise CLISyntaxError(
|
87 |
'Arguments %s and %s are mutually exclusive' % (
|
88 |
self.arguments['email'].parsed_name, |
89 |
self.arguments['username'].parsed_name |
90 |
), importance=1)
|
91 |
elif email:
|
92 |
reply = self.client.get_user_by_email(email,
|
93 |
admin=True,
|
94 |
token=admin_token) |
95 |
elif username:
|
96 |
reply = self.client.get_user_by_username(username,
|
97 |
admin=True,
|
98 |
token=admin_token) |
99 |
else:
|
100 |
raise CLISyntaxError(
|
101 |
'Exactly one of %s or %s is obligatory' % (
|
102 |
self.arguments['email'].parsed_name, |
103 |
self.arguments['username'].parsed_name |
104 |
), importance=1)
|
105 |
except Exception as err: |
106 |
raiseCLIError(err) |
107 |
print_dict(reply) |
108 |
|
109 |
|
110 |
@command(astakos_cmds)
|
111 |
class astakos_service_userinfo(_astakos_init): |
112 |
"""Get user info with service token"""
|
113 |
|
114 |
def __init__(self, arguments={}): |
115 |
super(self.__class__, self).__init__(arguments) |
116 |
self.arguments['email'] = ValueArgument('target user email', '--email') |
117 |
self.arguments['username'] = ValueArgument('target username', |
118 |
'--username')
|
119 |
|
120 |
def main(self, service_token=None): |
121 |
super(self.__class__, self).main() |
122 |
email = self.get_argument('email') |
123 |
username = self.get_argument('username') |
124 |
try:
|
125 |
if email and username: |
126 |
raise CLISyntaxError(
|
127 |
'Arguments %s and %s are mutually exclusive' % (
|
128 |
self.arguments['email'].parsed_name, |
129 |
self.arguments['username'].parsed_name |
130 |
), importance=1)
|
131 |
elif email:
|
132 |
reply = self.client.get_user_by_email(email,
|
133 |
token=service_token) |
134 |
elif username:
|
135 |
reply = self.client.get_user_by_username(username,
|
136 |
token=service_token) |
137 |
else:
|
138 |
raise CLISyntaxError(
|
139 |
'Exactly one of %s or %s is obligatory' % (
|
140 |
self.arguments['email'].parsed_name, |
141 |
self.arguments['username'].parsed_name |
142 |
), importance=1)
|
143 |
except Exception as err: |
144 |
raiseCLIError(err) |
145 |
print_dict(reply) |
146 |
|
147 |
|
148 |
@command(astakos_cmds)
|
149 |
class astakos_service_list(_astakos_init): |
150 |
"""List cloud services associated with astakos"""
|
151 |
|
152 |
def main(self, token=None): |
153 |
super(self.__class__, self).main() |
154 |
reply = self.client.list_services(token)
|
155 |
print_items(reply) |