~rvb/maas/dj-migrations

« back to all changes in this revision

Viewing changes to src/maasserver/models/virtualblockdevice.py

  • Committer: Raphael Badin
  • Date: 2015-03-13 15:22:22 UTC
  • mfrom: (3631.1.15 maas)
  • Revision ID: raphael.badin@canonical.com-20150313152222-nhq1vs6hop1ooq7e
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2015 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Model for a nodes virtual block device."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
str = None
 
13
 
 
14
__metaclass__ = type
 
15
__all__ = [
 
16
    'VirtualBlockDevice',
 
17
    ]
 
18
 
 
19
from uuid import uuid4
 
20
 
 
21
from django.core.exceptions import ValidationError
 
22
from django.db.models import (
 
23
    CharField,
 
24
    ForeignKey,
 
25
    )
 
26
from maasserver import DefaultMeta
 
27
from maasserver.models.blockdevice import (
 
28
    BlockDevice,
 
29
    BlockDeviceManager,
 
30
    )
 
31
from maasserver.models.filesystemgroup import FilesystemGroup
 
32
from maasserver.models.node import Node
 
33
 
 
34
 
 
35
class VirtualBlockDeviceManager(BlockDeviceManager):
 
36
    """Manager for `VirtualBlockDevice` class."""
 
37
 
 
38
 
 
39
class VirtualBlockDevice(BlockDevice):
 
40
    """A virtual block device attached to a node."""
 
41
 
 
42
    class Meta(DefaultMeta):
 
43
        """Needed for South to recognize this model."""
 
44
 
 
45
    objects = VirtualBlockDeviceManager()
 
46
 
 
47
    uuid = CharField(
 
48
        max_length=36, unique=True, null=False, blank=False, editable=False)
 
49
 
 
50
    filesystem_group = ForeignKey(
 
51
        FilesystemGroup, null=False, blank=False,
 
52
        related_name="virtual_devices")
 
53
 
 
54
    def clean(self, *args, **kwargs):
 
55
        super(VirtualBlockDevice, self).clean(*args, **kwargs)
 
56
 
 
57
        # First time called the node might not be set, so we handle the
 
58
        # DoesNotExist exception accordingly.
 
59
        try:
 
60
            node = self.node
 
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
 
67
        else:
 
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.")
 
73
 
 
74
    def save(self, *args, **kwargs):
 
75
        if not self.uuid:
 
76
            self.uuid = uuid4()
 
77
        return super(VirtualBlockDevice, self).save(*args, **kwargs)