Commit 452214f8 by Kálmán Viktor

occi: add actions

parent aff5f70f
import re
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.template.loader import render_to_string from django.template.loader import render_to_string
...@@ -7,6 +9,29 @@ from vm.models.instance import ACCESS_METHODS ...@@ -7,6 +9,29 @@ from vm.models.instance import ACCESS_METHODS
OCCI_ADDR = "http://localhost:8080/" OCCI_ADDR = "http://localhost:8080/"
occi_attribute_regex = re.compile(
'^X-OCCI-Attribute: ?method="(?P<method>[a-zA-Z]+)"$')
occi_action_regex = re.compile(
'^Category: (?P<term>[a-zA-Z]+); ?scheme=".+"; ?class="action"$')
compute_action_to_operation = {
'stop': {
'graceful': "shutdown",
'acpioff': "shutdown",
'poweroff': "shut_off",
},
'restart': {
'graceful': "restart",
'warm': "restart",
'cold': "reset",
},
'suspend': {
'suspend': "sleep",
'hibernate': "sleep",
}
}
class Category(): class Category():
"""Represents a Category object """Represents a Category object
...@@ -147,6 +172,30 @@ class Compute(Resource): ...@@ -147,6 +172,30 @@ class Compute(Resource):
for k, v in self.translate.items(): for k, v in self.translate.items():
self.attrs[k] = getattr(self.instance, v, None) self.attrs[k] = getattr(self.instance, v, None)
def trigger_action(self, data):
method = None
action_term = None
for d in data:
m = occi_attribute_regex.match(d)
if m:
method = m.group("method")
m = occi_action_regex.match(d)
if m:
action_term = m.group("term")
if action_term == "start":
if self.instance.status == "SUSPENDED":
operation = "wake_up"
else:
operation = "deploy"
else:
action = compute_action_to_operation.get(action_term)
operation = action.get(method)
# TODO user
user = User.objects.get(username="test")
getattr(self.instance, operation).async(user=user)
class OsTemplate(Mixin): class OsTemplate(Mixin):
def __init__(self, template): def __init__(self, template):
......
...@@ -80,8 +80,23 @@ class VmInterface(DetailView): ...@@ -80,8 +80,23 @@ class VmInterface(DetailView):
) )
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
# actions, resource change data = self.get_post_data(request)
action = request.GET.get("action")
vm = self.get_object()
if action:
Compute(instance=vm).trigger_action(data)
return HttpResponse()
def get_post_data(self, request):
post_data = []
accept = request.META.get("HTTP_ACCEPT")
if accept and accept.split(",")[0] == "text/occi":
pass pass
else: # text/plain or missing
for l in request.readlines():
if l:
post_data.append(l.strip())
return post_data
@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):
......
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