Commit fd50252f authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

astakos: Register resources with services

If service-import finds resource definitions included in a service
definition, it imports them, too.
parent 9ef530af
......@@ -37,7 +37,7 @@ from django.core.management.base import BaseCommand, CommandError
from django.utils import simplejson as json
from snf_django.lib.db.transaction import commit_on_success_strict
from astakos.im.register import add_resource, ResourceException
from astakos.im.register import add_resource, RegisterException
from ._common import read_from_file
......@@ -77,7 +77,7 @@ class Command(BaseCommand):
raise CommandError("Malformed resource dict.")
try:
r, exists = add_resource(resource)
except ResourceException as e:
except RegisterException as e:
raise CommandError(e.message)
name = r.name
if exists:
......
......@@ -37,7 +37,7 @@ from django.core.management.base import BaseCommand, CommandError
from django.utils import simplejson as json
from snf_django.lib.db.transaction import commit_on_success_strict
from astakos.im.register import add_service, ServiceException
from astakos.im.register import add_service, add_resource, RegisterException
from astakos.im.models import Component
from ._common import read_from_file
......@@ -91,11 +91,30 @@ class Command(BaseCommand):
try:
existed = add_service(component, name, service_type, endpoints)
except ServiceException as e:
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 default "
"quota limit 0.\n" % (r.name))
output.append(m)
for line in output:
write(line)
......@@ -41,7 +41,7 @@ logger = logging.getLogger(__name__)
resource_fields = ['desc', 'unit', 'allow_in_projects']
class ResourceException(Exception):
class RegisterException(Exception):
pass
......@@ -50,13 +50,13 @@ def add_resource(resource_dict):
service_type = resource_dict.get('service_type')
service_origin = resource_dict.get('service_origin')
if not name or not service_type or not service_origin:
raise ResourceException("Malformed resource dict.")
raise RegisterException("Malformed resource dict.")
try:
service = Service.objects.get(name=service_origin)
except Service.DoesNotExist:
m = "There is no service %s." % service_origin
raise ResourceException(m)
raise RegisterException(m)
try:
r = Resource.objects.get_for_update(name=name)
......@@ -64,11 +64,11 @@ def add_resource(resource_dict):
if r.service_type != service_type:
m = ("There already exists a resource named %s with service "
"type %s." % (name, r.service_type))
raise ResourceException(m)
raise RegisterException(m)
if r.service_origin != service_origin:
m = ("There already exists a resource named %s registered for "
"service %s." % (name, r.service_origin))
raise ResourceException(m)
raise RegisterException(m)
except Resource.DoesNotExist:
r = Resource(name=name,
......@@ -128,10 +128,6 @@ def add_endpoint(service, endpoint_dict):
endpoint=endpoint, key=key, value=value)
class ServiceException(Exception):
pass
def add_service(component, name, service_type, endpoints):
defaults = {'component': component,
'type': service_type,
......@@ -143,7 +139,7 @@ def add_service(component, name, service_type, endpoints):
if service.component != component:
m = ("There is already a service named %s registered by %s." %
(name, service.component.name))
raise ServiceException(m)
raise RegisterException(m)
service.endpoints.all().delete()
else:
service.component = component
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment