resource-import.py 3.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# Copyright 2013 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

from optparse import make_option

from django.core.management.base import BaseCommand, CommandError
from django.db.utils import IntegrityError
from django.utils import simplejson as json

40
from snf_django.lib.db.transaction import commit_on_success_strict
41
from astakos.im.register import add_resource, ResourceException
42
from astakos.im.models import Service
43
44
45


class Command(BaseCommand):
46
    help = "Register resources"
47
48
49
50
51

    option_list = BaseCommand.option_list + (
        make_option('--json',
                    dest='json',
                    metavar='<json.file>',
52
                    help="Load resource definitions from a json file"),
53
54
55
56
57
    )

    def handle(self, *args, **options):

        json_file = options['json']
58
59
        if not json_file:
            m = "Expecting option --json."
60
61
            raise CommandError(m)

62
        else:
63
            with open(json_file) as file_data:
64
                m = 'Input should be a JSON list.'
65
66
67
                try:
                    data = json.load(file_data)
                except json.JSONDecodeError:
68
                    raise CommandError(m)
69
                if not isinstance(data, list):
70
                    raise CommandError(m)
71
        self.add_resources(data)
72
73
74


    @commit_on_success_strict()
75
76
    def add_resources(self, resources):
        output = []
77
        for resource in resources:
78
79
            if not isinstance(resource, dict):
                raise CommandError("Malformed resource dict.")
80
81
82
83
            try:
                r, exists = add_resource(resource)
            except ResourceException as e:
                raise CommandError(e.message)
84
            name = r.name
85
86
87
88
89
            if exists:
                m = "Resource '%s' updated in database.\n" % (name)
            else:
                m = ("Resource '%s' created in database with default "
                     "quota limit 0.\n" % (name))
90
91
92
93
            output.append(m)

        for line in output:
            self.stdout.write(line)