Introduce disk->image relationship

Signed-off-by: Nico Schottelius <nico@nico-notebook.schottelius.org>
This commit is contained in:
Nico Schottelius 2020-02-29 16:45:52 +01:00
commit bcbd6f6f83
5 changed files with 161 additions and 56 deletions

View file

@ -46,11 +46,27 @@ class VMProduct(Product):
class VMWithOSProduct(VMProduct):
pass
class VMDiskProduct(models.Model):
class VMDiskImageProduct(models.Model):
"""
Images are used for cloning/linking.
They are the base for images.
"""
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
owner = models.ForeignKey(get_user_model(),
on_delete=models.CASCADE,
editable=False)
name = models.CharField(max_length=256)
is_os_image = models.BooleanField(default=False)
is_public = models.BooleanField(default=False)
size_in_gb = models.FloatField()
storage_class = models.CharField(max_length=32,
choices = (
('hdd', 'HDD'),
@ -59,9 +75,32 @@ class VMDiskProduct(models.Model):
default='ssd'
)
class OperatingSystemDisk(VMDiskProduct):
""" Defines an Operating System Disk that can be cloned for a VM """
os_name = models.CharField(max_length=128)
# source = models.CharField(max_length=32,
# choices = (
# ('url', 'HDD'),
# ('ssd', 'SSD'),
# ),
# default='ssd'
# )
class VMDiskProduct(models.Model):
"""
The VMDiskProduct is attached to a VM.
It is based on a VMDiskImageProduct that will be used as a basis.
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, on_delete=models.CASCADE)
image = models.ForeignKey(VMDiskImageProduct, on_delete=models.CASCADE)
size_in_gb = models.FloatField()
class VMNetworkCard(models.Model):
@ -74,44 +113,7 @@ class VMNetworkCard(models.Model):
class VMSnapshotProduct(Product):
price_per_gb_ssd = 0.35
price_per_gb_hdd = 1.5/100
# This we need to get from the VM
gb_ssd = models.FloatField(editable=False)
gb_hdd = models.FloatField(editable=False)
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
#vm_uuid = models.UUIDField()
# Need to setup recurring_price and one_time_price and recurring period
sample_ssd = 10
sample_hdd = 100
def recurring_price(self):
return 0
def one_time_price(self):
return 0
@classmethod
def sample_price(cls):
return cls.sample_ssd * cls.price_per_gb_ssd + cls.sample_hdd * cls.price_per_gb_hdd
description = "Create snapshot of a VM"
recurring_period = "monthly"
@classmethod
def pricing_model(cls):
return """
Pricing is on monthly basis and storage prices are equivalent to the storage
price in the VM.
Price per GB SSD is: {}
Price per GB HDD is: {}
Sample price for a VM with {} GB SSD and {} GB HDD VM is: {}.
""".format(cls.price_per_gb_ssd, cls.price_per_gb_hdd,
cls.sample_ssd, cls.sample_hdd, cls.sample_price())