Commit 4baa3506 by Kálmán Viktor

occi: handle storagelink errors

detect if the target storage and source vm exists
parent a2911611
import re
from django.shortcuts import get_object_or_404
from django.contrib.auth.models import User
from django.template.loader import render_to_string
from django.utils import timezone
......@@ -428,17 +427,24 @@ class StorageLink(Link):
g = re.match(occi_attribute_link_regex % "vm", source)
vm_pk = g.group("id")
disk = get_object_or_404(Disk, pk=disk_pk)
vm = get_object_or_404(Instance, pk=vm_pk)
try:
vm = Instance.objects.filter(destroyed_at=None).get(pk=vm_pk)
disk = Disk.objects.filter(destroyed=None).get(pk=disk_pk)
except (Instance.DoesNotExist, Disk.DoesNotExist):
return None
try:
vm.attach_disk(user=user, disk=disk)
except:
pass
vm.attach_disk(user=user, disk=disk)
cls.location = "%sstoragelink/%svm_%sdisk" % (OCCI_ADDR, vm_pk,
disk_pk)
return cls
def render_location(self):
return "/link/storagelink/vm%d_disk%d" % (instance.pk, disk.pk)
return "/link/storagelink/vm%d_disk%d" % (self.instance.pk,
self.disk.pk)
def render_as_link(self):
kind = STORAGE_LINK_KIND
......
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, Http404
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View, DetailView
......@@ -191,9 +190,14 @@ class DiskInterface(DetailView):
class StorageLinkInterface(View):
def get_vm_and_disk(self):
vm = get_object_or_404(Instance, pk=self.kwargs['vm_pk'])
disk = get_object_or_404(Disk, pk=self.kwargs['disk_pk'])
return vm, disk
try:
vm = Instance.objects.filter(destroyed=None
).get(pk=self.kwargs['vm_pk'])
disk = Disk.objects.filter(destroyed=None
).get(pk=self.kwargs['disk_pk'])
return vm, disk
except (Instance.DoesNotExist, Disk.DoesNotExist):
raise Http404
def get(self, request, *args, **kwargs):
vm, disk = self.get_vm_and_disk()
......@@ -208,17 +212,18 @@ class StorageLinkInterface(View):
# (they don't even exist in the model)
if request.GET.get("action"):
return HttpResponse("", status=500)
else:
data = get_post_data_from_request(request)
sl = StorageLink.create_object(data=data)
data = get_post_data_from_request(request)
sl = StorageLink.create_object(data=data)
if sl:
response = HttpResponse(
"X-OCCI-Location: %s" % sl.location,
status=201,
content_type="text/plain",
)
return response
return HttpResponse()
else:
return HttpResponse("VM or Storage does not exist.", status=500)
@method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs):
......
......@@ -1295,6 +1295,12 @@ class AttachDiskOperation(InstanceOperation):
accept_states = ('STOPPED', 'PENDING', 'RUNNING')
def _operation(self, user, activity, disk):
if disk in self.instance.disks.all():
raise humanize_exception(ugettext_noop(
"Disk (#%d) is already attached to this virtual machine."
) % disk.pk,
Exception())
devnums = list(ascii_lowercase)
for d in self.instance.disks.all():
devnums.remove(d.dev_num)
......
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