Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / astakos.py @ dc7ecb7f

History | View | Annotate | Download (28.7 kB)

1 e3f01d64 Stavros Sachtouris
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2 7493ccb6 Stavros Sachtouris
#
3 7493ccb6 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 7493ccb6 Stavros Sachtouris
# without modification, are permitted provided that the following
5 7493ccb6 Stavros Sachtouris
# conditions are met:
6 7493ccb6 Stavros Sachtouris
#
7 7493ccb6 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 7493ccb6 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 7493ccb6 Stavros Sachtouris
#      disclaimer.
10 7493ccb6 Stavros Sachtouris
#
11 7493ccb6 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 7493ccb6 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 7493ccb6 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 7493ccb6 Stavros Sachtouris
#      provided with the distribution.
15 7493ccb6 Stavros Sachtouris
#
16 7493ccb6 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 7493ccb6 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 7493ccb6 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 7493ccb6 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 7493ccb6 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 7493ccb6 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 7493ccb6 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 7493ccb6 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 7493ccb6 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 7493ccb6 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 7493ccb6 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 7493ccb6 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 7493ccb6 Stavros Sachtouris
#
29 7493ccb6 Stavros Sachtouris
# The views and conclusions contained in the software and
30 7493ccb6 Stavros Sachtouris
# documentation are those of the authors and should not be
31 7493ccb6 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 7493ccb6 Stavros Sachtouris
# or implied, of GRNET S.A.command
33 7493ccb6 Stavros Sachtouris
34 ab863157 Stavros Sachtouris
from json import load, loads
35 c308d73f Stavros Sachtouris
from os.path import abspath
36 c308d73f Stavros Sachtouris
37 c2be5c96 Stavros Sachtouris
from kamaki.cli import command
38 cd42f8d8 Stavros Sachtouris
from kamaki.clients.astakos import LoggedAstakosClient
39 b4f69041 Stavros Sachtouris
from kamaki.cli.commands import (
40 ad696342 Stavros Sachtouris
    _command_init, errors, _optional_json, addLogSettings, _name_filter)
41 be99b6ad Stavros Sachtouris
from kamaki.cli.command_tree import CommandTree
42 fa7d08b6 Stavros Sachtouris
from kamaki.cli.errors import CLIBaseUrlError, CLISyntaxError, CLIError
43 ab863157 Stavros Sachtouris
from kamaki.cli.argument import (
44 ab863157 Stavros Sachtouris
    FlagArgument, ValueArgument, IntArgument, CommaSeparatedListArgument)
45 ad696342 Stavros Sachtouris
from kamaki.cli.utils import format_size, filter_dicts_by_dict
46 7493ccb6 Stavros Sachtouris
47 25f9a991 Stavros Sachtouris
#  Mandatory
48 25f9a991 Stavros Sachtouris
49 e8d3b957 Stavros Sachtouris
user_commands = CommandTree('user', 'Astakos/Identity API commands')
50 25f9a991 Stavros Sachtouris
quota_commands = CommandTree(
51 25f9a991 Stavros Sachtouris
    'quota', 'Astakos/Account API commands for quotas')
52 25f9a991 Stavros Sachtouris
resource_commands = CommandTree(
53 25f9a991 Stavros Sachtouris
    'resource', 'Astakos/Account API commands for resources')
54 e8d3b957 Stavros Sachtouris
project_commands = CommandTree('project', 'Astakos project API commands')
55 9d84caa4 Stavros Sachtouris
membership_commands = CommandTree(
56 9d84caa4 Stavros Sachtouris
    'membership', 'Astakos project membership API commands')
57 25f9a991 Stavros Sachtouris
58 25f9a991 Stavros Sachtouris
59 25f9a991 Stavros Sachtouris
#  Optional
60 25f9a991 Stavros Sachtouris
61 25f9a991 Stavros Sachtouris
endpoint_commands = CommandTree(
62 25f9a991 Stavros Sachtouris
    'endpoint', 'Astakos/Account API commands for endpoints')
63 25f9a991 Stavros Sachtouris
service_commands = CommandTree('service', 'Astakos API commands for services')
64 25f9a991 Stavros Sachtouris
commission_commands = CommandTree(
65 25f9a991 Stavros Sachtouris
    'commission', 'Astakos API commands for commissions')
66 25f9a991 Stavros Sachtouris
67 25f9a991 Stavros Sachtouris
_commands = [
68 25f9a991 Stavros Sachtouris
    user_commands, quota_commands, resource_commands, project_commands,
69 9d84caa4 Stavros Sachtouris
    service_commands, commission_commands, endpoint_commands,
70 9d84caa4 Stavros Sachtouris
    membership_commands]
71 234954d1 Stavros Sachtouris
72 234954d1 Stavros Sachtouris
73 392d902d Stavros Sachtouris
def with_temp_token(func):
74 392d902d Stavros Sachtouris
    """ Set token to self.client.token, run func, recover old token """
75 ab863157 Stavros Sachtouris
    def wrap(self, *args, **kwargs):
76 ab863157 Stavros Sachtouris
        try:
77 ab863157 Stavros Sachtouris
            token = kwargs.pop('token')
78 ab863157 Stavros Sachtouris
        except KeyError:
79 392d902d Stavros Sachtouris
            raise CLISyntaxError('A token is needed for %s' % func)
80 ab863157 Stavros Sachtouris
        token_bu = self.client.token
81 ab863157 Stavros Sachtouris
        try:
82 fa7d08b6 Stavros Sachtouris
            self.client.token = token or token_bu
83 392d902d Stavros Sachtouris
            return func(self, *args, **kwargs)
84 ab863157 Stavros Sachtouris
        finally:
85 ab863157 Stavros Sachtouris
            self.client.token = token_bu
86 ab863157 Stavros Sachtouris
    return wrap
87 ab863157 Stavros Sachtouris
88 ab863157 Stavros Sachtouris
89 e8d3b957 Stavros Sachtouris
class _init_synnefo_astakosclient(_command_init):
90 85115c12 Stavros Sachtouris
91 a03ade9e Stavros Sachtouris
    @errors.generic.all
92 4018326d Stavros Sachtouris
    @errors.user.load
93 e8d3b957 Stavros Sachtouris
    @errors.user.astakosclient
94 b4f69041 Stavros Sachtouris
    @addLogSettings
95 436bd910 Stavros Sachtouris
    def _run(self):
96 e8d3b957 Stavros Sachtouris
        if getattr(self, 'cloud', None):
97 b4f69041 Stavros Sachtouris
            base_url = self._custom_url('astakos')
98 b4f69041 Stavros Sachtouris
            if base_url:
99 f5ff79d9 Stavros Sachtouris
                token = self._custom_token(
100 ab863157 Stavros Sachtouris
                    'astakos') or self.config.get_cloud(
101 ab863157 Stavros Sachtouris
                    self.cloud, 'token')
102 dc897a7e Stavros Sachtouris
                token = token.split()[0] if ' ' in token else token
103 cd42f8d8 Stavros Sachtouris
                self.client = LoggedAstakosClient(base_url, token)
104 b4f69041 Stavros Sachtouris
                return
105 b4f69041 Stavros Sachtouris
        else:
106 b4f69041 Stavros Sachtouris
            self.cloud = 'default'
107 e8d3b957 Stavros Sachtouris
        if getattr(self, 'auth_base', None):
108 5033585e Stavros Sachtouris
            self.client = self.auth_base.get_client()
109 b4f69041 Stavros Sachtouris
            return
110 b4f69041 Stavros Sachtouris
        raise CLIBaseUrlError(service='astakos')
111 7493ccb6 Stavros Sachtouris
112 436bd910 Stavros Sachtouris
    def main(self):
113 5fdccdec Stavros Sachtouris
        self._run()
114 436bd910 Stavros Sachtouris
115 234954d1 Stavros Sachtouris
116 e8d3b957 Stavros Sachtouris
@command(user_commands)
117 fa7d08b6 Stavros Sachtouris
class user_authenticate(_init_synnefo_astakosclient, _optional_json):
118 fa7d08b6 Stavros Sachtouris
    """Authenticate a user and get all authentication information"""
119 7493ccb6 Stavros Sachtouris
120 a03ade9e Stavros Sachtouris
    @errors.generic.all
