Class: AWS::EC2::RouteTable

Inherits:
Resource show all
Includes:
TaggedItem
Defined in:
lib/aws/ec2/route_table/route.rb,
lib/aws/ec2/route_table.rb,
lib/aws/ec2/route_table/association.rb

Overview

Represents a single route in a RouteTable.

# enumerating routes within a route table
ec2 = AWS::EC2.new
route_table = ec2.route_tables.first
route_table.routes.each do |route|
  # ...  
end

Defined Under Namespace

Classes: Association, Route

Instance Attribute Summary collapse

Attributes included from Core::Model

#config

Instance Method Summary collapse

Methods included from TaggedItem

#add_tag, #cached_tags, #clear_tags, #tagging_resource_type, #tags

Methods inherited from Core::Resource

attribute_providers, attribute_providers_for, attributes, #attributes_from_response, define_attribute_type, #eql?, #inspect, new_from

Methods included from Core::Cacheable

included, #retrieve_attribute

Methods included from Core::Model

#client, #config_prefix, #inspect

Constructor Details

#initialize(route_table_id, options = {}) ⇒ RouteTable

Returns a new instance of RouteTable.



24
25
26
27
# File 'lib/aws/ec2/route_table.rb', line 24

def initialize route_table_id, options = {}
  @route_table_id = route_table_id
  super
end

Instance Attribute Details

#route_table_idString (readonly) Also known as: id

Returns:

  • (String)


30
31
32
# File 'lib/aws/ec2/route_table.rb', line 30

def route_table_id
  @route_table_id
end

Instance Method Details

#associationsArray<RouteTable::Association>

Returns an array of Association objects (association to subnets).

Returns:



108
109
110
111
112
113
114
# File 'lib/aws/ec2/route_table.rb', line 108

def associations
  association_set.collect do |details| 
    Association.new(self, 
      details[:route_table_association_id], 
      details[:subnet_id])
  end
end

#create_route(destination_cidr_block, options = {}) ⇒ nil

Creates a new route in this route route. The route must be attached to a gateway, instance or network interface.

Parameters:

  • destination_cidr_block (String)

    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.

  • options (Hash) (defaults to: {})

Options Hash (options):

Returns:

  • (nil)


146
147
148
149
# File 'lib/aws/ec2/route_table.rb', line 146

def create_route destination_cidr_block, options = {}
  client.create_route(route_options(destination_cidr_block, options))
  nil
end

#deletenil

Deletes this route table. The route table must not be associated with a subnet. You can’t delete the main route table.

Returns:

  • (nil)


171
172
173
174
# File 'lib/aws/ec2/route_table.rb', line 171

def delete
  client.delete_route_table(:route_table_id => route_table_id)
  nil
end

#delete_route(destination_cidr_block) ⇒ nil

Parameters:

  • destination_cidr_block (String)

    The CIDR block address of the route to delete.

Returns:

  • (nil)


163
164
165
166
# File 'lib/aws/ec2/route_table.rb', line 163

def delete_route destination_cidr_block
  client.delete_route(route_options(destination_cidr_block))
  nil
end

#main?Boolean

Returns true if this is the main (default) route table.

Returns:

  • (Boolean)

    Returns true if this is the main (default) route table.



54
55
56
57
# File 'lib/aws/ec2/route_table.rb', line 54

def main?
  @main = !!associations.find{|a| a.main? } if @main.nil?
  @main
end

#replace_route(destination_cidr_block, options = {}) ⇒ nil

Replaces an existing route within a route table in a VPC.

Parameters:

  • destination_cidr_block (String)

    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.

  • options (Hash) (defaults to: {})

Options Hash (options):

Returns:

  • (nil)


155
156
157
158
# File 'lib/aws/ec2/route_table.rb', line 155

def replace_route destination_cidr_block, options = {}
  client.replace_route(route_options(destination_cidr_block, options))
  nil
end

#routesArray<Route>

Returns an array of routes (Route objects) belonging to this route table.

Returns:

  • (Array<Route>)

    Returns an array of routes (Route objects) belonging to this route table.



118
119
120
121
122
# File 'lib/aws/ec2/route_table.rb', line 118

def routes
  route_set.map do |route_details|
    Route.new(self, route_details)
  end
end

#subnetsArray<Subnet>

Returns an array of subnets (Subnet) that currently associated to this route table.

Returns:

  • (Array<Subnet>)

    Returns an array of subnets (Subnet) that currently associated to this route table.



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
# File 'lib/aws/ec2/route_table.rb', line 66

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

#vpcVPC

Returns the VPC this route table belongs to.

Returns:

  • (VPC)

    Returns the VPC this route table belongs to.



60
61
62
# File 'lib/aws/ec2/route_table.rb', line 60

def vpc
  VPC.new(vpc_id, :config => config)
end