resource-import.py 4.26 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 40
# 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

from synnefo.lib.db.transaction import commit_on_success_strict
41
from astakos.im.resources import add_resource
42 43 44 45 46 47 48 49 50 51 52


class Command(BaseCommand):
    args = "<service> <resource> <desc> <unit>"
    help = "Import resources"

    option_list = BaseCommand.option_list + (
        make_option('--json',
                    dest='json',
                    metavar='<json.file>',
                    help="Load resource info from a json file"),
53 54 55 56 57
        make_option('--service',
                    dest='service_id',
                    metavar='<service_id>',
                    help=("Automatically load resource info for a given "
                          "service")),
58 59 60 61 62 63 64 65
        make_option('--conf',
                    dest='conf',
                    metavar='<conf.json>',
                    help="Limit configuration file"),
    )

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

66
        config = {}
67
        conf_file = options['conf']
68 69 70
        if conf_file is not None:
            with open(conf_file) as file_data:
                config = json.load(file_data)
71 72 73


        json_file = options['json']
74 75 76 77 78 79 80 81
        service_id = options['service_id']
        if bool(json_file) == bool(service_id):
            m = "Please provide either --service or --json option."
            raise CommandError(m)

        if service_id:
            raise NotImplementedError()

82 83 84
        if json_file:
            with open(json_file) as file_data:
                data = json.load(file_data)
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
                service = data.get('service')
                resources = data.get('resources')
                if service is None or resources is None:
                    m = "JSON file should contain service and resource data."
                    raise CommandError(m)

        self.add_resources(service, resources, config)


    @commit_on_success_strict()
    def add_resources(self, service, resources, config):
        for resource in resources:
            name = resource['name']
            uplimit = config.get(name)
            if uplimit is None:
                desc = resource['desc']
                unit = resource.get('unit')
                self.stdout.write(
                    "Provide default base quota for resource '%s' (%s)" %
                    (name, desc))
                m = (" in %s: " % unit) if unit else ": "
                self.stdout.write(m)
                uplimit = raw_input()
108

109 110 111 112
            try:
                uplimit = int(uplimit)
            except ValueError:
                m = "Limit for resource %s is not an integer." % (name)
113 114
                raise CommandError(m)

115
            add_resource(service, resource, uplimit)