Sha256: 879cbea35237927777c4a97af538ba8c5099535b248d004abf22735d32407dee

Contents?: true

Size: 1.68 KB

Versions: 30

Compression:

Stored size: 1.68 KB

Contents

module Terraspace::Compiler
  class Expander
    extend Memoist
    delegate :expand, :expansion, to: :expander

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

    def expander
      expander_class.new(@mod)
    end
    memoize :expander

    # IE: TerraspacePluginAws::Interfaces::Expander
    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

    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

    # autodetect by parsing backend.tf or backend.rb
    def parse_backend
      Backend.new(@mod).detect
    end

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

    class << self
      extend Memoist
      def autodetect(mod, opts={})
        new(mod, opts)
      end
      memoize :autodetect
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
terraspace-2.2.17 lib/terraspace/compiler/expander.rb
terraspace-2.2.16 lib/terraspace/compiler/expander.rb
terraspace-2.2.15 lib/terraspace/compiler/expander.rb
terraspace-2.2.14 lib/terraspace/compiler/expander.rb
terraspace-2.2.13 lib/terraspace/compiler/expander.rb
terraspace-2.2.12 lib/terraspace/compiler/expander.rb
terraspace-2.2.11 lib/terraspace/compiler/expander.rb
terraspace-2.2.10 lib/terraspace/compiler/expander.rb
terraspace-2.2.9 lib/terraspace/compiler/expander.rb
terraspace-2.2.8 lib/terraspace/compiler/expander.rb
terraspace-2.2.7 lib/terraspace/compiler/expander.rb
terraspace-2.2.6 lib/terraspace/compiler/expander.rb
terraspace-2.2.5 lib/terraspace/compiler/expander.rb
terraspace-2.2.4 lib/terraspace/compiler/expander.rb
terraspace-2.2.3 lib/terraspace/compiler/expander.rb
terraspace-2.2.2 lib/terraspace/compiler/expander.rb
terraspace-2.2.1 lib/terraspace/compiler/expander.rb
terraspace-2.2.0 lib/terraspace/compiler/expander.rb
terraspace-2.1.7 lib/terraspace/compiler/expander.rb
terraspace-2.1.6 lib/terraspace/compiler/expander.rb