Commit d67c1c4b authored by Daisuke Fujita's avatar Daisuke Fujita

Merge pull request #134 from dtan4/autoscaling-group

Support AutoScaling Group
parents 5df1e0c6 11524f08
......@@ -11,6 +11,7 @@ require "terraforming/util"
require "terraforming/version"
require "terraforming/cli"
require "terraforming/resource/auto_scaling_group"
require "terraforming/resource/db_parameter_group"
require "terraforming/resource/db_security_group"
require "terraforming/resource/db_subnet_group"
......
......@@ -4,6 +4,11 @@ module Terraforming
class_option :overwrite, type: :boolean, desc: "Overwrite existng tfstate"
class_option :tfstate, type: :boolean, desc: "Generate tfstate"
desc "asg", "AutoScaling Group"
def asg
execute(Terraforming::Resource::AutoScalingGroup, options)
end
desc "dbpg", "Database Parameter Group"
def dbpg
execute(Terraforming::Resource::DBParameterGroup, options)
......
module Terraforming
module Resource
class AutoScalingGroup
include Terraforming::Util
def self.tf(client: Aws::AutoScaling::Client.new)
self.new(client).tf
end
def self.tfstate(client: Aws::AutoScaling::Client.new)
self.new(client).tfstate
end
def initialize(client)
@client = client
end
def tf
apply_template(@client, "tf/auto_scaling_group")
end
def tfstate
auto_scaling_groups.inject({}) do |resources, group|
vpc_zone_specified = vpc_zone_specified?(group)
attributes = {
"availability_zones.#" => vpc_zone_specified ? "0" : 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_specified ? vpc_zone_identifier_of(group).length.to_s : "0",
}
group.tags.each do |tag|
hashcode = tag_hashcode_of(tag)
attributes.merge!({
"tag.#{hashcode}.key" => tag.key,
"tag.#{hashcode}.propagate_at_launch" => tag.propagate_at_launch.to_s,
"tag.#{hashcode}.value" => tag.value,
})
end
resources["aws_autoscaling_group.#{module_name_of(group)}"] = {
"type" => "aws_autoscaling_group",
"primary" => {
"id" => group.auto_scaling_group_name,
"attributes" => attributes,
"meta" => {
"schema_version" => "1"
}
}
}
resources
end
end
private
def auto_scaling_groups
@client.describe_auto_scaling_groups.auto_scaling_groups
end
def module_name_of(group)
normalize_module_name(group.auto_scaling_group_name)
end
def tag_hashcode_of(tag)
Zlib.crc32("#{tag.key}-#{tag.value}-#{tag.propagate_at_launch}-")
end
def vpc_zone_identifier_of(group)
group.vpc_zone_identifier.split(",")
end
def vpc_zone_specified?(group)
group.vpc_zone_identifier && vpc_zone_identifier_of(group).length > 0
end
end
end
end
<% auto_scaling_groups.each do |group| -%>
<%- vpc_zone_specified = vpc_zone_specified?(group) -%>
resource "aws_autoscaling_group" "<%= module_name_of(group) %>" {
<%- unless vpc_zone_specified -%>
availability_zones = <%= group.availability_zones.inspect %>
<%- end -%>
desired_capacity = <%= group.desired_capacity %>
health_check_grace_period = <%= group.health_check_grace_period %>
health_check_type = "<%= group.health_check_type %>"
launch_configuration = "<%= group.launch_configuration_name %>"
max_size = <%= group.max_size %>
min_size = <%= group.min_size %>
name = "<%= group.auto_scaling_group_name %>"
<%- if vpc_zone_specified -%>
vpc_zone_identifier = <%= vpc_zone_identifier_of(group).inspect %>
<%- end -%>
tag {
<% group.tags.each do |tag| -%>
key = "<%= tag.key %>"
value = "<%= tag.value %>"
propagate_at_launch = <%= tag.propagate_at_launch.to_s %>
<% end -%>
}
}
<% end -%>
......@@ -32,6 +32,13 @@ module Terraforming
allow(klass).to receive(:tfstate).and_return({})
end
describe "asg" do
let(:klass) { Terraforming::Resource::AutoScalingGroup }
let(:command) { :asg }
it_behaves_like "CLI examples"
end
describe "dbpg" do
let(:klass) { Terraforming::Resource::DBParameterGroup }
let(:command) { :dbpg }
......
require "spec_helper"
require "spec_helper"
module Terraforming
module Resource
describe AutoScalingGroup do
let(:client) do
Aws::AutoScaling::Client.new(stub_responses: true)
end
let(:auto_scaling_groups) do
[
{
auto_scaling_group_name: "hoge",
auto_scaling_group_arn:
"arn:aws:autoscaling:ap-northeast-1:123456789012:autoScalingGroup:1234abcd-1dd4-4089-b8c9-12345abcdefg:autoScalingGroupName/hoge",
launch_configuration_name: "hoge-lc",
min_size: 1,
max_size: 4,
desired_capacity: 2,
default_cooldown: 300,
availability_zones: ["ap-northeast-1b"],
load_balancer_names: [],
health_check_type: "EC2",
health_check_grace_period: 300,
instances: [
{
instance_id: "i-1234abcd",
availability_zone: "ap-northeast-1b",
lifecycle_state: "InService",
health_status: "Healthy",
launch_configuration_name: "hoge-lc"
},
{
instance_id: "i-5678efgh",
availability_zone: "ap-northeast-1b",
lifecycle_state: "InService",
health_status: "Healthy",
launch_configuration_name: "hoge-lc"
},
],
created_time: Time.parse("2015-10-21 04:08:39 UTC"),
suspended_processes: [],
vpc_zone_identifier: "",
enabled_metrics: [],
tags: [
{
resource_id: "hoge",
resource_type: "auto-scaling-group",
key: "foo1",
value: "bar",
propagate_at_launch: true,
}
],
termination_policies: ["Default"],
},
{
auto_scaling_group_name: "fuga",
auto_scaling_group_arn:
"arn:aws:autoscaling:ap-northeast-1:123456789012:autoScalingGroup:1234abcd-1dd4-4089-b8c9-12345abcdefg:autoScalingGroupName/fuga",
launch_configuration_name: "fuga-lc",
min_size: 1,
max_size: 4,
desired_capacity: 2,
default_cooldown: 300,
availability_zones: ["ap-northeast-1b", "ap-northeast-1c"],
load_balancer_names: [],
health_check_type: "EC2",
health_check_grace_period: 300,
instances: [
{
instance_id: "i-9012ijkl",
availability_zone: "ap-northeast-1c",
lifecycle_state: "InService",
health_status: "Healthy",
launch_configuration_name: "fuga-lc"
},
{
instance_id: "i-3456mnop",
availability_zone: "ap-northeast-1c",
lifecycle_state: "InService",
health_status: "Healthy",
launch_configuration_name: "fuga-lc"
},
],
created_time: Time.parse("2015-10-20 04:08:39 UTC"),
suspended_processes: [],
vpc_zone_identifier: "subnet-1234abcd,subnet-5678efgh",
enabled_metrics: [],
tags: [],
termination_policies: ["Default"],
},
]
end
before do
client.stub_responses(:describe_auto_scaling_groups, auto_scaling_groups: auto_scaling_groups)
end
describe ".tf" do
it "should generate tf" do
expect(described_class.tf(client: client)).to eq <<-EOS
resource "aws_autoscaling_group" "hoge" {
availability_zones = ["ap-northeast-1b"]
desired_capacity = 2
health_check_grace_period = 300
health_check_type = "EC2"
launch_configuration = "hoge-lc"
max_size = 4
min_size = 1
name = "hoge"
tag {
key = "foo1"
value = "bar"
propagate_at_launch = true
}
}
resource "aws_autoscaling_group" "fuga" {
desired_capacity = 2
health_check_grace_period = 300
health_check_type = "EC2"
launch_configuration = "fuga-lc"
max_size = 4
min_size = 1
name = "fuga"
vpc_zone_identifier = ["subnet-1234abcd", "subnet-5678efgh"]
tag {
}
}
EOS
end
end
describe ".tfstate" do
it "should generate tfstate" do
expect(described_class.tfstate(client: client)).to eq({
"aws_autoscaling_group.hoge" => {
"type" => "aws_autoscaling_group",
"primary" => {
"id" => "hoge",
"attributes" => {
"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",
"tag.3921462319.key" => "foo1",
"tag.3921462319.propagate_at_launch" => "true",
"tag.3921462319.value" => "bar",
"termination_policies.#" => "0",
"vpc_zone_identifier.#" => "0",
},
"meta" => {
"schema_version" => "1"
}
}
},
"aws_autoscaling_group.fuga" => {
"type" => "aws_autoscaling_group",
"primary" => {
"id" => "fuga",
"attributes" => {
"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"
}
}
},
})
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