From a32f7522b551deaeb3f7dbf0a5534762a49f9b51 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Wed, 18 Mar 2020 15:43:01 +0100 Subject: [PATCH] Relate VM to disks and snapshots --- uncloud/uncloud_vm/models.py | 4 +++- uncloud/uncloud_vm/serializers.py | 35 ++++++++++++++++++------------- uncloud/uncloud_vm/views.py | 7 ++++++- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/uncloud/uncloud_vm/models.py b/uncloud/uncloud_vm/models.py index 57b54cf..7e38ded 100644 --- a/uncloud/uncloud_vm/models.py +++ b/uncloud/uncloud_vm/models.py @@ -152,7 +152,9 @@ class VMDiskProduct(models.Model): on_delete=models.CASCADE, 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) size_in_gb = models.FloatField(blank=True) diff --git a/uncloud/uncloud_vm/serializers.py b/uncloud/uncloud_vm/serializers.py index f759d01..96454f7 100644 --- a/uncloud/uncloud_vm/serializers.py +++ b/uncloud/uncloud_vm/serializers.py @@ -31,20 +31,6 @@ class VMDiskImageProductSerializer(serializers.ModelSerializer): model = VMDiskImageProduct 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): @@ -92,3 +78,24 @@ class VMSnapshotProductSerializer(serializers.ModelSerializer): pricing['per_gb_ssd'] = 0.012 pricing['per_gb_hdd'] = 0.0006 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) diff --git a/uncloud/uncloud_vm/views.py b/uncloud/uncloud_vm/views.py index 7b5fa4f..1ef4974 100644 --- a/uncloud/uncloud_vm/views.py +++ b/uncloud/uncloud_vm/views.py @@ -65,7 +65,12 @@ class VMDiskProductViewSet(viewsets.ModelViewSet): serializer_class = VMDiskProductSerializer 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): serializer = VMDiskProductSerializer(data=request.data, context={'request': request})