Commit 1f79d0e3 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Add VolumeType DB model

Create new 'VolumeType' database model to represent the available types
for Cyclades Volumes. Currently, this model contains only the volume's
disk template and the human-readable name, but can be extended
to include extra specs, e.g. QoS specs.

The 'disk_template' column from the 'Flavor' and 'Volume' models is
replaced with a foreign key to the 'VolumeType' model.
parent d5e374bc
......@@ -35,24 +35,39 @@ import logging
log = logging.getLogger(__name__)
class VolumeType(models.Model):
NAME_LENGTH = 255
name = models.CharField("Name", max_length=NAME_LENGTH)
disk_template = models.CharField('Disk Template', max_length=32)
deleted = models.BooleanField('Deleted', default=False)
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return u"<VolumeType %s(disk_template:%s)>" % \
(self.name, self.disk_template)
class Flavor(models.Model):
cpu = models.IntegerField('Number of CPUs', default=0)
ram = models.IntegerField('RAM size in MiB', default=0)
disk = models.IntegerField('Disk size in GiB', default=0)
disk_template = models.CharField('Disk template', max_length=32)
volume_type = models.ForeignKey(VolumeType, related_name="flavors",
on_delete=models.PROTECT, null=False)
deleted = models.BooleanField('Deleted', default=False)
# Whether the flavor can be used to create new servers
allow_create = models.BooleanField(default=True, null=False)
class Meta:
verbose_name = u'Virtual machine flavor'
unique_together = ('cpu', 'ram', 'disk', 'disk_template')
unique_together = ('cpu', 'ram', 'disk', 'volume_type')
@property
def name(self):
"""Returns flavor name (generated)"""
return u'C%sR%sD%s%s' % (self.cpu, self.ram, self.disk,
self.disk_template)
self.volume_type.disk_template)
def __str__(self):
return self.__unicode__()
......@@ -1046,8 +1061,8 @@ class Volume(models.Model):
userid = models.CharField("Owner's UUID", max_length=100, null=False,
db_index=True)
size = models.IntegerField("Volume size in GB", null=False)
disk_template = models.CharField('Disk template', max_length=32,
null=False)
volume_type = models.ForeignKey(VolumeType, related_name="volumes",
on_delete=models.PROTECT, null=False)
delete_on_termination = models.BooleanField("Delete on Server Termination",
default=True, null=False)
......@@ -1055,8 +1070,6 @@ class Volume(models.Model):
source = models.CharField(max_length=128, null=True)
origin = models.CharField(max_length=128, null=True)
# TODO: volume_type should be foreign key to VolumeType model
volume_type = None
deleted = models.BooleanField("Deleted", default=False, null=False)
# Datetime fields
created = models.DateTimeField(auto_now_add=True)
......
......@@ -14,6 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import factory
from factory.fuzzy import FuzzyChoice
from synnefo.db import models
from random import choice
from string import letters, digits
......@@ -42,13 +43,23 @@ def random_string(x):
return ''.join([choice(digits + letters) for i in range(x)])
class VolumeTypeFactory(factory.DjangoModelFactory):
FACTORY_FOR = models.VolumeType
FACTORY_DJANGO_GET_OR_CREATE = ("disk_template",)
name = factory.Sequence(prefix_seq("vtype"))
disk_template = FuzzyChoice(
choices=["file", "plain", "drbd", "ext_archipelago"]
)
deleted = False
class FlavorFactory(factory.DjangoModelFactory):
FACTORY_FOR = models.Flavor
cpu = factory.Sequence(lambda n: n + 2, type=int)
ram = factory.Sequence(lambda n: n * 512, type=int)
disk = factory.Sequence(lambda n: n * 10, type=int)
disk_template = 'drbd'
volume_type = factory.SubFactory(VolumeTypeFactory)
deleted = False
......@@ -69,7 +80,7 @@ class BackendFactory(factory.DjangoModelFactory):
pinst_cnt = 2
ctotal = 80
disk_templates = ["file", "plain", "drbd"]
disk_templates = ["file", "plain", "drbd", "ext"]
class DrainedBackend(BackendFactory):
......@@ -101,9 +112,7 @@ class VolumeFactory(factory.DjangoModelFactory):
name = factory.Sequence(lambda x: "volume-name-"+x, type=str)
machine = factory.SubFactory(VirtualMachineFactory,
userid=factory.SelfAttribute('..userid'))
disk_template = factory.LazyAttribute(lambda v:
v.machine.flavor.disk_template
if v.machine else "drbd")
volume_type = factory.SubFactory(VolumeTypeFactory)
class DeletedVirtualMachine(VirtualMachineFactory):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment