Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / management / commands / service-import.py @ def409e0

History | View | Annotate | Download (4.6 kB)

1 bd1f667b Giorgos Korfiatis
# Copyright 2013 GRNET S.A. All rights reserved.
2 bd1f667b Giorgos Korfiatis
#
3 bd1f667b Giorgos Korfiatis
# Redistribution and use in source and binary forms, with or
4 bd1f667b Giorgos Korfiatis
# without modification, are permitted provided that the following
5 bd1f667b Giorgos Korfiatis
# conditions are met:
6 bd1f667b Giorgos Korfiatis
#
7 bd1f667b Giorgos Korfiatis
#   1. Redistributions of source code must retain the above
8 bd1f667b Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
9 bd1f667b Giorgos Korfiatis
#      disclaimer.
10 bd1f667b Giorgos Korfiatis
#
11 bd1f667b Giorgos Korfiatis
#   2. Redistributions in binary form must reproduce the above
12 bd1f667b Giorgos Korfiatis
#      copyright notice, this list of conditions and the following
13 bd1f667b Giorgos Korfiatis
#      disclaimer in the documentation and/or other materials
14 bd1f667b Giorgos Korfiatis
#      provided with the distribution.
15 bd1f667b Giorgos Korfiatis
#
16 bd1f667b Giorgos Korfiatis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 bd1f667b Giorgos Korfiatis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 bd1f667b Giorgos Korfiatis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 bd1f667b Giorgos Korfiatis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 bd1f667b Giorgos Korfiatis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 bd1f667b Giorgos Korfiatis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 bd1f667b Giorgos Korfiatis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 bd1f667b Giorgos Korfiatis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 bd1f667b Giorgos Korfiatis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 bd1f667b Giorgos Korfiatis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 bd1f667b Giorgos Korfiatis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 bd1f667b Giorgos Korfiatis
# POSSIBILITY OF SUCH DAMAGE.
28 bd1f667b Giorgos Korfiatis
#
29 bd1f667b Giorgos Korfiatis
# The views and conclusions contained in the software and
30 bd1f667b Giorgos Korfiatis
# documentation are those of the authors and should not be
31 bd1f667b Giorgos Korfiatis
# interpreted as representing official policies, either expressed
32 bd1f667b Giorgos Korfiatis
# or implied, of GRNET S.A.
33 bd1f667b Giorgos Korfiatis
34 bd1f667b Giorgos Korfiatis
from optparse import make_option
35 bd1f667b Giorgos Korfiatis
36 a3e3917f Giorgos Korfiatis
from django.db import transaction
37 bd1f667b Giorgos Korfiatis
from django.core.management.base import BaseCommand, CommandError
38 bd1f667b Giorgos Korfiatis
from django.utils import simplejson as json
39 bd1f667b Giorgos Korfiatis
40 38807757 Giorgos Korfiatis
from astakos.im.register import add_service, add_resource, RegisterException
41 bd1f667b Giorgos Korfiatis
from astakos.im.models import Component
42 c25f5ed0 Giorgos Korfiatis
from ._common import read_from_file
43 bd1f667b Giorgos Korfiatis
44 bd1f667b Giorgos Korfiatis
45 bd1f667b Giorgos Korfiatis
class Command(BaseCommand):
46 bd1f667b Giorgos Korfiatis
    help = "Register services"
47 bd1f667b Giorgos Korfiatis
48 bd1f667b Giorgos Korfiatis
    option_list = BaseCommand.option_list + (
49 bd1f667b Giorgos Korfiatis
        make_option('--json',
50 bd1f667b Giorgos Korfiatis
                    dest='json',
51 bd1f667b Giorgos Korfiatis
                    metavar='<json.file>',
52 bd1f667b Giorgos Korfiatis
                    help="Load service definitions from a json file"),
53 bd1f667b Giorgos Korfiatis
    )
54 bd1f667b Giorgos Korfiatis
55 a3e3917f Giorgos Korfiatis
    @transaction.commit_on_success
56 bd1f667b Giorgos Korfiatis
    def handle(self, *args, **options):
57 bd1f667b Giorgos Korfiatis
58 bd1f667b Giorgos Korfiatis
        json_file = options['json']
59 bd1f667b Giorgos Korfiatis
        if not json_file:
60 bd1f667b Giorgos Korfiatis
            m = "Expecting option --json."
61 bd1f667b Giorgos Korfiatis
            raise CommandError(m)
62 bd1f667b Giorgos Korfiatis
63 bd1f667b Giorgos Korfiatis
        else:
