@@ -34,12 +34,21 @@ def self.prefetch(resources)
34
34
end
35
35
36
36
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
43
52
end
44
53
45
54
def self . route_table_to_hash ( region , table )
@@ -88,31 +97,45 @@ def create
88
97
)
89
98
end
90
99
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
+ }
95
104
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 : [
98
107
{ name : 'tag:Name' , values : [ route [ 'gateway' ] ] } ,
99
108
] )
100
- found_vpn_gateway = !vpn_gateway_response . data . vpn_gateways . empty?
101
- end
109
+ found_internet_gateway = !internet_gateway_response . data . internet_gateways . empty?
102
110
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
110
117
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 ]
116
139
end
117
140
@property_hash [ :ensure ] = :present
118
141
end
0 commit comments