~ubuntu-branches/ubuntu/wily/heat/wily

« back to all changes in this revision

Viewing changes to heat/engine/resources/openstack/designate/record.py

  • Committer: Package Import Robot
  • Author(s): Corey Bryant
  • Date: 2015-08-19 08:11:50 UTC
  • mfrom: (1.1.27)
  • Revision ID: package-import@ubuntu.com-20150819081150-m969fd35xn8bdmfu
Tags: 1:5.0.0~b2-0ubuntu1
* New upstream milestone for OpenStack Liberty.
* d/control: Align (build-)depends with upstream.
* d/p/fix-requirements.patch: Dropped. No longer needed.
* d/p/fixup-assert-regex.patch: Rebased.
* d/rules: Remove .eggs directory in override_dh_auto_clean.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
3
#    not use this file except in compliance with the License. You may obtain
 
4
#    a copy of the License at
 
5
#
 
6
#         http://www.apache.org/licenses/LICENSE-2.0
 
7
#
 
8
#    Unless required by applicable law or agreed to in writing, software
 
9
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
10
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
11
#    License for the specific language governing permissions and limitations
 
12
#    under the License.
 
13
 
 
14
from heat.common.i18n import _
 
15
from heat.engine import constraints
 
16
from heat.engine import properties
 
17
from heat.engine import resource
 
18
from heat.engine import support
 
19
 
 
20
 
 
21
class DesignateRecord(resource.Resource):
 
22
    """Heat Template Resource for Designate Record."""
 
23
 
 
24
    support_status = support.SupportStatus(
 
25
        version='5.0.0')
 
26
 
 
27
    PROPERTIES = (
 
28
        NAME, TTL, DESCRIPTION, TYPE, DATA, PRIORITY, DOMAIN
 
29
    ) = (
 
30
        'name', 'ttl', 'description', 'type', 'data', 'priority', 'domain'
 
31
    )
 
32
 
 
33
    _ALLOWED_TYPES = (
 
34
        A, AAAA, CNAME, MX, SRV, TXT, SPF,
 
35
        NS, PTR, SSHFP, SOA
 
36
    ) = (
 
37
        'A', 'AAAA', 'CNAME', 'MX', 'SRV', 'TXT', 'SPF',
 
38
        'NS', 'PTR', 'SSHFP', 'SOA'
 
39
    )
 
40
 
 
41
    properties_schema = {
 
42
        # Based on RFC 1035, length of name is set to max of 255
 
43
        NAME: properties.Schema(
 
44
            properties.Schema.STRING,
 
45
            _('Record name.'),
 
46
            required=True,
 
47
            constraints=[constraints.Length(max=255)]
 
48
        ),
 
49
        # Based on RFC 1035, range for ttl is set to 0 to signed 32 bit number
 
50
        TTL: properties.Schema(
 
51
            properties.Schema.INTEGER,
 
52
            _('Time To Live (Seconds).'),
 
53
            update_allowed=True,
 
54
            constraints=[constraints.Range(min=0,
 
55
                                           max=2147483647)]
 
56
        ),
 
57
        # designate mandates to the max length of 160 for description
 
58
        DESCRIPTION: properties.Schema(
 
59
            properties.Schema.STRING,
 
60
            _('Description of record.'),
 
61
            update_allowed=True,
 
62
            constraints=[constraints.Length(max=160)]
 
63
        ),
 
64
        TYPE: properties.Schema(
 
65
            properties.Schema.STRING,
 
66
            _('DNS Record type.'),
 
67
            update_allowed=True,
 
68
            required=True,
 
69
            constraints=[constraints.AllowedValues(
 
70
                _ALLOWED_TYPES
 
71
            )]
 
72
        ),
 
73
        DATA: properties.Schema(
 
74
            properties.Schema.STRING,
 
75
            _('DNS record data, varies based on the type of record. For more '
 
76
              'details, please refer rfc 1035.'),
 
77
            update_allowed=True,
 
78
            required=True
 
79
        ),
 
80
        # Based on RFC 1035, range for priority is set to 0 to signed 16 bit
 
81
        # number
 
82
        PRIORITY: properties.Schema(
 
83
            properties.Schema.INTEGER,
 
84
            _('DNS record priority. It is considered only for MX and SRV '
 
85
              'types, otherwise, it is ignored.'),
 
86
            update_allowed=True,
 
87
            constraints=[constraints.Range(min=0,
 
88
                                           max=65536)]
 
89
        ),
 
90
        DOMAIN: properties.Schema(
 
91
            properties.Schema.STRING,
 
92
            _('DNS Domain id or name.'),
 
93
            required=True,
 
94
            constraints=[constraints.CustomConstraint('designate.domain')]
 
95
        ),
 
96
    }
 
97
 
 
98
    default_client_name = 'designate'
 
99
 
 
100
    def handle_create(self):
 
101
        args = dict(
 
102
            name=self.properties[self.NAME],
 
103
            type=self.properties[self.TYPE],
 
104
            description=self.properties[self.DESCRIPTION],
 
105
            ttl=self.properties[self.TTL],
 
106
            data=self.properties[self.DATA],
 
107
            # priority is considered only for MX and SRV record.
 
108
            priority=(self.properties[self.PRIORITY]
 
109
                      if self.properties[self.TYPE] in (self.MX, self.SRV)
 
110
                      else None),
 
111
            domain=self.properties[self.DOMAIN]
 
112
        )
 
113
 
 
114
        domain = self.client_plugin().record_create(**args)
 
115
 
 
116
        self.resource_id_set(domain.id)
 
117
 
 
118
    def handle_update(self,
 
119
                      json_snippet=None,
 
120
                      tmpl_diff=None,
 
121
                      prop_diff=None):
 
122
        args = dict()
 
123
 
 
124
        if prop_diff.get(self.TTL):
 
125
            args['ttl'] = prop_diff.get(self.TTL)
 
126
 
 
127
        if prop_diff.get(self.DESCRIPTION):
 
128
            args['description'] = prop_diff.get(self.DESCRIPTION)
 
129
 
 
130
        if prop_diff.get(self.TYPE):
 
131
            args['type'] = prop_diff.get(self.TYPE)
 
132
 
 
133
        # priority is considered only for MX and SRV record.
 
134
        if prop_diff.get(self.PRIORITY):
 
135
            args['priority'] = (prop_diff.get(self.PRIORITY)
 
136
                                if (prop_diff.get(self.TYPE) or
 
137
                                self.properties[self.TYPE]) in
 
138
                                (self.MX, self.SRV)
 
139
                                else None)
 
140
 
 
141
        if prop_diff.get(self.DATA):
 
142
            args['data'] = prop_diff.get(self.DATA)
 
143
 
 
144
        if len(args.keys()) > 0:
 
145
            args['id'] = self.resource_id
 
146
            args['domain'] = self.properties[self.DOMAIN]
 
147
            self.client_plugin().record_update(**args)
 
148
 
 
149
    def handle_delete(self):
 
150
        if self.resource_id is not None:
 
151
            try:
 
152
                self.client_plugin().record_delete(
 
153
                    id=self.resource_id,
 
154
                    domain=self.properties[self.DOMAIN]
 
155
                )
 
156
            except Exception as ex:
 
157
                self.client_plugin().ignore_not_found(ex)
 
158
 
 
159
 
 
160
def resource_mapping():
 
161
    return {
 
162
        'OS::Designate::Record': DesignateRecord
 
163
    }