Commit 0f34885e by Kálmán Viktor

occi: look for missing attributes

parent d61fb9f3
...@@ -2,12 +2,8 @@ import re ...@@ -2,12 +2,8 @@ import re
import logging import logging
from django.template.loader import render_to_string from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.formats import date_format
from django.utils.translation import ugettext_noop from django.utils.translation import ugettext_noop
from django_sshkey.models import UserKey
from firewall.models import Vlan from firewall.models import Vlan
from storage.models import Disk from storage.models import Disk
from vm.models import Instance, InstanceTemplate, Lease, Interface from vm.models import Instance, InstanceTemplate, Lease, Interface
...@@ -60,6 +56,15 @@ occi_inline_attribute_regex = '.*%(attribute)s="?(?P<value>[\w/\.:#\-]+)"?.*' ...@@ -60,6 +56,15 @@ occi_inline_attribute_regex = '.*%(attribute)s="?(?P<value>[\w/\.:#\-]+)"?.*'
occi_attribute_link_regex = '^/%s/(?P<id>\d+)/?' occi_attribute_link_regex = '^/%s/(?P<id>\d+)/?'
def check_missing_attributes(required, given):
subset = set(required) - set(given.keys())
if subset:
raise humanize_exception(ugettext_noop(
"Missing attribute(s): %s" % ", ".join(subset)),
Exception()
)
class Category(): class Category():
"""Represents a Category object """Represents a Category object
...@@ -177,12 +182,21 @@ class Compute(Resource): ...@@ -177,12 +182,21 @@ class Compute(Resource):
params['owner'] = user params['owner'] = user
title = attributes.get("occi.core.title") title = attributes.get("occi.core.title")
if title: if title:
params['name'] = title params['name'] = title.decode("utf-8")
if template: if template:
inst = Instance.create_from_template(template=template, **params) inst = Instance.create_from_template(template=template, **params)
else: else:
# trivial # trivial
required = [
"occi.compute.architecture",
"occi.compute.cores",
"occi.compute.speed",
"occi.compute.memory",
"occi.core.title"
]
check_missing_attributes(required, attributes)
if "x86" in attributes['occi.compute.architecture']: if "x86" in attributes['occi.compute.architecture']:
params['arch'] = X86_ARCH params['arch'] = X86_ARCH
else: else:
...@@ -206,10 +220,6 @@ class Compute(Resource): ...@@ -206,10 +220,6 @@ class Compute(Resource):
params['lease'] = Lease.objects.all()[0] params['lease'] = Lease.objects.all()[0]
params['access_method'] = ACCESS_METHODS[0][0] params['access_method'] = ACCESS_METHODS[0][0]
# if no name is given
if not params.get("name"):
params['name'] = "Created via OCCI by %s" % user
inst = Instance.create(params=params, disks=[], networks=[], inst = Instance.create(params=params, disks=[], networks=[],
req_traits=[], tags=[]) req_traits=[], tags=[])
...@@ -287,14 +297,12 @@ class Compute(Resource): ...@@ -287,14 +297,12 @@ class Compute(Resource):
"inactive") "inactive")
def trigger_action(self, data, user): def trigger_action(self, data, user):
method = None attributes = {}
action_term = None action_term = None
for d in data: for d in data:
m = occi_attribute_regex.match(d) attr = occi_attribute_regex.match(d)
if m: if attr:
attribute = m.group("attribute") attributes[attr.group("attribute")] = attr.group("value")
if attribute == "method":
method = m.group("value")
m = occi_action_regex.match(d) m = occi_action_regex.match(d)
if m: if m:
...@@ -307,11 +315,8 @@ class Compute(Resource): ...@@ -307,11 +315,8 @@ class Compute(Resource):
operation = "deploy" operation = "deploy"
else: else:
action = compute_action_to_operation.get(action_term) action = compute_action_to_operation.get(action_term)
if not method: check_missing_attributes(["method"], attributes)
raise humanize_exception(ugettext_noop( operation = action.get(attributes['method'])
"Missing 'method' attribute."),
Exception())
operation = action.get(method)
getattr(self.instance, operation).async(user=user) getattr(self.instance, operation).async(user=user)
...@@ -359,13 +364,14 @@ class Storage(Resource): ...@@ -359,13 +364,14 @@ class Storage(Resource):
if attr: if attr:
attributes[attr.group("attribute")] = attr.group("value") attributes[attr.group("attribute")] = attr.group("value")
check_missing_attributes(
["occi.storage.size", "occi.core.title"], attributes)
size = attributes.get("occi.storage.size") size = attributes.get("occi.storage.size")
if not (size and size.isdigit()): if not (size and size.isdigit()):
return None return None
name = attributes.get("occi.core.title") name = attributes.get("occi.core.title")
if not name:
name = "disk create from OCCI at %s" % timezone.now()
params = { params = {
'user': user, # not used 'user': user, # not used
......
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 sign in to comment