Skip to content

Commit

Permalink
parsing: check for categories instead of kinds
Browse files Browse the repository at this point in the history
We should check for kinds and actions when parsing and validating the
data, since POSTing an action is needed when we are triggering an action
on a resource.
  • Loading branch information
alvarolopez committed Apr 23, 2015
1 parent a44e211 commit 379383b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ooi/api/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def create(self, req, body):
tenant_id = req.environ["keystone.token_auth"].user.project_id
parser = req.get_parser()(req.headers, req.body)
scheme = {
"kind": compute.ComputeResource.kind,
"category": compute.ComputeResource.kind,
"mixins": [
templates.OpenStackOSTemplate,
templates.OpenStackResourceTemplate,
Expand Down
14 changes: 8 additions & 6 deletions ooi/occi/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ class Validator(object):
def __init__(self, obj):
self.parsed_obj = obj

def _validate_kind(self, kind):
def _validate_category(self, category):
try:
if kind.type_id != self.parsed_obj["kind"]:
if category.type_id != self.parsed_obj["category"]:
raise exception.OCCISchemaMismatch(
expected=kind.type_id, found=self.parsed_obj["kind"])
expected=category.type_id,
found=self.parsed_obj["category"]
)
except KeyError:
raise exception.OCCIMissingType(
type_id=kind.type_id)
type_id=category.type_id)

def _compare_schemes(self, expected_type, actual):
actual_scheme, actual_term = helpers.decompose_type(actual)
Expand Down Expand Up @@ -64,8 +66,8 @@ def _validate_optional_mixins(self, mixins, unmatched):
return unmatched

def validate(self, schema):
if "kind" in schema:
self._validate_kind(schema["kind"])
if "category" in schema:
self._validate_category(schema["category"])
unmatched = copy.copy(self.parsed_obj["mixins"])
unmatched = self._validate_mandatory_mixins(
schema.get("mixins", []), unmatched)
Expand Down
8 changes: 4 additions & 4 deletions ooi/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_kind(self):
'class="kind"')
parser = parsers.TextParser({}, body)
res = parser.parse()
self.assertEqual("http://example.com/scheme#foo", res["kind"])
self.assertEqual("http://example.com/scheme#foo", res["category"])
self.assertItemsEqual(["foo"],
res["schemes"]["http://example.com/scheme#"])
self.assertEqual({}, res["mixins"])
Expand Down Expand Up @@ -91,7 +91,7 @@ def test_kind(self):
}
parser = parsers.HeaderParser(headers, None)
res = parser.parse()
self.assertEqual("http://example.com/scheme#foo", res["kind"])
self.assertEqual("http://example.com/scheme#foo", res["category"])
self.assertItemsEqual(["foo"],
res["schemes"]["http://example.com/scheme#"])
self.assertEqual({}, res["mixins"])
Expand Down Expand Up @@ -142,9 +142,9 @@ def test_attributes(self):
'Category': ('foo; '
'scheme="http://example.com/scheme#"; '
'class="kind"'),
'X-OCCI-Attribute': ('foo="bar", baz=1234'),
'X-OCCI-Attribute': 'foo="bar", baz=1234, bazonk="foo=123"',
}
parser = parsers.HeaderParser(headers, None)
res = parser.parse()
expected_attrs = {"foo": "bar", "baz": "1234"}
expected_attrs = {"foo": "bar", "baz": "1234", "bazonk": "foo=123"}
self.assertEqual(expected_attrs, res["attributes"])
10 changes: 8 additions & 2 deletions ooi/wsgi/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def _lexise_header(s):

class TextParser(BaseParser):
def parse_categories(self, headers):
kind = None
kind = action = None
mixins = collections.Counter()
schemes = collections.defaultdict(list)
try:
Expand All @@ -69,11 +69,17 @@ def parse_categories(self, headers):
if kind is not None:
raise exception.OCCIInvalidSchema("Duplicated Kind")
kind = ctg_type
elif ctg_class == "action":
if action is not None:
raise exception.OCCIInvalidSchema("Duplicated action")
action = ctg_type
elif ctg_class == "mixin":
mixins[ctg_type] += 1
schemes[d["scheme"]].append(d["term"])
if action and kind:
raise exception.OCCIInvalidSchema("Action and kind together?")
return {
"kind": kind,
"category": kind or action,
"mixins": mixins,
"schemes": schemes,
}
Expand Down

0 comments on commit 379383b

Please sign in to comment.