Class: TerraformWrapper::Shared::Variables

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/terraform-wrapper/shared/variables.rb

Constant Summary collapse

@@variable_files_name =
"tfvars"
@@variable_files_exts =
[ ".tfvars" ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, logger_for

Constructor Details

#initialize(component:, config:, service:, identifiers: Hash.new, sort: false) ⇒ Variables

Returns a new instance of Variables.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/terraform-wrapper/shared/variables.rb', line 31

def initialize(component:, config:, service:, identifiers: Hash.new, sort: false)
  logger.fatal("Identifiers provided must be a hash!") unless identifiers.kind_of?(Hash)

  @core             = Hash.new()
  @core[:component] = component
  @core[:config]    = config
  @core[:service]   = service

  user   = cleanse(variables: identifiers, reserved: @core.keys, downcase: true)
  merged = @core.merge(user)

  @identifiers = sort ? merged.sort.to_h : merged
  @values      = Hash.new
  @files       = Array.new
end

Instance Attribute Details

#coreObject (readonly)

Returns the value of attribute core.



24
25
26
# File 'lib/terraform-wrapper/shared/variables.rb', line 24

def core
  @core
end

#filesObject (readonly)

Returns the value of attribute files.



25
26
27
# File 'lib/terraform-wrapper/shared/variables.rb', line 25

def files
  @files
end

#identifiersObject (readonly)

Returns the value of attribute identifiers.



26
27
28
# File 'lib/terraform-wrapper/shared/variables.rb', line 26

def identifiers
  @identifiers
end

#valuesObject (readonly)

Returns the value of attribute values.



27
28
29
# File 'lib/terraform-wrapper/shared/variables.rb', line 27

def values
  @values
end

Instance Method Details

#add_files(base:, files:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/terraform-wrapper/shared/variables.rb', line 49

def add_files(base:, files:)
  logger.fatal("Variable files provided must be an array!") unless files.kind_of?(Array)

  files.each do |file|
    logger.fatal("All provided variable file names must be strings!") unless file.kind_of?(String)
    logger.fatal("All provided variable file names must not be blank!") if file.strip.empty?

    path = ::TerraformWrapper.find(base: File.join(base, @@variable_files_name), name: file.strip, exts: @@variable_files_exts, description: "Terraform values file")

    if @files.include?(path) then
      logger.warn("Terraform variables file is included more than once: #{file.strip}")
    else
      @files.append(path)
    end
  end
end

#add_variables(variables:, sort: false) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/terraform-wrapper/shared/variables.rb', line 68

def add_variables(variables:, sort: false)
  logger.fatal("Variables provided must be a hash!") unless variables.kind_of?(Hash)

  cleansed = cleanse(variables: variables, reserved: @values.keys)

  begin
    cleansed = cleansed.map{ |key, value| [ key, value % @identifiers ] }.to_h
  rescue
    logger.fatal("Variables contain identifiers that are not included in the configuration file!")
  end

  merged  = @values.merge(cleansed)
  @values = sort ? merged.sort.to_h : merged
end

#clear_filesObject



85
86
87
# File 'lib/terraform-wrapper/shared/variables.rb', line 85

def clear_files()
  @files = Array.new
end

#clear_variablesObject



91
92
93
# File 'lib/terraform-wrapper/shared/variables.rb', line 91

def clear_variables()
  @values = Hash.new
end