A prog2-höz tartozó friss repo anyagok itt elérhetőek: https://git.iit.bme.hu/

You need to sign in or sign up before continuing.
Commit 61658071 by Chif Gergő

DELETE request only indicates, that the user wants delete, doesn't deletes the instance

parent 77caa3dd
Pipeline #1064 failed with stage
in 1 minute 33 seconds
# Generated by Django 3.0.4 on 2020-04-08 13:03
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('image', '0001_initial'),
('instance', '0002_instance_template'),
]
operations = [
migrations.RemoveField(
model_name='instance',
name='owner',
),
migrations.AlterField(
model_name='instance',
name='disks',
field=models.ManyToManyField(blank=True, help_text='Disks attached to instance', related_name='instance', to='image.Disk', verbose_name='disks'),
),
]
...@@ -171,11 +171,10 @@ class Instance(BaseMachineDescriptor): ...@@ -171,11 +171,10 @@ class Instance(BaseMachineDescriptor):
on_delete=models.DO_NOTHING) on_delete=models.DO_NOTHING)
disks = models.ManyToManyField(Disk, related_name="instance", disks = models.ManyToManyField(Disk, related_name="instance",
blank=True,
help_text="Disks attached to instance", help_text="Disks attached to instance",
verbose_name="disks") verbose_name="disks")
owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
@classmethod @classmethod
def create(cls, lease, owner, flavor, template, remote_id, params): def create(cls, lease, owner, flavor, template, remote_id, params):
params["password"] = cls.generate_password() params["password"] = cls.generate_password()
...@@ -272,3 +271,7 @@ class Instance(BaseMachineDescriptor): ...@@ -272,3 +271,7 @@ class Instance(BaseMachineDescriptor):
def change_description(self, new_description): def change_description(self, new_description):
self.description = new_description self.description = new_description
self.save() self.save()
def destroy(self):
self.deleted = True
self.save()
...@@ -62,7 +62,8 @@ class InstanceViewSet(AuthorizationMixin, ViewSet): ...@@ -62,7 +62,8 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
raise Http404 raise Http404
def list(self, request): def list(self, request):
instances = self.get_objects_with_perms(request.user, "list", Instance) # instances = self.get_objects_with_perms(request.user, "list", Instance)
instances = Instance.objects.filter(Q(created_by=request.user) & Q(deleted=False))
return Response(InstanceListItemSerializer(instances, many=True).data) return Response(InstanceListItemSerializer(instances, many=True).data)
def create(self, request): def create(self, request):
...@@ -114,14 +115,11 @@ class InstanceViewSet(AuthorizationMixin, ViewSet): ...@@ -114,14 +115,11 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
instance.change_description(request.data["description"]) instance.change_description(request.data["description"])
return Response(InstanceSerializer(instance).data) return Response(InstanceSerializer(instance).data)
def destroy(self, request, pk, format=None): def destroy(self, request, pk):
instance = self.get_object(pk) instance = self.get_object(pk)
if not instance: if not instance:
return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_204_NO_CONTENT)
if not self.has_perms_for_object(request.user, 'destroy', instance): instance.destroy()
return Response({"error": "No permission to destroy the Virtual Machine."},
status=status.HTTP_401_UNAUTHORIZED)
instance.delete()
return Response(status=status.HTTP_204_NO_CONTENT) return Response(status=status.HTTP_204_NO_CONTENT)
@action(detail=True, methods=["post"]) @action(detail=True, methods=["post"])
...@@ -182,10 +180,12 @@ class InstanceViewSet(AuthorizationMixin, ViewSet): ...@@ -182,10 +180,12 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
@action(detail=True, methods=["POST"]) @action(detail=True, methods=["POST"])
def attach_disk(self, request, pk): def attach_disk(self, request, pk):
# TODO: attach not implemented in model
pass pass
@action(detail=True, methods=["POST"]) @action(detail=True, methods=["POST"])
def detach_disk(self, request, pk): def detach_disk(self, request, pk):
# TODO: attach not implemented in model
pass pass
@action(detail=True, methods=["POST"]) @action(detail=True, methods=["POST"])
......
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