Refactor action rendering

When an action is rendered when it is associated with an instance its
rendering is different than the action object. In the former case it
shall be rendered as a link, whereas in the latter it is rendered as a
category.
This commit is contained in:
Alvaro Lopez Garcia 2015-04-07 17:23:54 +02:00
parent f53f9744da
commit edac5e2383
4 changed files with 39 additions and 1 deletions

View File

@ -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"

View File

@ -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)

View File

@ -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

View File

@ -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