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.common import exception
18
from heat.openstack.common import log as logging
19
from heat.engine import resource
21
logger = logging.getLogger(__name__)
24
class InternetGateway(resource.Resource):
25
tags_schema = {'Key': {'Type': 'String',
27
'Value': {'Type': 'String',
31
'Tags': {'Type': 'List', 'Schema': {
34
'Schema': tags_schema}}
37
def handle_create(self):
38
self.resource_id_set(self.physical_resource_name())
40
def handle_delete(self):
44
def get_external_network_id(client):
45
ext_filter = {'router:external': True}
46
ext_nets = client.list_networks(**ext_filter)['networks']
47
if len(ext_nets) != 1:
48
# TODO(sbaker) if there is more than one external network
49
# add a heat configuration variable to set the ID of
51
raise exception.Error(
52
'Expected 1 external network, found %d' % len(ext_nets))
53
external_network_id = ext_nets[0]['id']
54
return external_network_id
57
class VPCGatewayAttachment(resource.Resource):
63
'InternetGatewayId': {'Type': 'String'},
69
def _vpc_route_tables(self):
70
for resource in self.stack.resources.itervalues():
71
if (resource.type() == 'AWS::EC2::RouteTable' and
72
resource.properties.get('VpcId') ==
73
self.properties.get('VpcId')):
76
def add_dependencies(self, deps):
77
super(VPCGatewayAttachment, self).add_dependencies(deps)
78
# Depend on any route table in this template with the same
79
# VpcId as this VpcId.
80
# All route tables must exist before gateway attachment
81
# as attachment happens to routers (not VPCs)
82
for route_table in self._vpc_route_tables():
83
deps += (self, route_table)
85
def handle_create(self):
86
client = self.quantum()
87
external_network_id = InternetGateway.get_external_network_id(client)
88
for router in self._vpc_route_tables():
89
client.add_gateway_router(router.resource_id, {
90
'network_id': external_network_id})
92
def handle_delete(self):
93
from quantumclient.common.exceptions import QuantumClientException
95
client = self.quantum()
96
for router in self._vpc_route_tables():
98
client.remove_gateway_router(router.resource_id)
99
except QuantumClientException as ex:
100
if ex.status_code != 404:
104
def resource_mapping():
105
if clients.quantumclient is None:
109
'AWS::EC2::InternetGateway': InternetGateway,
110
'AWS::EC2::VPCGatewayAttachment': VPCGatewayAttachment,