Revision 3a527b3a snf-astakos-app/astakos/im/management/commands/resource-import.py
b/snf-astakos-app/astakos/im/management/commands/resource-import.py | ||
---|---|---|
39 | 39 |
|
40 | 40 |
from snf_django.lib.db.transaction import commit_on_success_strict |
41 | 41 |
from astakos.im.resources import add_resource |
42 |
from astakos.im.models import Service |
|
42 | 43 |
|
43 | 44 |
|
44 | 45 |
class Command(BaseCommand): |
... | ... | |
81 | 82 |
|
82 | 83 |
if json_file: |
83 | 84 |
with open(json_file) as file_data: |
84 |
data = json.load(file_data)
|
|
85 |
service = data.get('service')
|
|
86 |
resources = data.get('resources')
|
|
87 |
if service is None or resources is None:
|
|
88 |
m = "JSON file should contain service and resource data."
|
|
85 |
m = ('Input should be a JSON dict containing "service" '
|
|
86 |
'and "resource" keys.')
|
|
87 |
try:
|
|
88 |
data = json.load(file_data)
|
|
89 |
except json.JSONDecodeError:
|
|
89 | 90 |
raise CommandError(m) |
91 |
if not isinstance(data, dict): |
|
92 |
raise CommandError(m) |
|
93 |
else: |
|
94 |
try: |
|
95 |
service = data['service'] |
|
96 |
resources = data['resources'] |
|
97 |
except KeyError: |
|
98 |
raise CommandError(m) |
|
90 | 99 |
|
91 |
self.add_resources(service, resources, config)
|
|
100 |
self.add_resources(service, resources) |
|
92 | 101 |
|
93 | 102 |
|
94 | 103 |
@commit_on_success_strict() |
95 |
def add_resources(self, service, resources, config): |
|
104 |
def add_resources(self, service, resources): |
|
105 |
|
|
106 |
try: |
|
107 |
s = Service.objects.get(name=service) |
|
108 |
except Service.DoesNotExist: |
|
109 |
raise CommandError("Service '%s' is not registered." % (service)) |
|
110 |
|
|
96 | 111 |
for resource in resources: |
112 |
if not isinstance(resource, dict): |
|
113 |
raise CommandError("Malformed resource dict.") |
|
114 |
exists = add_resource(s, resource) |
|
97 | 115 |
name = resource['name'] |
98 |
uplimit = config.get(name) |
|
99 |
if uplimit is None: |
|
100 |
desc = resource['desc'] |
|
101 |
unit = resource.get('unit') |
|
102 |
self.stdout.write( |
|
103 |
"Provide default base quota for resource '%s' (%s)" % |
|
104 |
(name, desc)) |
|
105 |
m = (" in %s: " % unit) if unit else ": " |
|
106 |
self.stdout.write(m) |
|
107 |
uplimit = raw_input() |
|
108 |
|
|
109 |
try: |
|
110 |
uplimit = int(uplimit) |
|
111 |
except ValueError: |
|
112 |
m = "Limit for resource %s is not an integer." % (name) |
|
113 |
raise CommandError(m) |
|
114 |
|
|
115 |
add_resource(service, resource, uplimit) |
|
116 |
if exists: |
|
117 |
m = "Resource '%s' updated in database.\n" % (name) |
|
118 |
else: |
|
119 |
m = ("Resource '%s' created in database with default " |
|
120 |
"quota limit 0.\n" % (name)) |
|
121 |
self.stdout.write(m) |
Also available in: Unified diff