Skip to content
This repository was archived by the owner on Jun 5, 2020. It is now read-only.

Commit f1e30db

Browse files
authored
Merge pull request #505 from daveseff/peering_connection
VPC peering connections (#158)
2 parents 78aab92 + 665a352 commit f1e30db

File tree

4 files changed

+67
-33
lines changed

4 files changed

+67
-33
lines changed

lib/puppet/provider/ec2_vpc_routetable/v2.rb

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,21 @@ def self.prefetch(resources)
3434
end
3535

3636
def self.route_to_hash(region, route)
37-
gateway_name = route.state == 'active' ? gateway_name_from_id(region, route.gateway_id) : nil
38-
hash = {
39-
'destination_cidr_block' => route.destination_cidr_block,
40-
'gateway' => gateway_name,
41-
}
42-
gateway_name.nil? ? nil : hash
37+
if route.gateway_id
38+
gateway_name = route.state == 'active' ? gateway_name_from_id(region, route.gateway_id) : nil
39+
hash = {
40+
'destination_cidr_block' => route.destination_cidr_block,
41+
'gateway' => gateway_name,
42+
}
43+
gateway_name.nil? ? nil : hash
44+
elsif route.vpc_peering_connection_id
45+
peering_name = route.state == 'active' ? peering_name_from_id(region, route.vpc_peering_connection_id) : nil
46+
hash = {
47+
'destination_cidr_block' => route.destination_cidr_block,
48+
'peering_connection' => peering_name,
49+
}
50+
peering_name.nil? ? nil : hash
51+
end
4352
end
4453

4554
def self.route_table_to_hash(region, table)
@@ -88,31 +97,45 @@ def create
8897
)
8998
end
9099
routes.each do |route|
91-
internet_gateway_response = ec2.describe_internet_gateways(filters: [
92-
{name: 'tag:Name', values: [route['gateway']]},
93-
])
94-
found_internet_gateway = !internet_gateway_response.data.internet_gateways.empty?
100+
route_config = {
101+
route_table_id: id,
102+
destination_cidr_block: route['destination_cidr_block'],
103+
}
95104

96-
unless found_internet_gateway
97-
vpn_gateway_response = ec2.describe_vpn_gateways(filters: [
105+
if !route['gateway'].nil?
106+
internet_gateway_response = ec2.describe_internet_gateways(filters: [
98107
{name: 'tag:Name', values: [route['gateway']]},
99108
])
100-
found_vpn_gateway = !vpn_gateway_response.data.vpn_gateways.empty?
101-
end
109+
found_internet_gateway = !internet_gateway_response.data.internet_gateways.empty?
102110

103-
gateway_id = if found_internet_gateway
104-
internet_gateway_response.data.internet_gateways.first.internet_gateway_id
105-
elsif found_vpn_gateway
106-
vpn_gateway_response.data.vpn_gateways.first.vpn_gateway_id
107-
else
108-
nil
109-
end
111+
unless found_internet_gateway
112+
vpn_gateway_response = ec2.describe_vpn_gateways(filters: [
113+
{name: 'tag:Name', values: [route['gateway']]},
114+
])
115+
found_vpn_gateway = !vpn_gateway_response.data.vpn_gateways.empty?
116+
end
110117

111-
ec2.create_route(
112-
route_table_id: id,
113-
destination_cidr_block: route['destination_cidr_block'],
114-
gateway_id: gateway_id,
115-
) if gateway_id
118+
route_config[:gateway_id] = if found_internet_gateway
119+
internet_gateway_response.data.internet_gateways.first.internet_gateway_id
120+
elsif found_vpn_gateway
121+
vpn_gateway_response.data.vpn_gateways.first.vpn_gateway_id
122+
else
123+
nil
124+
end
125+
126+
elsif !route['peering_connection'].nil?
127+
vpc_peering_connection_response = ec2.describe_vpc_peering_connections(filters: [
128+
{name: 'tag:Name', values: [route['peering_connection']]},
129+
])
130+
found_peering_connection = !vpc_peering_connection_response.data.vpc_peering_connections.empty?
131+
132+
route_config[:vpc_peering_connection_id] = if found_peering_connection
133+
vpc_peering_connection_response.data.vpc_peering_connections.first.vpc_peering_connection_id
134+
else
135+
nil
136+
end
137+
end
138+
ec2.create_route(route_config) if route_config[:gateway_id] or route_config[:vpc_peering_connection_id]
116139
end
117140
@property_hash[:ensure] = :present
118141
end

lib/puppet/type/ec2_vpc_routetable.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
newproperty(:routes, :array_matching => :all) do
2929
desc 'Individual routes for the routing table.'
3030
validate do |value|
31-
['destination_cidr_block', 'gateway'].each do |key|
31+
['destination_cidr_block', ].each do |key|
3232
fail "routes must include a #{key}" unless value.keys.include?(key)
3333
end
3434
end

lib/puppet_x/puppetlabs/aws.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,23 @@ def self.gateway_name_from_id(region, gateway_id)
653653
@gateways[gateway_id]
654654
end
655655

656+
def self.peering_name_from_id(region, peering_id)
657+
ec2 = ec2_client(region)
658+
@peering ||= Hash.new do |h, key|
659+
if key
660+
begin
661+
pcx_response = ec2.describe_vpc_peering_connections(vpc_peering_connection_ids: [key])
662+
extract_name_from_tag(pcx_response.data.vpc_peering_connections.first)
663+
rescue ::Aws::EC2::Errors::InvalidVpcPeeringConnectionIDNotFound
664+
nil
665+
end
666+
else
667+
nil
668+
end
669+
end
670+
@peering[peering_id]
671+
end
672+
656673
def self.normalize_hash(hash)
657674
# Sort and format the received hash for simpler comparison.
658675
#

spec/unit/type/ec2_vpc_routetable_spec.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,6 @@
4949
}.to raise_error(Puppet::ResourceError, /routes must include a destination_cidr_block/)
5050
end
5151

52-
it 'routes should contain a gateway' do
53-
expect {
54-
type_class.new(:name => 'sample', :routes => [{'destination_cidr_block' => '10.0.0.0/16' }])
55-
}.to raise_error(Puppet::ResourceError, /routes must include a gateway/)
56-
end
57-
5852
[
5953
'name',
6054
'vpc',

0 commit comments

Comments
 (0)