Class: Terraspace::Compiler::Strategy::Tfvar

Inherits:
Object
  • Object
show all
Defined in:
lib/terraspace/compiler/strategy/tfvar/layer.rb,
lib/terraspace/compiler/strategy/tfvar.rb,
lib/terraspace/compiler/strategy/tfvar/rb.rb,
lib/terraspace/compiler/strategy/tfvar/base.rb,
lib/terraspace/compiler/strategy/tfvar/tfvars.rb

Overview

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 |  | subscription | project
region    | region  | location     | region

Defined Under Namespace

Classes: Base, Layer, Rb, Tfvars

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ Tfvar

Returns a new instance of Tfvar.



3
4
5
6
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 3

def initialize(mod)
  @mod = mod
  @order = 0
end

Instance Method Details

#layer_pathsObject



23
24
25
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 23

def layer_paths
  Layer.new(@mod).paths
end

#ordered_name(layer_path) ⇒ Object

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



29
30
31
32
33
34
35
36
37
# 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

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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) ⇒ Object



49
50
51
52
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
# 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