forked from uncloud/uncloud
Wire disk images to VM creation/ordering
This commit is contained in:
parent
a7e9f3c09d
commit
d3b7470294
5 changed files with 85 additions and 22 deletions
|
|
@ -69,6 +69,7 @@ class VMProduct(Product):
|
|||
cores = models.IntegerField()
|
||||
ram_in_gb = models.FloatField()
|
||||
|
||||
primary_disk = models.ForeignKey('VMDiskProduct', on_delete=models.CASCADE, null=True)
|
||||
|
||||
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):
|
||||
# TODO: move magic numbers in variables
|
||||
|
|
@ -141,7 +142,7 @@ class VMDiskImageProduct(UncloudModel):
|
|||
|
||||
|
||||
|
||||
class VMDiskProduct(UncloudModel):
|
||||
class VMDiskProduct(Product):
|
||||
"""
|
||||
The VMDiskProduct is attached to a VM.
|
||||
|
||||
|
|
@ -150,18 +151,27 @@ class VMDiskProduct(UncloudModel):
|
|||
It can be enlarged, but not shrinked compared to the VMDiskImageProduct.
|
||||
"""
|
||||
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
|
||||
vm = models.ForeignKey(VMProduct,
|
||||
related_name='disks',
|
||||
on_delete=models.CASCADE)
|
||||
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
|
||||
image = models.ForeignKey(VMDiskImageProduct, on_delete=models.CASCADE)
|
||||
|
||||
size_in_gb = models.FloatField(blank=True)
|
||||
|
||||
@property
|
||||
def description(self):
|
||||
return "Disk for VM '{}': {}GB".format(self.vm.name, self.size_in_gb)
|
||||
|
||||
# TODO: move magic numbers in variables
|
||||
def recurring_price(self, recurring_period=RecurringPeriod.PER_MONTH):
|
||||
# TODO: move magic numbers in variables
|
||||
if recurring_period == RecurringPeriod.PER_MONTH:
|
||||
return (self.size_in_gb / 10) * 3.5
|
||||
if recurring_period == RecurringPeriod.PER_YEAR:
|
||||
return recurring_price(self, recurring_period.PER_MONTH) * 12
|
||||
if recurring_period == RecurringPeriod.PER_HOUR:
|
||||
return recurring_price(self, recurring_period.PER_MONTH) / 25
|
||||
else:
|
||||
raise Exception('Invalid recurring period for VM Disk Product pricing.')
|
||||
|
||||
# Sample code for clean method
|
||||
|
||||
# Ensures that a VMDiskProduct can only be created from a VMDiskImageProduct
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue