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):
except Instance.DoesNotExist:
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):
instances = self.get_objects_with_perms(request.user, "list", Instance)
return Response(InstanceSerializer(instances, many=True).data)
......@@ -64,44 +76,32 @@ class InstanceViewSet(AuthorizationMixin, ViewSet):
if not self.has_perms_for_model(request.user, 'create'):
return Response({"error": "No permission to create Virtual Machine."},
status=status.HTTP_401_UNAUTHORIZED)
data = request.data
template = ImageTemplate.objects.get(pk=data["template"])
# 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"])
params = request.data
template = ImageTemplate.objects.get(pk=params["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
newInstance = Instance.create_instance_from_template(
params={"name": data["name"],
"description": data["description"],
"access_method": data["access"],
"system": data["system"],
params={"name": params["name"],
"description": params["description"],
"access_method": params["access"],
"system": params["system"],
},
lease=template.lease,
lease=lease,
networks=[{"uuid": template.network_id}],
template=template,
flavor=template.flavor,
flavor=flavor,
owner=request.user,
disks=None
)
return Response(newInstance.pk)
return Response(InstanceSerializer(newInstance).data)
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)
instanceDict = InstanceSerializer(instance).data
remoteInstance = instance.get_remote_instance()
remoteInstanceDict = remoteInstance.__dict__
merged_dict = {**instanceDict, **remoteInstanceDict}
return Response(merged_dict)
mergedInstance = self.get_merged_object(pk)
return Response(mergedInstance)
def update(self, request, pk, format=None):
if request.data["action"] in update_actions:
......@@ -208,7 +208,6 @@ class FlavorViewSet(ViewSet):
vcpu=data["vcpu"],
initial_disk=data["initial_disk"],
priority=data["priority"])
return Response(new_flavor.pk)
def update(self, request, pk):
......
......@@ -12,6 +12,7 @@ class ImageTemplateModelSerializer(serializers.ModelSerializer):
class Meta:
model = ImageTemplate
fields = (
"id",
"name",
"description",
"created_at",
......@@ -25,4 +26,5 @@ class ImageTemplateModelSerializer(serializers.ModelSerializer):
"created_at",
"created_by",
"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