1
# Copyright 2015 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Model for a nodes virtual block device."""
6
from __future__ import (
19
from uuid import uuid4
21
from django.core.exceptions import ValidationError
22
from django.db.models import (
26
from maasserver import DefaultMeta
27
from maasserver.models.blockdevice import (
31
from maasserver.models.filesystemgroup import FilesystemGroup
32
from maasserver.models.node import Node
35
class VirtualBlockDeviceManager(BlockDeviceManager):
36
"""Manager for `VirtualBlockDevice` class."""
39
class VirtualBlockDevice(BlockDevice):
40
"""A virtual block device attached to a node."""
42
class Meta(DefaultMeta):
43
"""Needed for South to recognize this model."""
45
objects = VirtualBlockDeviceManager()
48
max_length=36, unique=True, null=False, blank=False, editable=False)
50
filesystem_group = ForeignKey(
51
FilesystemGroup, null=False, blank=False,
52
related_name="virtual_devices")
54
def clean(self, *args, **kwargs):
55
super(VirtualBlockDevice, self).clean(*args, **kwargs)
57
# First time called the node might not be set, so we handle the
58
# DoesNotExist exception accordingly.
61
except Node.DoesNotExist:
62
# Set the node of this virtual block device, to the same node from
63
# the attached filesystem group.
64
fsgroup_node = self.filesystem_group.get_node()
65
if fsgroup_node is not None:
66
self.node = fsgroup_node
68
# The node on the virtual block device must be the same node from
69
# the attached filesystem group.
70
if node != self.filesystem_group.get_node():
71
raise ValidationError(
72
"Node must be the same node as the filesystem_group.")
74
def save(self, *args, **kwargs):
77
return super(VirtualBlockDevice, self).save(*args, **kwargs)