121 4018326d Stavros Sachtouris
    @errors.user.authenticate
122 e8d3b957 Stavros Sachtouris
    @errors.user.astakosclient
123 5033585e Stavros Sachtouris
    @with_temp_token
124 5033585e Stavros Sachtouris
    def _run(self):
125 fa7d08b6 Stavros Sachtouris
        self._print(self.client.authenticate(), self.print_dict)
126 3185cd6d Stavros Sachtouris
127 e8d3b957 Stavros Sachtouris
    def main(self, token=None):
128 9a8861d1 Stavros Sachtouris
        super(self.__class__, self)._run()
129 5033585e Stavros Sachtouris
        self._run(token=token)
130 9a8861d1 Stavros Sachtouris
131 9a8861d1 Stavros Sachtouris
132 e8d3b957 Stavros Sachtouris
@command(user_commands)
133 5033585e Stavros Sachtouris
class user_uuid2name(_init_synnefo_astakosclient, _optional_json):
134 5033585e Stavros Sachtouris
    """Get user name(s) from uuid(s)"""
135 9a8861d1 Stavros Sachtouris
136 8b4ba753 Stavros Sachtouris
    #@errors.generic.all
137 8b4ba753 Stavros Sachtouris
    #@errors.user.astakosclient
138 e8d3b957 Stavros Sachtouris
    def _run(self, uuids):
139 e8d3b957 Stavros Sachtouris
        r = self.client.get_usernames(uuids)
140 e8d3b957 Stavros Sachtouris
        self._print(r, self.print_dict)
141 e8d3b957 Stavros Sachtouris
        unresolved = set(uuids).difference(r)
142 e8d3b957 Stavros Sachtouris
        if unresolved:
143 e8d3b957 Stavros Sachtouris
            self.error('Unresolved uuids: %s' % ', '.join(unresolved))
144 e8d3b957 Stavros Sachtouris
145 e8d3b957 Stavros Sachtouris
    def main(self, uuid, *more_uuids):
146 9a8861d1 Stavros Sachtouris
        super(self.__class__, self)._run()
147 e8d3b957 Stavros Sachtouris
        self._run(uuids=((uuid, ) + more_uuids))
148 b91111b9 Stavros Sachtouris
149 b91111b9 Stavros Sachtouris
150 e8d3b957 Stavros Sachtouris
@command(user_commands)
151 5033585e Stavros Sachtouris
class user_name2uuid(_init_synnefo_astakosclient, _optional_json):
152 5033585e Stavros Sachtouris
    """Get user uuid(s) from name(s)"""
153 b91111b9 Stavros Sachtouris
154 b91111b9 Stavros Sachtouris
    @errors.generic.all
155 e8d3b957 Stavros Sachtouris
    @errors.user.astakosclient
156 e8d3b957 Stavros Sachtouris
    def _run(self, usernames):
157 e8d3b957 Stavros Sachtouris
        r = self.client.get_uuids(usernames)
158 e8d3b957 Stavros Sachtouris
        self._print(r, self.print_dict)
159 e8d3b957 Stavros Sachtouris
        unresolved = set(usernames).difference(r)
160 e8d3b957 Stavros Sachtouris
        if unresolved:
161 e8d3b957 Stavros Sachtouris
            self.error('Unresolved usernames: %s' % ', '.join(unresolved))
162 e8d3b957 Stavros Sachtouris
163 e8d3b957 Stavros Sachtouris
    def main(self, username, *more_usernames):
164 b91111b9 Stavros Sachtouris
        super(self.__class__, self)._run()
165 e8d3b957 Stavros Sachtouris
        self._run(usernames=((username, ) + more_usernames))
166 e8d3b957 Stavros Sachtouris
167 e8d3b957 Stavros Sachtouris
168 6893e31c Stavros Sachtouris
class _quota(_init_synnefo_astakosclient, _optional_json):
169 e8d3b957 Stavros Sachtouris
170 e8d3b957 Stavros Sachtouris
    _to_format = set(['cyclades.disk', 'pithos.diskspace', 'cyclades.ram'])
171 fe4940bc Stavros Sachtouris
172 e8d3b957 Stavros Sachtouris
    arguments = dict(
173 e8d3b957 Stavros Sachtouris
        bytes=FlagArgument('Show data size in bytes', '--bytes')
174 e8d3b957 Stavros Sachtouris
    )
175 fe4940bc Stavros Sachtouris
176 e8d3b957 Stavros Sachtouris
    def _print_quotas(self, quotas, *args, **kwargs):
177 e8d3b957 Stavros Sachtouris
        if not self['bytes']:
178 e8d3b957 Stavros Sachtouris
            for category in quotas.values():
179 e8d3b957 Stavros Sachtouris
                for service in self._to_format.intersection(category):
180 e8d3b957 Stavros Sachtouris
                    for attr, v in category[service].items():
181 e8d3b957 Stavros Sachtouris
                        category[service][attr] = format_size(v)
182 e8d3b957 Stavros Sachtouris
        self.print_dict(quotas, *args, **kwargs)
183 fe4940bc Stavros Sachtouris
184 6893e31c Stavros Sachtouris
185 6893e31c Stavros Sachtouris
@command(quota_commands)
186 6893e31c Stavros Sachtouris
class quota_info(_quota):
187 6893e31c Stavros Sachtouris
    """Get quota for a service (cyclades, pithos, astakos)"""
188 6893e31c Stavros Sachtouris
189 6893e31c Stavros Sachtouris
    @errors.generic.all
190 6893e31c Stavros Sachtouris
    @errors.user.astakosclient
191 6893e31c Stavros Sachtouris
    def _run(self, service):
192 6893e31c Stavros Sachtouris
        r = dict()
193 6893e31c Stavros Sachtouris
        for k, v in self.client.get_quotas()['system'].items():
194 6893e31c Stavros Sachtouris
            if (k.startswith(service)):
195 6893e31c Stavros Sachtouris
                r[k] = v
196 6893e31c Stavros Sachtouris
        self._print({'%s*' % service: r}, self._print_quotas)
197 6893e31c Stavros Sachtouris
198 6893e31c Stavros Sachtouris
    def main(self, service):
199 6893e31c Stavros Sachtouris
        super(self.__class__, self)._run()
200 6893e31c Stavros Sachtouris
        self._run(service)
201 6893e31c Stavros Sachtouris
202 6893e31c Stavros Sachtouris
203 6893e31c Stavros Sachtouris
@command(quota_commands)
204 6893e31c Stavros Sachtouris
class quota_list(_quota):
205 6893e31c Stavros Sachtouris
    """Get user quotas"""
206 6893e31c Stavros Sachtouris
207 fe4940bc Stavros Sachtouris
    @errors.generic.all
208 e8d3b957 Stavros Sachtouris
    @errors.user.astakosclient
209 e8d3b957 Stavros Sachtouris
    def _run(self):
210 e8d3b957 Stavros Sachtouris
        self._print(self.client.get_quotas(), self._print_quotas)
211 fe4940bc Stavros Sachtouris
212 e8d3b957 Stavros Sachtouris
    def main(self):
213 fe4940bc Stavros Sachtouris
        super(self.__class__, self)._run()
214 e8d3b957 Stavros Sachtouris
        self._run()
215 c308d73f Stavros Sachtouris
216 c308d73f Stavros Sachtouris
217 fa7d08b6 Stavros Sachtouris
#  command user session
218 fa7d08b6 Stavros Sachtouris
219 fa7d08b6 Stavros Sachtouris
220 fa7d08b6 Stavros Sachtouris
@command(user_commands)
221 25f9a991 Stavros Sachtouris
class user_info(_init_synnefo_astakosclient, _optional_json):
222 fa7d08b6 Stavros Sachtouris
    """Get info for (current) session user"""
223 fa7d08b6 Stavros Sachtouris
224 fa7d08b6 Stavros Sachtouris
    arguments = dict(
225 fa7d08b6 Stavros Sachtouris
        uuid=ValueArgument('Query user with uuid', '--uuid'),
226 fa7d08b6 Stavros Sachtouris
        name=ValueArgument('Query user with username/email', '--username')
227 fa7d08b6 Stavros Sachtouris
    )
228 fa7d08b6 Stavros Sachtouris
229 fa7d08b6 Stavros Sachtouris
    @errors.generic.all
