Commit 98d5479a authored by Daisuke Fujita's avatar Daisuke Fujita

Merge pull request #138 from k1LoW/add-route_table_association

Add route_table_association
parents 4a11fc6e 207bad71
......@@ -90,6 +90,7 @@ Commands:
terraforming r53z # Route53 Hosted Zone
terraforming rds # RDS
terraforming rt # Route Table
terraforming rta # Route Table Association
terraforming s3 # S3
terraforming sg # Security Group
terraforming sn # Subnet
......
......@@ -33,6 +33,7 @@ require "terraforming/resource/network_acl"
require "terraforming/resource/network_interface"
require "terraforming/resource/rds"
require "terraforming/resource/route_table"
require "terraforming/resource/route_table_association"
require "terraforming/resource/route53_record"
require "terraforming/resource/route53_zone"
require "terraforming/resource/s3"
......
......@@ -125,6 +125,11 @@ module Terraforming
execute(Terraforming::Resource::RouteTable, options)
end
desc "rta", "Route Table Association"
def rta
execute(Terraforming::Resource::RouteTableAssociation, options)
end
desc "s3", "S3"
def s3
execute(Terraforming::Resource::S3, options)
......
module Terraforming
module Resource
class RouteTableAssociation
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_association")
end
def tfstate
resources = {}
route_tables.each do |route_table|
associations_of(route_table).each do |assoc|
attributes = {
"id" => assoc.route_table_association_id,
"route_table_id" => assoc.route_table_id,
"subnet_id" => assoc.subnet_id,
}
resources["aws_route_table_association.#{module_name_of(route_table, assoc)}"] = {
"type" => "aws_route_table_association",
"primary" => {
"id" => assoc.route_table_association_id,
"attributes" => attributes
}
}
end
end
resources
end
private
def associations_of(route_table)
route_table.associations
end
def module_name_of(route_table, assoc)
normalize_module_name(name_from_tag(route_table, route_table.route_table_id) + '-' + assoc.route_table_association_id)
end
def route_tables
@client.describe_route_tables.route_tables
end
end
end
end
<% route_tables.each do |route_table| -%>
<% associations_of(route_table).each do |assoc| -%>
<% if assoc.subnet_id -%>
resource "aws_route_table_association" "<%= module_name_of(route_table, assoc) %>" {
route_table_id = "<%= assoc.route_table_id %>"
subnet_id = "<%= assoc.subnet_id %>"
}
<% end -%>
<% end -%>
<% end -%>
\ No newline at end of file
......@@ -200,6 +200,13 @@ module Terraforming
it_behaves_like "CLI examples"
end
describe "rta" do
let(:klass) { Terraforming::Resource::RouteTableAssociation }
let(:command) { :rta }
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 RouteTableAssociation 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-a12bcd34',
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_association" "my-route-table-rtbassoc-b123456cd" {
route_table_id = "rtb-a12bcd34"
subnet_id = "subnet-1234a567"
}
resource "aws_route_table_association" "my-route-table-rtbassoc-e789012fg" {
route_table_id = "rtb-a12bcd34"
subnet_id = "subnet-8901b123"
}
EOS
end
end
describe ".tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_route_table_association.my-route-table-rtbassoc-b123456cd" => {
"type" => "aws_route_table_association",
"primary" => {
"id" => "rtbassoc-b123456cd",
"attributes" => {
"id" => "rtbassoc-b123456cd",
"route_table_id" => "rtb-a12bcd34",
"subnet_id" => "subnet-1234a567"
}
}
},
"aws_route_table_association.my-route-table-rtbassoc-e789012fg" => {
"type" => "aws_route_table_association",
"primary" => {
"id"=>"rtbassoc-e789012fg",
"attributes" => {
"id" => "rtbassoc-e789012fg",
"route_table_id" => "rtb-a12bcd34",
"subnet_id" => "subnet-8901b123"
}
}
}
})
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