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

« back to all changes in this revision

Viewing changes to heat/engine/resources/neutron/subnet.py

  • Committer: Package Import Robot
  • Author(s): James Page, Corey Bryant, James Page
  • Date: 2015-03-30 11:11:18 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20150330111118-2qpycylx6swu4yhj
Tags: 2015.1~b3-0ubuntu1
[ Corey Bryant ]
* New upstream milestone release for OpenStack kilo:
  - d/control: Align with upstream dependencies.
  - d/p/sudoers_patch.patch: Rebased.
  - d/p/fix-requirements.patch: Rebased.

[ James Page ]
* d/p/fixup-assert-regex.patch: Tweak test to use assertRegexpMatches.

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 attributes
16
 
from heat.engine import constraints
17
 
from heat.engine import properties
18
 
from heat.engine.resources.neutron import neutron
19
 
from heat.engine import support
20
 
 
21
 
 
22
 
class Subnet(neutron.NeutronResource):
23
 
 
24
 
    PROPERTIES = (
25
 
        NETWORK_ID, NETWORK, CIDR, VALUE_SPECS, NAME, IP_VERSION,
26
 
        DNS_NAMESERVERS, GATEWAY_IP, ENABLE_DHCP, ALLOCATION_POOLS,
27
 
        TENANT_ID, HOST_ROUTES,
28
 
    ) = (
29
 
        'network_id', 'network', 'cidr', 'value_specs', 'name', 'ip_version',
30
 
        'dns_nameservers', 'gateway_ip', 'enable_dhcp', 'allocation_pools',
31
 
        'tenant_id', 'host_routes',
32
 
    )
33
 
 
34
 
    _ALLOCATION_POOL_KEYS = (
35
 
        ALLOCATION_POOL_START, ALLOCATION_POOL_END,
36
 
    ) = (
37
 
        'start', 'end',
38
 
    )
39
 
 
40
 
    _HOST_ROUTES_KEYS = (
41
 
        ROUTE_DESTINATION, ROUTE_NEXTHOP,
42
 
    ) = (
43
 
        'destination', 'nexthop',
44
 
    )
45
 
 
46
 
    ATTRIBUTES = (
47
 
        NAME_ATTR, NETWORK_ID_ATTR, TENANT_ID_ATTR, ALLOCATION_POOLS_ATTR,
48
 
        GATEWAY_IP_ATTR, HOST_ROUTES_ATTR, IP_VERSION_ATTR, CIDR_ATTR,
49
 
        DNS_NAMESERVERS_ATTR, ENABLE_DHCP_ATTR, SHOW,
50
 
    ) = (
51
 
        'name', 'network_id', 'tenant_id', 'allocation_pools',
52
 
        'gateway_ip', 'host_routes', 'ip_version', 'cidr',
53
 
        'dns_nameservers', 'enable_dhcp', 'show',
54
 
    )
55
 
 
56
 
    properties_schema = {
57
 
        NETWORK_ID: properties.Schema(
58
 
            properties.Schema.STRING,
59
 
            support_status=support.SupportStatus(
60
 
                support.DEPRECATED,
61
 
                _('Use property %s.') % NETWORK),
62
 
            required=False
63
 
        ),
64
 
        NETWORK: properties.Schema(
65
 
            properties.Schema.STRING,
66
 
            _('The ID of the attached network.'),
67
 
            required=False
68
 
        ),
69
 
        CIDR: properties.Schema(
70
 
            properties.Schema.STRING,
71
 
            _('The CIDR.'),
72
 
            required=True
73
 
        ),
74
 
        VALUE_SPECS: properties.Schema(
75
 
            properties.Schema.MAP,
76
 
            _('Extra parameters to include in the creation request.'),
77
 
            default={},
78
 
            update_allowed=True
79
 
        ),
80
 
        NAME: properties.Schema(
81
 
            properties.Schema.STRING,
82
 
            _('The name of the subnet.'),
83
 
            update_allowed=True
84
 
        ),
85
 
        IP_VERSION: properties.Schema(
86
 
            properties.Schema.INTEGER,
87
 
            _('The IP version, which is 4 or 6.'),
88
 
            default=4,
89
 
            constraints=[
90
 
                constraints.AllowedValues([4, 6]),
91
 
            ]
92
 
        ),
93
 
        DNS_NAMESERVERS: properties.Schema(
94
 
            properties.Schema.LIST,
95
 
            _('A specified set of DNS name servers to be used.'),
96
 
            default=[],
97
 
            update_allowed=True
98
 
        ),
99
 
        GATEWAY_IP: properties.Schema(
100
 
            properties.Schema.STRING,
101
 
            _('The gateway IP address.'),
102
 
            update_allowed=True
103
 
        ),
104
 
        ENABLE_DHCP: properties.Schema(
105
 
            properties.Schema.BOOLEAN,
106
 
            _('Set to true if DHCP is enabled and false if DHCP is disabled.'),
107
 
            default=True,
108
 
            update_allowed=True
109
 
        ),
110
 
        ALLOCATION_POOLS: properties.Schema(
111
 
            properties.Schema.LIST,
112
 
            _('The start and end addresses for the allocation pools.'),
113
 
            schema=properties.Schema(
114
 
                properties.Schema.MAP,
115
 
                schema={
116
 
                    ALLOCATION_POOL_START: properties.Schema(
117
 
                        properties.Schema.STRING,
118
 
                        required=True
119
 
                    ),
120
 
                    ALLOCATION_POOL_END: properties.Schema(
121
 
                        properties.Schema.STRING,
122
 
                        required=True
123
 
                    ),
124
 
                },
125
 
            )
126
 
        ),
127
 
        TENANT_ID: properties.Schema(
128
 
            properties.Schema.STRING,
129
 
            _('The ID of the tenant who owns the network. Only administrative'
130
 
              ' users can specify a tenant ID other than their own.')
131
 
        ),
132
 
        HOST_ROUTES: properties.Schema(
133
 
            properties.Schema.LIST,
134
 
            schema=properties.Schema(
135
 
                properties.Schema.MAP,
136
 
                schema={
137
 
                    ROUTE_DESTINATION: properties.Schema(
138
 
                        properties.Schema.STRING,
139
 
                        required=True
140
 
                    ),
141
 
                    ROUTE_NEXTHOP: properties.Schema(
142
 
                        properties.Schema.STRING,
143
 
                        required=True
144
 
                    ),
145
 
                },
146
 
            ),
147
 
            update_allowed=True
148
 
        ),
149
 
    }