230 fa7d08b6 Stavros Sachtouris
    @errors.user.astakosclient
231 fa7d08b6 Stavros Sachtouris
    def _run(self):
232 fa7d08b6 Stavros Sachtouris
        if self['uuid'] and self['name']:
233 fa7d08b6 Stavros Sachtouris
            raise CLISyntaxError(
234 fa7d08b6 Stavros Sachtouris
                'Arguments uuid and username are mutually exclusive',
235 fa7d08b6 Stavros Sachtouris
                details=['Use either uuid OR username OR none, not both'])
236 fa7d08b6 Stavros Sachtouris
        uuid = self['uuid'] or (self._username2uuid(self['name']) if (
237 fa7d08b6 Stavros Sachtouris
            self['name']) else None)
238 fa7d08b6 Stavros Sachtouris
        try:
239 fa7d08b6 Stavros Sachtouris
            token = self.auth_base.get_token(uuid) if uuid else None
240 fa7d08b6 Stavros Sachtouris
        except KeyError:
241 fa7d08b6 Stavros Sachtouris
            msg = ('id %s' % self['uuid']) if (
242 fa7d08b6 Stavros Sachtouris
                self['uuid']) else 'username %s' % self['name']
243 fa7d08b6 Stavros Sachtouris
            raise CLIError(
244 fa7d08b6 Stavros Sachtouris
                'No user with %s in the cached session list' % msg, details=[
245 fa7d08b6 Stavros Sachtouris
                    'To see all cached session users',
246 25f9a991 Stavros Sachtouris
                    '  /user list',
247 fa7d08b6 Stavros Sachtouris
                    'To authenticate and add a new user in the session list',
248 25f9a991 Stavros Sachtouris
                    '  /user add <new token>'])
249 fa7d08b6 Stavros Sachtouris
        self._print(self.auth_base.user_info(token), self.print_dict)
250 fa7d08b6 Stavros Sachtouris
251 fa7d08b6 Stavros Sachtouris
252 fa7d08b6 Stavros Sachtouris
@command(user_commands)
253 25f9a991 Stavros Sachtouris
class user_add(_init_synnefo_astakosclient, _optional_json):
254 25f9a991 Stavros Sachtouris
    """Authenticate a user by token and add to kamaki session (cache)"""
255 fa7d08b6 Stavros Sachtouris
256 fa7d08b6 Stavros Sachtouris
    @errors.generic.all
257 fa7d08b6 Stavros Sachtouris
    @errors.user.astakosclient
258 fa7d08b6 Stavros Sachtouris
    def _run(self, token=None):
259 fa7d08b6 Stavros Sachtouris
        ask = token and token not in self.auth_base._uuids
260 fa7d08b6 Stavros Sachtouris
        self._print(self.auth_base.authenticate(token), self.print_dict)
261 fa7d08b6 Stavros Sachtouris
        if ask and self.ask_user(
262 fa7d08b6 Stavros Sachtouris
                'Token is temporarily stored in memory. If it is stored in'
263 fa7d08b6 Stavros Sachtouris
                ' kamaki configuration file, it will be available in later'
264 fa7d08b6 Stavros Sachtouris
                ' sessions. Do you want to permanently store this token?'):
265 fa7d08b6 Stavros Sachtouris
            tokens = self.auth_base._uuids.keys()
266 fa7d08b6 Stavros Sachtouris
            tokens.remove(self.auth_base.token)
267 fa7d08b6 Stavros Sachtouris
            self['config'].set_cloud(
268 fa7d08b6 Stavros Sachtouris
                self.cloud, 'token', ' '.join([self.auth_base.token] + tokens))
269 fa7d08b6 Stavros Sachtouris
            self['config'].write()
270 fa7d08b6 Stavros Sachtouris
271 fa7d08b6 Stavros Sachtouris
    def main(self, new_token=None):
272 fa7d08b6 Stavros Sachtouris
        super(self.__class__, self)._run()
273 fa7d08b6 Stavros Sachtouris
        self._run(token=new_token)
274 fa7d08b6 Stavros Sachtouris
275 fa7d08b6 Stavros Sachtouris
276 fa7d08b6 Stavros Sachtouris
@command(user_commands)
277 25f9a991 Stavros Sachtouris
class user_list(_init_synnefo_astakosclient, _optional_json):
278 25f9a991 Stavros Sachtouris
    """List (cached) session users"""
279 fa7d08b6 Stavros Sachtouris
280 fa7d08b6 Stavros Sachtouris
    arguments = dict(
281 fa7d08b6 Stavros Sachtouris
        detail=FlagArgument('Detailed listing', ('-l', '--detail'))
282 fa7d08b6 Stavros Sachtouris
    )
283 fa7d08b6 Stavros Sachtouris
284 fa7d08b6 Stavros Sachtouris
    @errors.generic.all
285 fa7d08b6 Stavros Sachtouris
    @errors.user.astakosclient
286 fa7d08b6 Stavros Sachtouris
    def _run(self):
287 fa7d08b6 Stavros Sachtouris
        self._print([u if self['detail'] else (dict(
288 fa7d08b6 Stavros Sachtouris
            id=u['id'], name=u['name'])) for u in self.auth_base.list_users()])
289 fa7d08b6 Stavros Sachtouris
290 fa7d08b6 Stavros Sachtouris
    def main(self):
291 fa7d08b6 Stavros Sachtouris
        super(self.__class__, self)._run()
292 fa7d08b6 Stavros Sachtouris
        self._run()
293 fa7d08b6 Stavros Sachtouris
294 fa7d08b6 Stavros Sachtouris
295 fa7d08b6 Stavros Sachtouris
@command(user_commands)
296 25f9a991 Stavros Sachtouris
class user_select(_init_synnefo_astakosclient):
297 25f9a991 Stavros Sachtouris
    """Select a user from the (cached) list as the current session user"""
298 fa7d08b6 Stavros Sachtouris
299 fa7d08b6 Stavros Sachtouris
    @errors.generic.all
300 fa7d08b6 Stavros Sachtouris
    @errors.user.astakosclient
301 fa7d08b6 Stavros Sachtouris
    def _run(self, uuid):
302 fa7d08b6 Stavros Sachtouris
        try:
303 fa7d08b6 Stavros Sachtouris
            first_token = self.auth_base.get_token(uuid)
304 fa7d08b6 Stavros Sachtouris
        except KeyError:
305 fa7d08b6 Stavros Sachtouris
            raise CLIError(
306 fa7d08b6 Stavros Sachtouris
                'No user with uuid %s in the cached session list' % uuid,
307 fa7d08b6 Stavros Sachtouris
                details=[
308 fa7d08b6 Stavros Sachtouris
                    'To see all cached session users',
309 25f9a991 Stavros Sachtouris
                    '  /user list',
310 fa7d08b6 Stavros Sachtouris
                    'To authenticate and add a new user in the session list',
311 25f9a991 Stavros Sachtouris
                    '  /user add <new token>'])
312 fa7d08b6 Stavros Sachtouris
        if self.auth_base.token != first_token:
313 fa7d08b6 Stavros Sachtouris
            self.auth_base.token = first_token
314 fa7d08b6 Stavros Sachtouris
            msg = 'User with id %s is now the current session user.\n' % uuid
315 fa7d08b6 Stavros Sachtouris
            msg += 'Do you want future sessions to also start with this user?'
316 fa7d08b6 Stavros Sachtouris
            if self.ask_user(msg):
317 fa7d08b6 Stavros Sachtouris
                tokens = self.auth_base._uuids.keys()
318 fa7d08b6 Stavros Sachtouris
                tokens.remove(self.auth_base.token)
319 fa7d08b6 Stavros Sachtouris
                tokens.insert(0, self.auth_base.token)
320 fa7d08b6 Stavros Sachtouris
                self['config'].set_cloud(
321 fa7d08b6 Stavros Sachtouris
                    self.cloud, 'token',  ' '.join(tokens))
322 fa7d08b6 Stavros Sachtouris
                self['config'].write()
323 fa7d08b6 Stavros Sachtouris
                self.error('User is selected for next sessions')
324 fa7d08b6 Stavros Sachtouris
            else:
325 fa7d08b6 Stavros Sachtouris
                self.error('User is not permanently selected')
