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