Commit 36b78a0d authored by Daisuke Fujita's avatar Daisuke Fujita

Merge pull request #106 from dtan4/refactor-tfstate-gen

Refactor tfstate generation logic
parents 1905abd3 3fa19762
......@@ -126,20 +126,35 @@ module Terraforming
private
def execute(klass, options)
result = if options[:tfstate]
tfstate(klass, options[:merge])
else
klass.tf
end
result = options[:tfstate] ? tfstate(klass, options[:merge]) : tf(klass)
puts result
end
def tfstate(klass, tfstate_path)
return klass.tfstate unless tfstate_path
def tf(klass)
klass.tf
end
tfstate_base = JSON.parse(open(tfstate_path).read)
klass.tfstate(tfstate_base: tfstate_base)
def tfstate(klass, tfstate_path)
tfstate = tfstate_path ? JSON.parse(open(tfstate_path).read) : tfstate_skeleton
tfstate["serial"] = tfstate["serial"] + 1
tfstate["modules"][0]["resources"] = tfstate["modules"][0]["resources"].merge(klass.tfstate)
JSON.pretty_generate(tfstate)
end
def tfstate_skeleton
{
"version" => 1,
"serial" => 0,
"modules" => [
{
"path" => [
"root"
],
"outputs" => {},
"resources" => {},
}
]
}
end
end
end
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::RDS::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,8 +19,8 @@ module Terraforming
apply_template(@client, "tf/db_parameter_group")
end
def tfstate(tfstate_base)
resources = db_parameter_groups.inject({}) do |result, parameter_group|
def tfstate
db_parameter_groups.inject({}) do |resources, parameter_group|
attributes = {
"description" => parameter_group.description,
"family" => parameter_group.db_parameter_group_family,
......@@ -28,7 +28,7 @@ module Terraforming
"name" => parameter_group.db_parameter_group_name,
"parameter.#" => db_parameters_in(parameter_group).length.to_s
}
result["aws_db_parameter_group.#{module_name_of(parameter_group)}"] = {
resources["aws_db_parameter_group.#{module_name_of(parameter_group)}"] = {
"type" => "aws_db_parameter_group",
"primary" => {
"id" => parameter_group.db_parameter_group_name,
......@@ -36,10 +36,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::RDS::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,15 +19,15 @@ module Terraforming
apply_template(@client, "tf/db_security_group")
end
def tfstate(tfstate_base)
resources = db_security_groups.inject({}) do |result, security_group|
def tfstate
db_security_groups.inject({}) do |resources, security_group|
attributes = {
"db_subnet_group_name" => security_group.db_security_group_name,
"id" => security_group.db_security_group_name,
"ingress.#" => (security_group.ec2_security_groups.length + security_group.ip_ranges.length).to_s,
"name" => security_group.db_security_group_name,
}
result["aws_db_security_group.#{module_name_of(security_group)}"] = {
resources["aws_db_security_group.#{module_name_of(security_group)}"] = {
"type" => "aws_db_security_group",
"primary" => {
"id" => security_group.db_security_group_name,
......@@ -35,10 +35,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::RDS::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,14 +19,14 @@ module Terraforming
apply_template(@client, "tf/db_subnet_group")
end
def tfstate(tfstate_base)
resources = db_subnet_groups.inject({}) do |result, subnet_group|
def tfstate
db_subnet_groups.inject({}) do |resources, subnet_group|
attributes = {
"description" => subnet_group.db_subnet_group_description,
"name" => subnet_group.db_subnet_group_name,
"subnet_ids.#" => subnet_group.subnets.length.to_s
}
result["aws_db_subnet_group.#{module_name_of(subnet_group)}"] = {
resources["aws_db_subnet_group.#{module_name_of(subnet_group)}"] = {
"type" => "aws_db_subnet_group",
"primary" => {
"id" => subnet_group.db_subnet_group_name,
......@@ -34,10 +34,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::EC2::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::EC2::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,8 +19,8 @@ module Terraforming
apply_template(@client, "tf/ec2")
end
def tfstate(tfstate_base)
resources = instances.inject({}) do |result, instance|
def tfstate
instances.inject({}) do |resources, instance|
in_vpc = in_vpc?(instance)
block_devices = block_devices_of(instance)
......@@ -46,7 +46,7 @@ module Terraforming
attributes["subnet_id"] = instance.subnet_id if in_vpc?(instance)
result["aws_instance.#{module_name_of(instance)}"] = {
resources["aws_instance.#{module_name_of(instance)}"] = {
"type" => "aws_instance",
"primary" => {
"id" => instance.instance_id,
......@@ -57,10 +57,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::ElastiCache::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::ElastiCache::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,8 +19,8 @@ module Terraforming
apply_template(@client, "tf/elasti_cache_cluster")
end
def tfstate(tfstate_base)
resources = cache_clusters.inject({}) do |result, cache_cluster|
def tfstate
cache_clusters.inject({}) do |resources, cache_cluster|
attributes = {
"cache_nodes.#" => cache_cluster.cache_nodes.length.to_s,
"cluster_id" => cache_cluster.cache_cluster_id,
......@@ -36,7 +36,7 @@ module Terraforming
"subnet_group_name" => cache_cluster.cache_subnet_group_name,
"tags.#" => "0",
}
result["aws_elasticache_cluster.#{cache_cluster.cache_cluster_id}"] = {
resources["aws_elasticache_cluster.#{cache_cluster.cache_cluster_id}"] = {
"type" => "aws_elasticache_cluster",
"primary" => {
"id" => cache_cluster.cache_cluster_id,
......@@ -44,10 +44,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::ElastiCache::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::ElastiCache::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,14 +19,14 @@ module Terraforming
apply_template(@client, "tf/elasti_cache_subnet_group")
end
def tfstate(tfstate_base)
resources = cache_subnet_groups.inject({}) do |result, cache_subnet_group|
def tfstate
cache_subnet_groups.inject({}) do |resources, cache_subnet_group|
attributes = {
"description" => cache_subnet_group.cache_subnet_group_description,
"name" => cache_subnet_group.cache_subnet_group_name,
"subnet_ids.#" => subnet_ids_of(cache_subnet_group).length.to_s,
}
result["aws_elasticache_subnet_group.#{cache_subnet_group.cache_subnet_group_name}"] = {
resources["aws_elasticache_subnet_group.#{cache_subnet_group.cache_subnet_group_name}"] = {
"type" => "aws_elasticache_subnet_group",
"primary" => {
"id" => cache_subnet_group.cache_subnet_group_name,
......@@ -34,10 +34,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::ElasticLoadBalancing::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::ElasticLoadBalancing::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,8 +19,8 @@ module Terraforming
apply_template(@client, "tf/elb")
end
def tfstate(tfstate_base)
resources = load_balancers.inject({}) do |result, load_balancer|
def tfstate
load_balancers.inject({}) do |resources, load_balancer|
load_balancer_attributes = load_balancer_attributes_of(load_balancer)
attributes = {
"availability_zones.#" => load_balancer.availability_zones.length.to_s,
......@@ -43,7 +43,7 @@ module Terraforming
attributes.merge!(tags_attributes_of(load_balancer))
result["aws_elb.#{module_name_of(load_balancer)}"] = {
resources["aws_elb.#{module_name_of(load_balancer)}"] = {
"type" => "aws_elb",
"primary" => {
"id" => load_balancer.load_balancer_name,
......@@ -51,10 +51,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
def healthcheck_attributes_of(elb)
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,8 +19,8 @@ module Terraforming
apply_template(@client, "tf/iam_group")
end
def tfstate(tfstate_base)
resources = iam_groups.inject({}) do |result, group|
def tfstate
iam_groups.inject({}) do |resources, group|
attributes = {
"arn"=> group.arn,
"id" => group.group_name,
......@@ -28,7 +28,7 @@ module Terraforming
"path" => group.path,
"unique_id" => group.group_id,
}
result["aws_iam_group.#{group.group_name}"] = {
resources["aws_iam_group.#{group.group_name}"] = {
"type" => "aws_iam_group",
"primary" => {
"id" => group.group_name,
......@@ -36,10 +36,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,8 +19,8 @@ module Terraforming
apply_template(@client, "tf/iam_group_membership")
end
def tfstate(tfstate_base)
resources = iam_groups.inject({}) do |result, group|
def tfstate
iam_groups.inject({}) do |resources, group|
membership_name = membership_name_of(group)
attributes = {
......@@ -29,7 +29,7 @@ module Terraforming
"name" => membership_name,
"users.#" => group_members_of(group).length.to_s,
}
result["aws_iam_group_membership.#{group.group_name}"] = {
resources["aws_iam_group_membership.#{group.group_name}"] = {
"type" => "aws_iam_group_membership",
"primary" => {
"id" => membership_name,
......@@ -37,10 +37,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,15 +19,15 @@ module Terraforming
apply_template(@client, "tf/iam_group_policy")
end
def tfstate(tfstate_base)
resources = iam_group_policies.inject({}) do |result, policy|
def tfstate
iam_group_policies.inject({}) do |resources, policy|
attributes = {
"group" => policy.group_name,
"id" => iam_group_policy_id_of(policy),
"name" => policy.policy_name,
"policy" => prettify_policy(policy.policy_document, true)
}
result["aws_iam_group_policy.#{policy.policy_name}"] = {
resources["aws_iam_group_policy.#{policy.policy_name}"] = {
"type" => "aws_iam_group_policy",
"primary" => {
"id" => iam_group_policy_id_of(policy),
......@@ -35,10 +35,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,8 +19,8 @@ module Terraforming
apply_template(@client, "tf/iam_instance_profile")
end
def tfstate(tfstate_base)
resources = iam_instance_profiles.inject({}) do |result, profile|
def tfstate
iam_instance_profiles.inject({}) do |resources, profile|
attributes = {
"arn" => profile.arn,
"id" => profile.instance_profile_name,
......@@ -28,7 +28,7 @@ module Terraforming
"path" => profile.path,
"roles.#" => profile.roles.length.to_s,
}
result["aws_iam_instance_profile.#{profile.instance_profile_name}"] = {
resources["aws_iam_instance_profile.#{profile.instance_profile_name}"] = {
"type" => "aws_iam_instance_profile",
"primary" => {
"id" => profile.instance_profile_name,
......@@ -36,10 +36,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,8 +19,8 @@ module Terraforming
apply_template(@client, "tf/iam_policy")
end
def tfstate(tfstate_base)
resources = iam_policies.inject({}) do |result, policy|
def tfstate
iam_policies.inject({}) do |resources, policy|
version = iam_policy_version_of(policy)
attributes = {
"id" => policy.arn,
......@@ -28,7 +28,7 @@ module Terraforming
"path" => policy.path,
"policy" => prettify_policy(version.document, true),
}
result["aws_iam_policy.#{policy.policy_name}"] = {
resources["aws_iam_policy.#{policy.policy_name}"] = {
"type" => "aws_iam_policy",
"primary" => {
"id" => policy.arn,
......@@ -36,10 +36,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -7,8 +7,8 @@ module Terraforming
self.new(client).tf
end
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
def self.tfstate(client: Aws::IAM::Client.new)
self.new(client).tfstate
end
def initialize(client)
......@@ -19,8 +19,8 @@ module Terraforming
apply_template(@client, "tf/iam_role")
end
def tfstate(tfstate_base)
resources = iam_roles.inject({}) do |result, role|
def tfstate
iam_roles.inject({}) do |resources, role|
attributes = {
"arn" => role.arn,
"assume_role_policy" => prettify_policy(role.assume_role_policy_document, true),
......@@ -29,7 +29,7 @@ module Terraforming
"path" => role.path,
"unique_id" => role.role_id,
}
result["aws_iam_role.#{role.role_name}"] = {
resources["aws_iam_role.#{role.role_name}"] = {
"type" => "aws_iam_role",
"primary" => {
"id" => role.role_name,
......@@ -37,10 +37,8 @@ module Terraforming
}
}
result
resources
end
generate_tfstate(resources, tfstate_base)
end
private
......