def add_services(self, data):
        write = self.stderr.write
        output = []
        for name, service_dict in data.iteritems():
            try:
                component_name = service_dict['component']
                service_type = service_dict['type']
                endpoints = service_dict['endpoints']
            except KeyError:
                raise CommandError('Malformed service definition.')

            try:
                component = Component.objects.get(name=component_name)
            except Component.DoesNotExist:
                m = "Component '%s' is not registered." % component_name
                raise CommandError(m)

            try:
                existed = add_service(component,
                                      name,
                                      service_type,
                                      endpoints,
                                      out=self.stderr)
            except RegisterException as e:
                raise CommandError(e.message)

            m = "%s service %s.\n" % ("Updated" if existed else "Added", name)
            output.append(m)

            resources = service_dict.get('resources', {}).values()
            for resource in resources:
                if not isinstance(resource, dict):
                    raise CommandError("Malformed resource dict.")

                service_origin = resource.get('service_origin')
                if name != service_origin:
                    raise CommandError("service_origin mismatch.")
                try:
                    r, exists = add_resource(resource)
                except RegisterException as e:
                    raise CommandError(e.message)
                if exists:
                    m = "Resource '%s' updated in database.\n" % (r.name)
                else:
                    m = ("Resource '%s' created in database with unlimited "
                         "quota.\n" % (r.name))
                output.append(m)

        for line in output:
            write(line)
Exemple #2
0
    def add_services(self, data):
        write = self.stderr.write
        output = []
        for name, service_dict in data.iteritems():
            try:
                component_name = service_dict['component']
                service_type = service_dict['type']
                endpoints = service_dict['endpoints']
            except KeyError:
                raise CommandError('Malformed service definition.')

            try:
                component = Component.objects.get(name=component_name)
            except Component.DoesNotExist:
                m = "Component '%s' is not registered." % component_name
                raise CommandError(m)

            try:
                existed = add_service(component, name, service_type, endpoints,
                                      out=self.stderr)
            except RegisterException as e:
                raise CommandError(e.message)

            m = "%s service %s.\n" % ("Updated" if existed else "Added", name)
            output.append(m)

            resources = service_dict.get('resources', {}).values()
            for resource in resources:
                if not isinstance(resource, dict):
                    raise CommandError("Malformed resource dict.")

                service_origin = resource.get('service_origin')
                if name != service_origin:
                    raise CommandError("service_origin mismatch.")
                try:
                    r, exists = add_resource(resource)
                except RegisterException as e:
                    raise CommandError(e.message)
                if exists:
                    m = "Resource '%s' updated in database.\n" % (r.name)
                else:
                    m = ("Resource '%s' created in database with unlimited "
                         "quota.\n" % (r.name))
                output.append(m)

        for line in output:
            write(line)