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
6
# http://www.apache.org/licenses/LICENSE-2.0
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
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
22
class Subnet(neutron.NeutronResource):
25
NETWORK_ID, NETWORK, CIDR, VALUE_SPECS, NAME, IP_VERSION,
26
DNS_NAMESERVERS, GATEWAY_IP, ENABLE_DHCP, ALLOCATION_POOLS,
27
TENANT_ID, HOST_ROUTES,
29
'network_id', 'network', 'cidr', 'value_specs', 'name', 'ip_version',
30
'dns_nameservers', 'gateway_ip', 'enable_dhcp', 'allocation_pools',
31
'tenant_id', 'host_routes',
34
_ALLOCATION_POOL_KEYS = (
35
ALLOCATION_POOL_START, ALLOCATION_POOL_END,
41
ROUTE_DESTINATION, ROUTE_NEXTHOP,
43
'destination', 'nexthop',
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,
51
'name', 'network_id', 'tenant_id', 'allocation_pools',
52
'gateway_ip', 'host_routes', 'ip_version', 'cidr',
53
'dns_nameservers', 'enable_dhcp', 'show',
57
NETWORK_ID: properties.Schema(
58
properties.Schema.STRING,
59
support_status=support.SupportStatus(
61
_('Use property %s.') % NETWORK),
64
NETWORK: properties.Schema(
65
properties.Schema.STRING,
66
_('The ID of the attached network.'),
69
CIDR: properties.Schema(
70
properties.Schema.STRING,
74
VALUE_SPECS: properties.Schema(
75
properties.Schema.MAP,
76
_('Extra parameters to include in the creation request.'),
80
NAME: properties.Schema(
81
properties.Schema.STRING,
82
_('The name of the subnet.'),
85
IP_VERSION: properties.Schema(
86
properties.Schema.INTEGER,
87
_('The IP version, which is 4 or 6.'),
90
constraints.AllowedValues([4, 6]),
93
DNS_NAMESERVERS: properties.Schema(
94
properties.Schema.LIST,
95
_('A specified set of DNS name servers to be used.'),
99
GATEWAY_IP: properties.Schema(
100
properties.Schema.STRING,
101
_('The gateway IP address.'),
104
ENABLE_DHCP: properties.Schema(
105
properties.Schema.BOOLEAN,
106
_('Set to true if DHCP is enabled and false if DHCP is disabled.'),
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,
116
ALLOCATION_POOL_START: properties.Schema(
117
properties.Schema.STRING,
120
ALLOCATION_POOL_END: properties.Schema(
121
properties.Schema.STRING,
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.')
132
HOST_ROUTES: properties.Schema(
133
properties.Schema.LIST,
134
schema=properties.Schema(
135
properties.Schema.MAP,
137
ROUTE_DESTINATION: properties.Schema(
138
properties.Schema.STRING,
141
ROUTE_NEXTHOP: properties.Schema(
142
properties.Schema.STRING,
151
attributes_schema = {
152
NAME_ATTR: attributes.Schema(
153
_("Friendly name of the subnet.")
155
NETWORK_ID_ATTR: attributes.Schema(
156
_("Parent network of the subnet.")
158
TENANT_ID_ATTR: attributes.Schema(
159
_("Tenant owning the subnet.")
161
ALLOCATION_POOLS_ATTR: attributes.Schema(
162
_("Ip allocation pools and their ranges.")
164
GATEWAY_IP_ATTR: attributes.Schema(
165
_("Ip of the subnet's gateway.")
167
HOST_ROUTES_ATTR: attributes.Schema(
168
_("Additional routes for this subnet.")
170
IP_VERSION_ATTR: attributes.Schema(
171
_("Ip version for the subnet.")
173
CIDR_ATTR: attributes.Schema(
174
_("CIDR block notation for this subnet.")
176
DNS_NAMESERVERS_ATTR: attributes.Schema(
177
_("List of dns nameservers.")
179
ENABLE_DHCP_ATTR: attributes.Schema(
180
_("'true' if DHCP is enabled for this subnet; 'false' otherwise.")
182
SHOW: attributes.Schema(
188
def _null_gateway_ip(cls, props):
189
if cls.GATEWAY_IP not in props:
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
200
super(Subnet, self).validate()
201
self._validate_depr_property_required(self.properties,
202
self.NETWORK, self.NETWORK_ID)
204
def handle_create(self):
205
props = self.prepare_properties(
207
self.physical_resource_name())
208
self.client_plugin().resolve_network(props, self.NETWORK, 'network_id')
209
self._null_gateway_ip(props)
211
subnet = self.neutron().create_subnet({'subnet': props})['subnet']
212
self.resource_id_set(subnet['id'])
214
def handle_delete(self):
215
client = self.neutron()
217
client.delete_subnet(self.resource_id)
218
except Exception as ex:
219
self.client_plugin().ignore_not_found(ex)
223
def _show_resource(self):
224
return self.neutron().show_subnet(self.resource_id)['subnet']
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})
232
def resource_mapping():
234
'OS::Neutron::Subnet': Subnet,