326 fa7d08b6 Stavros Sachtouris
        else:
327 fa7d08b6 Stavros Sachtouris
            self.error('User was already the selected session user')
328 fa7d08b6 Stavros Sachtouris
329 fa7d08b6 Stavros Sachtouris
    def main(self, user_uuid):
330 fa7d08b6 Stavros Sachtouris
        super(self.__class__, self)._run()
331 fa7d08b6 Stavros Sachtouris
        self._run(uuid=user_uuid)
332 fa7d08b6 Stavros Sachtouris
333 fa7d08b6 Stavros Sachtouris
334 fa7d08b6 Stavros Sachtouris
@command(user_commands)
335 25f9a991 Stavros Sachtouris
class user_delete(_init_synnefo_astakosclient):
336 25f9a991 Stavros Sachtouris
    """Delete a user (token) from the (cached) list of session users"""
337 fa7d08b6 Stavros Sachtouris
338 fa7d08b6 Stavros Sachtouris
    @errors.generic.all
339 fa7d08b6 Stavros Sachtouris
    @errors.user.astakosclient
340 fa7d08b6 Stavros Sachtouris
    def _run(self, uuid):
341 fa7d08b6 Stavros Sachtouris
        if uuid == self.auth_base.user_term('id'):
342 fa7d08b6 Stavros Sachtouris
            raise CLIError('Cannot remove current session user', details=[
343 fa7d08b6 Stavros Sachtouris
                'To see all cached session users',
344 25f9a991 Stavros Sachtouris
                '  /user list',
345 fa7d08b6 Stavros Sachtouris
                'To see current session user',
346 25f9a991 Stavros Sachtouris
                '  /user info',
347 fa7d08b6 Stavros Sachtouris
                'To select a different session user',
348 25f9a991 Stavros Sachtouris
                '  /user select <user uuid>'])
349 fa7d08b6 Stavros Sachtouris
        try:
350 fa7d08b6 Stavros Sachtouris
            self.auth_base.remove_user(uuid)
351 fa7d08b6 Stavros Sachtouris
        except KeyError:
352 fa7d08b6 Stavros Sachtouris
            raise CLIError('No user with uuid %s in session list' % uuid,
353 fa7d08b6 Stavros Sachtouris
                details=[
354 fa7d08b6 Stavros Sachtouris
                    'To see all cached session users',
355 25f9a991 Stavros Sachtouris
                    '  /user list',
356 fa7d08b6 Stavros Sachtouris
                    'To authenticate and add a new user in the session list',
357 25f9a991 Stavros Sachtouris
                    '  /user add <new token>'])
358 fa7d08b6 Stavros Sachtouris
        if self.ask_user(
359 fa7d08b6 Stavros Sachtouris
                'User is removed from current session, but will be restored in'
360 fa7d08b6 Stavros Sachtouris
                ' the next session. Remove the user from future sessions?'):
361 fa7d08b6 Stavros Sachtouris
            self['config'].set_cloud(
362 fa7d08b6 Stavros Sachtouris
                self.cloud, 'token', ' '.join(self.auth_base._uuids.keys()))
363 fa7d08b6 Stavros Sachtouris
            self['config'].write()
364 fa7d08b6 Stavros Sachtouris
365 fa7d08b6 Stavros Sachtouris
    def main(self, user_uuid):
366 fa7d08b6 Stavros Sachtouris
        super(self.__class__, self)._run()
367 fa7d08b6 Stavros Sachtouris
        self._run(uuid=user_uuid)
368 fa7d08b6 Stavros Sachtouris
369 fa7d08b6 Stavros Sachtouris
370 ab863157 Stavros Sachtouris
#  command admin
371 ab863157 Stavros Sachtouris
372 25f9a991 Stavros Sachtouris
@command(service_commands)
373 25f9a991 Stavros Sachtouris
class service_list(_init_synnefo_astakosclient, _optional_json):
374 ab863157 Stavros Sachtouris
    """List available services"""
375 ab863157 Stavros Sachtouris
376 ab863157 Stavros Sachtouris
    @errors.generic.all
377 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
378 ab863157 Stavros Sachtouris
    def _run(self):
379 ab863157 Stavros Sachtouris
        self._print(self.client.get_services())
380 ab863157 Stavros Sachtouris
381 ab863157 Stavros Sachtouris
    def main(self):
382 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
383 ab863157 Stavros Sachtouris
        self._run()
384 ab863157 Stavros Sachtouris
385 ab863157 Stavros Sachtouris
386 25f9a991 Stavros Sachtouris
@command(service_commands)
387 25f9a991 Stavros Sachtouris
class service_uuid2username(_init_synnefo_astakosclient, _optional_json):
388 ab863157 Stavros Sachtouris
    """Get service username(s) from uuid(s)"""
389 ab863157 Stavros Sachtouris
390 ab863157 Stavros Sachtouris
    @errors.generic.all
391 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
392 ab863157 Stavros Sachtouris
    @with_temp_token
393 ab863157 Stavros Sachtouris
    def _run(self, uuids):
394 ab863157 Stavros Sachtouris
        if 1 == len(uuids):
395 ab863157 Stavros Sachtouris
            self._print(self.client.service_get_username(uuids[0]))
396 ab863157 Stavros Sachtouris
        else:
397 ab863157 Stavros Sachtouris
            self._print(
398 ab863157 Stavros Sachtouris
                self.client.service_get_usernames(uuids),
399 ab863157 Stavros Sachtouris
                self.print_dict)
400 ab863157 Stavros Sachtouris
401 ab863157 Stavros Sachtouris
    def main(self, service_token, uuid, *more_uuids):
402 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
403 ab863157 Stavros Sachtouris
        self._run([uuid] + list(more_uuids), token=service_token)
404 ab863157 Stavros Sachtouris
405 ab863157 Stavros Sachtouris
406 25f9a991 Stavros Sachtouris
@command(service_commands)
407 25f9a991 Stavros Sachtouris
class service_username2uuid(_init_synnefo_astakosclient, _optional_json):
408 ab863157 Stavros Sachtouris
    """Get service uuid(s) from username(s)"""
409 ab863157 Stavros Sachtouris
410 ab863157 Stavros Sachtouris
    @errors.generic.all
411 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
412 ab863157 Stavros Sachtouris
    @with_temp_token
413 ab863157 Stavros Sachtouris
    def _run(self, usernames):
414 ab863157 Stavros Sachtouris
        if 1 == len(usernames):
415 ab863157 Stavros Sachtouris
            self._print(self.client.service_get_uuid(usernames[0]))
416 ab863157 Stavros Sachtouris
        else:
417 ab863157 Stavros Sachtouris
            self._print(
418 ab863157 Stavros Sachtouris
                self.client.service_get_uuids(usernames),
419 ab863157 Stavros Sachtouris
                self.print_dict)
420 ab863157 Stavros Sachtouris
421 ab863157 Stavros Sachtouris
    def main(self, service_token, usernames, *more_usernames):
422 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
423 ab863157 Stavros Sachtouris
        self._run([usernames] + list(more_usernames), token=service_token)
424 ab863157 Stavros Sachtouris
425 ab863157 Stavros Sachtouris
426 25f9a991 Stavros Sachtouris
@command(service_commands)
427 25f9a991 Stavros Sachtouris
class service_quotas(_init_synnefo_astakosclient, _optional_json):
428 ab863157 Stavros Sachtouris
    """Get service quotas"""
429 ab863157 Stavros Sachtouris
430 ab863157 Stavros Sachtouris
    arguments = dict(
431 ab863157 Stavros Sachtouris
        uuid=ValueArgument('A user uuid to get quotas for', '--uuid')
432 ab863157 Stavros Sachtouris
    )
433 ab863157 Stavros Sachtouris
434 ab863157 Stavros Sachtouris
    @errors.generic.all
435 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
436 ab863157 Stavros Sachtouris
    @with_temp_token
437 ab863157 Stavros Sachtouris
    def _run(self):
438 ab863157 Stavros Sachtouris
        self._print(self.client.service_get_quotas(self['uuid']))
439 ab863157 Stavros Sachtouris
440 ab863157 Stavros Sachtouris
    def main(self, service_token):
441 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
442 ab863157 Stavros Sachtouris
        self._run(token=service_token)
