Commit 7196c9a3 by Chif Gergő

Separate executable actions from update method

parent a94cb580
Pipeline #1048 failed with stage
in 1 minute 29 seconds
......@@ -261,6 +261,10 @@ class Instance(BaseMachineDescriptor):
allowed_chars='abcdefghijklmnopqrstuvwx'
'ABCDEFGHIJKLMNOPQRSTUVWX123456789')
def reset_password(self):
self.password = self.generate_password()
self.save()
def change_name(self, new_name):
self.name = new_name
self.save()
......
......@@ -61,19 +61,6 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
except Instance.DoesNotExist:
raise Http404
def get_merged_object(self, pk):
instance = self.get_object(pk)
instanceDict = InstanceSerializer(instance).data
instance.update_status()
remoteInstance = instance.get_remote_instance()
remoteInstanceDict = {
"remote_id": remoteInstance.id,
"status": remoteInstance.status,
"disks": remoteInstance.disks,
"addresses": remoteInstance.addresses,
}
return({**instanceDict, **remoteInstanceDict})
def list(self, request):
instances = self.get_objects_with_perms(request.user, "list", Instance)
return Response(InstanceListItemSerializer(instances, many=True).data)
......@@ -105,63 +92,32 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
print(perm)
assign_perm(perm, request.user, newInstance)
return Response(InstanceSerializer(newInstance).data)
return Response(InstanceSerializer(newInstance).data, status=status.HTTP_201_CREATED)
def retrieve(self, request, pk):
instance = self.get_object(pk)
if not self.has_perms_for_object(request.user, 'retrieve', instance):
return Response({"error": "No permission to access the Virtual Machine."},
status=status.HTTP_401_UNAUTHORIZED)
mergedInstance = self.get_merged_object(pk)
return Response(mergedInstance)
instance.update_status()
return Response(instance)
# PUT method for updating the instance own properties BUT not the related fields
def update(self, request, pk, format=None):
if request.data["action"] in update_actions:
instance = self.get_object(pk)
if not self.has_perms_for_object(request.user, 'update', instance):
return Response({"error": "No permission to access the Virtual Machine."},
status=status.HTTP_401_UNAUTHORIZED)
action = request.data["action"]
if action == "change_name":
instance.change_name(request.data["name"])
elif action == "change_description":
instance.change_description(request.data["description"])
elif action == "renew":
instance.renew()
elif action == "change_lease":
lease = Lease.objects.get(pk=request.data["lease"])
instance.renew(lease)
elif action == "change_flavor":
pass
elif action == "attach_disk":
pass
elif action == "resize_disk":
pass
elif action == "add_permission":
pass
elif action == "remove_permission":
pass
elif action == "open_port":
pass
elif action == "close_port":
pass
elif action == "add_network":
pass
elif action == "remove_network":
pass
elif action == "new_password":
pass
instanceDict = InstanceSerializer(instance).data
remoteInstance = instance.get_remote_instance()
remoteInstanceDict = remoteInstance.__dict__
merged_dict = {"db": instanceDict, "openstack": remoteInstanceDict}
return Response(merged_dict)
else:
return Response({"error": "Unknown update action."}, status=status.HTTP_400_BAD_REQUEST)
instance = self.get_object(pk)
if not self.has_perms_for_object(request.user, 'update', instance):
return Response({"error": "No permission to access the Virtual Machine."},
status=status.HTTP_401_UNAUTHORIZED)
if "name" in request.data:
instance.change_name(request.data["name"])
if "description" in request.data:
instance.change_description(request.data["description"])
return Response(InstanceSerializer(instance).data)
def destroy(self, request, pk, format=None):
instance = self.get_object(pk)
if not instance:
return Response(status=status.HTTP_204_NO_CONTENT)
if not self.has_perms_for_object(request.user, 'destroy', instance):
return Response({"error": "No permission to destroy the Virtual Machine."},
status=status.HTTP_401_UNAUTHORIZED)
......@@ -196,6 +152,48 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
success = instance.execute_common_action(action=request.data["action"])
return Response(success)
@action(detail=True, methods=["POST"])
def renew_lease(self, request, pk):
instance = self.get_object(pk)
instance.renew_lease()
return Response(status=status.HTTP_200_OK)
@action(detail=True, methods=["POST"])
def change_lease(self, request, pk):
instance = self.get_object(pk)
if "lease" in request.data:
lease = Lease.objects.get(pk=request.data["lease"])
instance.renew(lease)
return Response(status=status.HTTP_200_OK)
else:
return Response(data="No new lease provided", status=status.HTTP_400_BAD_REQUEST)
@action(detail=True, methods=["POST"])
def resize(self, request, pk):
pass
# TODO: resize not implemented in model
# instance = self.get_object(pk)
# if "flavor" in request.data:
# flavor = Flavor.objects.get(pk=request.data["flavor"])
# instance.resize(flavor)
# return Response(status=status.HTTP_200_OK)
# else:
# return Response(data="No new flavor provided", status=status.HTTP_400_BAD_REQUEST)
@action(detail=True, methods=["POST"])
def attach_disk(self, request, pk):
pass
@action(detail=True, methods=["POST"])
def detach_disk(self, request, pk):
pass
@action(detail=True, methods=["POST"])
def reset_password(self, request, pk):
instance = self.get_object(pk)
instance.reset_password()
return Response(status=status.HTTP_200_OK)
class FlavorViewSet(ViewSet):
"""
......
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