From df9e7d08e1e916be87afab2fb55e94425ae2a378 Mon Sep 17 00:00:00 2001 From: Kálmán Viktor <kviktor@cloud.bme.hu> Date: Fri, 17 Oct 2014 14:20:11 +0200 Subject: [PATCH] occi: os tpl --- circle/circle/urls.py | 2 +- circle/occi/occi.py | 41 ++++++++++++++++++++++++++++++++++++++--- circle/occi/templates/occi/os_tpl.html | 3 +++ circle/occi/urls.py | 5 ++++- circle/occi/views.py | 27 ++++++++++++++++++++++++++- 5 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 circle/occi/templates/occi/os_tpl.html diff --git a/circle/circle/urls.py b/circle/circle/urls.py index bfb757e..b446a72 100644 --- a/circle/circle/urls.py +++ b/circle/circle/urls.py @@ -68,7 +68,7 @@ urlpatterns = patterns( url(r'^info/support/$', TemplateView.as_view(template_name="info/support.html"), name="info.support"), - url(r'^occi/', include('occi.urls')), + url(r'^', include('occi.urls')), ) diff --git a/circle/occi/occi.py b/circle/occi/occi.py index 1e3d8b0..777dea1 100644 --- a/circle/occi/occi.py +++ b/circle/occi/occi.py @@ -5,7 +5,7 @@ from vm.models import Instance, Lease from vm.models.common import ARCHITECTURES from vm.models.instance import ACCESS_METHODS -OCCI_ADDR = "http://pc3.szgt.uni-miskolc.hu:5863/" +OCCI_ADDR = "http://localhost:8080/" class Category(): @@ -50,6 +50,10 @@ class Kind(Category): pass +class Mixin(Category): + pass + + class Attribute(): def __init__(self, name, property=None): self.name = name @@ -87,7 +91,7 @@ class Compute(Resource): def __init__(self, instance=None, attrs=None, **kwargs): self.attrs = {} if instance: - self.location = "%socci/vm/%d" % (OCCI_ADDR, instance.pk) + self.location = "%svm/%d/" % (OCCI_ADDR, instance.pk) self.instance = instance self.init_attrs() elif attrs: @@ -124,7 +128,7 @@ class Compute(Resource): params['name'] = "from occi yo" i = Instance.create(params=params, disks=[], networks=[], req_traits=[], tags=[]) - self.location = "%socci/vm/%d" % (OCCI_ADDR, i.pk) + self.location = "%svm/%d/" % (OCCI_ADDR, i.pk) def render_location(self): return "X-OCCI-Location: %s" % self.location @@ -141,6 +145,28 @@ class Compute(Resource): self.attrs[k] = getattr(self.instance, v, None) +class OsTemplate(Mixin): + def __init__(self, template): + self.term = "os_tpl_%d" % template.pk + self.title = template.system + self.scheme = "http://cloud.bme.hu/occi/infrastructure/os_tpl#" + self.rel = "http://schemas.ogf.org/occi/infrastructure#os_tpl" + self.location = "/mixin/os_tpl/%s/" % self.term + + def render_location(self): + return self.location + + def render_body(self): + return render_to_string("occi/os_tpl.html", { + 'term': self.term, + 'scheme': self.scheme, + 'rel': self.rel, + 'location': self.location, + 'class': "mixin", + 'title': self.title, + }) + + """predefined stuffs @@ -203,3 +229,12 @@ COMPUTE_KIND = Kind( actions=COMPUTE_ACTIONS, location="%scompute/", ) + + +OS_TPL_MIXIN = Mixin( + term="os_tpl", + scheme="http://schemas.ogf.org/occi/infrastructure#", + class_="mixin", + title="os template", + location="/mixin/os_tpl/", +) diff --git a/circle/occi/templates/occi/os_tpl.html b/circle/occi/templates/occi/os_tpl.html new file mode 100644 index 0000000..69cefa4 --- /dev/null +++ b/circle/occi/templates/occi/os_tpl.html @@ -0,0 +1,3 @@ +{% spaceless %} +Category: {{ term }}; scheme="{{ scheme }}"; class="{{ class }}"; title="{{ title }}"; rel="{{ rel }}"; location="{{ location }}"; +{% endspaceless %} diff --git a/circle/occi/urls.py b/circle/occi/urls.py index 741cc7a..60fa565 100644 --- a/circle/occi/urls.py +++ b/circle/occi/urls.py @@ -18,11 +18,14 @@ from __future__ import absolute_import from django.conf.urls import url, patterns -from occi.views import QueryInterface, ComputeInterface, VmInterface +from occi.views import ( + QueryInterface, ComputeInterface, VmInterface, OsTplInterface, +) urlpatterns = patterns( '', url(r'^-/$', QueryInterface.as_view(), name="occi.query"), url(r'^compute/$', ComputeInterface.as_view(), name="occi.compute"), + url(r'^os_tpl/$', OsTplInterface.as_view(), name="occi.os_tpl"), url(r'^vm/(?P<pk>\d+)/$', VmInterface.as_view(), name="occi.vm"), ) diff --git a/circle/occi/views.py b/circle/occi/views.py index f8ac35d..7c58fd3 100644 --- a/circle/occi/views.py +++ b/circle/occi/views.py @@ -3,12 +3,14 @@ from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt from django.views.generic import View, DetailView -from vm.models import Instance +from vm.models import Instance, InstanceTemplate from .occi import ( Compute, + OsTemplate, COMPUTE_KIND, COMPUTE_ACTIONS, + OS_TPL_MIXIN, ) @@ -16,9 +18,13 @@ class QueryInterface(View): def get(self, request, *args, **kwargs): response = "Category: %s\n" % COMPUTE_KIND.render_values() + response = "Category: %s\n" % OS_TPL_MIXIN.render_values() for c in COMPUTE_ACTIONS: response += "Category: %s\n" % c.render_values() + for t in InstanceTemplate.objects.all(): + response += OsTemplate(t).render_body() + return HttpResponse( response, content_type="text/plain", @@ -81,6 +87,25 @@ class VmInterface(DetailView): def dispatch(self, *args, **kwargs): return super(VmInterface, self).dispatch(*args, **kwargs) + +class OsTplInterface(View): + + def get(self, request, *args, **kwargs): + response = "\n".join([OsTemplate(template=t).render_location() + for t in InstanceTemplate.objects.all()]) + return HttpResponse( + response, + content_type="text/plain", + ) + + def post(self, request, *args, **kwargs): + pass + + @method_decorator(csrf_exempt) # decorator on post method doesn't work + def dispatch(self, *args, **kwargs): + return super(OsTplInterface, self).dispatch(*args, **kwargs) + + """ test commands: curl 10.7.0.103:8080/occi/-/ -X GET -- libgit2 0.26.0