443 ab863157 Stavros Sachtouris
444 ab863157 Stavros Sachtouris
445 25f9a991 Stavros Sachtouris
@command(commission_commands)
446 25f9a991 Stavros Sachtouris
class commission_pending(_init_synnefo_astakosclient, _optional_json):
447 ab863157 Stavros Sachtouris
    """List pending commissions (special privileges required)"""
448 ab863157 Stavros Sachtouris
449 ab863157 Stavros Sachtouris
    @errors.generic.all
450 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
451 ab863157 Stavros Sachtouris
    def _run(self):
452 ab863157 Stavros Sachtouris
        self._print(self.client.get_pending_commissions())
453 ab863157 Stavros Sachtouris
454 ab863157 Stavros Sachtouris
    def main(self):
455 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
456 ab863157 Stavros Sachtouris
        self._run()
457 ab863157 Stavros Sachtouris
458 ab863157 Stavros Sachtouris
459 25f9a991 Stavros Sachtouris
@command(commission_commands)
460 25f9a991 Stavros Sachtouris
class commission_info(_init_synnefo_astakosclient, _optional_json):
461 ab863157 Stavros Sachtouris
    """Get commission info (special privileges required)"""
462 ab863157 Stavros Sachtouris
463 ab863157 Stavros Sachtouris
    @errors.generic.all
464 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
465 ab863157 Stavros Sachtouris
    def _run(self, commission_id):
466 ab863157 Stavros Sachtouris
        commission_id = int(commission_id)
467 ab863157 Stavros Sachtouris
        self._print(
468 ab863157 Stavros Sachtouris
            self.client.get_commission_info(commission_id), self.print_dict)
469 ab863157 Stavros Sachtouris
470 ab863157 Stavros Sachtouris
    def main(self, commission_id):
471 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
472 ab863157 Stavros Sachtouris
        self._run(commission_id)
473 ab863157 Stavros Sachtouris
474 ab863157 Stavros Sachtouris
475 25f9a991 Stavros Sachtouris
@command(commission_commands)
476 25f9a991 Stavros Sachtouris
class commission_accept(_init_synnefo_astakosclient):
477 ab863157 Stavros Sachtouris
    """Accept a pending commission  (special privileges required)"""
478 ab863157 Stavros Sachtouris
479 ab863157 Stavros Sachtouris
    @errors.generic.all
480 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
481 ab863157 Stavros Sachtouris
    def _run(self, commission_id):
482 ab863157 Stavros Sachtouris
        commission_id = int(commission_id)
483 ab863157 Stavros Sachtouris
        self.client.accept_commission(commission_id)
484 ab863157 Stavros Sachtouris
485 ab863157 Stavros Sachtouris
    def main(self, commission_id):
486 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
487 ab863157 Stavros Sachtouris
        self._run(commission_id)
488 ab863157 Stavros Sachtouris
489 ab863157 Stavros Sachtouris
490 25f9a991 Stavros Sachtouris
@command(commission_commands)
491 25f9a991 Stavros Sachtouris
class commission_reject(_init_synnefo_astakosclient):
492 ab863157 Stavros Sachtouris
    """Reject a pending commission (special privileges required)"""
493 ab863157 Stavros Sachtouris
494 ab863157 Stavros Sachtouris
    @errors.generic.all
495 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
496 ab863157 Stavros Sachtouris
    def _run(self, commission_id):
497 ab863157 Stavros Sachtouris
        commission_id = int(commission_id)
498 ab863157 Stavros Sachtouris
        self.client.reject_commission(commission_id)
499 ab863157 Stavros Sachtouris
500 ab863157 Stavros Sachtouris
    def main(self, commission_id):
501 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
502 ab863157 Stavros Sachtouris
        self._run(commission_id)
503 ab863157 Stavros Sachtouris
504 ab863157 Stavros Sachtouris
505 25f9a991 Stavros Sachtouris
@command(commission_commands)
506 25f9a991 Stavros Sachtouris
class commission_resolve(_init_synnefo_astakosclient, _optional_json):
507 ab863157 Stavros Sachtouris
    """Resolve multiple commissions (special privileges required)"""
508 ab863157 Stavros Sachtouris
509 ab863157 Stavros Sachtouris
    arguments = dict(
510 ab863157 Stavros Sachtouris
        accept=CommaSeparatedListArgument(
511 ab863157 Stavros Sachtouris
            'commission ids to accept (e.g., --accept=11,12,13,...',
512 ab863157 Stavros Sachtouris
            '--accept'),
513 ab863157 Stavros Sachtouris
        reject=CommaSeparatedListArgument(
514 ab863157 Stavros Sachtouris
            'commission ids to reject (e.g., --reject=11,12,13,...',
515 ab863157 Stavros Sachtouris
            '--reject')
516 ab863157 Stavros Sachtouris
    )
517 ab863157 Stavros Sachtouris
518 ab863157 Stavros Sachtouris
    @errors.generic.all
519 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
520 ab863157 Stavros Sachtouris
    def _run(self):
521 ab863157 Stavros Sachtouris
        self.writeln('accepted ', self['accept'])
522 ab863157 Stavros Sachtouris
        self.writeln('rejected ', self['reject'])
523 ab863157 Stavros Sachtouris
        self._print(
524 ab863157 Stavros Sachtouris
            self.client.resolve_commissions(self['accept'], self['reject']),
525 ab863157 Stavros Sachtouris
            self.print_dict)
526 ab863157 Stavros Sachtouris
527 ab863157 Stavros Sachtouris
    def main(self):
528 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
529 ab863157 Stavros Sachtouris
        self._run()
530 ab863157 Stavros Sachtouris
531 ab863157 Stavros Sachtouris
532 25f9a991 Stavros Sachtouris
@command(commission_commands)
533 25f9a991 Stavros Sachtouris
class commission_issue(_init_synnefo_astakosclient, _optional_json):
534 ab863157 Stavros Sachtouris
    """Issue commissions as a json string (special privileges required)
535 ab863157 Stavros Sachtouris
    Parameters:
536 ab863157 Stavros Sachtouris
    holder      -- user's id (string)
537 ab863157 Stavros Sachtouris
    source      -- commission's source (ex system) (string)
538 ab863157 Stavros Sachtouris
    provisions  -- resources with their quantity (json-dict from string to int)
539 ab863157 Stavros Sachtouris
    name        -- description of the commission (string)
540 ab863157 Stavros Sachtouris
    """
541 ab863157 Stavros Sachtouris
542 ab863157 Stavros Sachtouris
    arguments = dict(
543 ab863157 Stavros Sachtouris
        force=FlagArgument('Force commission', '--force'),
544 ab863157 Stavros Sachtouris
        accept=FlagArgument('Do not wait for verification', '--accept')
545 ab863157 Stavros Sachtouris
    )
546 ab863157 Stavros Sachtouris
547 ab863157 Stavros Sachtouris
    @errors.generic.all
548 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
549 ab863157 Stavros Sachtouris
    def _run(self, holder, source, provisions, name=''):
550 ab863157 Stavros Sachtouris
        provisions = loads(provisions)
551 ab863157 Stavros Sachtouris
        self._print(self.client.issue_one_commission(
552 ab863157 Stavros Sachtouris
            holder, source, provisions, name,
553 ab863157 Stavros Sachtouris
            self['force'], self['accept']))
554 ab863157 Stavros Sachtouris
555 ab863157 Stavros Sachtouris
    def main(self, user_uuid, source, provisions_file, name=''):
556 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
557 ab863157 Stavros Sachtouris
        self._run(user_uuid, source, provisions_file, name)
558 ab863157 Stavros Sachtouris
559 ab863157 Stavros Sachtouris
560 25f9a991 Stavros Sachtouris
@command(resource_commands)
561 25f9a991 Stavros Sachtouris
class resource_list(_init_synnefo_astakosclient, _optional_json):
562 ab863157 Stavros Sachtouris
    """List user resources"""
563 ab863157 Stavros Sachtouris
564 ab863157 Stavros Sachtouris
    @errors.generic.all
565 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
566 ab863157 Stavros Sachtouris
    def _run(self):
567 ab863157 Stavros Sachtouris
        self._print(self.client.get_resources(), self.print_dict)
