Module: Inspec::Backend

Defined in:
lib/inspec/backend.rb

Defined Under Namespace

Modules: Base

Class Method Summary collapse

Class Method Details

.create(config) ⇒ TransportBackend

Create the transport backend with aggregated resources.

Parameters:

Returns:

  • (TransportBackend)

    enriched transport instance



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
# File 'lib/inspec/backend.rb', line 45

def self.create(config) # rubocop:disable Metrics/AbcSize
  train_credentials = config.unpack_train_credentials
  transport_name = Train.validate_backend(train_credentials)
  transport = Train.create(transport_name, train_credentials)
  if transport.nil?
    raise "Can't find transport backend '#{transport_name}'."
  end

  connection = transport.connection
  if connection.nil?
    raise "Can't connect to transport backend '#{transport_name}'."
  end

  # Set caching settings. We always want to enable caching for
  # the Mock transport for testing.
  if config[:backend_cache] || config[:backend] == :mock
    Inspec::Log.debug "Option backend_cache is enabled"
    connection.enable_cache(:file)
    connection.enable_cache(:command)
  elsif config[:debug_shell]
    Inspec::Log.debug "Option backend_cache is disabled"
    connection.disable_cache(:file)
    connection.disable_cache(:command)
  else
    Inspec::Log.debug "Option backend_cache is disabled"
    connection.disable_cache(:file)
    connection.disable_cache(:command)
  end

  cls = Class.new do
    include Base

    define_method :backend do
      connection
    end

    def method_missing(id, *args, &blk)
      Inspec::DSL.method_missing_resource(self, id, *args)
    rescue LoadError
      super
    end

    Inspec::Resource.registry.each do |id, r|
      define_method id.to_sym do |*args|
        r.new(self, id.to_s, *args)
      end
    end
  end

  cls.new
rescue Train::ClientError => e
  raise "Client error, can't connect to '#{transport_name}' backend: #{e.message}"
rescue Train::TransportError => e
  raise "Transport error, can't connect to '#{transport_name}' backend: #{e.message}"
end