diff --git a/ooi/occi/core/action.py b/ooi/occi/core/action.py index 307247b..8ccbe36 100644 --- a/ooi/occi/core/action.py +++ b/ooi/occi/core/action.py @@ -24,5 +24,10 @@ class Action(category.Category): instance. """ + def __init__(self, scheme, term, title, attributes=None, location=None): + super(Action, self).__init__(scheme, term, title, + attributes=attributes, + location="?action=%s" % term) + def _class_name(self): return "action" diff --git a/ooi/occi/core/category.py b/ooi/occi/core/category.py index 7d10c72..c69f9f4 100644 --- a/ooi/occi/core/category.py +++ b/ooi/occi/core/category.py @@ -15,6 +15,7 @@ # under the License. from ooi.occi.core import attribute +from ooi.occi import helpers class Category(object): @@ -40,3 +41,7 @@ class Category(object): @property def occi_class(self): return self._class_name() + + @property + def type_id(self): + return helpers.join_url(self.scheme, "", fragments=self.term) diff --git a/ooi/occi/rendering/headers.py b/ooi/occi/rendering/headers.py index 7e4237f..9085552 100644 --- a/ooi/occi/rendering/headers.py +++ b/ooi/occi/rendering/headers.py @@ -22,6 +22,7 @@ from ooi.occi.core import collection from ooi.occi.core import kind from ooi.occi.core import mixin from ooi.occi.core import resource +from ooi.occi import helpers class HeaderRenderer(object): @@ -54,7 +55,20 @@ class KindRenderer(CategoryRenderer): class ActionRenderer(CategoryRenderer): - pass + def render(self, instance=None, env={}): + # FIXME(aloga): ugly code + + # We have an instance id, render it as a link + if instance is not None: + url = env.get("application_url", "") + url = helpers.join_url(url, instance) + d = {"location": helpers.join_url(url, self.obj.location), + "rel": self.obj.type_id} + link = "<%(location)s>; rel=%(rel)s" % d + return [('Link', link)] + else: + # Otherwise, render as category + return super(ActionRenderer, self).render(env=env) class MixinRenderer(CategoryRenderer): @@ -96,6 +110,8 @@ class ResourceRenderer(HeaderRenderer): if self.obj.attributes[a].value is None: continue ret.extend(AttributeRenderer(self.obj.attributes[a]).render()) + for a in self.obj.actions: + ret.extend(ActionRenderer(a).render(instance=self.obj.id)) for l in self.obj.links: pass # FIXME(aloga): we need to fix this diff --git a/ooi/tests/middleware/test_compute_controller.py b/ooi/tests/middleware/test_compute_controller.py index 0621cef..95abf44 100644 --- a/ooi/tests/middleware/test_compute_controller.py +++ b/ooi/tests/middleware/test_compute_controller.py @@ -57,9 +57,21 @@ def build_occi_server(server): 'occi.compute.hostname="%s"' % name, 'occi.core.id="%s"' % server_id, ] + links = [] + links.append('<%s?action=restart>; rel=http://schemas.ogf.org/occi/' + 'infrastructure/compute/action#restart' % server_id) + links.append('<%s?action=start>; rel=http://schemas.ogf.org/occi/' + 'infrastructure/compute/action#start' % server_id) + links.append('<%s?action=stop>; rel=http://schemas.ogf.org/occi/' + 'infrastructure/compute/action#stop' % server_id) + links.append('<%s?action=suspend>; rel=http://schemas.ogf.org/occi/' + 'infrastructure/compute/action#suspend' % server_id) + result = [] for c in cats: result.append(("Category", c)) + for l in links: + result.append(("Link", l)) for a in attrs: result.append(("X-OCCI-Attribute", a)) return result