summaryrefslogtreecommitdiff
path: root/designate/objects/zone.py
blob: 69ee20e30b892f8b41358ca6db85492a11aa50be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Copyright (c) 2014 Rackspace Hosting
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
from designate import exceptions
from designate.objects import base
from designate.objects import fields
from designate.objects.validation_error import ValidationError
from designate.objects.validation_error import ValidationErrorList
from designate import utils


@base.DesignateRegistry.register
class Zone(base.DesignateObject, base.DictObjectMixin,
           base.PersistentObjectMixin, base.SoftDeleteObjectMixin):
    def __init__(self, *args, **kwargs):
        super(Zone, self).__init__(*args, **kwargs)

    fields = {
        'shard': fields.IntegerFields(nullable=True, minimum=0, maximum=4095),
        'tenant_id': fields.StringFields(nullable=True, read_only=False),
        'name': fields.DomainField(maxLength=255),
        'email': fields.EmailField(maxLength=255, nullable=True),
        'ttl': fields.IntegerFields(nullable=True, minimum=0,
                                    maximum=2147483647),
        'refresh': fields.IntegerFields(nullable=True, minimum=0,
                                        maximum=2147483647, read_only=False),
        'retry': fields.IntegerFields(nullable=True, minimum=0,
                                      maximum=2147483647, read_only=False),
        'expire': fields.IntegerFields(nullable=True, minimum=0,
                                       maximum=2147483647, read_only=False),
        'minimum': fields.IntegerFields(nullable=True, minimum=0,
                                        maximum=2147483647, read_only=False),
        'parent_zone_id': fields.UUIDFields(nullable=True, read_only=False),
        'serial': fields.IntegerFields(nullable=True, minimum=0,
                                       maximum=4294967295, read_only=False),
        'description': fields.StringFields(nullable=True, maxLength=160),
        'status': fields.EnumField(nullable=True, read_only=False,
                                   valid_values=[
                                       'ACTIVE', 'PENDING', 'ERROR',
                                       'DELETED', 'SUCCESS', 'NO_ZONE']

                                   ),
        'action': fields.EnumField(nullable=True,
                                   valid_values=[
                                       'CREATE', 'DELETE', 'UPDATE', 'NONE']
                                   ),
        'pool_id': fields.UUIDFields(nullable=True, read_only=False),
        'recordsets': fields.ObjectField('RecordSetList', nullable=True),
        'attributes': fields.ObjectField('ZoneAttributeList', nullable=True),
        'masters': fields.ObjectField('ZoneMasterList', nullable=True),
        'shared': fields.BooleanField(default=False, nullable=True),
        'type': fields.EnumField(nullable=True,
                                 valid_values=['SECONDARY', 'PRIMARY'],
                                 read_only=False
                                 ),
        'transferred_at': fields.DateTimeField(nullable=True, read_only=False),
        'delayed_notify': fields.BooleanField(nullable=True),
    }

    STRING_KEYS = [
        'id', 'type', 'name', 'pool_id', 'serial', 'action', 'status'
    ]

    def get_master_by_ip(self, host):
        """
        Utility to get the master by it's ip for this zone.
        """
        for srv in self.masters:
            srv_host, _ = utils.split_host_port(srv.to_data())
            if host == srv_host:
                return srv
        return False

    def _raise(self, errors):
        if len(errors) != 0:
            raise exceptions.InvalidObject(
                "Provided object does not match "
                "schema", errors=errors, object=self)

    def __hash__(self):
        return hash(self.id)

    def validate(self):
        errors = ValidationErrorList()

        if self.type == 'PRIMARY':
            if self.obj_attr_is_set('masters') and len(self.masters) != 0:
                e = ValidationError()
                e.path = ['type']
                e.validator = 'maxItems'
                e.validator_value = ['masters']
                e.message = "'masters' has more items than allowed"
                errors.append(e)
            if self.email is None:
                e = ValidationError()
                e.path = ['type']
                e.validator = 'required'
                e.validator_value = 'email'
                e.message = "'email' is a required property"
                errors.append(e)
            self._raise(errors)

        try:
            if self.type == 'SECONDARY':
                if self.masters is None or len(self.masters) == 0:
                    e = ValidationError()
                    e.path = ['type']
                    e.validator = 'required'
                    e.validator_value = ['masters']
                    e.message = "'masters' is a required property"
                    errors.append(e)

                for i in ['email', 'ttl']:
                    if i in self.obj_what_changed():
                        e = ValidationError()
                        e.path = ['type']
                        e.validator = 'not_allowed'
                        e.validator_value = i
                        e.message = "'%s' can't be specified when type is " \
                                    "SECONDARY" % i
                        errors.append(e)
                self._raise(errors)

            super(Zone, self).validate()
        except exceptions.RelationNotLoaded as ex:
            errors = ValidationErrorList()
            e = ValidationError()
            e.path = ['type']
            e.validator = 'required'
            e.validator_value = [ex.relation]
            e.message = "'%s' is a required property" % ex.relation
            errors.append(e)
            self._raise(errors)


@base.DesignateRegistry.register
class ZoneList(base.ListObjectMixin, base.DesignateObject,
               base.PagedListObjectMixin):
    LIST_ITEM_TYPE = Zone

    fields = {
        'objects': fields.ListOfObjectsField('Zone'),
    }