Commit c45a17d0 by Kálmán Viktor

occi: display disks of vms

parent e46e344b
......@@ -114,6 +114,10 @@ class Entity():
self.kind = kind
class Link(Entity):
pass
class Resource(Entity):
def __init__(self, id, title, kind, summary, links):
super(Resource, self).__init__(id, title, kind)
......@@ -128,7 +132,7 @@ class Compute(Resource):
def __init__(self, instance=None, data=None):
self.attrs = {}
if instance:
self.location = "%svm/%d/" % (OCCI_ADDR, instance.pk)
self.location = "/vm/%d/" % (instance.pk)
self.instance = instance
self.init_attrs()
......@@ -198,13 +202,18 @@ class Compute(Resource):
def render_body(self):
kind = COMPUTE_KIND
mixins = []
links = []
if self.instance.template:
mixins.append(OsTemplate(self.instance.template))
for d in self.instance.disks.all():
links.append(StorageLink(self.instance, d))
return render_to_string("occi/compute.html", {
'kind': kind,
'attrs': self.attrs,
'mixins': mixins,
'links': links,
})
def init_attrs(self):
......@@ -287,7 +296,7 @@ class Storage(Resource):
def __init__(self, disk=None, data=None):
self.attrs = {}
if disk:
self.location = "%sdisk/%d/" % (OCCI_ADDR, disk.pk)
self.location = "/disk/%d/" % (disk.pk)
self.disk = disk
self.init_attrs()
......@@ -348,6 +357,38 @@ class Storage(Resource):
getattr(self.instance, operation).async(user=user)
class StorageLink(Link):
def __init__(self, instance=None, disk=None, data=None):
if instance and disk:
self.init_attrs(instance, disk)
elif data:
pass
def init_attrs(self, instance, disk):
self.attrs = {}
self.attrs['occi.core.id'] = "%d_at_%d" % (disk.pk, instance.pk)
self.attrs['occi.core.target'] = Storage(disk).render_location()
self.attrs['occi.core.source'] = Compute(instance).render_location()
# deviceid? mountpoint?
self.attrs['occi.core.state'] = "active"
self.instance = instance
self.disk = disk
def render_location(self):
return "/link/storagelink/%d_at_%d" % (self.disk.pk, self.instance.pk)
def render_body(self):
kind = STORAGE_LINK_KIND
return render_to_string("occi/link.html", {
'kind': kind,
'location': self.render_location(),
'target': self.attrs['occi.core.target'],
'attrs': self.attrs,
})
"""predefined stuffs
......@@ -446,3 +487,38 @@ OS_TPL_MIXIN = Mixin(
title="os template",
location="/mixin/os_tpl/",
)
LINK_ATTRS = [
Attribute("occi.core.id", "immutable"),
Attribute("occi.core.title"),
Attribute("occi.core.target"),
Attribute("occi.core.source"),
]
LINK_KIND = Kind(
term="link",
scheme="http://schemas.ogf.org/occi/core#",
class_="kind",
title="Link",
rel="http://schemas.ogf.org/occi/core#entity",
location="/link/",
attributes=LINK_ATTRS
)
STORAGE_LINK_ATTRS = LINK_ATTRS + [
Attribute("occi.storagelink.deviceid"),
Attribute("occi.storagelink.mountpoint"),
Attribute("occi.core.state", "immutable"),
]
STORAGE_LINK_KIND = Kind(
term="storagelink",
scheme="http://schemas.ogf.org/occi/infrastructure#",
class_="kind",
title="Storage link",
rel="http://schemas.ogf.org/occi/core#link",
location="/link/storagelink/",
attributes=STORAGE_LINK_ATTRS
)
Category: compute; scheme="{{ kind.scheme }}"; class="{{ kind.class }}";
{% spaceless %}
{% for m in mixins %}
{{ m.render_body }}
{% endfor %}
{% for k, v in attrs.items %}
X-OCCI-Attribute: {{ k }}={% if v.isdigit == False or k == "occi.core.id" %}"{{ v }}"{% else %}{{ v }}{% endif %}{% endfor %}
{% for m in mixins %}{{ m.render_body }}{% endfor %}
{% endspaceless %}
X-OCCI-Attribute: {{ k }}={% if v.isdigit == False or k == "occi.core.id" %}"{{ v }}"{% else %}{{ v }}{% endif %}
{% endfor %}
{% for l in links %}
{{ l.render_body }}
{% endfor %}
{% spaceless %}
Link: <{{ target }}>; rel="{{ kind.rel }}"; self="{{ location }}"; category="{{ kind.scheme }}storagelink"; {% for k, v in attrs.items %}{{ k }}={% if v.isdigit == False or k == "occi.core.id" %}"{{ v }}"{% else %}{{ v }}{% endif %}{% if not forloop.last %}; {% endif %}{% endfor %}
{% endspaceless %}
......@@ -12,6 +12,8 @@ from .occi import (
OsTemplate,
COMPUTE_KIND,
STORAGE_KIND,
LINK_KIND,
STORAGE_LINK_KIND,
COMPUTE_ACTIONS,
OS_TPL_MIXIN,
)
......@@ -42,6 +44,8 @@ class QueryInterface(View):
def get(self, request, *args, **kwargs):
response = "Category: %s\n" % COMPUTE_KIND.render_values()
response += "Category: %s\n" % STORAGE_KIND.render_values()
response += "Category: %s\n" % LINK_KIND.render_values()
response += "Category: %s\n" % STORAGE_LINK_KIND.render_values()
response += "Category: %s\n" % OS_TPL_MIXIN.render_values()
for c in COMPUTE_ACTIONS:
response += "Category: %s\n" % c.render_values()
......@@ -173,16 +177,3 @@ class DiskInterface(DetailView):
@method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs):
return super(DiskInterface, self).dispatch(*args, **kwargs)
"""
test commands:
curl 10.7.0.103:8080/occi/-/ -X GET
curl 10.7.0.103:8080/occi/compute/ -X GET
curl 10.7.0.103:8080/occi/compute/ -X POST
--header "X-OCCI-Attribute: occi.compute.cores=2"
--header "X-OCCI-Attribute: occi.compute.architecture=x86"
--header "X-OCCI-Attribute: occi.compute.speed=1"
--header "X-OCCI-Attribute: occi.compute.memory=1024" -I
"""
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