class PracticeTerraforming::CLI

Public Instance Methods

iamgpa() click to toggle source
# File lib/practice_terraforming/cli.rb, line 46
def iamgpa
  execute(PracticeTerraforming::Resource::IAMGroupPolicyAttachment, options)
end
iampa() click to toggle source
# File lib/practice_terraforming/cli.rb, line 31
def iampa
  execute(PracticeTerraforming::Resource::IamPolicyAttachment, options)
end
iamr() click to toggle source
# File lib/practice_terraforming/cli.rb, line 17
def iamr
  execute(PracticeTerraforming::Resource::IAMRole, options)
end
iamrpa() click to toggle source
# File lib/practice_terraforming/cli.rb, line 36
def iamrpa
  execute(PracticeTerraforming::Resource::IamRolePolicyAttachment, options)
end
iamu() click to toggle source
# File lib/practice_terraforming/cli.rb, line 22
def iamu
  execute(PracticeTerraforming::Resource::IAMUser, options)
end
iamugm() click to toggle source
# File lib/practice_terraforming/cli.rb, line 51
def iamugm
  execute(PracticeTerraforming::Resource::IAMUserGroupMembership, options)
end
iamupa() click to toggle source
# File lib/practice_terraforming/cli.rb, line 41
def iamupa
  execute(PracticeTerraforming::Resource::IAMUserPolicyAttachment, options)
end
s3() click to toggle source
# File lib/practice_terraforming/cli.rb, line 26
def s3
  execute(PracticeTerraforming::Resource::S3, options)
end

Private Instance Methods

configure_aws(options) click to toggle source
# File lib/practice_terraforming/cli.rb, line 57
def configure_aws(options)
  Aws.config[:credentials] = Aws::SharedCredentials.new(profile_name: options[:profile]) if options[:profile]
  Aws.config[:region] = options[:region] if options[:region]

  if options[:assume]
    args = { role_arn: options[:assume], role_session_name: "terraforming-session-#{Time.now.to_i}" }
    args[:client] = Aws::STS::Client.new(profile: options[:profile]) if options[:profile]
    Aws.config[:credentials] = Aws::AssumeRoleCredentials.new(args)
  end

  Aws.use_bundled_cert! if options[:use_bundled_cert]
end
execute(klass, options) click to toggle source
# File lib/practice_terraforming/cli.rb, line 70
def execute(klass, options)
  configure_aws(options)
  result = options[:tfstate] ? tfstate(klass, options[:merge]) : tf(klass)

  if options[:tfstate] && options[:merge] && options[:overwrite]
    open(options[:merge], "w+") do |f|
      f.write(result)
      f.flush
    end
  else
    puts result
  end
end
tf(klass) click to toggle source
# File lib/practice_terraforming/cli.rb, line 84
def tf(klass)
  klass.tf
end
tfstate(klass, tfstate_path) click to toggle source
# File lib/practice_terraforming/cli.rb, line 88
def tfstate(klass, tfstate_path)
  tfstate = tfstate_path ? MultiJson.load(open(tfstate_path).read) : tfstate_skeleton
  tfstate["serial"] = tfstate["serial"] + 1
  tfstate["modules"][0]["resources"] = tfstate["modules"][0]["resources"].merge(klass.tfstate)
  MultiJson.encode(tfstate, pretty: true)
end
tfstate_skeleton() click to toggle source
# File lib/practice_terraforming/cli.rb, line 95
def tfstate_skeleton
  {
    "version" => 1,
    "serial" => 0,
    "modules" => [
      {
        "path" => [
          "root"
        ],
        "outputs" => {},
        "resources" => {}
      }
    ]
  }
end