Class: Terraspace::Compiler::Expander

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/terraspace/compiler/expander/backend.rb,
lib/terraspace/compiler/expander.rb

Overview

Simpler than Terraspace::Terraform::Runner::Backend::Parser because Terraspace::Compiler::Expander autodetect backend super early on. It’s so early that don’t want helper methods like <%= expansion(…) %> to be called. Calling the expansion helper itself results in Terraspace autodetecting a concrete Terraspace Plugin Expander, which creates an infinite loop. This simple detection class avoids calling ERB and avoids the infinite loop.

Defined Under Namespace

Classes: Backend

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod, backend = nil) ⇒ Expander

Returns a new instance of Expander.



6
7
8
# File 'lib/terraspace/compiler/expander.rb', line 6

def initialize(mod, backend=nil)
  @mod, @backend = mod, backend
end

Class Method Details

.autodetect(mod, opts = {}) ⇒ Object



56
57
58
# File 'lib/terraspace/compiler/expander.rb', line 56

def autodetect(mod, opts={})
  new(mod, opts)
end

Instance Method Details

#expanderObject



10
11
12
# File 'lib/terraspace/compiler/expander.rb', line 10

def expander
  expander_class.new(@mod)
end

#expander_classObject

IE: TerraspacePluginAws::Interfaces::Expander



16
17
18
19
20
21
22
# File 'lib/terraspace/compiler/expander.rb', line 16

def expander_class
  class_name = expander_class_name
  class_name ? class_name.constantize : Terraspace::Plugin::Expander::Generic
rescue NameError => e
  logger.debug "#{e.class}: #{e.message}"
  Terraspace::Plugin::Expander::Generic
end

#expander_class_nameObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/terraspace/compiler/expander.rb', line 24

def expander_class_name
  plugin = Terraspace.config.autodetect.expander # contains plugin name. IE: aws, azurerm, google
  if plugin
    # early return for user override of autodetection
    return Terraspace::Plugin.klass("Expander", plugin: plugin) # can return nil
  end

  backend = @backend || parse_backend
  class_name = Terraspace::Plugin.klass("Expander", backend: backend) # can return nil
  unless class_name
    backend = plugin_backend
    class_name = Terraspace::Plugin.klass("Expander", backend: backend) # can return nil
  end
  class_name
end

#parse_backendObject

autodetect by parsing backend.tf or backend.rb



41
42
43
# File 'lib/terraspace/compiler/expander.rb', line 41

def parse_backend
  Backend.new(@mod).detect
end

#plugin_backendObject

autodetect by looking up loaded plugins



46
47
48
49
50
51
52
# File 'lib/terraspace/compiler/expander.rb', line 46

def plugin_backend
  plugin = Terraspace::Plugin.autodetect
  if plugin
    data = Terraspace::Plugin.meta[plugin]
    data[:backend] # IE: s3, azurerm, gcs
  end
end