class Terraforming::Resource::RouteTable

Public Class Methods

new(client) click to toggle source
# File lib/terraforming/resource/route_table.rb, line 14
def initialize(client)
  @client = client
end
tf(client: Aws::EC2::Client.new) click to toggle source
# File lib/terraforming/resource/route_table.rb, line 6
def self.tf(client: Aws::EC2::Client.new)
  self.new(client).tf
end
tfstate(client: Aws::EC2::Client.new) click to toggle source
# File lib/terraforming/resource/route_table.rb, line 10
def self.tfstate(client: Aws::EC2::Client.new)
  self.new(client).tfstate
end

Public Instance Methods

tf() click to toggle source
# File lib/terraforming/resource/route_table.rb, line 18
def tf
  apply_template(@client, "tf/route_table")
end
tfstate() click to toggle source
# File lib/terraforming/resource/route_table.rb, line 22
def tfstate
  route_tables.inject({}) do |resources, route_table|
    attributes = {
      "id" => route_table.route_table_id,
      "vpc_id" => route_table.vpc_id,
    }

    attributes.merge!(tags_attributes_of(route_table))
    attributes.merge!(routes_attributes_of(route_table))
    attributes.merge!(propagating_vgws_attributes_of(route_table))

    resources["aws_route_table.#{module_name_of(route_table)}"] = {
      "type" => "aws_route_table",
      "primary" => {
        "id" => route_table.route_table_id,
        "attributes" => attributes
      }
    }

    resources
  end
end

Private Instance Methods

module_name_of(route_table) click to toggle source
# File lib/terraforming/resource/route_table.rb, line 55
def module_name_of(route_table)
  normalize_module_name(name_from_tag(route_table, route_table.route_table_id))
end
propagating_vgws_attributes_of(route_table) click to toggle source
# File lib/terraforming/resource/route_table.rb, line 102
def propagating_vgws_attributes_of(route_table)
  vgws = propagaving_vgws_of(route_table)
  attributes = { "propagating_vgws.#" => vgws.length.to_s }

  vgws.each do |gateway_id|
    hashcode = Zlib.crc32(gateway_id)
    attributes["propagating_vgws.#{hashcode}"] = gateway_id
  end

  attributes
end
propagaving_vgws_of(route_table) click to toggle source
# File lib/terraforming/resource/route_table.rb, line 98
def propagaving_vgws_of(route_table)
  route_table.propagating_vgws.map(&:gateway_id).map(&:to_s)
end
route_attributes_of(route) click to toggle source
# File lib/terraforming/resource/route_table.rb, line 74
def route_attributes_of(route)
  hashcode = route_hashcode_of(route)
  attributes = {
    "route.#{hashcode}.cidr_block" => route.destination_cidr_block.to_s,
    "route.#{hashcode}.gateway_id" => route.gateway_id.to_s,
    "route.#{hashcode}.instance_id" => route.instance_id.to_s,
    "route.#{hashcode}.network_interface_id" => route.network_interface_id.to_s,
    "route.#{hashcode}.vpc_peering_connection_id" => route.vpc_peering_connection_id.to_s
  }

  attributes
end
route_hashcode_of(route) click to toggle source
# File lib/terraforming/resource/route_table.rb, line 87
def route_hashcode_of(route)
  string = "#{route.destination_cidr_block}-#{route.gateway_id}-"
  instance_set = !route.instance_id.nil? && route.instance_id != ''

  string << route.instance_id.to_s if instance_set
  string << route.vpc_peering_connection_id.to_s
  string << route.network_interface_id.to_s unless instance_set

  Zlib.crc32(string)
end
route_tables() click to toggle source
# File lib/terraforming/resource/route_table.rb, line 59
def route_tables
  @client.describe_route_tables.map(&:route_tables).flatten
end
routes_attributes_of(route_table) click to toggle source
# File lib/terraforming/resource/route_table.rb, line 63
def routes_attributes_of(route_table)
  routes = routes_of(route_table)
  attributes = { "route.#" => routes.length.to_s }

  routes.each do |route|
    attributes.merge!(route_attributes_of(route))
  end

  attributes
end
routes_of(route_table) click to toggle source
# File lib/terraforming/resource/route_table.rb, line 47
def routes_of(route_table)
  route_table.routes.reject do |route|
    route.gateway_id.to_s == 'local' ||
      route.origin.to_s == 'EnableVgwRoutePropagation' ||
      route.destination_prefix_list_id
  end
end
tags_attributes_of(route_table) click to toggle source
# File lib/terraforming/resource/route_table.rb, line 114
def tags_attributes_of(route_table)
  tags = route_table.tags
  attributes = { "tags.#" => tags.length.to_s }
  tags.each { |tag| attributes["tags.#{tag.key}"] = tag.value }
  attributes
end