Commit 52626ae9 by Belákovics Ádám

Add update actions

parent 39705221
Pipeline #860 passed with stage
in 1 minute 21 seconds
...@@ -186,6 +186,16 @@ class Instance(models.Model): ...@@ -186,6 +186,16 @@ class Instance(models.Model):
seconds=lease.delete_interval_in_sec) seconds=lease.delete_interval_in_sec)
) )
def renew(self, lease=None):
"""Renew virtual machine, if a new lease is provided it changes it as well.
"""
if lease is None:
lease = self.lease
else:
self.lease = lease
self.time_of_suspend, self.time_of_delete = self.get_renew_times(lease)
self.save()
def delete(self): def delete(self):
try: try:
interface.destroy_vm(self.remote_id) interface.destroy_vm(self.remote_id)
...@@ -208,3 +218,11 @@ class Instance(models.Model): ...@@ -208,3 +218,11 @@ class Instance(models.Model):
return User.objects.make_random_password( return User.objects.make_random_password(
allowed_chars='abcdefghijklmnopqrstuvwx' allowed_chars='abcdefghijklmnopqrstuvwx'
'ABCDEFGHIJKLMNOPQRSTUVWX123456789') 'ABCDEFGHIJKLMNOPQRSTUVWX123456789')
def change_name(self, new_name):
self.name = new_name
self.save()
def change_description(self, new_description):
self.description = new_description
self.save()
...@@ -27,6 +27,23 @@ authorization = { ...@@ -27,6 +27,23 @@ authorization = {
"reboot": {"object": ["operate_instance"]}, "reboot": {"object": ["operate_instance"]},
} }
update_actions = [
"change_name",
"change_description",
"renew",
"change_lease",
"change_flavor",
"attach_disk",
"resize_disk",
"add_permission",
"remove_permission",
"open_port",
"close_port",
"add_network",
"remove_network",
"new_password",
]
class InstanceViewSet(AuthorizationMixin, ViewSet): class InstanceViewSet(AuthorizationMixin, ViewSet):
...@@ -85,15 +102,50 @@ class InstanceViewSet(AuthorizationMixin, ViewSet): ...@@ -85,15 +102,50 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
return Response(merged_dict) return Response(merged_dict)
# I think we need separate actions for this behaviour because of the access control - BAdam
def update(self, request, pk, format=None): def update(self, request, pk, format=None):
instance = self.get_object(pk) if request.data["action"] in update_actions:
serializer = InstanceSerializer(instance, data=request.data) instance = self.get_object(pk)
if serializer.is_valid(): if not self.has_perms_for_object(request.user, 'retrieve', instance):
serializer.save() return Response({"error": "No permission to access the Virtual Machine."},
return Response(serializer.data) status=status.HTTP_401_UNAUTHORIZED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) 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(id=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 = {**instanceDict, **remoteInstanceDict}
return Response(merged_dict)
else:
return Response({"error": "Unknown update action."}, status=status.HTTP_400_BAD_REQUEST)
def destroy(self, request, pk, format=None): def destroy(self, request, pk, format=None):
instance = self.get_object(pk) instance = self.get_object(pk)
......
Subproject commit e01d873c78ac17fed0438936f979de3cbaca6a5e Subproject commit 1a19e4355f4af1abb49a3f6e07dc3a6c3f8bdf47
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