Class: YleTf::BackendConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/yle_tf/backend_config.rb

Direct Known Subclasses

YleTfPlugins::Backends::File::Config

Constant Summary collapse

BACKEND_CONFIG_FILE =
'_backend.tf.json'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, config) ⇒ BackendConfig

Returns a new instance of BackendConfig.



9
10
11
12
# File 'lib/yle_tf/backend_config.rb', line 9

def initialize(type, config)
  @type = type
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/yle_tf/backend_config.rb', line 7

def config
  @config
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/yle_tf/backend_config.rb', line 7

def type
  @type
end

Instance Method Details

#cli_argsObject

Returns an ‘Array` of CLI args for Terraform pre 0.9 `init` command



15
16
17
18
19
20
21
# File 'lib/yle_tf/backend_config.rb', line 15

def cli_args
  args = ["-backend=#{type}"]
  config.each do |key, value|
    args << "-backend-config=#{key}=#{value}"
  end
  args
end

#generate_configObject

Generate backend configuration file for Terraform v0.9+



24
25
26
27
28
29
30
31
32
# File 'lib/yle_tf/backend_config.rb', line 24

def generate_config
  data = {
    terraform: [{
      backend: [to_h]
    }]
  }
  File.write(BACKEND_CONFIG_FILE, JSON.pretty_generate(data))
  yield if block_given?
end

#to_hObject Also known as: to_s

Returns the backend configuration as a ‘Hash` for Terraform v0.9+



35
36
37
# File 'lib/yle_tf/backend_config.rb', line 35

def to_h
  { type => config }
end