Commit a91dd322 authored by Daisuke Fujita's avatar Daisuke Fujita

Merge pull request #124 from sakazuki/eip

Add Eip
parents 0a903695 821bca2b
......@@ -15,6 +15,7 @@ require "terraforming/resource/db_parameter_group"
require "terraforming/resource/db_security_group"
require "terraforming/resource/db_subnet_group"
require "terraforming/resource/ec2"
require "terraforming/resource/eip"
require "terraforming/resource/elasti_cache_cluster"
require "terraforming/resource/elasti_cache_subnet_group"
require "terraforming/resource/elb"
......
......@@ -34,6 +34,11 @@ module Terraforming
execute(Terraforming::Resource::ElastiCacheSubnetGroup, options)
end
desc "eip", "EIP"
def eip
execute(Terraforming::Resource::EIP, options)
end
desc "elb", "ELB"
def elb
execute(Terraforming::Resource::ELB, options)
......
module Terraforming
module Resource
class EIP
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/eip")
end
def tfstate
eips.inject({}) do |resources, addr|
attributes = {
"association_id" => addr.association_id,
"domain" => addr.domain,
"id" => addr.allocation_id,
"instance" => addr.instance_id,
"network_interface" => addr.network_interface_id,
"private_ip" => addr.private_ip_address,
"public_ip" => addr.public_ip,
"vpc" => is_vpc?(addr).to_s,
}
attributes.delete_if{|k, v| v.nil?}
resources["aws_eip.#{addr.allocation_id}"] = {
"type" => "aws_eip",
"primary" => {
"id" => addr.allocation_id,
"attributes" => attributes
}
}
resources
end
end
private
def eips
@client.describe_addresses.addresses
end
def is_vpc?(addr)
addr.domain.eql?("vpc")
end
end
end
end
<% eips.each do |addr| -%>
resource "aws_eip" "<%= addr.allocation_id %>" {
<% if addr.instance_id -%>
instance = "<%= addr.instance_id %>"
<% elsif addr.network_interface_id -%>
network_interface_id = "<%= addr.network_interface_id %>"
<% end -%>
vpc = <%= is_vpc?(addr) %>
}
<% end -%>
......@@ -74,6 +74,13 @@ module Terraforming
it_behaves_like "CLI examples"
end
describe "eip" do
let(:klass) { Terraforming::Resource::EIP }
let(:command) { :eip }
it_behaves_like "CLI examples"
end
describe "elb" do
let(:klass) { Terraforming::Resource::ELB }
let(:command) { :elb }
......
require "spec_helper"
module Terraforming
module Resource
describe EIP do
let(:client) do
Aws::EC2::Client.new(stub_responses: true)
end
let(:eips) do
[
{
domain: "vpc",
instance_id: "i-12345678",
network_interface_id: "eni-12345678",
association_id: "eipassoc-98765432",
network_interface_owner_id: "123456789012",
public_ip: "12.34.56.78",
allocation_id: "eipalloc-87654321",
private_ip_address: "1.1.1.1",
},
{
domain: "vpc",
network_interface_id: "eni-23456789",
association_id: "eipassoc-87654321",
network_interface_owner_id: "234567890123",
public_ip: "2.2.2.2",
allocation_id: "eipalloc-76543210",
private_ip_address: "9.9.9.9",
},
{
public_ip: "3.3.3.3",
domain: "vpc",
allocation_id: "eipalloc-33333333",
},
]
end
before do
client.stub_responses(:describe_addresses, addresses: eips)
end
describe ".tf" do
it "should generate tf" do
expect(described_class.tf(client: client)).to eq <<-EOS
resource "aws_eip" "eipalloc-87654321" {
instance = "i-12345678"
vpc = true
}
resource "aws_eip" "eipalloc-76543210" {
network_interface_id = "eni-23456789"
vpc = true
}
resource "aws_eip" "eipalloc-33333333" {
vpc = true
}
EOS
end
end
describe ".tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_eip.eipalloc-87654321" => {
"type" => "aws_eip",
"primary" => {
"id" => "eipalloc-87654321",
"attributes" => {
"association_id" => "eipassoc-98765432",
"domain" => "vpc",
"id" => "eipalloc-87654321",
"instance" => "i-12345678",
"network_interface" => "eni-12345678",
"private_ip" => "1.1.1.1",
"public_ip" => "12.34.56.78",
"vpc" => "true"
}
}
},
"aws_eip.eipalloc-76543210" => {
"type" => "aws_eip",
"primary" => {
"id" => "eipalloc-76543210",
"attributes" => {
"association_id" => "eipassoc-87654321",
"domain" => "vpc",
"id" => "eipalloc-76543210",
"network_interface" => "eni-23456789",
"private_ip" => "9.9.9.9",
"public_ip" => "2.2.2.2",
"vpc" => "true"
}
}
},
"aws_eip.eipalloc-33333333" => {
"type" => "aws_eip",
"primary" => {
"id" => "eipalloc-33333333",
"attributes" => {
"domain" => "vpc",
"id" => "eipalloc-33333333",
"public_ip" => "3.3.3.3",
"vpc" => "true"
}
}
},
})
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