Class: TerraformWrapper::Shared::Config

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

Constant Summary collapse

@@config_exts =
[ "", ".yaml", ".yml" ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, logger_for

Constructor Details

#initialize(code:, options:) ⇒ Config

Returns a new instance of Config.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/terraform-wrapper/shared/config.rb', line 38

def initialize(code:, options:)
  logger.fatal("Configuration base path must be a string!") unless options["base"].kind_of?(String)
  logger.fatal("Configuration base path must not be blank!") if options["base"].strip.empty?

  @base = options["base"]

  logger.fatal("Configuration name must be a string!") unless options["name"].kind_of?(String)
  logger.fatal("Configuration name must not be blank!") if options["name"].strip.empty?

  @name = options["name"]

  logger.fatal("Configuration service name must be a string!") unless options["service"].kind_of?(String)
  logger.fatal("Configuration service name must not be blank!") if options["service"].strip.empty?

  @service = options["service"]

  logger.fatal("Configuration authenticator for Azure enabled must be a Boolean!") unless [ true, false ].include?(options["auth-azure"])

  auth_azure = options["auth-azure"]

  logger.fatal("Configuration authenticator for Azure options must be a Hash!") unless options["auth-azure-options"].kind_of?(Hash)

  auth_azure_options = options["auth-azure-options"]

  logger.fatal("Configuration backend name must be a string!") unless options["backend"].kind_of?(String)
  logger.fatal("Configuration backend name must not be blank!") if options["backend"].strip.empty?

  backend = options["backend"]

  logger.fatal("Configuration backend options must be a Hash!") unless options["backend-options"].kind_of?(Hash)

  backend_options = options["backend-options"]

  @code = code
  @path = ::TerraformWrapper.find(base: @base, name: @name, exts: @@config_exts, description: "Configuration")

  yaml = YAML.load(File.read(@path))
  logger.fatal("Invalid YAML in configuration file: #{@path}") unless yaml.kind_of?(Hash)

  identifiers = yaml.key?("identifiers") ? yaml["identifiers"] : Hash.new
  @variables  = TerraformWrapper::Shared::Variables.new(config: @name, component: @code.name, service: @service, identifiers: identifiers)

  if yaml.key?("globals") then
    logger.fatal("Key 'globals' is not a hash in configuration file: #{@path}") unless yaml["globals"].kind_of?(Hash)
    globals = yaml["globals"]

    @variables.add_variables(variables: globals["variables"]) if globals.key?("variables")
  end

  if yaml.key?("terraform") then
    logger.fatal("Key 'terraform' is not a hash in configuration file: #{@path}") unless yaml["terraform"].kind_of?(Hash)
    terraform = yaml["terraform"]

    [ "globals", @code.name ].each do |extra|
      if terraform.key?(extra) then
        logger.fatal("Key '#{extra}' under 'terraform' is not a hash in configuration file: #{@path}") unless terraform[extra].kind_of?(Hash)
        section = terraform[extra]

        @variables.add_variables(variables: section["variables"]) if section.key?("variables")
        @variables.add_files(base: @base, files: section["files"]) if section.key?("files")
      end
    end
  end

  @auths = Array.new
  @auths.append(TerraformWrapper::Shared::Auths::Azure.new(options: auth_azure_options, variables: @variables)) if auth_azure

  if backend == "local" then
    @backend = TerraformWrapper::Shared::Backends::Local.new(options: backend_options, variables: @variables)
  elsif backend == "aws" then
    @backend = TerraformWrapper::Shared::Backends::AWS.new(options: backend_options, variables: @variables)
  elsif backend == "azure" then
    @backend = TerraformWrapper::Shared::Backends::Azure.new(options: backend_options, variables: @variables)
  else
    logger.fatal("Backend: #{backend} is not valid!")
  end
end

Instance Attribute Details

#authsObject (readonly)

Returns the value of attribute auths.



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

def auths
  @auths
end

#backendObject (readonly)

Returns the value of attribute backend.



28
29
30
# File 'lib/terraform-wrapper/shared/config.rb', line 28

def backend
  @backend
end

#baseObject (readonly)

Returns the value of attribute base.



29
30
31
# File 'lib/terraform-wrapper/shared/config.rb', line 29

def base
  @base
end

#codeObject (readonly)

Returns the value of attribute code.



30
31
32
# File 'lib/terraform-wrapper/shared/config.rb', line 30

def code
  @code
end

#nameObject (readonly)

Returns the value of attribute name.



31
32
33
# File 'lib/terraform-wrapper/shared/config.rb', line 31

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



32
33
34
# File 'lib/terraform-wrapper/shared/config.rb', line 32

def path
  @path
end

#serviceObject (readonly)

Returns the value of attribute service.



33
34
35
# File 'lib/terraform-wrapper/shared/config.rb', line 33

def service
  @service
end

#variablesObject (readonly)

Returns the value of attribute variables.



34
35
36
# File 'lib/terraform-wrapper/shared/config.rb', line 34

def variables
  @variables
end