Commit ec1b0b86 by Kálmán Viktor

occi: compute interface

note: new vm creation is kinda flunky, no auth, a few random things
parent 33efa984
from django.contrib.auth.models import User
from vm.models import Instance, Lease
from vm.models.common import ARCHITECTURES
from vm.models.instance import ACCESS_METHODS
class Category(): class Category():
"""Represents a Category object """Represents a Category object
...@@ -56,10 +63,62 @@ class Action(Category): ...@@ -56,10 +63,62 @@ class Action(Category):
def render(self): def render(self):
return "%s%s" % (self.scheme, self.term) return "%s%s" % (self.scheme, self.term)
# TODO Entity
# TODO ^ Resource
# TODO ^ Compute
class Entity():
def __init__(self, id, title, kind):
self.id = id
self.title = title
self.kind = kind
class Resource(Entity):
def __init__(self, id, title, kind, summary, links):
super(Resource, self).__init__(id, title, kind)
self.summary = summary
self.links = links
class Compute(Resource):
# TODO better init, for new resources
def __init__(self, instance=None, attrs=None, **kwargs):
if instance:
self.location = "http://10.7.0.103:8080/occi/vm/%d" % instance.pk
elif attrs:
self.attrs = attrs
self._create_object()
translate = {
'occi.compute.architecture': "arch",
'occi.compute.cores': "num_cores",
'occi.compute.hostname': "???",
'occi.compute.speed': "priority",
'occi.compute.memory': "ram_size",
}
def _create_object(self):
params = {}
for a in self.attrs:
t = a.split("=")
params[self.translate.get(t[0])] = t[1]
print params
params['lease'] = Lease.objects.all()[0]
params['priority'] = 10
params['max_ram_size'] = params['ram_size']
params['system'] = "welp"
params['pw'] = "killmenow"
params['arch'] = (ARCHITECTURES[0][0] if "64" in params['arch'] else
ARCHITECTURES[1][0])
params['access_method'] = ACCESS_METHODS[0][0]
params['owner'] = User.objects.get(username="test")
params['name'] = "from occi yo"
i = Instance.create(params=params, disks=[], networks=[],
req_traits=[], tags=[])
self.location = "http://10.7.0.103:8080/occi/vm/%d" % i.pk
def render_location(self):
return "X-OCCI-Location: %s" % self.location
"""predefined stuffs """predefined stuffs
......
...@@ -18,9 +18,10 @@ ...@@ -18,9 +18,10 @@
from __future__ import absolute_import from __future__ import absolute_import
from django.conf.urls import url, patterns from django.conf.urls import url, patterns
from occi.views import QueryInterface from occi.views import QueryInterface, ComputeInterface
urlpatterns = patterns( urlpatterns = patterns(
'', '',
url(r'^-/$', QueryInterface.as_view(), name="occi.query"), url(r'^-/$', QueryInterface.as_view(), name="occi.query"),
url(r'^compute/$', ComputeInterface.as_view(), name="occi.compute"),
) )
...@@ -3,7 +3,10 @@ from django.utils.decorators import method_decorator ...@@ -3,7 +3,10 @@ from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View from django.views.generic.base import View
from vm.models import Instance
from .occi import ( from .occi import (
Compute,
COMPUTE_KIND, COMPUTE_KIND,
COMPUTE_ACTIONS, COMPUTE_ACTIONS,
) )
...@@ -25,3 +28,42 @@ class QueryInterface(View): ...@@ -25,3 +28,42 @@ class QueryInterface(View):
@method_decorator(csrf_exempt) # decorator on post method doesn't work @method_decorator(csrf_exempt) # decorator on post method doesn't work
def dispatch(self, *args, **kwargs): def dispatch(self, *args, **kwargs):
return super(QueryInterface, self).dispatch(*args, **kwargs) return super(QueryInterface, self).dispatch(*args, **kwargs)
class ComputeInterface(View):
def get(self, request, *args, **kwargs):
response = "\n".join([Compute(instance=i).render_location()
for i in Instance.active.all()])
return HttpResponse(response)
def post(self, request, *args, **kwargs):
occi_attrs = None
category = None
for k, v in request.META.iteritems():
if k.startswith("HTTP_X_OCCI_ATTRIBUTE"):
occi_attrs = v.split(",")
elif k.startswith("HTTP_CATEGORY") and category is None:
category = v
c = Compute(attrs=occi_attrs)
response = HttpResponse()
response['Location'] = c.location
return response
@method_decorator(csrf_exempt) # decorator on post method doesn't work
def dispatch(self, *args, **kwargs):
return super(ComputeInterface, 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