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.1.5 lib/terraspace/compiler/expander.rb
terraspace-2.1.4 lib/terraspace/compiler/expander.rb
terraspace-2.1.3 lib/terraspace/compiler/expander.rb
terraspace-2.1.2 lib/terraspace/compiler/expander.rb
terraspace-2.1.1 lib/terraspace/compiler/expander.rb
terraspace-2.1.0 lib/terraspace/compiler/expander.rb
terraspace-2.0.3 lib/terraspace/compiler/expander.rb
terraspace-2.0.2 lib/terraspace/compiler/expander.rb
terraspace-2.0.1 lib/terraspace/compiler/expander.rb
terraspace-2.0.0 lib/terraspace/compiler/expander.rb