Sha256: 8b600a0e947f6ae2388f998b457f53beace88a73d6e19fa59526ab4d9d895ff0

Contents?: true

Size: 1.77 KB

Versions: 9

Compression:

Stored size: 1.77 KB

Contents

# encoding: utf-8
# copyright: 2015, Dominik Richter
# license: All rights reserved
# author: Dominik Richter
# author: Christoph Hartmann

require 'train'

module Inspec
  module Backend
    module Base
      attr_accessor :profile

      # Provide a shorthand to retrieve the inspec version from within a profile
      #
      # @return [String] inspec version
      def version
        Inspec::VERSION
      end

      # Ruby internal for printing a nice name for this class
      def to_s
        'Inspec::Backend::Class'
      end

      # Ruby internal for pretty-printing a summary for this class
      def inspect
        "Inspec::Backend::Class @transport=#{backend.class}"
      end
    end

    # Create the transport backend with aggregated resources.
    #
    # @param [Hash] config for the transport backend
    # @return [TransportBackend] enriched transport instance
    def self.create(config)
      conf = Train.target_config(config)
      name = Train.validate_backend(conf)
      transport = Train.create(name, conf)
      if transport.nil?
        raise "Can't find transport backend '#{name}'."
      end

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

      cls = Class.new do
        include Base

        define_method :backend do
          connection
        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 '#{name}' backend: #{e.message}"
    rescue Train::TransportError => e
      raise "Transport error, can't connect to '#{name}' backend: #{e.message}"
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
inspec-1.29.0 lib/inspec/backend.rb
inspec-1.28.1 lib/inspec/backend.rb
inspec-1.28.0 lib/inspec/backend.rb
inspec-1.27.0 lib/inspec/backend.rb
inspec-1.26.0 lib/inspec/backend.rb
inspec-1.25.1 lib/inspec/backend.rb
inspec-1.25.0 lib/inspec/backend.rb
inspec-1.24.0 lib/inspec/backend.rb
inspec-1.23.0 lib/inspec/backend.rb