Relate VM to disks and snapshots

This commit is contained in:
Nico Schottelius 2020-03-18 15:43:01 +01:00
parent 4b4cbbf009
commit a32f7522b5
3 changed files with 30 additions and 16 deletions

View file

@ -152,7 +152,9 @@ class VMDiskProduct(models.Model):
on_delete=models.CASCADE, on_delete=models.CASCADE,
editable=False) editable=False)
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE) vm = models.ForeignKey(VMProduct,
related_name='disks',
on_delete=models.CASCADE)
image = models.ForeignKey(VMDiskImageProduct, on_delete=models.CASCADE) image = models.ForeignKey(VMDiskImageProduct, on_delete=models.CASCADE)
size_in_gb = models.FloatField(blank=True) size_in_gb = models.FloatField(blank=True)

View file

@ -31,20 +31,6 @@ class VMDiskImageProductSerializer(serializers.ModelSerializer):
model = VMDiskImageProduct model = VMDiskImageProduct
fields = '__all__' fields = '__all__'
class VMProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMProduct
fields = ['uuid', 'order', 'owner', 'status', 'name',
'cores', 'ram_in_gb', 'recurring_period',
'snapshots' ]
read_only_fields = ['uuid', 'order', 'owner', 'status']
# Custom field used at creation (= ordering) only.
recurring_period = serializers.ChoiceField(
choices=VMProduct.allowed_recurring_periods())
snapshots = serializers.PrimaryKeyRelatedField(many=True,
read_only=True)
class DCLVMProductSerializer(serializers.HyperlinkedModelSerializer): class DCLVMProductSerializer(serializers.HyperlinkedModelSerializer):
@ -92,3 +78,24 @@ class VMSnapshotProductSerializer(serializers.ModelSerializer):
pricing['per_gb_ssd'] = 0.012 pricing['per_gb_ssd'] = 0.012
pricing['per_gb_hdd'] = 0.0006 pricing['per_gb_hdd'] = 0.0006
pricing['recurring_period'] = 'per_day' pricing['recurring_period'] = 'per_day'
class VMProductSerializer(serializers.ModelSerializer):
class Meta:
model = VMProduct
fields = ['uuid', 'order', 'owner', 'status', 'name',
'cores', 'ram_in_gb', 'recurring_period',
'snapshots', 'disks' ]
read_only_fields = ['uuid', 'order', 'owner', 'status']
# Custom field used at creation (= ordering) only.
recurring_period = serializers.ChoiceField(
choices=VMProduct.allowed_recurring_periods())
# snapshots = serializers.PrimaryKeyRelatedField(many=True,
# read_only=True)
snapshots = VMSnapshotProductSerializer(many=True,
read_only=True)
disks = VMDiskProductSerializer(many=True,
read_only=True)

View file

@ -65,7 +65,12 @@ class VMDiskProductViewSet(viewsets.ModelViewSet):
serializer_class = VMDiskProductSerializer serializer_class = VMDiskProductSerializer
def get_queryset(self): def get_queryset(self):
return VMDiskProduct.objects.filter(owner=self.request.user) if self.request.user.is_superuser:
obj = VMDiskProduct.objects.all()
else:
obj = VMDiskProduct.objects.filter(owner=self.request.user)
return obj
def create(self, request): def create(self, request):
serializer = VMDiskProductSerializer(data=request.data, context={'request': request}) serializer = VMDiskProductSerializer(data=request.data, context={'request': request})