From 880e4d046bc5d599aaf662f45d15c8555fb6d3ad Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Sat, 1 Aug 2020 18:30:40 +0200 Subject: [PATCH] [vm] cleanup --- uncloud_vm/models.py | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/uncloud_vm/models.py b/uncloud_vm/models.py index e8504a6..3eb46b7 100644 --- a/uncloud_vm/models.py +++ b/uncloud_vm/models.py @@ -97,11 +97,6 @@ class VMProduct(Product): RecurringPeriod.PER_30D, RecurringPeriod.PER_HOUR], RecurringPeriod.choices)) - def __str__(self): - return "VM {} ({} Cores/{} GB RAM) running on {} in cluster {}".format( - self.uuid, self.cores, self.ram_in_gb, - self.vmhost, self.vmcluster) - class VMWithOSProduct(VMProduct): primary_disk = models.ForeignKey('VMDiskProduct', on_delete=models.CASCADE, null=True) @@ -115,9 +110,6 @@ class VMDiskImageProduct(UncloudModel): """ - uuid = models.UUIDField( - primary_key=True, default=uuid.uuid4, editable=False - ) owner = models.ForeignKey( get_user_model(), on_delete=models.CASCADE, editable=False ) @@ -166,7 +158,9 @@ class VMDiskProduct(Product): """ vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE) - image = models.ForeignKey(VMDiskImageProduct, on_delete=models.CASCADE) + + image = models.ForeignKey(VMDiskImageProduct, on_delete=models.CASCADE, + blank=True, null=True) size_in_gb = models.FloatField(blank=True) @@ -181,22 +175,16 @@ class VMDiskProduct(Product): @property def recurring_price(self): - return (self.size_in_gb / 10) * 3.5 - - # Ensures that a VMDiskProduct can only be created from a VMDiskImageProduct - # that is in status 'active' - # This might not be need in "for billing only" constellations - - # def clean(self): - # if self.image.status != 'active': - # raise ValidationError({ - # 'image': 'VM Disk must be created from an active disk image.' - # }) - - def save(self, *args, **kwargs): - self.full_clean() - super().save(*args, **kwargs) + if self.disk_type == VMDiskType.CEPH_SSD: + price_per_gb = 3.5/10 + elif self.disk_type == VMDiskType.CEPH_HDD: + price_per_gb = 1.5/100 + elif self.disk_type == VMDiskType.LOCAL_SSD: + price_per_gb = 3.5/10 + elif self.disk_type == VMDiskType.CEPH_HDD: + price_per_gb = 1.5/100 + return self.size_in_gb * price_per_gb class VMNetworkCard(models.Model):