568 ab863157 Stavros Sachtouris
569 ab863157 Stavros Sachtouris
    def main(self):
570 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
571 ab863157 Stavros Sachtouris
        self._run()
572 ab863157 Stavros Sachtouris
573 ab863157 Stavros Sachtouris
574 25f9a991 Stavros Sachtouris
@command(endpoint_commands)
575 ad696342 Stavros Sachtouris
class endpoint_list(
576 ad696342 Stavros Sachtouris
        _init_synnefo_astakosclient, _optional_json, _name_filter):
577 ab863157 Stavros Sachtouris
    """Get endpoints service endpoints"""
578 ab863157 Stavros Sachtouris
579 ad696342 Stavros Sachtouris
    arguments = dict(endpoint_type=ValueArgument('Filter by type', '--type'))
580 ad696342 Stavros Sachtouris
581 ab863157 Stavros Sachtouris
    @errors.generic.all
582 ab863157 Stavros Sachtouris
    @errors.user.astakosclient
583 ab863157 Stavros Sachtouris
    def _run(self):
584 ad696342 Stavros Sachtouris
        r = self.client.get_endpoints()['access']['serviceCatalog']
585 ad696342 Stavros Sachtouris
        r = self._filter_by_name(r)
586 ad696342 Stavros Sachtouris
        if self['endpoint_type']:
587 ad696342 Stavros Sachtouris
            r = filter_dicts_by_dict(r, dict(type=self['endpoint_type']))
588 ad696342 Stavros Sachtouris
        self._print(r)
589 ab863157 Stavros Sachtouris
590 ab863157 Stavros Sachtouris
    def main(self):
591 ab863157 Stavros Sachtouris
        super(self.__class__, self)._run()
592 ab863157 Stavros Sachtouris
        self._run()
593 ab863157 Stavros Sachtouris
594 ab863157 Stavros Sachtouris
595 c308d73f Stavros Sachtouris
#  command project
596 c308d73f Stavros Sachtouris
597 c308d73f Stavros Sachtouris
598 6893e31c Stavros Sachtouris
_project_specs = """{
599 6893e31c Stavros Sachtouris
    "name": name,
600 6893e31c Stavros Sachtouris
    "owner": uuid,
601 6893e31c Stavros Sachtouris
    "homepage": homepage,         # optional
602 6893e31c Stavros Sachtouris
    "description": description,   # optional
603 6893e31c Stavros Sachtouris
    "comments": comments,         # optional
604 6893e31c Stavros Sachtouris
    "start_date": date,           # optional
605 6893e31c Stavros Sachtouris
    "end_date": date,
606 6893e31c Stavros Sachtouris
    "join_policy": "auto" | "moderated" | "closed",  # default: "moderated"
607 6893e31c Stavros Sachtouris
    "leave_policy": "auto" | "moderated" | "closed", # default: "auto"
608 de329b4c Stavros Sachtouris
    "resources": {"cyclades.vm": {
609 de329b4c Stavros Sachtouris
    "project_capacity": int or null,
610 de329b4c Stavros Sachtouris
    "member_capacity": int
611 de329b4c Stavros Sachtouris
    }}}
612 de329b4c Stavros Sachtouris

613 6893e31c Stavros Sachtouris
"""
614 c308d73f Stavros Sachtouris
615 c308d73f Stavros Sachtouris
616 392d902d Stavros Sachtouris
def apply_notification(func):
617 c308d73f Stavros Sachtouris
    def wrap(self, *args, **kwargs):
618 392d902d Stavros Sachtouris
        r = func(self, *args, **kwargs)
619 c308d73f Stavros Sachtouris
        self.writeln('Application is submitted successfully')
620 c308d73f Stavros Sachtouris
        return r
621 c308d73f Stavros Sachtouris
    return wrap
622 c308d73f Stavros Sachtouris
623 c308d73f Stavros Sachtouris
624 c308d73f Stavros Sachtouris
@command(project_commands)
625 c308d73f Stavros Sachtouris
class project_list(_init_synnefo_astakosclient, _optional_json):
626 c308d73f Stavros Sachtouris
    """List all projects"""
627 c308d73f Stavros Sachtouris
628 c308d73f Stavros Sachtouris
    arguments = dict(
629 dc7ecb7f Stavros Sachtouris
        details=FlagArgument('Show details', ('-l', '--details')),
630 c308d73f Stavros Sachtouris
        name=ValueArgument('Filter by name', ('--with-name', )),
631 c308d73f Stavros Sachtouris
        state=ValueArgument('Filter by state', ('--with-state', )),
632 c308d73f Stavros Sachtouris
        owner=ValueArgument('Filter by owner', ('--with-owner', ))
633 c308d73f Stavros Sachtouris
    )
634 c308d73f Stavros Sachtouris
635 c308d73f Stavros Sachtouris
    @errors.generic.all
636 c308d73f Stavros Sachtouris
    @errors.user.astakosclient
637 c308d73f Stavros Sachtouris
    def _run(self):
638 dc7ecb7f Stavros Sachtouris
        r = self.client.get_projects(
639 dc7ecb7f Stavros Sachtouris
            self['name'], self['state'], self['owner'])
640 dc7ecb7f Stavros Sachtouris
        if not (self['details'] or self['output_format']):
641 dc7ecb7f Stavros Sachtouris
            r = [dict(
642 dc7ecb7f Stavros Sachtouris
                id=i['id'],
643 dc7ecb7f Stavros Sachtouris
                name=i['name'],
644 dc7ecb7f Stavros Sachtouris
                description=i['description']) for i in r]
645 dc7ecb7f Stavros Sachtouris
        self._print(r)
646 c308d73f Stavros Sachtouris
647 c308d73f Stavros Sachtouris
    def main(self):
648 c308d73f Stavros Sachtouris
        super(self.__class__, self)._run()
649 c308d73f Stavros Sachtouris
        self._run()
650 c308d73f Stavros Sachtouris
651 c308d73f Stavros Sachtouris
652 c308d73f Stavros Sachtouris
@command(project_commands)
653 c308d73f Stavros Sachtouris
class project_info(_init_synnefo_astakosclient, _optional_json):
654 c308d73f Stavros Sachtouris
    """Get details for a project"""
655 c308d73f Stavros Sachtouris
656 c308d73f Stavros Sachtouris
    @errors.generic.all
657 c308d73f Stavros Sachtouris
    @errors.user.astakosclient
658 c308d73f Stavros Sachtouris
    def _run(self, project_id):
659 c308d73f Stavros Sachtouris
        self._print(
660 c308d73f Stavros Sachtouris
            self.client.get_project(project_id), self.print_dict)
661 c308d73f Stavros Sachtouris
662 c308d73f Stavros Sachtouris
    def main(self, project_id):
663 c308d73f Stavros Sachtouris
        super(self.__class__, self)._run()
664 c308d73f Stavros Sachtouris
        self._run(project_id)
665 c308d73f Stavros Sachtouris
666 c308d73f Stavros Sachtouris
667 c308d73f Stavros Sachtouris
@command(project_commands)
668 c308d73f Stavros Sachtouris
class project_create(_init_synnefo_astakosclient, _optional_json):
669 6893e31c Stavros Sachtouris
    """Apply for a new project (enter data though standard input or file path)
670 de329b4c Stavros Sachtouris

671 6893e31c Stavros Sachtouris
    Project details must be provided as a json-formated dict from the
672 6893e31c Stavros Sachtouris
    standard input, or through a file
673 c308d73f Stavros Sachtouris
    """
674 c308d73f Stavros Sachtouris
    __doc__ += _project_specs
675 c308d73f Stavros Sachtouris
676 c308d73f Stavros Sachtouris
    arguments = dict(
677 c308d73f Stavros Sachtouris
        specs_path=ValueArgument(
678 c308d73f Stavros Sachtouris
            'Specification file path (content must be in json)', '--spec-file')
679 c308d73f Stavros Sachtouris
    )
680 c308d73f Stavros Sachtouris
681 c308d73f Stavros Sachtouris
    @errors.generic.all
682 c308d73f Stavros Sachtouris
    @errors.user.astakosclient
683 c308d73f Stavros Sachtouris
    @apply_notification
684 c308d73f Stavros Sachtouris
    def _run(self):
