add url for importing disk image

Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
Nico Schottelius 2020-02-29 17:00:13 +01:00
parent bcbd6f6f83
commit 6a38e4e0a4
4 changed files with 45 additions and 6 deletions

View file

@ -0,0 +1,23 @@
# Generated by Django 3.0.3 on 2020-02-29 15:59
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('uncloud_vm', '0006_auto_20200229_1545'),
]
operations = [
migrations.AddField(
model_name='vmdiskimageproduct',
name='import_url',
field=models.URLField(blank=True, null=True),
),
migrations.AlterField(
model_name='vmdiskimageproduct',
name='size_in_gb',
field=models.FloatField(blank=True, null=True),
),
]

View file

@ -55,7 +55,9 @@ class VMDiskImageProduct(models.Model):
""" """
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) uuid = models.UUIDField(primary_key=True,
default=uuid.uuid4,
editable=False)
owner = models.ForeignKey(get_user_model(), owner = models.ForeignKey(get_user_model(),
on_delete=models.CASCADE, on_delete=models.CASCADE,
editable=False) editable=False)
@ -64,7 +66,10 @@ class VMDiskImageProduct(models.Model):
is_os_image = models.BooleanField(default=False) is_os_image = models.BooleanField(default=False)
is_public = models.BooleanField(default=False) is_public = models.BooleanField(default=False)
size_in_gb = models.FloatField() size_in_gb = models.FloatField(null=True,
blank=True)
import_url = models.URLField(null=True,
blank=True)
storage_class = models.CharField(max_length=32, storage_class = models.CharField(max_length=32,

View file

@ -3,6 +3,13 @@ from django.contrib.auth import get_user_model
from rest_framework import serializers from rest_framework import serializers
from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct from .models import VMHost, VMProduct, VMSnapshotProduct, VMDiskProduct, VMDiskImageProduct
GB_SSD_PER_DAY=0.012
GB_HDD_PER_DAY=0.0006
GB_SSD_PER_DAY=0.012
GB_HDD_PER_DAY=0.0006
class VMHostSerializer(serializers.ModelSerializer): class VMHostSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = VMHost model = VMHost
@ -32,11 +39,9 @@ class VMSnapshotProductSerializer(serializers.ModelSerializer):
# verify that vm.owner == user.request # verify that vm.owner == user.request
def validate_vm(self, value): def validate_vm(self, value):
if not value.owner == self.context['request'].user: if not value.owner == self.context['request'].user:
raise serializers.ValidationError("VM {} not found for owner {}.".format(value, raise serializers.ValidationError("VM {} not found for owner {}.".format(value,
self.context['request'].user)) self.context['request'].user))
disks = VMDiskProduct.objects.filter(vm=value) disks = VMDiskProduct.objects.filter(vm=value)
if len(disks) == 0: if len(disks) == 0:

View file

@ -26,7 +26,14 @@ class VMDiskImageProductMineViewSet(viewsets.ModelViewSet):
def get_queryset(self): def get_queryset(self):
return VMDiskImageProduct.objects.filter(owner=self.request.user) return VMDiskImageProduct.objects.filter(owner=self.request.user)
class VMDiskImageProductPublicViewSet(viewsets.ModelViewSet): def create(self, request):
serializer = VMProductSerializer(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
serializer.save(owner=request.user)
return Response(serializer.data)
class VMDiskImageProductPublicViewSet(viewsets.ReadOnlyModelViewSet):
permission_classes = [permissions.IsAuthenticated] permission_classes = [permissions.IsAuthenticated]
serializer_class = VMDiskImageProductSerializer serializer_class = VMDiskImageProductSerializer
@ -55,7 +62,6 @@ class VMProductViewSet(viewsets.ModelViewSet):
order.save() order.save()
serializer.save(owner=request.user, order=order) serializer.save(owner=request.user, order=order)
return Response(serializer.data) return Response(serializer.data)