root / snf-astakos-app / astakos / im / management / commands / resource-import.py @ 9747707e
History | View | Annotate | Download (4.3 kB)
1 |
# Copyright 2013 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 |
|
36 |
from django.core.management.base import BaseCommand, CommandError |
37 |
from django.db.utils import IntegrityError |
38 |
from django.utils import simplejson as json |
39 |
|
40 |
from synnefo.lib.db.transaction import commit_on_success_strict |
41 |
from astakos.im.resources import add_resource |
42 |
|
43 |
|
44 |
class Command(BaseCommand): |
45 |
args = "<service> <resource> <desc> <unit>"
|
46 |
help = "Import resources"
|
47 |
|
48 |
option_list = BaseCommand.option_list + ( |
49 |
make_option('--json',
|
50 |
dest='json',
|
51 |
metavar='<json.file>',
|
52 |
help="Load resource info from a json file"),
|
53 |
make_option('--service',
|
54 |
dest='service_id',
|
55 |
metavar='<service_id>',
|
56 |
help=("Automatically load resource info for a given "
|
57 |
"service")),
|
58 |
make_option('--conf',
|
59 |
dest='conf',
|
60 |
metavar='<conf.json>',
|
61 |
help="Limit configuration file"),
|
62 |
) |
63 |
|
64 |
def handle(self, *args, **options): |
65 |
|
66 |
config = {} |
67 |
conf_file = options['conf']
|
68 |
if conf_file is not None: |
69 |
with open(conf_file) as file_data: |
70 |
config = json.load(file_data) |
71 |
|
72 |
|
73 |
json_file = options['json']
|
74 |
service_id = options['service_id']
|
75 |
if bool(json_file) == bool(service_id): |
76 |
m = "Please provide either --service or --json option."
|
77 |
raise CommandError(m)
|
78 |
|
79 |
if service_id:
|
80 |
raise NotImplementedError() |
81 |
|
82 |
if json_file:
|
83 |
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."
|
89 |
raise CommandError(m)
|
90 |
|
91 |
self.add_resources(service, resources, config)
|
92 |
|
93 |
|
94 |
@commit_on_success_strict()
|
95 |
def add_resources(self, service, resources, config): |
96 |
for resource in resources: |
97 |
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) |