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

Inherits:
Object
  • Object
show all
Extended by:
Memoist
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 | account | 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.



5
6
7
8
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 5

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

Instance Method Details

#layer_pathsObject



26
27
28
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 26

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



33
34
35
36
37
38
39
40
41
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 33

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(write: true) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 10

def run(write: true)
  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

    next unless write
    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



53
54
55
56
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 53

def strategy_class(ext)
  "Terraspace::Compiler::Strategy::Tfvar::#{ext.camelize}".constantize
rescue NameError
end

#tfvar_name(layer_path) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/terraspace/compiler/strategy/tfvar.rb', line 43

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