2020-03-02 11:54:36 +00:00
|
|
|
import uuid
|
|
|
|
|
2020-02-23 13:07:37 +00:00
|
|
|
from django.db import models
|
2020-02-26 10:31:17 +00:00
|
|
|
from django.contrib.auth import get_user_model
|
2020-03-02 11:54:36 +00:00
|
|
|
|
|
|
|
# Uncomment if you override model's clean method
|
|
|
|
# from django.core.exceptions import ValidationError
|
2020-02-23 13:07:37 +00:00
|
|
|
|
2020-03-02 06:17:04 +00:00
|
|
|
from uncloud_pay.models import Product
|
|
|
|
|
|
|
|
STATUS_CHOICES = (
|
|
|
|
('pending', 'Pending'), # Initial state
|
2020-03-02 11:54:36 +00:00
|
|
|
('creating', 'Creating'), # Creating VM/image/etc.
|
|
|
|
('active', 'Active'), # Is usable / active
|
|
|
|
('disabled', 'Disabled'), # Is usable, but cannot be used for new things
|
|
|
|
('unusable', 'Unusable'), # Has some kind of error
|
2020-03-02 06:17:04 +00:00
|
|
|
('deleted', 'Deleted'), # Does not exist anymore, only DB entry as a log
|
|
|
|
)
|
|
|
|
|
2020-03-02 11:54:36 +00:00
|
|
|
STATUS_DEFAULT = 'pending'
|
|
|
|
|
2020-02-23 13:07:37 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
class VMHost(models.Model):
|
2020-02-23 13:07:37 +00:00
|
|
|
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
2020-02-26 10:31:17 +00:00
|
|
|
|
|
|
|
# 253 is the maximum DNS name length
|
|
|
|
hostname = models.CharField(max_length=253, unique=True)
|
|
|
|
|
|
|
|
# indirectly gives a maximum number of cores / VM - f.i. 32
|
|
|
|
physical_cores = models.IntegerField(default=0)
|
|
|
|
|
|
|
|
# determines the maximum usable cores - f.i. 320 if you overbook by a factor of 10
|
|
|
|
usable_cores = models.IntegerField(default=0)
|
|
|
|
|
|
|
|
# ram that can be used of the server
|
|
|
|
usable_ram_in_gb = models.FloatField(default=0)
|
|
|
|
|
2020-03-02 12:20:30 +00:00
|
|
|
status = models.CharField(
|
|
|
|
max_length=32, choices=STATUS_CHOICES, default=STATUS_DEFAULT
|
|
|
|
)
|
2020-02-26 10:31:17 +00:00
|
|
|
|
|
|
|
|
2020-03-02 06:17:04 +00:00
|
|
|
class VMProduct(Product):
|
2020-03-02 12:20:30 +00:00
|
|
|
vmhost = models.ForeignKey(
|
|
|
|
VMHost, on_delete=models.CASCADE, editable=False, blank=True, null=True
|
|
|
|
)
|
2020-02-23 13:07:37 +00:00
|
|
|
|
|
|
|
cores = models.IntegerField()
|
2020-02-26 10:31:17 +00:00
|
|
|
ram_in_gb = models.FloatField()
|
|
|
|
|
|
|
|
|
|
|
|
class VMWithOSProduct(VMProduct):
|
|
|
|
pass
|
|
|
|
|
2020-03-02 06:17:04 +00:00
|
|
|
|
|
|
|
class VMDiskImageProduct(models.Model):
|
|
|
|
"""
|
|
|
|
Images are used for cloning/linking.
|
|
|
|
|
|
|
|
They are the base for images.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-03-02 12:05:23 +00:00
|
|
|
uuid = models.UUIDField(
|
|
|
|
primary_key=True, default=uuid.uuid4, editable=False
|
|
|
|
)
|
|
|
|
owner = models.ForeignKey(
|
|
|
|
get_user_model(), on_delete=models.CASCADE, editable=False
|
|
|
|
)
|
2020-03-02 06:17:04 +00:00
|
|
|
|
|
|
|
name = models.CharField(max_length=256)
|
|
|
|
is_os_image = models.BooleanField(default=False)
|
|
|
|
is_public = models.BooleanField(default=False)
|
|
|
|
|
2020-03-02 11:54:36 +00:00
|
|
|
size_in_gb = models.FloatField(null=True, blank=True)
|
|
|
|
import_url = models.URLField(null=True, blank=True)
|
2020-02-26 10:31:17 +00:00
|
|
|
|
2020-03-02 11:54:36 +00:00
|
|
|
storage_class = models.CharField(
|
|
|
|
max_length=32,
|
|
|
|
choices=(
|
|
|
|
('hdd', 'HDD'),
|
|
|
|
('ssd', 'SSD'),
|
|
|
|
),
|
|
|
|
default='ssd'
|
2020-02-26 10:31:17 +00:00
|
|
|
)
|
|
|
|
|
2020-03-02 11:54:36 +00:00
|
|
|
status = models.CharField(
|
|
|
|
max_length=32, choices=STATUS_CHOICES, default=STATUS_DEFAULT
|
2020-03-02 06:17:04 +00:00
|
|
|
)
|
|
|
|
|
2020-03-02 11:54:36 +00:00
|
|
|
|
2020-03-02 06:17:04 +00:00
|
|
|
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)
|
|
|
|
|
2020-03-02 11:54:36 +00:00
|
|
|
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
|
2020-03-02 06:17:04 +00:00
|
|
|
image = models.ForeignKey(VMDiskImageProduct, on_delete=models.CASCADE)
|
|
|
|
|
|
|
|
size_in_gb = models.FloatField(blank=True)
|
2020-02-23 13:07:37 +00:00
|
|
|
|
2020-03-02 11:54:36 +00:00
|
|
|
# Sample code for clean method
|
|
|
|
|
|
|
|
# Ensures that a VMDiskProduct can only be created from a VMDiskImageProduct
|
|
|
|
# that is in status 'active'
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2020-02-23 13:07:37 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
class VMNetworkCard(models.Model):
|
2020-03-02 11:54:36 +00:00
|
|
|
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
|
2020-03-02 06:17:04 +00:00
|
|
|
|
2020-02-26 10:31:17 +00:00
|
|
|
mac_address = models.IntegerField()
|
2020-03-02 06:17:04 +00:00
|
|
|
|
|
|
|
ip_address = models.GenericIPAddressField(blank=True,
|
|
|
|
null=True)
|
|
|
|
|
|
|
|
|
|
|
|
class VMSnapshotProduct(Product):
|
|
|
|
gb_ssd = models.FloatField(editable=False)
|
|
|
|
gb_hdd = models.FloatField(editable=False)
|
|
|
|
|
|
|
|
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
|