685 c308d73f Stavros Sachtouris
        input_stream = open(abspath(self['specs_path'])) if (
686 c308d73f Stavros Sachtouris
            self['specs_path']) else self._in
687 c308d73f Stavros Sachtouris
        specs = load(input_stream)
688 c308d73f Stavros Sachtouris
        self._print(
689 c308d73f Stavros Sachtouris
            self.client.create_project(specs), self.print_dict)
690 c308d73f Stavros Sachtouris
691 c308d73f Stavros Sachtouris
    def main(self):
692 c308d73f Stavros Sachtouris
        super(self.__class__, self)._run()
693 c308d73f Stavros Sachtouris
        self._run()
694 c308d73f Stavros Sachtouris
695 c308d73f Stavros Sachtouris
696 c308d73f Stavros Sachtouris
@command(project_commands)
697 c308d73f Stavros Sachtouris
class project_modify(_init_synnefo_astakosclient, _optional_json):
698 c308d73f Stavros Sachtouris
    """Modify a project (input a json-dict)
699 c308d73f Stavros Sachtouris
    Project details must be provided as a json-formated dict from the standard
700 c308d73f Stavros Sachtouris
    input, or through a file
701 c308d73f Stavros Sachtouris
    """
702 c308d73f Stavros Sachtouris
703 c308d73f Stavros Sachtouris
    __doc__ += _project_specs
704 c308d73f Stavros Sachtouris
705 c308d73f Stavros Sachtouris
    arguments = dict(
706 c308d73f Stavros Sachtouris
        specs_path=ValueArgument(
707 c308d73f Stavros Sachtouris
            'Specification file path (content must be in json)', '--spec-file')
708 c308d73f Stavros Sachtouris
    )
709 c308d73f Stavros Sachtouris
710 c308d73f Stavros Sachtouris
    @errors.generic.all
711 c308d73f Stavros Sachtouris
    @errors.user.astakosclient
712 c308d73f Stavros Sachtouris
    @apply_notification
713 c308d73f Stavros Sachtouris
    def _run(self, project_id):
714 c308d73f Stavros Sachtouris
        input_stream = open(abspath(self['specs_path'])) if (
715 c308d73f Stavros Sachtouris
            self['specs_path']) else self._in
716 c308d73f Stavros Sachtouris
        specs = load(input_stream)
717 c308d73f Stavros Sachtouris
        self._print(
718 c308d73f Stavros Sachtouris
            self.client.modify_project(project_id, specs),
719 c308d73f Stavros Sachtouris
            self.print_dict)
720 c308d73f Stavros Sachtouris
721 c308d73f Stavros Sachtouris
    def main(self, project_id):
722 c308d73f Stavros Sachtouris
        super(self.__class__, self)._run()
723 c308d73f Stavros Sachtouris
        self._run(project_id)
724 c308d73f Stavros Sachtouris
725 c308d73f Stavros Sachtouris
726 c308d73f Stavros Sachtouris
class _project_action(_init_synnefo_astakosclient):
727 c308d73f Stavros Sachtouris
728 c308d73f Stavros Sachtouris
    action = ''
729 c308d73f Stavros Sachtouris
730 dc7ecb7f Stavros Sachtouris
    arguments = dict(
731 dc7ecb7f Stavros Sachtouris
        reason=ValueArgument('Quote a reason for this action', '--reason'),
732 dc7ecb7f Stavros Sachtouris
    )
733 dc7ecb7f Stavros Sachtouris
734 c308d73f Stavros Sachtouris
    @errors.generic.all
735 c308d73f Stavros Sachtouris
    @errors.user.astakosclient
736 c308d73f Stavros Sachtouris
    def _run(self, project_id, quote_a_reason):
737 c308d73f Stavros Sachtouris
        self.client.project_action(project_id, self.action, quote_a_reason)
738 c308d73f Stavros Sachtouris
739 dc7ecb7f Stavros Sachtouris
    def main(self, project_id):
740 c308d73f Stavros Sachtouris
        super(_project_action, self)._run()
741 dc7ecb7f Stavros Sachtouris
        self._run(project_id, self['reason'] or '')
742 c308d73f Stavros Sachtouris
743 c308d73f Stavros Sachtouris
744 c308d73f Stavros Sachtouris
@command(project_commands)
745 c308d73f Stavros Sachtouris
class project_suspend(_project_action):
746 c308d73f Stavros Sachtouris
    """Suspend a project (special privileges needed)"""
747 c308d73f Stavros Sachtouris
    action = 'suspend'
748 c308d73f Stavros Sachtouris
749 c308d73f Stavros Sachtouris
750 c308d73f Stavros Sachtouris
@command(project_commands)
751 c308d73f Stavros Sachtouris
class project_unsuspend(_project_action):
752 c308d73f Stavros Sachtouris
    """Resume a suspended project (special privileges needed)"""
753 c308d73f Stavros Sachtouris
    action = 'unsuspend'
754 c308d73f Stavros Sachtouris
755 c308d73f Stavros Sachtouris
756 c308d73f Stavros Sachtouris
@command(project_commands)
757 c308d73f Stavros Sachtouris
class project_terminate(_project_action):
758 c308d73f Stavros Sachtouris
    """Terminate a project (special privileges needed)"""
759 c308d73f Stavros Sachtouris
    action = 'terminate'
760 c308d73f Stavros Sachtouris
761 c308d73f Stavros Sachtouris
762 c308d73f Stavros Sachtouris
@command(project_commands)
763 c308d73f Stavros Sachtouris
class project_reinstate(_project_action):
764 c308d73f Stavros Sachtouris
    """Reinstate a terminated project (special privileges needed)"""
765 c308d73f Stavros Sachtouris
    action = 'reinstate'
766 c308d73f Stavros Sachtouris
767 c308d73f Stavros Sachtouris
768 dc7ecb7f Stavros Sachtouris
class _application_action(_init_synnefo_astakosclient):
769 c308d73f Stavros Sachtouris
770 dc7ecb7f Stavros Sachtouris
    action = ''
771 c308d73f Stavros Sachtouris
772 c308d73f Stavros Sachtouris
    arguments = dict(
773 dc7ecb7f Stavros Sachtouris
        app_id=ValueArgument('The application ID', '--app-id'),
774 dc7ecb7f Stavros Sachtouris
        reason=ValueArgument('Quote a reason for this action', '--reason'),
775 c308d73f Stavros Sachtouris
    )
776 dc7ecb7f Stavros Sachtouris
    required = ('app_id', )
777 c308d73f Stavros Sachtouris
778 c308d73f Stavros Sachtouris
    @errors.generic.all
779 c308d73f Stavros Sachtouris
    @errors.user.astakosclient
780 dc7ecb7f Stavros Sachtouris
    def _run(self, project_id, app_id, quote_a_reason):
781 dc7ecb7f Stavros Sachtouris
        self.client.application_action(
782 dc7ecb7f Stavros Sachtouris
            project_id, app_id, self.action, quote_a_reason)
783 c308d73f Stavros Sachtouris
784 dc7ecb7f Stavros Sachtouris
    def main(self, project_id):
785 c308d73f Stavros Sachtouris
        super(_application_action, self)._run()
786 dc7ecb7f Stavros Sachtouris
        self._run(self['app_id'], self['reason'] or '')
787 c308d73f Stavros Sachtouris
788 c308d73f Stavros Sachtouris
789 c308d73f Stavros Sachtouris
@command(project_commands)
790 dc7ecb7f Stavros Sachtouris
class project_approve(_application_action):
791 c308d73f Stavros Sachtouris
    """Approve an application (special privileges needed)"""
792 c308d73f Stavros Sachtouris
    action = 'approve'
793 c308d73f Stavros Sachtouris
794 c308d73f Stavros Sachtouris
795 c308d73f Stavros Sachtouris
@command(project_commands)
796 dc7ecb7f Stavros Sachtouris
class project_deny(_application_action):
797 c308d73f Stavros Sachtouris
    """Deny an application (special privileges needed)"""
798 c308d73f Stavros Sachtouris
    action = 'deny'
799 c308d73f Stavros Sachtouris
800 c308d73f Stavros Sachtouris
801 c308d73f Stavros Sachtouris
@command(project_commands)
802 dc7ecb7f Stavros Sachtouris
class project_dismiss(_application_action):
803 c308d73f Stavros Sachtouris
    """Dismiss your denied application"""
