Commit 70bd9c63 by Belákovics Ádám

Merge branch 'api_changes' into 'DEV'

Api changes

See merge request !20
parents b5fdffad 94f951c4
Pipeline #922 passed with stage
in 1 minute 20 seconds
...@@ -56,6 +56,18 @@ class InstanceViewSet(AuthorizationMixin, ViewSet): ...@@ -56,6 +56,18 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
except Instance.DoesNotExist: except Instance.DoesNotExist:
raise Http404 raise Http404
def get_merged_object(self, pk):
instance = self.get_object(pk)
instanceDict = InstanceSerializer(instance).data
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): def list(self, request):
instances = self.get_objects_with_perms(request.user, "list", Instance) instances = self.get_objects_with_perms(request.user, "list", Instance)
return Response(InstanceSerializer(instances, many=True).data) return Response(InstanceSerializer(instances, many=True).data)
...@@ -64,44 +76,32 @@ class InstanceViewSet(AuthorizationMixin, ViewSet): ...@@ -64,44 +76,32 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
if not self.has_perms_for_model(request.user, 'create'): if not self.has_perms_for_model(request.user, 'create'):
return Response({"error": "No permission to create Virtual Machine."}, return Response({"error": "No permission to create Virtual Machine."},
status=status.HTTP_401_UNAUTHORIZED) status=status.HTTP_401_UNAUTHORIZED)
params = request.data
data = request.data template = ImageTemplate.objects.get(pk=params["template"])
template = ImageTemplate.objects.get(pk=data["template"]) flavor = Flavor.objects.get(pk=params["flavor"]) if "flavor" in params else template.flavor
lease = Lease.objects.get(pk=params["lease"]) if "lease" in params else template.lease
# TODO: if the user can select anouther lease and flavor
# That will be applied, not from the template
# flavor = Flavor.objects.get(pk=data["flavor"])
# lease = Lease.objects.get(pk=data["lease"])
newInstance = Instance.create_instance_from_template( newInstance = Instance.create_instance_from_template(
params={"name": data["name"], params={"name": params["name"],
"description": data["description"], "description": params["description"],
"access_method": data["access"], "access_method": params["access"],
"system": data["system"], "system": params["system"],
}, },
lease=template.lease, lease=lease,
networks=[{"uuid": template.network_id}], networks=[{"uuid": template.network_id}],
template=template, template=template,
flavor=template.flavor, flavor=flavor,
owner=request.user, owner=request.user,
disks=None disks=None
) )
return Response(newInstance.pk) return Response(InstanceSerializer(newInstance).data)
def retrieve(self, request, pk): def retrieve(self, request, pk):
instance = self.get_object(pk) instance = self.get_object(pk)
if not self.has_perms_for_object(request.user, 'retrieve', instance): if not self.has_perms_for_object(request.user, 'retrieve', instance):
return Response({"error": "No permission to access the Virtual Machine."}, return Response({"error": "No permission to access the Virtual Machine."},
status=status.HTTP_401_UNAUTHORIZED) status=status.HTTP_401_UNAUTHORIZED)
mergedInstance = self.get_merged_object(pk)
instanceDict = InstanceSerializer(instance).data return Response(mergedInstance)
remoteInstance = instance.get_remote_instance()
remoteInstanceDict = remoteInstance.__dict__
merged_dict = {**instanceDict, **remoteInstanceDict}
return Response(merged_dict)
def update(self, request, pk, format=None): def update(self, request, pk, format=None):
if request.data["action"] in update_actions: if request.data["action"] in update_actions:
...@@ -208,7 +208,6 @@ class FlavorViewSet(ViewSet): ...@@ -208,7 +208,6 @@ class FlavorViewSet(ViewSet):
vcpu=data["vcpu"], vcpu=data["vcpu"],
initial_disk=data["initial_disk"], initial_disk=data["initial_disk"],
priority=data["priority"]) priority=data["priority"])
return Response(new_flavor.pk) return Response(new_flavor.pk)
def update(self, request, pk): def update(self, request, pk):
......
...@@ -12,6 +12,7 @@ class ImageTemplateModelSerializer(serializers.ModelSerializer): ...@@ -12,6 +12,7 @@ class ImageTemplateModelSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = ImageTemplate model = ImageTemplate
fields = ( fields = (
"id",
"name", "name",
"description", "description",
"created_at", "created_at",
...@@ -25,4 +26,5 @@ class ImageTemplateModelSerializer(serializers.ModelSerializer): ...@@ -25,4 +26,5 @@ class ImageTemplateModelSerializer(serializers.ModelSerializer):
"created_at", "created_at",
"created_by", "created_by",
"type", "type",
"id",
) )
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