Commit d263bf24 by Kálmán Viktor

occi: delete storagelink

remove disk from instance, but don't destroy it
parent c3dd4099
......@@ -505,6 +505,13 @@ class StorageLink(Link):
'attrs': self.attrs,
})
def delete(self):
# TODO
user = User.objects.get(username="test")
if self.disk in self.instance.disks.all():
self.instance.detach_disk(user=user, disk=self.disk)
"""predefined stuffs
......
......@@ -33,6 +33,6 @@ urlpatterns = patterns(
url(r'^disk/(?P<pk>\d+)/$', DiskInterface.as_view(), name="occi.disk"),
url(r'^link/storagelink/$', StorageLinkInterface.as_view()),
url(r'^link/storagelink/vm(?P<vm_pk>\d+)_disk(?P<disk_pk>\d+)/$',
url(r'^link/storagelink/vm(?P<vm_pk>\d+)_disk(?P<disk_pk>\d+)/?$',
StorageLinkInterface.as_view(), name="occi.storagelink"),
)
......@@ -201,15 +201,16 @@ class DiskInterface(DetailView):
class StorageLinkInterface(View):
def get_vm_and_disk(self):
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):
vm = get_object_or_404(Instance.objects.filter(destroyed_at=None),
pk=self.kwargs['vm_pk'])
disk = get_object_or_404(Disk.objects.filter(destroyed=None),
pk=self.kwargs['disk_pk'])
if disk not in vm.disks.all():
raise Http404
return vm, disk
def get(self, request, *args, **kwargs):
vm, disk = self.get_vm_and_disk()
sl = StorageLink(instance=vm, disk=disk)
......@@ -236,6 +237,13 @@ class StorageLinkInterface(View):
else:
return HttpResponse("VM or Storage does not exist.", status=500)
def delete(self, request, *args, **kwargs):
vm, disk = self.get_vm_and_disk()
sl = StorageLink(instance=vm, disk=disk)
sl.delete()
return HttpResponse("")
@method_decorator(csrf_exempt)
def dispatch(self, *args, **kwargs):
return super(StorageLinkInterface, self).dispatch(*args, **kwargs)
......@@ -817,7 +817,8 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
return acts
def get_merged_activities(self, user=None):
whitelist = ("create_disk", "download_disk", "attach_disk")
whitelist = ("create_disk", "download_disk", "attach_disk",
"detach_disk", )
acts = self.get_activities(user)
merged_acts = []
latest = None
......
......@@ -1326,3 +1326,21 @@ class AttachDiskOperation(InstanceOperation):
return create_readable(
ugettext_noop("attach disk %(name)s"),
name=kwargs['disk'].name)
@register_operation
class DetachDiskOperation(InstanceOperation):
id = 'detach_disk'
name = _("detach disk")
description = _("Detach the specified disk from the virtual machine.")
required_perms = ()
accept_states = ('STOPPED', 'PENDING', 'RUNNING')
def _operation(self, activity, user, disk):
if self.instance.is_running and disk.type not in ["iso"]:
self.instance._detach_disk(disk=disk, parent_activity=activity)
return self.instance.disks.remove(disk)
def get_activity_name(self, kwargs):
return create_readable(ugettext_noop('deatch disk %(name)s'),
name=kwargs["disk"].name)
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