Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / management / cli_options.py @ 2be50766

History | View | Annotate | Download (3.8 kB)

1
# Copyright 2014 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29
#
30

    
31
import astakosclient
32
from django.conf import settings
33
from optparse import make_option, OptionValueError
34
from snf_django.management.utils import parse_bool
35

    
36

    
37
class WrappedOptions(object):
38
    """Wrapper class to provide access to options as object attributes"""
39
    def __init__(self, options_dict):
40
        self.__dict__.update(options_dict)
41

    
42

    
43
def make_boolean_option(*args, **kwargs):
44
    """Helper function to create a boolean option."""
45
    def parse_boolean_option(option, option_str, value, parser):
46
        if value is not None:
47
            try:
48
                value = parse_bool(value)
49
            except ValueError:
50
                choices = "True, False"
51
                raise OptionValueError(
52
                    "option %s: invalid choice: %r (choose from %s)"
53
                    % (option, value, choices))
54
        setattr(parser.values, option.dest, value)
55

    
56
    return make_option(*args,
57
                       metavar="True|False",
58
                       type=str,
59
                       action="callback",
60
                       callback=parse_boolean_option,
61
                       **kwargs)
62

    
63

    
64
def parse_user_option(option, option_str, value, parser):
65
    """Callback to parser -u/--user option
66

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

70
    """
71
    astakos = astakosclient.AstakosClient(settings.CYCLADES_SERVICE_TOKEN,
72
                                          settings.ASTAKOS_AUTH_URL,
73
                                          retry=2)
74
    try:
75
        if "@" in value:
76
            email = value
77
            uuid = astakos.service_get_uuid(email)
78
        else:
79
            uuid = value
80
            email = astakos.service_get_username(uuid)
81
    except astakosclient.errors.NoUUID:
82
        raise OptionValueError("User with email %r does not exist" % email)
83
    except astakosclient.errors.NoUserName:
84
        raise OptionValueError("User with uuid %r does not exist" % uuid)
85
    except astakosclient.errors.AstakosClientException as e:
86
        raise OptionValueError("Failed to get user info:\n%r" % e)
87

    
88
    setattr(parser.values, 'user_id', uuid)
89
    setattr(parser.values, 'user_email', email)
90

    
91

    
92
USER_OPT = make_option("-u", "--user",
93
                       default=None, type=str,
94
                       action="callback", callback=parse_user_option,
95
                       help="Specify the UUID or email of the user")