Commit 33efa984 by Kálmán Viktor

occi: base classes

parent 7260a162
class Category():
"""Represents a Category object
actions needs a list of Actions
attributes needs a list of Attributes
"""
optional_arguments = ["title", "rel", "location", "attributes",
"actions", "class"]
def __init__(self, term, scheme, class_, **kwargs):
# workaround for class named keyword argument
kwargs['class'] = class_
self.term = term
self.scheme = scheme
self.class_ = class_
for k, v in kwargs.iteritems():
if k in self.optional_arguments:
setattr(self, k, v)
def render_values(self):
ret = "%s;" % self.term
simple_arguments = ["scheme", "class", "title", "rel", "location"]
for i in simple_arguments:
if hasattr(self, i):
ret += ' %s="%s";' % (i, getattr(self, i))
if hasattr(self, "attributes"):
ret += ' attributes="%s";' % " ".join(
[a.render() for a in self.attributes])
if hasattr(self, "actions"):
ret += ' actions="%s";' % " ".join(
[a.render() for a in self.actions])
return ret[:-1] # trailing semicolon
# TODO related, entity_type, entities
class Kind(Category):
pass
class Attribute():
def __init__(self, name, property=None):
self.name = name
self.property = property
def render(self):
attr = self.name
if self.property:
attr += "{%s}" % self.property
return attr
class Action(Category):
def render(self):
return "%s%s" % (self.scheme, self.term)
# TODO Entity
# TODO ^ Resource
# TODO ^ Compute
"""predefined stuffs
storage attributes and actions
http://ogf.org/documents/GFD.184.pdf 3.3 (page 7)
storagelink attributes
http://ogf.org/documents/GFD.184.pdf 3.4.2 (page 10)
"""
# compute attributes and actions
# http://ogf.org/documents/GFD.184.pdf 3.1 (page 5)
COMPUTE_ATTRS = [
Attribute("occi.compute.architecture"),
Attribute("occi.compute.cores"),
Attribute("occi.compute.hostname"),
Attribute("occi.compute.speed"),
Attribute("occi.compute.memory"),
Attribute("occi.compute.state", "immutable"),
]
COMPUTE_ACTIONS = [
Action(
"start",
"http://schemas.ogf.org/occi/infrastructure/compute/action#",
"action",
title="Start compute resource",
),
Action(
"stop",
"http://schemas.ogf.org/occi/infrastructure/compute/action#",
"action",
title="Stop compute resource",
attributes=[Attribute("method")],
),
Action(
"restart",
"http://schemas.ogf.org/occi/infrastructure/compute/action#",
"action",
title="Restart compute resource",
attributes=[Attribute("method")],
),
Action(
"suspend",
"http://schemas.ogf.org/occi/infrastructure/compute/action#",
"action",
title="Suspend compute resource",
attributes=[Attribute("method")],
),
]
COMPUTE_KIND = Kind(
term="compute",
scheme="http://schemas.ogf.org/occi/infrastructure#",
class_="kind",
title="Compute Resource type",
rel="http://schemas.ogf.org/occi/core#resource",
attributes=COMPUTE_ATTRS,
actions=COMPUTE_ACTIONS,
location="10.7.0.103:8080/occi/compute/",
)
......@@ -3,13 +3,20 @@ from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View
from .occi import (
COMPUTE_KIND,
COMPUTE_ACTIONS,
)
class QueryInterface(View):
def get(self, request, *args, **kwargs):
response = HttpResponse("Hai!")
response['yo'] = "as"
return response
response = "Category: %s\n" % COMPUTE_KIND.render_values()
for c in COMPUTE_ACTIONS:
response += "Category: %s\n" % c.render_values()
return HttpResponse(response)
def post(self, request, *args, **kwargs):
response = HttpResponse(status=501)
......
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