root / snf-astakos-app / astakos / im / management / commands / resource-modify.py @ bf644f91
History | View | Annotate | Download (4.8 kB)
1 |
# Copyright 2013, 2014 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.
|
33 |
|
34 |
from optparse import make_option |
35 |
from django.core.management.base import BaseCommand, CommandError |
36 |
|
37 |
from snf_django.management import utils |
38 |
from astakos.im.models import Resource |
39 |
from astakos.im import register |
40 |
from ._common import style_options, check_style, units |
41 |
|
42 |
|
43 |
class Command(BaseCommand): |
44 |
args = "<resource name>"
|
45 |
help = "Modify a resource's default base quota and boolean flags."
|
46 |
|
47 |
option_list = BaseCommand.option_list + ( |
48 |
make_option('--base-default',
|
49 |
metavar='<limit>',
|
50 |
help="Specify default base quota"),
|
51 |
make_option('--unit-style',
|
52 |
default='mb',
|
53 |
help=("Specify display unit for resource values "
|
54 |
"(one of %s); defaults to mb") % style_options),
|
55 |
make_option('--api-visible',
|
56 |
metavar='True|False',
|
57 |
help="Control visibility of this resource in the API"),
|
58 |
make_option('--ui-visible',
|
59 |
metavar='True|False',
|
60 |
help="Control visibility of this resource in the UI"),
|
61 |
) |
62 |
|
63 |
def handle(self, *args, **options): |
64 |
resource_name = args[0] if len(args) > 0 else None |
65 |
if resource_name is None: |
66 |
raise CommandError("Please provide a resource name.") |
67 |
resource = self.get_resource(resource_name)
|
68 |
|
69 |
actions = { |
70 |
'base_default': self.change_base_default, |
71 |
'api_visible': self.set_api_visible, |
72 |
'ui_visible': self.set_ui_visible, |
73 |
} |
74 |
|
75 |
opts = [(key, value) |
76 |
for (key, value) in options.items() |
77 |
if key in actions and value is not None] |
78 |
|
79 |
self.unit_style = options['unit_style'] |
80 |
check_style(self.unit_style)
|
81 |
|
82 |
for key, value in opts: |
83 |
action = actions[key] |
84 |
action(resource, value) |
85 |
|
86 |
def set_api_visible(self, resource, allow): |
87 |
try:
|
88 |
allow = utils.parse_bool(allow) |
89 |
except ValueError: |
90 |
raise CommandError("Expecting a boolean value.") |
91 |
resource.api_visible = allow |
92 |
if not allow and resource.ui_visible: |
93 |
self.stderr.write("Also resetting 'ui_visible' for consistency.\n") |
94 |
resource.ui_visible = False
|
95 |
resource.save() |
96 |
|
97 |
def set_ui_visible(self, resource, allow): |
98 |
try:
|
99 |
allow = utils.parse_bool(allow) |
100 |
except ValueError: |
101 |
raise CommandError("Expecting a boolean value.") |
102 |
resource.ui_visible = allow |
103 |
if allow and not resource.api_visible: |
104 |
self.stderr.write("Also setting 'api_visible' for consistency.\n") |
105 |
resource.api_visible = True
|
106 |
resource.save() |
107 |
|
108 |
def get_resource(self, resource_name): |
109 |
try:
|
110 |
return Resource.objects.select_for_update().get(name=resource_name)
|
111 |
except Resource.DoesNotExist:
|
112 |
raise CommandError("Resource %s does not exist." |
113 |
% resource_name) |
114 |
|
115 |
def change_base_default(self, resource, limit): |
116 |
limit = self.parse_limit(limit)
|
117 |
register.update_base_default(resource, limit) |
118 |
|
119 |
def parse_limit(self, limit): |
120 |
try:
|
121 |
return units.parse(limit)
|
122 |
except units.ParseError:
|
123 |
m = ("Quota limit should be an integer, "
|
124 |
"optionally followed by a unit, or 'inf'.")
|
125 |
raise CommandError(m)
|