-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathroute_table.rb
210 lines (167 loc) · 6.49 KB
/
route_table.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# Copyright 2011-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
require 'aws/ec2/route_table/route'
require 'aws/ec2/route_table/association'
module AWS
class EC2
class RouteTable < Resource
include TaggedItem
def initialize route_table_id, options = {}
@route_table_id = route_table_id
super
end
# @return [String]
attr_reader :route_table_id
alias_method :id, :route_table_id
attribute :vpc_id, :static => true
attribute :route_set
protected :route_set
attribute :association_set
protected :association_set
populates_from(:create_route_table) do |resp|
resp.route_table if resp.route_table.route_table_id == route_table_id
end
populates_from(:describe_route_tables) do |resp|
resp.route_table_set.find{|t| t.route_table_id == route_table_id }
end
# @return [Boolean] Returns true if this is the main (default)
# route table.
def main?
@main = !!associations.find{|a| a.main? } if @main.nil?
@main
end
# @return [VPC] Returns the VPC this route table belongs to.
def vpc
VPC.new(vpc_id, :config => config)
end
# @return [Array<Subnet>] Returns an array of subnets ({Subnet})
# that currently associated to this route table.
def subnets
subnets = associations.map(&:subnet)
# The default route table has a single association where #subnet
# returns nil (the main association). If this is not the main
# route table we can safely return the subnets.
return subnets unless subnets.include?(nil)
subnets.compact!
# This is the default route table and to get the complete list of
# subnets we have to find all subnets without an association
AWS.memoize do
# every subnet
all_subnets = vpc.subnets.to_a
# subnets assigned directly to a route table
associated_subnets = vpc.route_tables.
map(&:associations).flatten.
map(&:subnet).flatten.
compact
# subnets NOT assigned to a route table, these default as
# belonging to the default route table through the "main"
# association
unassociated_subnets = all_subnets.inject([]) do |list,subnet|
unless associated_subnets.include?(subnet)
list << subnet
end
list
end
subnets + unassociated_subnets
end
end
# @return [Array<RouteTable::Association>] Returns an array of
# {RouteTable::Association} objects (association to subnets).
def associations
association_set.collect do |details|
Association.new(self,
details[:route_table_association_id],
details[:subnet_id])
end
end
# @return [Array<Route>] Returns an array of routes ({Route} objects)
# belonging to this route table.
def routes
route_set.map do |route_details|
Route.new(self, route_details)
end
end
# Creates a new route in this route route. The route must be attached
# to a gateway, instance or network interface.
#
# @param [String] destination_cidr_block The CIDR address block
# used for the destination match. For example: 0.0.0.0/0.
# Routing decisions are based on the most specific match.
#
# @param [Hash] options
#
# @option options [InternetGateway,String] :internet_gateway
# An {InternetGateway} object or an internet gateway id string to
# attach the route to.
#
# @option options [Instance,String] :instance An {Instance} object
# or instance id string to attach the route to.
#
# @option options [NetworkInterface,String] :network_interface
# A {NetworkInterface} object or network interface id string to
# attach the route to.
#
# @return [nil]
#
def create_route destination_cidr_block, options = {}
client.create_route(route_options(destination_cidr_block, options))
nil
end
# Replaces an existing route within a route table in a VPC.
# @param (see #create_route)
# @option (see #create_route)
# @return [nil]
def replace_route destination_cidr_block, options = {}
client.replace_route(route_options(destination_cidr_block, options))
nil
end
# @param [String] destination_cidr_block The CIDR block address of the
# route to delete.
# @return [nil]
def delete_route destination_cidr_block
client.delete_route(route_options(destination_cidr_block))
nil
end
# Deletes this route table. The route table must not be
# associated with a subnet. You can't delete the main route table.
# @return [nil]
def delete
client.delete_route_table(:route_table_id => route_table_id)
nil
end
protected
def route_options destination_cidr_block, options = {}
client_opts = {}
client_opts[:route_table_id] = route_table_id
client_opts[:destination_cidr_block] = destination_cidr_block
if gateway = options[:internet_gateway]
gateway = gateway.id if gateway.is_a?(InternetGateway)
client_opts[:gateway_id] = gateway
end
if instance = options[:instance]
instance = instance.id if instance.is_a?(Instance)
client_opts[:instance_id] = instance
end
if interface = options[:network_interface]
interface = interface.id if interface.is_a?(NetworkInterface)
client_opts[:network_interface_id] = interface
end
if vpc_peering_connection = options[:vpc_peering_connection]
vpc_peering_connection = vpc_peering_connection.id if vpc_peering_connection.is_a?(VPCPeeringConnection)
client_opts[:vpc_peering_connection_id] = vpc_peering_connection
end
client_opts
end
end
end
end