Commit 862df557 authored by Daisuke Fujita's avatar Daisuke Fujita

Generate AutoScaling Group tfstate

parent 3e4bd364
......@@ -20,37 +20,28 @@ module Terraforming
end
def tfstate
instances.inject({}) do |resources, instance|
in_vpc = in_vpc?(instance)
block_devices = block_devices_of(instance)
auto_scaling_groups.inject({}) do |resources, group|
attributes = {
"ami"=> instance.image_id,
"associate_public_ip_address"=> "true",
"availability_zone"=> instance.placement.availability_zone,
"ebs_block_device.#"=> ebs_block_devices_in(block_devices, instance).length.to_s,
"ebs_optimized"=> instance.ebs_optimized.to_s,
"ephemeral_block_device.#" => "0", # Terraform 0.6.1 cannot fetch this field from AWS
"id"=> instance.instance_id,
"instance_type"=> instance.instance_type,
"monitoring" => monitoring_state(instance).to_s,
"private_dns"=> instance.private_dns_name,
"private_ip"=> instance.private_ip_address,
"public_dns"=> instance.public_dns_name,
"public_ip"=> instance.public_ip_address,
"root_block_device.#"=> root_block_devices_in(block_devices, instance).length.to_s,
"security_groups.#"=> in_vpc ? "0" : instance.security_groups.length.to_s,
"source_dest_check"=> instance.source_dest_check.to_s,
"tenancy"=> instance.placement.tenancy,
"vpc_security_group_ids.#"=> in_vpc ? instance.security_groups.length.to_s : "0",
"availability_zones.#" => group.availability_zones.length.to_s,
"default_cooldown" => "300",
"desired_capacity" => group.desired_capacity.to_s,
"health_check_grace_period" => group.health_check_grace_period.to_s,
"health_check_type" => group.health_check_type,
"id" => group.auto_scaling_group_name,
"launch_configuration" => group.launch_configuration_name,
"load_balancers.#" => "0",
"max_size" => group.max_size.to_s,
"min_size" => group.min_size.to_s,
"name" => group.auto_scaling_group_name,
"tag.#" => group.tags.length.to_s,
"termination_policies.#" => "0",
"vpc_zone_identifier.#" => vpc_zone_identifier_of(group).length.to_s,
}
attributes["subnet_id"] = instance.subnet_id if in_vpc?(instance)
resources["aws_instance.#{module_name_of(instance)}"] = {
"type" => "aws_instance",
resources["aws_autoscaling_group.#{module_name_of(group)}"] = {
"type" => "aws_autoscaling_group",
"primary" => {
"id" => instance.instance_id,
"id" => group.auto_scaling_group_name,
"attributes" => attributes,
"meta" => {
"schema_version" => "1"
......
......@@ -128,96 +128,58 @@ resource "aws_autoscaling_group" "fuga" {
end
describe ".tfstate" do
xit "should generate tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_instance.hoge" => {
"type" => "aws_instance",
"aws_autoscaling_group.hoge" => {
"type" => "aws_autoscaling_group",
"primary" => {
"id" => "i-1234abcd",
"id" => "hoge",
"attributes" => {
"ami" => "ami-1234abcd",
"associate_public_ip_address" => "true",
"availability_zone" => "ap-northeast-1b",
"ebs_block_device.#" => "0",
"ebs_optimized" => "false",
"ephemeral_block_device.#" => "0",
"id" => "i-1234abcd",
"instance_type" => "t2.micro",
"monitoring" => "false",
"private_dns" => "ip-10-0-0-100.ap-northeast-1.compute.internal",
"private_ip" => "10.0.0.100",
"public_dns" => "ec2-54-12-0-0.ap-northeast-1.compute.amazonaws.com",
"public_ip" => "54.12.0.0",
"root_block_device.#" => "1",
"security_groups.#" => "0",
"source_dest_check" => "true",
"tenancy" => "default",
"vpc_security_group_ids.#" => "1",
"subnet_id" => "subnet-1234abcd",
"availability_zones.#" => "1",
"default_cooldown" => "300",
"desired_capacity" => "2",
"health_check_grace_period" => "300",
"health_check_type" => "EC2",
"id" => "hoge",
"launch_configuration" => "hoge-lc",
"load_balancers.#" => "0",
"max_size" => "4",
"min_size" => "1",
"name" => "hoge",
"tag.#" => "1",
"termination_policies.#" => "0",
"vpc_zone_identifier.#" => "0",
},
"meta" => {
"schema_version" => "1"
}
}
},
"aws_instance.i-5678efgh" => {
"type" => "aws_instance",
"aws_autoscaling_group.fuga" => {
"type" => "aws_autoscaling_group",
"primary" => {
"id" => "i-5678efgh",
"id" => "fuga",
"attributes" => {
"ami" => "ami-5678efgh",
"associate_public_ip_address" => "true",
"availability_zone" => "ap-northeast-1b",
"ebs_block_device.#" => "1",
"ebs_optimized" => "false",
"ephemeral_block_device.#" => "0",
"id" => "i-5678efgh",
"instance_type" => "t2.micro",
"monitoring" => "true",
"private_dns" => "ip-10-0-0-101.ap-northeast-1.compute.internal",
"private_ip" => "10.0.0.101",
"public_dns" => "ec2-54-12-0-1.ap-northeast-1.compute.amazonaws.com",
"public_ip" => "54.12.0.1",
"root_block_device.#" => "0",
"security_groups.#" => "1",
"source_dest_check" => "true",
"tenancy" => "default",
"vpc_security_group_ids.#" => "0",
"availability_zones.#" => "0",
"default_cooldown" => "300",
"desired_capacity" => "2",
"health_check_grace_period" => "300",
"health_check_type" => "EC2",
"id" => "fuga",
"launch_configuration" => "fuga-lc",
"load_balancers.#" => "0",
"max_size" => "4",
"min_size" => "1",
"name" => "fuga",
"tag.#" => "0",
"termination_policies.#" => "0",
"vpc_zone_identifier.#" => "2",
},
"meta" => {
"schema_version" => "1"
}
},
}
},
"aws_instance.i-9012ijkl" => {
"type" => "aws_instance",
"primary" => {
"id" => "i-9012ijkl",
"attributes" => {
"ami" => "ami-9012ijkl",
"associate_public_ip_address" => "true",
"availability_zone" => "ap-northeast-1b",
"ebs_block_device.#" => "0",
"ebs_optimized" => "false",
"ephemeral_block_device.#" => "0",
"id" => "i-9012ijkl",
"instance_type" => "t2.micro",
"monitoring" => "true",
"private_dns" => "ip-10-0-0-102.ap-northeast-1.compute.internal",
"private_ip" => "10.0.0.102",
"public_dns" => "ec2-54-12-0-2.ap-northeast-1.compute.amazonaws.com",
"public_ip" => "54.12.0.2",
"root_block_device.#" => "0",
"security_groups.#" => "1",
"source_dest_check" => "true",
"tenancy" => "default",
"vpc_security_group_ids.#" => "0",
},
"meta" => {
"schema_version" => "1"
}
},
}
})
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