Commit e743d217 authored by Daisuke Fujita's avatar Daisuke Fujita

Merge pull request #77 from dtan4/merge-tfstate

Merge generated tfstate to existing tfstate
parents 0f032c89 4a519657
# v0.1.0 (UNRELEASED)
### New feature
- `--merge TFSTATE_PATH` option: Merge generated tfstate to specified `terraform.tfstate`
### Fixed
- Include AWS Network ACL subnet ids
......
module Terraforming
class CLI < Thor
class_option :merge, type: :string, desc: "tfstate file to merge"
class_option :tfstate, type: :boolean, desc: "Generate tfstate"
desc "dbpg", "Database Parameter Group"
option :tfstate, type: :boolean
def dbpg
execute(Terraforming::Resource::DBParameterGroup, options)
end
desc "dbsg", "Database Security Group"
option :tfstate, type: :boolean
def dbsg
execute(Terraforming::Resource::DBSecurityGroup, options)
end
desc "dbsn", "Database Subnet Group"
option :tfstate, type: :boolean
def dbsn
execute(Terraforming::Resource::DBSubnetGroup, options)
end
desc "ec2", "EC2"
option :tfstate, type: :boolean
def ec2
execute(Terraforming::Resource::EC2, options)
end
desc "elb", "ELB"
option :tfstate, type: :boolean
def elb
execute(Terraforming::Resource::ELB, options)
end
desc "iamg", "IAM Group"
option :tfstate, type: :boolean
def iamg
execute(Terraforming::Resource::IAMGroup, options)
end
desc "iamgp", "IAM Group Policy"
option :tfstate, type: :boolean
def iamgp
execute(Terraforming::Resource::IAMGroupPolicy, options)
end
desc "iamip", "IAM Instance Profile"
option :tfstate, type: :boolean
def iamip
execute(Terraforming::Resource::IAMInstanceProfile, options)
end
desc "iamp", "IAM Policy"
option :tfstate, type: :boolean
def iamp
execute(Terraforming::Resource::IAMPolicy, options)
end
desc "iamr", "IAM Role"
option :tfstate, type: :boolean
def iamr
execute(Terraforming::Resource::IAMRole, options)
end
desc "iamrp", "IAM Role Policy"
option :tfstate, type: :boolean
def iamrp
execute(Terraforming::Resource::IAMRolePolicy, options)
end
desc "iamu", "IAM User"
option :tfstate, type: :boolean
def iamu
execute(Terraforming::Resource::IAMUser, options)
end
desc "iamup", "IAM User Policy"
option :tfstate, type: :boolean
def iamup
execute(Terraforming::Resource::IAMUserPolicy, options)
end
desc "nacl", "Network ACL"
option :tfstate, type: :boolean
def nacl
execute(Terraforming::Resource::NetworkACL, options)
end
desc "r53r", "Route53 Record"
option :tfstate, type: :boolean
def r53r
execute(Terraforming::Resource::Route53Record, options)
end
desc "r53z", "Route53 Hosted Zone"
option :tfstate, type: :boolean
def r53z
execute(Terraforming::Resource::Route53Zone, options)
end
desc "rds", "RDS"
option :tfstate, type: :boolean
def rds
execute(Terraforming::Resource::RDS, options)
end
desc "s3", "S3"
option :tfstate, type: :boolean
def s3
execute(Terraforming::Resource::S3, options)
end
desc "sg", "Security Group"
option :tfstate, type: :boolean
def sg
execute(Terraforming::Resource::SecurityGroup, options)
end
desc "sn", "Subnet"
option :tfstate, type: :boolean
def sn
execute(Terraforming::Resource::Subnet, options)
end
desc "vpc", "VPC"
option :tfstate, type: :boolean
def vpc
execute(Terraforming::Resource::VPC, options)
end
......@@ -129,7 +111,20 @@ module Terraforming
private
def execute(klass, options)
puts options[:tfstate] ? klass.tfstate : klass.tf
result = if options[:tfstate]
tfstate(klass, options[:merge])
else
klass.tf
end
puts result
end
def tfstate(klass, tfstate_path)
return klass.tfstate unless tfstate_path
base_tfstate = JSON.parse(open(tfstate_path).read)
klass.tfstate(tfstate_base: base_tfstate)
end
end
end
......@@ -3,12 +3,12 @@ module Terraforming
class DBParameterGroup
include Terraforming::Util
def self.tf(client = Aws::RDS::Client.new)
def self.tf(client: Aws::RDS::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::RDS::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/db_parameter_group")
end
def tfstate
def tfstate(tfstate_base)
resources = db_parameter_groups.inject({}) do |result, parameter_group|
attributes = {
"description" => parameter_group.description,
......@@ -39,7 +39,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -3,12 +3,12 @@ module Terraforming
class DBSecurityGroup
include Terraforming::Util
def self.tf(client = Aws::RDS::Client.new)
def self.tf(client: Aws::RDS::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::RDS::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/db_security_group")
end
def tfstate
def tfstate(tfstate_base)
resources = db_security_groups.inject({}) do |result, security_group|
attributes = {
"db_subnet_group_name" => security_group.db_security_group_name,
......@@ -38,7 +38,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -3,12 +3,12 @@ module Terraforming
class DBSubnetGroup
include Terraforming::Util
def self.tf(client = Aws::RDS::Client.new)
def self.tf(client: Aws::RDS::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::RDS::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::RDS::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/db_subnet_group")
end
def tfstate
def tfstate(tfstate_base)
resources = db_subnet_groups.inject({}) do |result, subnet_group|
attributes = {
"description" => subnet_group.db_subnet_group_description,
......@@ -37,7 +37,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -3,12 +3,12 @@ module Terraforming
class EC2
include Terraforming::Util
def self.tf(client = Aws::EC2::Client.new)
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
def self.tfstate(client: Aws::EC2::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/ec2")
end
def tfstate
def tfstate(tfstate_base)
resources = instances.inject({}) do |result, instance|
attributes = {
"ami"=> instance.image_id,
......@@ -54,7 +54,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -3,12 +3,12 @@ module Terraforming
class ELB
include Terraforming::Util
def self.tf(client = Aws::ElasticLoadBalancing::Client.new)
def self.tf(client: Aws::ElasticLoadBalancing::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::ElasticLoadBalancing::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::ElasticLoadBalancing::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/elb")
end
def tfstate
def tfstate(tfstate_base)
resources = load_balancers.inject({}) do |result, load_balancer|
load_balancer_attributes = load_balancer_attributes_of(load_balancer)
attributes = {
......@@ -49,7 +49,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
def load_balancers
......
......@@ -3,12 +3,12 @@ module Terraforming
class IAMGroup
include Terraforming::Util
def self.tf(client = Aws::IAM::Client.new)
def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::IAM::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/iam_group")
end
def tfstate
def tfstate(tfstate_base)
resources = iam_groups.inject({}) do |result, group|
attributes = {
"arn"=> group.arn,
......@@ -39,7 +39,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -3,12 +3,12 @@ module Terraforming
class IAMGroupPolicy
include Terraforming::Util
def self.tf(client = Aws::IAM::Client.new)
def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::IAM::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/iam_group_policy")
end
def tfstate
def tfstate(tfstate_base)
resources = iam_group_policies.inject({}) do |result, policy|
attributes = {
"group" => policy.group_name,
......@@ -38,7 +38,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -3,12 +3,12 @@ module Terraforming
class IAMInstanceProfile
include Terraforming::Util
def self.tf(client = Aws::IAM::Client.new)
def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::IAM::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/iam_instance_profile")
end
def tfstate
def tfstate(tfstate_base)
resources = iam_instance_profiles.inject({}) do |result, profile|
attributes = {
"arn" => profile.arn,
......@@ -39,7 +39,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -3,12 +3,12 @@ module Terraforming
class IAMPolicy
include Terraforming::Util
def self.tf(client = Aws::IAM::Client.new)
def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::IAM::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/iam_policy")
end
def tfstate
def tfstate(tfstate_base)
resources = iam_policies.inject({}) do |result, policy|
version = iam_policy_version_of(policy)
attributes = {
......@@ -39,7 +39,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -3,12 +3,12 @@ module Terraforming
class IAMRole
include Terraforming::Util
def self.tf(client = Aws::IAM::Client.new)
def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::IAM::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/iam_role")
end
def tfstate
def tfstate(tfstate_base)
resources = iam_roles.inject({}) do |result, role|
attributes = {
"arn" => role.arn,
......@@ -40,7 +40,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -3,12 +3,12 @@ module Terraforming
class IAMRolePolicy
include Terraforming::Util
def self.tf(client = Aws::IAM::Client.new)
def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::IAM::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/iam_role_policy")
end
def tfstate
def tfstate(tfstate_base)
resources = iam_role_policies.inject({}) do |result, policy|
attributes = {
"id" => iam_role_policy_id_of(policy),
......@@ -38,7 +38,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -3,12 +3,12 @@ module Terraforming
class IAMUser
include Terraforming::Util
def self.tf(client = Aws::IAM::Client.new)
def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::IAM::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/iam_user")
end
def tfstate
def tfstate(tfstate_base)
resources = iam_users.inject({}) do |result, user|
attributes = {
"arn"=> user.arn,
......@@ -39,7 +39,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end
private
......
......@@ -3,12 +3,12 @@ module Terraforming
class IAMUserPolicy
include Terraforming::Util
def self.tf(client = Aws::IAM::Client.new)
def self.tf(client: Aws::IAM::Client.new)
self.new(client).tf
end
def self.tfstate(client = Aws::IAM::Client.new)
self.new(client).tfstate
def self.tfstate(client: Aws::IAM::Client.new, tfstate_base: nil)
self.new(client).tfstate(tfstate_base)
end
def initialize(client)
......@@ -19,7 +19,7 @@ module Terraforming
apply_template(@client, "tf/iam_user_policy")
end
def tfstate
def tfstate(tfstate_base)
resources = iam_user_policies.inject({}) do |result, policy|
attributes = {
"id" => iam_user_policy_id_of(policy),
......@@ -38,7 +38,7 @@ module Terraforming
result
end
generate_tfstate(resources)
generate_tfstate(resources, tfstate_base)
end