64 c25f5ed0 Giorgos Korfiatis
            data = read_from_file(json_file)
65 c25f5ed0 Giorgos Korfiatis
            m = ('Input should be a JSON dict mapping service names '
66 c25f5ed0 Giorgos Korfiatis
                 'to definitions.')
67 c25f5ed0 Giorgos Korfiatis
            try:
68 c25f5ed0 Giorgos Korfiatis
                data = json.loads(data)
69 c25f5ed0 Giorgos Korfiatis
            except json.JSONDecodeError:
70 c25f5ed0 Giorgos Korfiatis
                raise CommandError(m)
71 c25f5ed0 Giorgos Korfiatis
            if not isinstance(data, dict):
72 c25f5ed0 Giorgos Korfiatis
                raise CommandError(m)
73 bd1f667b Giorgos Korfiatis
        self.add_services(data)
74 bd1f667b Giorgos Korfiatis
75 bd1f667b Giorgos Korfiatis
    def add_services(self, data):
76 d9a7f0bb Giorgos Korfiatis
        write = self.stderr.write
77 bd1f667b Giorgos Korfiatis
        output = []
78 bd1f667b Giorgos Korfiatis
        for name, service_dict in data.iteritems():
79 bd1f667b Giorgos Korfiatis
            try:
80 bd1f667b Giorgos Korfiatis
                component_name = service_dict['component']
81 bd1f667b Giorgos Korfiatis
                service_type = service_dict['type']
82 bd1f667b Giorgos Korfiatis
                endpoints = service_dict['endpoints']
83 bd1f667b Giorgos Korfiatis
            except KeyError:
84 bd1f667b Giorgos Korfiatis
                raise CommandError('Malformed service definition.')
85 bd1f667b Giorgos Korfiatis
86 bd1f667b Giorgos Korfiatis
            try:
87 bd1f667b Giorgos Korfiatis
                component = Component.objects.get(name=component_name)
88 bd1f667b Giorgos Korfiatis
            except Component.DoesNotExist:
89 bd1f667b Giorgos Korfiatis
                m = "Component '%s' is not registered." % component_name
90 bd1f667b Giorgos Korfiatis
                raise CommandError(m)
91 bd1f667b Giorgos Korfiatis
92 bd1f667b Giorgos Korfiatis
            try:
93 eb765213 Giorgos Korfiatis
                existed = add_service(component, name, service_type, endpoints,
94 d9a7f0bb Giorgos Korfiatis
                                      out=self.stderr)
95 38807757 Giorgos Korfiatis
            except RegisterException as e:
96 bd1f667b Giorgos Korfiatis
                raise CommandError(e.message)
97 bd1f667b Giorgos Korfiatis
98 bd1f667b Giorgos Korfiatis
            m = "%s service %s.\n" % ("Updated" if existed else "Added", name)
99 bd1f667b Giorgos Korfiatis
            output.append(m)
100 bd1f667b Giorgos Korfiatis
101 38807757 Giorgos Korfiatis
            resources = service_dict.get('resources', {}).values()
102 38807757 Giorgos Korfiatis
            for resource in resources:
103 38807757 Giorgos Korfiatis
                if not isinstance(resource, dict):
104 38807757 Giorgos Korfiatis
                    raise CommandError("Malformed resource dict.")
105 38807757 Giorgos Korfiatis
106 38807757 Giorgos Korfiatis
                service_origin = resource.get('service_origin')
107 38807757 Giorgos Korfiatis
                if name != service_origin:
108 38807757 Giorgos Korfiatis
                    raise CommandError("service_origin mismatch.")
109 38807757 Giorgos Korfiatis
                try:
110 38807757 Giorgos Korfiatis
                    r, exists = add_resource(resource)
111 38807757 Giorgos Korfiatis
                except RegisterException as e:
112 38807757 Giorgos Korfiatis
                    raise CommandError(e.message)
113 38807757 Giorgos Korfiatis
                if exists:
114 38807757 Giorgos Korfiatis
                    m = "Resource '%s' updated in database.\n" % (r.name)
115 38807757 Giorgos Korfiatis
                else:
116 2e46be99 Giorgos Korfiatis
                    m = ("Resource '%s' created in database with unlimited "
117 2e46be99 Giorgos Korfiatis
                         "quota.\n" % (r.name))
118 38807757 Giorgos Korfiatis
                output.append(m)
119 38807757 Giorgos Korfiatis
120 bd1f667b Giorgos Korfiatis
        for line in output:
121 bd1f667b Giorgos Korfiatis
            write(line)