Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / management / cli_options.py @ 1ec2f17e

History | View | Annotate | Download (3.8 kB)

1 2be50766 Christos Stavrakakis
# Copyright 2014 GRNET S.A. All rights reserved.
2 2be50766 Christos Stavrakakis
#
3 2be50766 Christos Stavrakakis
# Redistribution and use in source and binary forms, with or without
4 2be50766 Christos Stavrakakis
# modification, are permitted provided that the following conditions
5 2be50766 Christos Stavrakakis
# are met:
6 2be50766 Christos Stavrakakis
#
7 2be50766 Christos Stavrakakis
#   1. Redistributions of source code must retain the above copyright
8 2be50766 Christos Stavrakakis
#      notice, this list of conditions and the following disclaimer.
9 2be50766 Christos Stavrakakis
#
10 2be50766 Christos Stavrakakis
#  2. Redistributions in binary form must reproduce the above copyright
11 2be50766 Christos Stavrakakis
#     notice, this list of conditions and the following disclaimer in the
12 2be50766 Christos Stavrakakis
#     documentation and/or other materials provided with the distribution.
13 2be50766 Christos Stavrakakis
#
14 2be50766 Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 2be50766 Christos Stavrakakis
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 2be50766 Christos Stavrakakis
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 2be50766 Christos Stavrakakis
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 2be50766 Christos Stavrakakis
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 2be50766 Christos Stavrakakis
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 2be50766 Christos Stavrakakis
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 2be50766 Christos Stavrakakis
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 2be50766 Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 2be50766 Christos Stavrakakis
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 2be50766 Christos Stavrakakis
# SUCH DAMAGE.
25 2be50766 Christos Stavrakakis
#
26 2be50766 Christos Stavrakakis
# The views and conclusions contained in the software and documentation are
27 2be50766 Christos Stavrakakis
# those of the authors and should not be interpreted as representing official
28 2be50766 Christos Stavrakakis
# policies, either expressed or implied, of GRNET S.A.
29 2be50766 Christos Stavrakakis
#
30 2be50766 Christos Stavrakakis
31 2be50766 Christos Stavrakakis
import astakosclient
32 2be50766 Christos Stavrakakis
from django.conf import settings
33 2be50766 Christos Stavrakakis
from optparse import make_option, OptionValueError
34 2be50766 Christos Stavrakakis
from snf_django.management.utils import parse_bool
35 2be50766 Christos Stavrakakis
36 2be50766 Christos Stavrakakis
37 2be50766 Christos Stavrakakis
class WrappedOptions(object):
38 2be50766 Christos Stavrakakis
    """Wrapper class to provide access to options as object attributes"""
39 2be50766 Christos Stavrakakis
    def __init__(self, options_dict):
40 2be50766 Christos Stavrakakis
        self.__dict__.update(options_dict)
41 2be50766 Christos Stavrakakis
42 2be50766 Christos Stavrakakis
43 2be50766 Christos Stavrakakis
def make_boolean_option(*args, **kwargs):
44 2be50766 Christos Stavrakakis
    """Helper function to create a boolean option."""
45 2be50766 Christos Stavrakakis
    def parse_boolean_option(option, option_str, value, parser):
46 2be50766 Christos Stavrakakis
        if value is not None:
47 2be50766 Christos Stavrakakis
            try:
48 2be50766 Christos Stavrakakis
                value = parse_bool(value)
49 2be50766 Christos Stavrakakis
            except ValueError:
50 2be50766 Christos Stavrakakis
                choices = "True, False"
51 2be50766 Christos Stavrakakis
                raise OptionValueError(
52 2be50766 Christos Stavrakakis
                    "option %s: invalid choice: %r (choose from %s)"
53 2be50766 Christos Stavrakakis
                    % (option, value, choices))
54 2be50766 Christos Stavrakakis
        setattr(parser.values, option.dest, value)
55 2be50766 Christos Stavrakakis
56 2be50766 Christos Stavrakakis
    return make_option(*args,
57 2be50766 Christos Stavrakakis
                       metavar="True|False",
58 2be50766 Christos Stavrakakis
                       type=str,
59 2be50766 Christos Stavrakakis
                       action="callback",
60 2be50766 Christos Stavrakakis
                       callback=parse_boolean_option,
61 2be50766 Christos Stavrakakis
                       **kwargs)
62 2be50766 Christos Stavrakakis
63 2be50766 Christos Stavrakakis
64 2be50766 Christos Stavrakakis
def parse_user_option(option, option_str, value, parser):
65 2be50766 Christos Stavrakakis
    """Callback to parser -u/--user option
66 2be50766 Christos Stavrakakis

67 2be50766 Christos Stavrakakis
    Translate uuid <-> email and add 'user_id' and 'user_email' to command
68 2be50766 Christos Stavrakakis
    options.
69 2be50766 Christos Stavrakakis

70 2be50766 Christos Stavrakakis
    """
71 2be50766 Christos Stavrakakis
    astakos = astakosclient.AstakosClient(settings.CYCLADES_SERVICE_TOKEN,
72 2be50766 Christos Stavrakakis
                                          settings.ASTAKOS_AUTH_URL,
73 2be50766 Christos Stavrakakis
                                          retry=2)
74 2be50766 Christos Stavrakakis
    try:
75 2be50766 Christos Stavrakakis
        if "@" in value:
76 2be50766 Christos Stavrakakis
            email = value
77 2be50766 Christos Stavrakakis
            uuid = astakos.service_get_uuid(email)
78 2be50766 Christos Stavrakakis
        else:
79 2be50766 Christos Stavrakakis
            uuid = value
80 2be50766 Christos Stavrakakis
            email = astakos.service_get_username(uuid)
81 2be50766 Christos Stavrakakis
    except astakosclient.errors.NoUUID:
82 2be50766 Christos Stavrakakis
        raise OptionValueError("User with email %r does not exist" % email)
83 2be50766 Christos Stavrakakis
    except astakosclient.errors.NoUserName:
84 2be50766 Christos Stavrakakis
        raise OptionValueError("User with uuid %r does not exist" % uuid)
85 2be50766 Christos Stavrakakis
    except astakosclient.errors.AstakosClientException as e:
86 2be50766 Christos Stavrakakis
        raise OptionValueError("Failed to get user info:\n%r" % e)
87 2be50766 Christos Stavrakakis
88 2be50766 Christos Stavrakakis
    setattr(parser.values, 'user_id', uuid)
89 2be50766 Christos Stavrakakis
    setattr(parser.values, 'user_email', email)
90 2be50766 Christos Stavrakakis
91 2be50766 Christos Stavrakakis
92 2be50766 Christos Stavrakakis
USER_OPT = make_option("-u", "--user",
93 2be50766 Christos Stavrakakis
                       default=None, type=str,
94 2be50766 Christos Stavrakakis
                       action="callback", callback=parse_user_option,
95 2be50766 Christos Stavrakakis
                       help="Specify the UUID or email of the user")