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 constraints
16
from heat.engine import properties
17
from heat.engine import resource
18
from heat.engine.resources.aws.ec2 import vpc
19
from heat.engine.resources.openstack.neutron import neutron
20
from heat.engine import support
23
class RouteTable(resource.Resource):
25
support_status = support.SupportStatus(version='2014.1')
40
VPC_ID: properties.Schema(
41
properties.Schema.STRING,
42
_('VPC ID for where the route table is created.'),
45
TAGS: properties.Schema(
46
properties.Schema.LIST,
47
schema=properties.Schema(
48
properties.Schema.MAP,
49
_('List of tags to be attached to this resource.'),
51
TAG_KEY: properties.Schema(
52
properties.Schema.STRING,
55
TAG_VALUE: properties.Schema(
56
properties.Schema.STRING,
65
default_client_name = 'neutron'
67
def handle_create(self):
68
client = self.client()
69
props = {'name': self.physical_resource_name()}
70
router = client.create_router({'router': props})['router']
71
self.resource_id_set(router['id'])
73
def check_create_complete(self, *args):
74
client = self.client()
75
attributes = client.show_router(
76
self.resource_id)['router']
77
if not neutron.NeutronResource.is_built(attributes):
80
network_id = self.properties.get(self.VPC_ID)
81
default_router = vpc.VPC.router_for_vpc(client, network_id)
82
if default_router and default_router.get('external_gateway_info'):
83
# the default router for the VPC is connected
84
# to the external router, so do it for this too.
85
external_network_id = default_router[
86
'external_gateway_info']['network_id']
87
client.add_gateway_router(self.resource_id, {
88
'network_id': external_network_id})
91
def handle_delete(self):
92
client = self.client()
94
router_id = self.resource_id
96
client.delete_router(router_id)
97
except Exception as ex:
98
self.client_plugin().ignore_not_found(ex)
100
# just in case this router has been added to a gateway, remove it
102
client.remove_gateway_router(router_id)
103
except Exception as ex:
104
self.client_plugin().ignore_not_found(ex)
107
class SubnetRouteTableAssociation(resource.Resource):
110
ROUTE_TABLE_ID, SUBNET_ID,
112
'RouteTableId', 'SubnetId',
115
properties_schema = {
116
ROUTE_TABLE_ID: properties.Schema(
117
properties.Schema.STRING,
118
_('Route table ID.'),
121
SUBNET_ID: properties.Schema(
122
properties.Schema.STRING,
126
constraints.CustomConstraint('neutron.subnet')
131
default_client_name = 'neutron'
133
def handle_create(self):
134
client = self.client()
135
subnet_id = self.properties.get(self.SUBNET_ID)
137
router_id = self.properties.get(self.ROUTE_TABLE_ID)
139
# remove the default router association for this subnet.
141
previous_router = self._router_for_subnet(subnet_id)
143
client.remove_interface_router(
144
previous_router['id'],
145
{'subnet_id': subnet_id})
146
except Exception as ex:
147
self.client_plugin().ignore_not_found(ex)
149
client.add_interface_router(
150
router_id, {'subnet_id': subnet_id})
152
def _router_for_subnet(self, subnet_id):
153
client = self.client()
154
subnet = client.show_subnet(
156
network_id = subnet['network_id']
157
return vpc.VPC.router_for_vpc(client, network_id)
159
def handle_delete(self):
160
client = self.client()
161
subnet_id = self.properties.get(self.SUBNET_ID)
163
router_id = self.properties.get(self.ROUTE_TABLE_ID)
166
client.remove_interface_router(router_id, {
167
'subnet_id': subnet_id})
168
except Exception as ex:
169
self.client_plugin().ignore_not_found(ex)
171
# add back the default router
173
default_router = self._router_for_subnet(subnet_id)
175
client.add_interface_router(
176
default_router['id'], {'subnet_id': subnet_id})
177
except Exception as ex:
178
self.client_plugin().ignore_not_found(ex)
181
def resource_mapping():
183
'AWS::EC2::RouteTable': RouteTable,
184
'AWS::EC2::SubnetRouteTableAssociation': SubnetRouteTableAssociation,