Commit 8565696b authored by Daisuke Fujita's avatar Daisuke Fujita

Merge pull request #137 from k1LoW/add-route_table

Add route_table
parents ce784409 c3f84626
......@@ -89,6 +89,7 @@ Commands:
terraforming r53r # Route53 Record
terraforming r53z # Route53 Hosted Zone
terraforming rds # RDS
terraforming rt # Route Table
terraforming s3 # S3
terraforming sg # Security Group
terraforming sn # Subnet
......
......@@ -32,6 +32,7 @@ require "terraforming/resource/iam_user_policy"
require "terraforming/resource/network_acl"
require "terraforming/resource/network_interface"
require "terraforming/resource/rds"
require "terraforming/resource/route_table"
require "terraforming/resource/route53_record"
require "terraforming/resource/route53_zone"
require "terraforming/resource/s3"
......
......@@ -120,6 +120,11 @@ module Terraforming
execute(Terraforming::Resource::RDS, options)
end
desc "rt", "Route Table"
def rt
execute(Terraforming::Resource::RouteTable, options)
end
desc "s3", "S3"
def s3
execute(Terraforming::Resource::S3, options)
......
module Terraforming
module Resource
class RouteTable
include Terraforming::Util
def self.tf(client: Aws::EC2::Client.new)
self.new(client).tf
end
def self.tfstate(client: Aws::EC2::Client.new)
self.new(client).tfstate
end
def initialize(client)
@client = client
end
def tf
apply_template(@client, "tf/route_table")
end
def tfstate
route_tables.inject({}) do |resources, route_table|
attributes = {
"id" => route_table.route_table_id,
"route.#" => routes_of(route_table).length.to_s,
"tags.#" => route_table.tags.length.to_s,
"vpc_id" => route_table.vpc_id,
}
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
def routes_of(route_table)
route_table.routes
end
def module_name_of(route_table)
normalize_module_name(name_from_tag(route_table, route_table.route_table_id))
end
def route_tables
@client.describe_route_tables.route_tables
end
end
end
end
<% route_tables.each do |route_table| -%>
resource "aws_route_table" "<%= module_name_of(route_table) %>" {
vpc_id = "<%= route_table.vpc_id %>"
<% routes_of(route_table).each do |route| -%>
route {
cidr_block = "<%= route.destination_cidr_block %>"
<% if route.gateway_id -%>
gateway_id = "<%= route.gateway_id %>"
<% end -%>
<% if route.instance_id -%>
instance_id = "<%= route.instance_id %>"
<% end -%>
<% if route.vpc_peering_connection_id -%>
vpc_peering_connection_id = "<%= route.vpc_peering_connection_id %>"
<% end -%>
<% if route.network_interface_id -%>
network_interface_id = "<%= route.network_interface_id %>"
<% end -%>
}
<% end -%>
tags {
<% route_table.tags.each do |tag| -%>
"<%= tag.key %>" = "<%= tag.value %>"
<% end -%>
}
}
<% end -%>
......@@ -193,6 +193,13 @@ module Terraforming
it_behaves_like "CLI examples"
end
describe "rt" do
let(:klass) { Terraforming::Resource::RouteTable }
let(:command) { :rt }
it_behaves_like "CLI examples"
end
describe "s3" do
let(:klass) { Terraforming::Resource::S3 }
let(:command) { :s3 }
......
require "spec_helper"
module Terraforming
module Resource
describe RouteTable do
let(:client) do
Aws::EC2::Client.new(stub_responses: true)
end
let(:route_tables) do
[
{
route_table_id: 'rtb-a12bcd34',
vpc_id: 'vpc-ab123cde',
routes: [
{
destination_cidr_block: '10.0.0.0/16',
destination_prefix_list_id: nil,
gateway_id: 'local',
instance_id: nil,
instance_owner_id: nil,
network_interface_id: nil,
vpc_peering_connection_id: nil,
state: 'active'
},
{
destination_cidr_block: '0.0.0.0/0',
destination_prefix_list_id: nil,
gateway_id: 'igw-1ab2345c',
instance_id: nil,
instance_owner_id: nil,
network_interface_id: nil,
vpc_peering_connection_id: nil,
state: 'active'
},
{
destination_cidr_block: '192.168.1.0/24',
destination_prefix_list_id: nil,
gateway_id: nil,
instance_id: 'i-ec12345a',
instance_owner_id: nil,
network_interface_id: nil,
vpc_peering_connection_id: nil,
state: 'active'
},
{
destination_cidr_block: '192.168.2.0/24',
destination_prefix_list_id: nil,
gateway_id: nil,
instance_id: nil,
instance_owner_id: nil,
network_interface_id: nil,
vpc_peering_connection_id: 'pcx-c56789de',
state: 'active'
}
],
associations: [
{
route_table_association_id: 'rtbassoc-b123456cd',
route_table_id: 'rtb-a12bcd34',
subnet_id: 'subnet-1234a567',
main: false
},
{
route_table_association_id: 'rtbassoc-e789012fg',
route_table_id: 'rtb-e56egf78',
subnet_id: 'subnet-8901b123',
main: false
}
],
tags: [
{
key: 'Name',
value: 'my-route-table'
}
]
},
{
route_table_id: 'rtb-efgh5678',
vpc_id: 'vpc-ab123cde',
routes: [
{
destination_cidr_block: '0.0.0.0/0',
destination_prefix_list_id: nil,
gateway_id: 'vgw-2345cdef',
instance_id: nil,
instance_owner_id: nil,
network_interface_id: nil,
vpc_peering_connection_id: nil,
state: 'active'
}
],
associations: [
],
tags: [
{
key: 'Name',
value: 'my-route-table-2'
}
]
}
]
end
before do
client.stub_responses(:describe_route_tables, route_tables: route_tables)
end
describe ".tf" do
it "should generate tf" do
expect(described_class.tf(client: client)).to eq <<-EOS
resource "aws_route_table" "my-route-table" {
vpc_id = "vpc-ab123cde"
route {
cidr_block = "10.0.0.0/16"
gateway_id = "local"
}
route {
cidr_block = "0.0.0.0/0"
gateway_id = "igw-1ab2345c"
}
route {
cidr_block = "192.168.1.0/24"
instance_id = "i-ec12345a"
}
route {
cidr_block = "192.168.2.0/24"
vpc_peering_connection_id = "pcx-c56789de"
}
tags {
"Name" = "my-route-table"
}
}
resource "aws_route_table" "my-route-table-2" {
vpc_id = "vpc-ab123cde"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "vgw-2345cdef"
}
tags {
"Name" = "my-route-table-2"
}
}
EOS
end
end
describe ".tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_route_table.my-route-table" => {
"type" => "aws_route_table",
"primary" => {
"id" => "rtb-a12bcd34",
"attributes" => {
"id" => "rtb-a12bcd34",
"route.#" => "4",
"tags.#" => "1",
"vpc_id" => "vpc-ab123cde",
}
}
},
"aws_route_table.my-route-table-2" => {
"type" => "aws_route_table",
"primary" => {
"id"=>"rtb-efgh5678",
"attributes" => {
"id" => "rtb-efgh5678",
"route.#" => "1",
"tags.#" => "1",
"vpc_id" => "vpc-ab123cde"
}
}
}
})
end
end
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment