class Terraspace::Compiler::Strategy::Tfvar

Layers in order

Name / Pattern                 | Example
-------------------------------|---------------
base                           | base.tfvars
env                            | dev.tfvars
region/base                    | us-west-2/base.tfvars (provider specific)
region/env                     | us-west-2/dev.tfvars (provider specific)
namespace/base                 | 112233445566/base.tfvars (provider specific)
namespace/env                  | 112233445566/dev.tfvars (provider specific)
namespace/region/base          | 112233445566/us-west-2/base.tfvars (provider specific)
namespace/region/env           | 112233445566/us-west-2/dev.tfvars (provider specific)
provider/base                  | aws/base.tfvars (provider specific)
provider/env                   | aws/dev.tfvars (provider specific)
provider/region/base           | aws/us-west-2/base.tfvars (provider specific)
provider/region/env            | aws/us-west-2/dev.tfvars (provider specific)
provider/namespace/base        | aws/112233445566/base.tfvars (provider specific)
provider/namespace/env         | aws/112233445566/dev.tfvars (provider specific)
provider/namespace/region/base | aws/112233445566/us-west-2/base.tfvars (provider specific)
provider/namespace/region/env  | aws/112233445566/us-west-2/dev.tfvars (provider specific)

namespace and region depends on the provider. Here an example of the mapping:

          | AWS     | Azure        | Google
----------|---------|--------------|-------
namespace | account | subscription | project
region    | region  | location     | region

Public Class Methods

new(mod) click to toggle source
# File lib/terraspace/compiler/strategy/tfvar.rb, line 3
def initialize(mod)
  @mod = mod
  @order = 0
end

Public Instance Methods

layer_paths() click to toggle source
# File lib/terraspace/compiler/strategy/tfvar.rb, line 23
def layer_paths
  Layer.new(@mod).paths
end
ordered_name(layer_path) click to toggle source

Tact on number to ensure that tfvars will be processed in desired order. Also name auto.tfvars so it will automatically load

# File lib/terraspace/compiler/strategy/tfvar.rb, line 29
def ordered_name(layer_path)
  @order += 1
  prefix = @order.to_s
  # add leading 0 when more than 10 layers
  prefix = prefix.rjust(2, '0') if layer_paths.size > 9
  name = "#{prefix}-#{tfvar_name(layer_path)}"
  name.sub('.tfvars','.auto.tfvars')
      .sub('.rb','.auto.tfvars.json')
end
run() click to toggle source
# File lib/terraspace/compiler/strategy/tfvar.rb, line 8
def run
  layer_paths.each do |layer_path|
    ext = File.extname(layer_path).sub('.','')
    klass = strategy_class(ext)
    next unless klass

    strategy = klass.new(@mod, layer_path)
    content = strategy.run

    dest_name = ordered_name(layer_path)
    writer = Terraspace::Compiler::Writer.new(@mod, dest_name: dest_name)
    writer.write(content)
  end
end
strategy_class(ext) click to toggle source
# File lib/terraspace/compiler/strategy/tfvar.rb, line 49
def strategy_class(ext)
  "Terraspace::Compiler::Strategy::Tfvar::#{ext.camelize}".constantize
rescue NameError
end
tfvar_name(layer_path) click to toggle source
# File lib/terraspace/compiler/strategy/tfvar.rb, line 39
def tfvar_name(layer_path)
  if layer_path.include?('/tfvars/')
    name = layer_path.sub(%r{.*/tfvars/},'').gsub('/','-')
    name = "project-#{name}" if layer_path.include?("config/terraform/tfvars")
    name
  else
    File.basename(layer_path)
  end
end