Revision a0fcfb35
b/snf-astakos-app/astakos/im/management/commands/resource-modify.py | ||
---|---|---|
35 | 35 |
from django.core.management.base import BaseCommand, CommandError |
36 | 36 |
from django.utils import simplejson as json |
37 | 37 |
|
38 |
from synnefo.webproject.management import utils |
|
38 | 39 |
from astakos.im.models import Resource |
39 | 40 |
from astakos.im.resources import update_resource |
40 | 41 |
from ._common import show_resource_value, style_options, check_style, units |
... | ... | |
42 | 43 |
|
43 | 44 |
class Command(BaseCommand): |
44 | 45 |
args = "<resource name>" |
45 |
help = ("Modify a resource (currently only change the default base quota)." |
|
46 |
"\nIf no resource is specified, all resources are considered.") |
|
46 |
help = "Modify a resource's default base quota and boolean flags." |
|
47 | 47 |
|
48 | 48 |
option_list = BaseCommand.option_list + ( |
49 | 49 |
make_option('--limit', |
50 |
dest='limit', |
|
51 | 50 |
help="Specify default base quota"), |
52 |
make_option('--interactive', |
|
51 |
make_option('--limit-interactive',
|
|
53 | 52 |
action='store_true', |
54 |
dest='interactive', |
|
55 | 53 |
default=None, |
56 |
help="Prompt user to change default base quotas"), |
|
57 |
make_option('--from-file', |
|
58 |
dest='from_file', |
|
54 |
help=("Prompt user to change default base quota. " |
|
55 |
"If no resource is given, prompts for all " |
|
56 |
"resources.")), |
|
57 |
make_option('--limit-from-file', |
|
59 | 58 |
metavar='<limits_file.json>', |
60 |
help="Read default base quotas from a json file"), |
|
59 |
help=("Read default base quota from a file. " |
|
60 |
"File should contain a json dict mapping resource " |
|
61 |
"names to limits")), |
|
61 | 62 |
make_option('--unit-style', |
62 | 63 |
default='mb', |
63 | 64 |
help=("Specify display unit for resource values " |
64 | 65 |
"(one of %s); defaults to mb") % style_options), |
66 |
make_option('--allow-in-projects', |
|
67 |
metavar='True|False', |
|
68 |
help=("Specify whether to allow this resource " |
|
69 |
"in projects.")), |
|
65 | 70 |
) |
66 | 71 |
|
67 | 72 |
def handle(self, *args, **options): |
... | ... | |
69 | 74 |
|
70 | 75 |
actions = { |
71 | 76 |
'limit': self.change_limit, |
72 |
'interactive': self.change_interactive, |
|
73 |
'from_file': self.change_from_file, |
|
77 |
'limit_interactive': self.change_interactive, |
|
78 |
'limit_from_file': self.change_from_file, |
|
79 |
'allow_in_projects': self.set_allow_in_projects, |
|
74 | 80 |
} |
75 | 81 |
|
76 | 82 |
opts = [(key, value) |
... | ... | |
78 | 84 |
if key in actions and value is not None] |
79 | 85 |
|
80 | 86 |
if len(opts) != 1: |
81 |
raise CommandError("Please provide exactly one of the options: %s." |
|
82 |
% ", ".join(actions.keys())) |
|
87 |
raise CommandError("Please provide exactly one of the options: " |
|
88 |
"--limit, --limit-interactive, " |
|
89 |
"--limit-from-file, --allow-in-projects.") |
|
83 | 90 |
|
84 | 91 |
self.unit_style = options['unit_style'] |
85 | 92 |
check_style(self.unit_style) |
... | ... | |
88 | 95 |
action = actions[key] |
89 | 96 |
action(resource_name, value) |
90 | 97 |
|
98 |
def set_allow_in_projects(self, resource_name, allow): |
|
99 |
if resource_name is None: |
|
100 |
raise CommandError("Please provide a resource name.") |
|
101 |
|
|
102 |
try: |
|
103 |
allow = utils.parse_bool(allow) |
|
104 |
except ValueError: |
|
105 |
raise CommandError("Expecting a boolean value.") |
|
106 |
resource = self.get_resource(resource_name) |
|
107 |
resource.allow_in_projects = allow |
|
108 |
resource.save() |
|
109 |
|
|
91 | 110 |
def get_resource(self, resource_name): |
92 | 111 |
try: |
93 | 112 |
return Resource.objects.get_for_update(name=resource_name) |
Also available in: Unified diff