804 c308d73f Stavros Sachtouris
    action = 'dismiss'
805 c308d73f Stavros Sachtouris
806 c308d73f Stavros Sachtouris
807 c308d73f Stavros Sachtouris
@command(project_commands)
808 dc7ecb7f Stavros Sachtouris
class project_cancel(_application_action):
809 c308d73f Stavros Sachtouris
    """Cancel your application"""
810 c308d73f Stavros Sachtouris
    action = 'cancel'
811 c308d73f Stavros Sachtouris
812 c308d73f Stavros Sachtouris
813 9d84caa4 Stavros Sachtouris
@command(membership_commands)
814 9d84caa4 Stavros Sachtouris
class membership(_init_synnefo_astakosclient):
815 c308d73f Stavros Sachtouris
    """Project membership management commands"""
816 c308d73f Stavros Sachtouris
817 c308d73f Stavros Sachtouris
818 9d84caa4 Stavros Sachtouris
@command(membership_commands)
819 9d84caa4 Stavros Sachtouris
class membership_list(_init_synnefo_astakosclient, _optional_json):
820 c308d73f Stavros Sachtouris
    """List all memberships"""
821 c308d73f Stavros Sachtouris
822 c308d73f Stavros Sachtouris
    arguments = dict(
823 c308d73f Stavros Sachtouris
        project=IntArgument('Filter by project id', '--with-project-id')
824 c308d73f Stavros Sachtouris
    )
825 c308d73f Stavros Sachtouris
826 c308d73f Stavros Sachtouris
    @errors.generic.all
827 c308d73f Stavros Sachtouris
    @errors.user.astakosclient
828 c308d73f Stavros Sachtouris
    def _run(self):
829 c308d73f Stavros Sachtouris
        self._print(self.client.get_memberships(self['project']))
830 c308d73f Stavros Sachtouris
831 c308d73f Stavros Sachtouris
    def main(self):
832 c308d73f Stavros Sachtouris
        super(self.__class__, self)._run()
833 c308d73f Stavros Sachtouris
        self._run()
834 c308d73f Stavros Sachtouris
835 c308d73f Stavros Sachtouris
836 9d84caa4 Stavros Sachtouris
@command(membership_commands)
837 9d84caa4 Stavros Sachtouris
class membership_info(_init_synnefo_astakosclient, _optional_json):
838 c308d73f Stavros Sachtouris
    """Details on a membership"""
839 c308d73f Stavros Sachtouris
840 c308d73f Stavros Sachtouris
    @errors.generic.all
841 c308d73f Stavros Sachtouris
    @errors.user.astakosclient
842 c308d73f Stavros Sachtouris
    def _run(self, memb_id):
843 c308d73f Stavros Sachtouris
        self._print(
844 c308d73f Stavros Sachtouris
            self.client.get_membership(memb_id), self.print_dict)
845 c308d73f Stavros Sachtouris
846 c308d73f Stavros Sachtouris
    def main(self, membership_id):
847 c308d73f Stavros Sachtouris
        super(self.__class__, self)._run()
848 c308d73f Stavros Sachtouris
        self._run(membership_id)
849 c308d73f Stavros Sachtouris
850 c308d73f Stavros Sachtouris
851 c308d73f Stavros Sachtouris
class _membership_action(_init_synnefo_astakosclient, _optional_json):
852 c308d73f Stavros Sachtouris
853 c308d73f Stavros Sachtouris
    action = ''
854 c308d73f Stavros Sachtouris
855 c308d73f Stavros Sachtouris
    @errors.generic.all
856 c308d73f Stavros Sachtouris
    @errors.user.astakosclient
857 c308d73f Stavros Sachtouris
    def _run(self, memb_id, quote_a_reason):
858 c308d73f Stavros Sachtouris
        self._print(self.client.membership_action(
859 c308d73f Stavros Sachtouris
            memb_id, self.action, quote_a_reason))
860 c308d73f Stavros Sachtouris
861 c308d73f Stavros Sachtouris
    def main(self, membership_id, quote_a_reason=''):
862 c308d73f Stavros Sachtouris
        super(_membership_action, self)._run()
863 c308d73f Stavros Sachtouris
        self._run(membership_id, quote_a_reason)
864 c308d73f Stavros Sachtouris
865 c308d73f Stavros Sachtouris
866 9d84caa4 Stavros Sachtouris
@command(membership_commands)
867 9d84caa4 Stavros Sachtouris
class membership_leave(_membership_action):
868 c308d73f Stavros Sachtouris
    """Leave a project you have membership to"""
869 c308d73f Stavros Sachtouris
    action = 'leave'
870 c308d73f Stavros Sachtouris
871 c308d73f Stavros Sachtouris
872 9d84caa4 Stavros Sachtouris
@command(membership_commands)
873 9d84caa4 Stavros Sachtouris
class membership_cancel(_membership_action):
874 c308d73f Stavros Sachtouris
    """Cancel your (probably pending) membership to a project"""
875 c308d73f Stavros Sachtouris
    action = 'cancel'
876 c308d73f Stavros Sachtouris
877 c308d73f Stavros Sachtouris
878 9d84caa4 Stavros Sachtouris
@command(membership_commands)
879 9d84caa4 Stavros Sachtouris
class membership_accept(_membership_action):
880 c308d73f Stavros Sachtouris
    """Accept a membership for a project you manage"""
881 c308d73f Stavros Sachtouris
    action = 'accept'
882 c308d73f Stavros Sachtouris
883 c308d73f Stavros Sachtouris
884 9d84caa4 Stavros Sachtouris
@command(membership_commands)
885 9d84caa4 Stavros Sachtouris
class membership_reject(_membership_action):
886 c308d73f Stavros Sachtouris
    """Reject a membership for a project you manage"""
887 c308d73f Stavros Sachtouris
    action = 'reject'
888 c308d73f Stavros Sachtouris
889 c308d73f Stavros Sachtouris
890 9d84caa4 Stavros Sachtouris
@command(membership_commands)
891 9d84caa4 Stavros Sachtouris
class membership_remove(_membership_action):
892 c308d73f Stavros Sachtouris
    """Remove a membership for a project you manage"""
893 c308d73f Stavros Sachtouris
    action = 'remove'
894 c308d73f Stavros Sachtouris
895 c308d73f Stavros Sachtouris
896 9d84caa4 Stavros Sachtouris
@command(membership_commands)
897 9d84caa4 Stavros Sachtouris
class membership_join(_init_synnefo_astakosclient):
898 c308d73f Stavros Sachtouris
    """Join a project"""
899 c308d73f Stavros Sachtouris
900 c308d73f Stavros Sachtouris
    @errors.generic.all
901 c308d73f Stavros Sachtouris
    @errors.user.astakosclient
902 c308d73f Stavros Sachtouris
    def _run(self, project_id):
903 c308d73f Stavros Sachtouris
        self.writeln(self.client.join_project(project_id))
904 c308d73f Stavros Sachtouris
905 c308d73f Stavros Sachtouris
    def main(self, project_id):
906 9d84caa4 Stavros Sachtouris
        super(membership_join, self)._run()
907 c308d73f Stavros Sachtouris
        self._run(project_id)
908 c308d73f Stavros Sachtouris
909 c308d73f Stavros Sachtouris
910 9d84caa4 Stavros Sachtouris
@command(membership_commands)
911 9d84caa4 Stavros Sachtouris
class membership_enroll(_init_synnefo_astakosclient):
912 c308d73f Stavros Sachtouris
    """Enroll somebody to a project you manage"""
913 c308d73f Stavros Sachtouris
914 c308d73f Stavros Sachtouris
    @errors.generic.all
915 c308d73f Stavros Sachtouris
    @errors.user.astakosclient
916 c308d73f Stavros Sachtouris
    def _run(self, project_id, email):
917 c308d73f Stavros Sachtouris
        self.writeln(self.client.enroll_member(project_id, email))
918 c308d73f Stavros Sachtouris
919 c308d73f Stavros Sachtouris
    def main(self, project_id, email):
920 f59635ab Stavros Sachtouris
        super(membership_enroll, self)._run()
921 c308d73f Stavros Sachtouris
        self._run(project_id, email)