Stop using decorators for parse and validate
Instead of using a decorator for parsing the incoming data we can use calls to the objects, making the code much more readable.
This commit is contained in:
parent
a55edb366a
commit
88c6a4f29d
|
@ -74,15 +74,6 @@ class Controller(object):
|
||||||
else:
|
else:
|
||||||
raise exception_from_response(response)
|
raise exception_from_response(response)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def validate(schema):
|
|
||||||
def accepts(f):
|
|
||||||
def _validate(obj, req, body, *args, **kwargs):
|
|
||||||
parsed_obj = req.validate(schema)
|
|
||||||
return f(obj, parsed_obj, req, body, *args, **kwargs)
|
|
||||||
return _validate
|
|
||||||
return accepts
|
|
||||||
|
|
||||||
|
|
||||||
def exception_from_response(response):
|
def exception_from_response(response):
|
||||||
"""Convert an OpenStack V2 Fault into a webob exception.
|
"""Convert an OpenStack V2 Fault into a webob exception.
|
||||||
|
|
|
@ -24,6 +24,7 @@ from ooi.occi.infrastructure import storage_link
|
||||||
from ooi.openstack import contextualization
|
from ooi.openstack import contextualization
|
||||||
from ooi.openstack import helpers
|
from ooi.openstack import helpers
|
||||||
from ooi.openstack import templates
|
from ooi.openstack import templates
|
||||||
|
from ooi.wsgi import parsers
|
||||||
|
|
||||||
|
|
||||||
class Controller(ooi.api.base.Controller):
|
class Controller(ooi.api.base.Controller):
|
||||||
|
@ -66,8 +67,11 @@ class Controller(ooi.api.base.Controller):
|
||||||
|
|
||||||
return collection.Collection(resources=occi_compute_resources)
|
return collection.Collection(resources=occi_compute_resources)
|
||||||
|
|
||||||
@ooi.api.base.Controller.validate(
|
def create(self, req, body):
|
||||||
{"kind": compute.ComputeResource.kind,
|
tenant_id = req.environ["keystone.token_auth"].user.project_id
|
||||||
|
parser = req.get_parser()(req.headers, req.body)
|
||||||
|
scheme = {
|
||||||
|
"kind": compute.ComputeResource.kind,
|
||||||
"mixins": [
|
"mixins": [
|
||||||
templates.OpenStackOSTemplate,
|
templates.OpenStackOSTemplate,
|
||||||
templates.OpenStackResourceTemplate,
|
templates.OpenStackResourceTemplate,
|
||||||
|
@ -76,9 +80,11 @@ class Controller(ooi.api.base.Controller):
|
||||||
contextualization.user_data,
|
contextualization.user_data,
|
||||||
contextualization.public_key,
|
contextualization.public_key,
|
||||||
]
|
]
|
||||||
})
|
}
|
||||||
def create(self, obj, req, body):
|
obj = parser.parse()
|
||||||
tenant_id = req.environ["keystone.token_auth"].user.project_id
|
validator = parsers.Validator(obj)
|
||||||
|
validator.validate(scheme)
|
||||||
|
|
||||||
name = obj.get("occi.core.title", "OCCI VM")
|
name = obj.get("occi.core.title", "OCCI VM")
|
||||||
image = obj["schemes"][templates.OpenStackOSTemplate.scheme][0]
|
image = obj["schemes"][templates.OpenStackOSTemplate.scheme][0]
|
||||||
flavor = obj["schemes"][templates.OpenStackResourceTemplate.scheme][0]
|
flavor = obj["schemes"][templates.OpenStackResourceTemplate.scheme][0]
|
||||||
|
|
|
@ -61,15 +61,9 @@ class Request(webob.Request):
|
||||||
return content_type
|
return content_type
|
||||||
|
|
||||||
def get_parser(self):
|
def get_parser(self):
|
||||||
mtype = parsers.get_media_map().get(self.get_content_type,
|
mtype = parsers.get_media_map().get(self.get_content_type(), "header")
|
||||||
"header")
|
|
||||||
return parsers.get_default_parsers()[mtype]
|
return parsers.get_default_parsers()[mtype]
|
||||||
|
|
||||||
def validate(self, schema):
|
|
||||||
parser = self.get_parser()(self.headers, self.body)
|
|
||||||
parser.validate(schema)
|
|
||||||
return parser.parsed_obj
|
|
||||||
|
|
||||||
|
|
||||||
class OCCIMiddleware(object):
|
class OCCIMiddleware(object):
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,11 @@ class BaseParser(object):
|
||||||
def parse(self):
|
def parse(self):
|
||||||
raise NotImplemented
|
raise NotImplemented
|
||||||
|
|
||||||
|
|
||||||
|
class Validator(object):
|
||||||
|
def __init__(self, obj):
|
||||||
|
self.parsed_obj = obj
|
||||||
|
|
||||||
def _validate_kind(self, kind):
|
def _validate_kind(self, kind):
|
||||||
try:
|
try:
|
||||||
if kind.type_id != self.parsed_obj["kind"]:
|
if kind.type_id != self.parsed_obj["kind"]:
|
||||||
|
@ -76,7 +81,6 @@ class BaseParser(object):
|
||||||
return unmatched
|
return unmatched
|
||||||
|
|
||||||
def validate(self, schema):
|
def validate(self, schema):
|
||||||
self.parsed_obj = self.parse()
|
|
||||||
if "kind" in schema:
|
if "kind" in schema:
|
||||||
self._validate_kind(schema["kind"])
|
self._validate_kind(schema["kind"])
|
||||||
unmatched = copy.copy(self.parsed_obj["mixins"])
|
unmatched = copy.copy(self.parsed_obj["mixins"])
|
||||||
|
|
Loading…
Reference in New Issue