Commit 9ef530af authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

astakos: Record the service originating a resource

Add field service_origin in model Resource as well as in the
resource definitions, indicating the service where the resource
comes from. It is not a foreign key, in order to allow deleting
a component/service, without affecting registered resources and
quota.

API call /service_quotas uses this field to identify relevant
resources.
parent a80d4e96
......@@ -33,13 +33,13 @@ Status Description
"cyclades.vm": {
"unit": null,
"description": "Number of virtual machines",
"service": "cyclades",
"service": "cyclades_compute",
"allow_in_projects": true
},
"cyclades.ram": {
"unit": "bytes",
"description": "Virtual machine memory",
"service": "cyclades",
"service": "cyclades_compute",
"allow_in_projects": true
}
}
......
......@@ -51,14 +51,15 @@ class Command(ListCommand):
FIELDS = {
"id": ("id", "ID"),
"name": ("name", "Resource Name"),
"service type": ("service_type", "Service"),
"service type": ("service_type", "Service type"),
"service origin": ("service_origin", "Service"),
"limit": ("limit_with_unit", "Base Quota"),
"description": ("desc", "Description"),
"allow_in_projects": ("allow_in_projects",
"Make resource available in projects"),
}
fields = ["id", "name", "service type", "limit", "allow_in_projects",
fields = ["id", "name", "service origin", "limit",
"description"]
def show_limit(self, resource):
......
This diff is collapsed.
......@@ -211,6 +211,7 @@ class Resource(models.Model):
name = models.CharField(_('Name'), max_length=255, unique=True)
desc = models.TextField(_('Description'), null=True)
service_type = models.CharField(_('Type'), max_length=255)
service_origin = models.CharField(max_length=255, db_index=True)
unit = models.CharField(_('Unit'), null=True, max_length=255)
uplimit = intDecimalField(default=0)
allow_in_projects = models.BooleanField(default=True)
......@@ -224,7 +225,7 @@ class Resource(models.Model):
return str(self)
def get_info(self):
return {'service_type': self.service_type,
return {'service': self.service_origin,
'description': self.desc,
'unit': self.unit,
'allow_in_projects': self.allow_in_projects,
......
......@@ -96,10 +96,10 @@ def get_user_quotas(user, resources=None, sources=None):
def service_get_quotas(component, users=None):
type_values = Service.objects.filter(
component=component).values_list('type')
service_types = [t for (t,) in type_values]
resources = Resource.objects.filter(service_type__in=service_types)
name_values = Service.objects.filter(
component=component).values_list('name')
service_names = [t for (t,) in name_values]
resources = Resource.objects.filter(service_origin__in=service_names)
resource_names = [r.name for r in resources]
counters = qh.get_quota(holders=users, resources=resource_names)
return transform_data(counters)
......
......@@ -48,9 +48,16 @@ class ResourceException(Exception):
def add_resource(resource_dict):
name = resource_dict.get('name')
service_type = resource_dict.get('service_type')
if not name or not 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.")
try:
service = Service.objects.get(name=service_origin)
except Service.DoesNotExist:
m = "There is no service %s." % service_origin
raise ResourceException(m)
try:
r = Resource.objects.get_for_update(name=name)
exists = True
......@@ -58,10 +65,16 @@ def add_resource(resource_dict):
m = ("There already exists a resource named %s with service "
"type %s." % (name, r.service_type))
raise ResourceException(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)
except Resource.DoesNotExist:
r = Resource(name=name,
uplimit=0,
service_type=service_type)
service_type=service_type,
service_origin=service_origin)
exists = False
for field in resource_fields:
......
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