150
 
 
151
 
    attributes_schema = {
152
 
        NAME_ATTR: attributes.Schema(
153
 
            _("Friendly name of the subnet.")
154
 
        ),
155
 
        NETWORK_ID_ATTR: attributes.Schema(
156
 
            _("Parent network of the subnet.")
157
 
        ),
158
 
        TENANT_ID_ATTR: attributes.Schema(
159
 
            _("Tenant owning the subnet.")
160
 
        ),
161
 
        ALLOCATION_POOLS_ATTR: attributes.Schema(
162
 
            _("Ip allocation pools and their ranges.")
163
 
        ),
164
 
        GATEWAY_IP_ATTR: attributes.Schema(
165
 
            _("Ip of the subnet's gateway.")
166
 
        ),
167
 
        HOST_ROUTES_ATTR: attributes.Schema(
168
 
            _("Additional routes for this subnet.")
169
 
        ),
170
 
        IP_VERSION_ATTR: attributes.Schema(
171
 
            _("Ip version for the subnet.")
172
 
        ),
173
 
        CIDR_ATTR: attributes.Schema(
174
 
            _("CIDR block notation for this subnet.")
175
 
        ),
176
 
        DNS_NAMESERVERS_ATTR: attributes.Schema(
177
 
            _("List of dns nameservers.")
178
 
        ),
179
 
        ENABLE_DHCP_ATTR: attributes.Schema(
180
 
            _("'true' if DHCP is enabled for this subnet; 'false' otherwise.")
181
 
        ),
182
 
        SHOW: attributes.Schema(
183
 
            _("All attributes.")
184
 
        ),
185
 
    }
186
 
 
187
 
    @classmethod
188
 
    def _null_gateway_ip(cls, props):
189
 
        if cls.GATEWAY_IP not in props:
190
 
            return
191
 
        # Specifying null in the gateway_ip will result in
192
 
        # a property containing an empty string.
193
 
        # A null gateway_ip has special meaning in the API
194
 
        # so this needs to be set back to None.
195
 
        # See bug https://bugs.launchpad.net/heat/+bug/1226666
196
 
        if props.get(cls.GATEWAY_IP) == '':
197
 
            props[cls.GATEWAY_IP] = None
198
 
 
199
 
    def validate(self):
200
 
        super(Subnet, self).validate()
201
 
        self._validate_depr_property_required(self.properties,
202
 
                                              self.NETWORK, self.NETWORK_ID)
203
 
 
204
 
    def handle_create(self):
205
 
        props = self.prepare_properties(
206
 
            self.properties,
207
 
            self.physical_resource_name())
208
 
        self.client_plugin().resolve_network(props, self.NETWORK, 'network_id')
209
 
        self._null_gateway_ip(props)
210
 
 
211
 
        subnet = self.neutron().create_subnet({'subnet': props})['subnet']
212
 
        self.resource_id_set(subnet['id'])
213
 
 
214
 
    def handle_delete(self):
215
 
        client = self.neutron()
216
 
        try:
217
 
            client.delete_subnet(self.resource_id)
218
 
        except Exception as ex:
219
 
            self.client_plugin().ignore_not_found(ex)
220
 
        else:
221
 
            return True
222
 
 
223
 
    def _show_resource(self):
224
 
        return self.neutron().show_subnet(self.resource_id)['subnet']
225
 
 
226
 
    def handle_update(self, json_snippet, tmpl_diff, prop_diff):
227
 
        props = self.prepare_update_properties(json_snippet)
228
 
        self.neutron().update_subnet(
229
 
            self.resource_id, {'subnet': props})
230
 
 
231
 
 
232
 
def resource_mapping():
233
 
    return {
234
 
        'OS::Neutron::Subnet': Subnet,
235
 
    }