Commit e00a9b7c authored by Daisuke Fujita's avatar Daisuke Fujita

Merge pull request #91 from OSS-TheWeatherCompany/elb-tfstate-fix

Elb tfstate fix
parents b59764ce d7f051b9
......@@ -28,16 +28,21 @@ module Terraforming
"connection_draining_timeout" => load_balancer_attributes.connection_draining.timeout.to_s,
"cross_zone_load_balancing" => load_balancer_attributes.cross_zone_load_balancing.enabled.to_s,
"dns_name" => load_balancer.dns_name,
"health_check.#" => "1",
"id" => load_balancer.load_balancer_name,
"idle_timeout" => load_balancer_attributes.connection_settings.idle_timeout.to_s,
"instances.#" => load_balancer.instances.length.to_s,
"listener.#" => load_balancer.listener_descriptions.length.to_s,
"name" => load_balancer.load_balancer_name,
"security_groups.#" => load_balancer.security_groups.length.to_s,
"source_security_group" => load_balancer.source_security_group.group_name,
"subnets.#" => load_balancer.subnets.length.to_s,
}
attributes.merge!(healthcheck_attributes_of(load_balancer))
attributes.merge!(listeners_attributes_of(load_balancer))
attributes.merge!(sg_attributes_of(load_balancer))
attributes.merge!(subnets_attributes_of(load_balancer))
attributes.merge!(instances_attributes_of(load_balancer))
attributes.merge!(tags_attributes_of(load_balancer))
result["aws_elb.#{module_name_of(load_balancer)}"] = {
"type" => "aws_elb",
"primary" => {
......@@ -52,6 +57,107 @@ module Terraforming
generate_tfstate(resources, tfstate_base)
end
def healthcheck_attributes_of(elb)
hashcode = healthcheck_hashcode_of(elb.health_check)
attributes = {
"health_check.#" => "1",
"health_check.#{hashcode}.healthy_threshold" => elb.health_check.healthy_threshold.to_s,
"health_check.#{hashcode}.interval" => elb.health_check.interval.to_s,
"health_check.#{hashcode}.target" => elb.health_check.target,
"health_check.#{hashcode}.timeout" => elb.health_check.timeout.to_s,
"health_check.#{hashcode}.unhealthy_threshold" => elb.health_check.unhealthy_threshold.to_s
}
attributes
end
def healthcheck_hashcode_of(health_check)
string =
"#{health_check.healthy_threshold}-" <<
"#{health_check.unhealthy_threshold}-" <<
"#{health_check.target}-" <<
"#{health_check.interval}-" <<
"#{health_check.timeout}-"
Zlib.crc32(string)
end
def tags_attributes_of(elb)
tags = @client.describe_tags(load_balancer_names: [elb.load_balancer_name]).tag_descriptions.first.tags
attributes = {"tags.#" => tags.length.to_s}
tags.each do |tag|
attributes["tags.#{tag.key}"] = tag.value
end
attributes
end
def instances_attributes_of(elb)
attributes = {"instances.#" => elb.instances.length.to_s}
elb.instances.each do |instance|
attributes["instances.#{Zlib.crc32(instance.instance_id)}"] = instance.instance_id
end
attributes
end
def subnets_attributes_of(elb)
attributes = {"subnets.#" => elb.subnets.length.to_s}
elb.subnets.each do |subnet_id|
attributes["subnets.#{Zlib.crc32(subnet_id)}"] = subnet_id
end
attributes
end
def sg_attributes_of(elb)
attributes = {"security_groups.#" => elb.security_groups.length.to_s}
elb.security_groups.each do |sg_id|
attributes["security_groups.#{Zlib.crc32(sg_id)}"] = sg_id
end
attributes
end
def listeners_attributes_of(elb)
attributes = {"listener.#" => elb.listener_descriptions.length.to_s}
elb.listener_descriptions.each do |listener_description|
attributes.merge!(listener_attributes_of(listener_description.listener))
end
attributes
end
def listener_attributes_of(listener)
hashcode = listener_hashcode_of(listener)
attributes = {
"listener.#{hashcode}.instance_port" => listener.instance_port.to_s,
"listener.#{hashcode}.instance_protocol" => listener.instance_protocol.downcase,
"listener.#{hashcode}.lb_port" => listener.load_balancer_port.to_s,
"listener.#{hashcode}.lb_protocol" => listener.protocol.downcase,
"listener.#{hashcode}.ssl_certificate_id" => listener.ssl_certificate_id
}
attributes
end
def listener_hashcode_of(listener)
string =
"#{listener.instance_port}-" <<
"#{listener.instance_protocol.downcase}-" <<
"#{listener.load_balancer_port}-" <<
"#{listener.protocol.downcase}-" <<
"#{listener.ssl_certificate_id}-"
Zlib.crc32(string)
end
def load_balancers
@client.describe_load_balancers.load_balancer_descriptions
end
......
......@@ -31,6 +31,12 @@ resource "aws_elb" "<%= module_name_of(load_balancer) %>" {
target = "<%= load_balancer.health_check.target %>"
timeout = <%= load_balancer.health_check.timeout %>
}
tags {
<% @client.describe_tags(load_balancer_names: [load_balancer.load_balancer_name]).tag_descriptions.first.tags.each do |tag| -%>
<%= tag.key %> = "<%= tag.value %>"
<% end -%>
}
}
<% end -%>
......@@ -152,12 +152,21 @@ module Terraforming
}
end
let(:tag_attributes) do
[{
tags: [
{key: 'name', value: 'elb-1'}
]
}]
end
before do
client.stub_responses(:describe_load_balancers, load_balancer_descriptions: load_balancer_descriptions)
client.stub_responses(:describe_load_balancer_attributes, [
{ load_balancer_attributes: hoge_attributes },
{ load_balancer_attributes: fuga_attributes }
])
client.stub_responses(:describe_tags, tag_descriptions: tag_attributes)
end
describe ".tf" do
......@@ -188,6 +197,10 @@ resource "aws_elb" "hoge" {
target = "HTTP:8080/status"
timeout = 5
}
tags {
name = "elb-1"
}
}
resource "aws_elb" "fuga" {
......@@ -215,6 +228,10 @@ resource "aws_elb" "fuga" {
target = "HTTP:8080/status"
timeout = 5
}
tags {
name = "elb-1"
}
}
EOS
......@@ -244,15 +261,31 @@ resource "aws_elb" "fuga" {
"connection_draining_timeout" => "300",
"cross_zone_load_balancing" => "true",
"dns_name" => "hoge-12345678.ap-northeast-1.elb.amazonaws.com",
"health_check.#" => "1",
"id" => "hoge",
"idle_timeout" => "60",
"instances.#" => "1",
"listener.#" => "1",
"name" => "hoge",
"security_groups.#" => "2",
"source_security_group" => "default",
"subnets.#" => "2",
"health_check.#" =>"1",
"health_check.362345074.healthy_threshold" =>"10",
"health_check.362345074.interval" =>"30",
"health_check.362345074.target" =>"HTTP:8080/status",
"health_check.362345074.timeout" =>"5",
"health_check.362345074.unhealthy_threshold" =>"2",
"listener.#" =>"1",
"listener.3051874140.instance_port" => "80",
"listener.3051874140.instance_protocol" => "http",
"listener.3051874140.lb_port" => "443",
"listener.3051874140.lb_protocol" => "https",
"listener.3051874140.ssl_certificate_id" => "arn:aws:iam::123456789012:server-certificate/foobar", "security_groups.#" =>"2",
"security_groups.550527283" => "sg-1234abcd",
"security_groups.3942994537" => "sg-5678efgh",
"subnets.#" =>"2",
"subnets.3229571749" => "subnet-1234abcd",
"subnets.195717631" => "subnet-5678efgh",
"instances.3520380136" => "i-1234abcd",
"tags.#" =>"1",
"tags.name" =>"elb-1"
}
}
},
......@@ -266,16 +299,32 @@ resource "aws_elb" "fuga" {
"connection_draining_timeout" => "900",
"cross_zone_load_balancing" => "true",
"dns_name" => "fuga-90123456.ap-northeast-1.elb.amazonaws.com",
"health_check.#" => "1",
"id" => "fuga",
"idle_timeout" => "90",
"instances.#" => "1",
"listener.#" => "1",
"name" => "fuga",
"security_groups.#" => "2",
"source_security_group" => "elb",
"subnets.#" => "2",
}
"health_check.#" =>"1",
"health_check.362345074.healthy_threshold" =>"10",
"health_check.362345074.interval" =>"30",
"health_check.362345074.target" =>"HTTP:8080/status",
"health_check.362345074.timeout" =>"5",
"health_check.362345074.unhealthy_threshold" =>"2",
"listener.#" =>"1",
"listener.1674021574.instance_port" =>"80",
"listener.1674021574.instance_protocol" =>"http",
"listener.1674021574.lb_port" =>"443",
"listener.1674021574.lb_protocol" =>"https",
"listener.1674021574.ssl_certificate_id" =>"arn:aws:iam::345678901234:server-certificate/foobar",
"security_groups.#" =>"2",
"security_groups.2877768809" =>"sg-9012ijkl",
"security_groups.1478442660" =>"sg-3456mnop",
"subnets.#" =>"2",
"subnets.1260945407" =>"subnet-9012ijkl",
"subnets.3098543410" =>"subnet-3456mnop",
"instances.436309938" =>"i-5678efgh",
"tags.#" =>"1",
"tags.name" =>"elb-1" }
}
}
}
......@@ -331,15 +380,32 @@ resource "aws_elb" "fuga" {
"connection_draining_timeout" => "300",
"cross_zone_load_balancing" => "true",
"dns_name" => "hoge-12345678.ap-northeast-1.elb.amazonaws.com",
"health_check.#" => "1",
"id" => "hoge",
"idle_timeout" => "60",
"instances.#" => "1",
"listener.#" => "1",
"name" => "hoge",
"security_groups.#" => "2",
"source_security_group" => "default",
"health_check.#" => "1",
"health_check.362345074.healthy_threshold" => "10",
"health_check.362345074.interval" => "30",
"health_check.362345074.target" => "HTTP:8080/status",
"health_check.362345074.timeout" => "5",
"health_check.362345074.unhealthy_threshold" => "2",
"listener.#" => "1",
"listener.3051874140.instance_port" => "80",
"listener.3051874140.instance_protocol" => "http",
"listener.3051874140.lb_port" => "443",
"listener.3051874140.lb_protocol" => "https",
"listener.3051874140.ssl_certificate_id" => "arn:aws:iam::123456789012:server-certificate/foobar",
"security_groups.#" => "2",
"security_groups.550527283" => "sg-1234abcd",
"security_groups.3942994537" => "sg-5678efgh",
"subnets.#" => "2",
"subnets.3229571749" => "subnet-1234abcd",
"subnets.195717631" => "subnet-5678efgh",
"instances.3520380136" => "i-1234abcd",
"tags.#" => "1",
"tags.name" => "elb-1"
}
}
},
......@@ -353,15 +419,32 @@ resource "aws_elb" "fuga" {
"connection_draining_timeout" => "900",
"cross_zone_load_balancing" => "true",
"dns_name" => "fuga-90123456.ap-northeast-1.elb.amazonaws.com",
"health_check.#" => "1",
"id" => "fuga",
"idle_timeout" => "90",
"instances.#" => "1",
"listener.#" => "1",
"name" => "fuga",
"security_groups.#" => "2",
"source_security_group" => "elb",
"health_check.#" => "1",
"health_check.362345074.healthy_threshold" => "10",
"health_check.362345074.interval" => "30",
"health_check.362345074.target" => "HTTP:8080/status",
"health_check.362345074.timeout" => "5",
"health_check.362345074.unhealthy_threshold" => "2",
"listener.#" => "1",
"listener.1674021574.instance_port" => "80",
"listener.1674021574.instance_protocol" => "http",
"listener.1674021574.lb_port" => "443",
"listener.1674021574.lb_protocol" => "https",
"listener.1674021574.ssl_certificate_id" => "arn:aws:iam::345678901234:server-certificate/foobar",
"security_groups.#" => "2",
"security_groups.2877768809" => "sg-9012ijkl",
"security_groups.1478442660" => "sg-3456mnop",
"subnets.#" => "2",
"subnets.1260945407" => "subnet-9012ijkl",
"subnets.3098543410" => "subnet-3456mnop",
"instances.436309938" => "i-5678efgh",
"tags.#" => "1",
"tags.name" => "elb-1"
}
}
}
......
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