1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may
5
# not use this file except in compliance with the License. You may obtain
6
# a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
# License for the specific language governing permissions and limitations
16
from heat.engine import clients
17
from heat.openstack.common import log as logging
18
from heat.engine import resource
19
from heat.engine.resources.quantum import quantum
20
from heat.engine.resources.vpc import VPC
22
if clients.quantumclient is not None:
23
from quantumclient.common.exceptions import QuantumClientException
25
logger = logging.getLogger(__name__)
28
class RouteTable(resource.Resource):
29
tags_schema = {'Key': {'Type': 'String',
31
'Value': {'Type': 'String',
38
'Tags': {'Type': 'List', 'Schema': {
41
'Schema': tags_schema}}
44
def handle_create(self):
45
client = self.quantum()
46
props = {'name': self.physical_resource_name()}
47
router = client.create_router({'router': props})['router']
48
self.resource_id_set(router['id'])
50
def check_create_complete(self, *args):
51
client = self.quantum()
52
attributes = client.show_router(
53
self.resource_id)['router']
54
if not quantum.QuantumResource.is_built(attributes):
57
network_id = self.properties.get('VpcId')
58
default_router = VPC.router_for_vpc(client, network_id)
59
if default_router and default_router.get('external_gateway_info'):
60
# the default router for the VPC is connected
61
# to the external router, so do it for this too.
62
external_network_id = default_router[
63
'external_gateway_info']['network_id']
64
client.add_gateway_router(self.resource_id, {
65
'network_id': external_network_id})
68
def handle_delete(self):
69
client = self.quantum()
71
router_id = self.resource_id
73
client.delete_router(router_id)
74
except QuantumClientException as ex:
75
if ex.status_code != 404:
78
# just in case this router has been added to a gateway, remove it
80
client.remove_gateway_router(router_id)
81
except QuantumClientException as ex:
82
if ex.status_code != 404:
86
class SubnetRouteTableAssocation(resource.Resource):
97
def handle_create(self):
98
client = self.quantum()
99
subnet_id = self.properties.get('SubnetId')
101
router_id = self.properties.get('RouteTableId')
103
#remove the default router association for this subnet.
105
previous_router = self._router_for_subnet(subnet_id)
107
client.remove_interface_router(
108
previous_router['id'],
109
{'subnet_id': subnet_id})
110
except QuantumClientException as ex:
111
if ex.status_code != 404:
114
client.add_interface_router(
115
router_id, {'subnet_id': subnet_id})
117
def _router_for_subnet(self, subnet_id):
118
client = self.quantum()
119
subnet = client.show_subnet(
121
network_id = subnet['network_id']
122
return VPC.router_for_vpc(client, network_id)
124
def handle_delete(self):
125
client = self.quantum()
126
subnet_id = self.properties.get('SubnetId')
128
router_id = self.properties.get('RouteTableId')
131
client.remove_interface_router(router_id, {
132
'subnet_id': subnet_id})
133
except QuantumClientException as ex:
134
if ex.status_code != 404:
137
# add back the default router
139
default_router = self._router_for_subnet(subnet_id)
141
client.add_interface_router(
142
default_router['id'], {'subnet_id': subnet_id})
143
except QuantumClientException as ex:
144
if ex.status_code != 404:
148
def resource_mapping():
149
if clients.quantumclient is None:
153
'AWS::EC2::RouteTable': RouteTable,
154
'AWS::EC2::SubnetRouteTableAssocation': SubnetRouteTableAssocation,