forked from uncloud/uncloud
Add sample clean() for model + Add tests for uncloud_vm
This commit is contained in:
parent
3228b91038
commit
6c9c63e0da
2 changed files with 153 additions and 38 deletions
|
|
@ -1,19 +1,24 @@
|
|||
import uuid
|
||||
|
||||
from django.db import models
|
||||
from django.contrib.auth import get_user_model
|
||||
import uuid
|
||||
|
||||
# Uncomment if you override model's clean method
|
||||
# from django.core.exceptions import ValidationError
|
||||
|
||||
from uncloud_pay.models import Product
|
||||
|
||||
STATUS_CHOICES = (
|
||||
('pending', 'Pending'), # Initial state
|
||||
('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
|
||||
('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
|
||||
('deleted', 'Deleted'), # Does not exist anymore, only DB entry as a log
|
||||
)
|
||||
|
||||
STATUS_DEFAULT='pending'
|
||||
STATUS_DEFAULT = 'pending'
|
||||
|
||||
|
||||
class VMHost(models.Model):
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
|
|
@ -30,19 +35,13 @@ class VMHost(models.Model):
|
|||
# ram that can be used of the server
|
||||
usable_ram_in_gb = models.FloatField(default=0)
|
||||
|
||||
|
||||
status = models.CharField(max_length=32,
|
||||
choices=STATUS_CHOICES,
|
||||
default=STATUS_DEFAULT
|
||||
)
|
||||
status = models.CharField(max_length=32, choices=STATUS_CHOICES, default=STATUS_DEFAULT)
|
||||
|
||||
|
||||
class VMProduct(Product):
|
||||
vmhost = models.ForeignKey(VMHost,
|
||||
on_delete=models.CASCADE,
|
||||
editable=False,
|
||||
blank=True,
|
||||
null=True)
|
||||
vmhost = models.ForeignKey(
|
||||
VMHost, on_delete=models.CASCADE, editable=False, blank=True, null=True
|
||||
)
|
||||
|
||||
cores = models.IntegerField()
|
||||
ram_in_gb = models.FloatField()
|
||||
|
|
@ -60,36 +59,30 @@ class VMDiskImageProduct(models.Model):
|
|||
|
||||
"""
|
||||
|
||||
uuid = models.UUIDField(primary_key=True,
|
||||
default=uuid.uuid4,
|
||||
editable=False)
|
||||
owner = models.ForeignKey(get_user_model(),
|
||||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
|
||||
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(null=True,
|
||||
blank=True)
|
||||
import_url = models.URLField(null=True,
|
||||
blank=True)
|
||||
size_in_gb = models.FloatField(null=True, blank=True)
|
||||
import_url = models.URLField(null=True, blank=True)
|
||||
|
||||
|
||||
storage_class = models.CharField(max_length=32,
|
||||
choices = (
|
||||
('hdd', 'HDD'),
|
||||
('ssd', 'SSD'),
|
||||
),
|
||||
default='ssd'
|
||||
storage_class = models.CharField(
|
||||
max_length=32,
|
||||
choices=(
|
||||
('hdd', 'HDD'),
|
||||
('ssd', 'SSD'),
|
||||
),
|
||||
default='ssd'
|
||||
)
|
||||
|
||||
status = models.CharField(max_length=32,
|
||||
choices=STATUS_CHOICES,
|
||||
default=STATUS_DEFAULT
|
||||
status = models.CharField(
|
||||
max_length=32, choices=STATUS_CHOICES, default=STATUS_DEFAULT
|
||||
)
|
||||
|
||||
|
||||
class VMDiskProduct(models.Model):
|
||||
"""
|
||||
The VMDiskProduct is attached to a VM.
|
||||
|
|
@ -104,14 +97,29 @@ class VMDiskProduct(models.Model):
|
|||
on_delete=models.CASCADE,
|
||||
editable=False)
|
||||
|
||||
vm = models.ForeignKey(VMProduct, 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)
|
||||
|
||||
# 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)
|
||||
|
||||
|
||||
class VMNetworkCard(models.Model):
|
||||
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
|
||||
vm = models.ForeignKey(VMProduct, on_delete=models.CASCADE)
|
||||
|
||||
mac_address = models.IntegerField()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue