module Inspec::ResourceDSL
def self.desc(description = nil)
def self.desc(description = nil) return @description if description.nil? @description = description end
def self.example(example = nil)
def self.example(example = nil) return @example if example.nil? @example = example end
def __register(name, obj) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def __register(name, obj) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity cl = Class.new(obj) do # rubocop:disable Metrics/BlockLength attr_reader :resource_exception_message def initialize(backend, name, *args) @resource_skipped = false @resource_failed = false @supports = Inspec::Resource.supports[name] # attach the backend to this instance @__backend_runner__ = backend @__resource_name__ = name # check resource supports supported = true supported = check_supports unless @supports.nil? test_backend = defined?(Train::Transports::Mock::Connection) && backend.backend.class == Train::Transports::Mock::Connection # do not return if we are supported, or for tests return unless supported || test_backend # call the resource initializer begin super(*args) rescue Inspec::Exceptions::ResourceSkipped => e skip_resource(e.message) rescue Inspec::Exceptions::ResourceFailed => e fail_resource(e.message) rescue NoMethodError => e # The new platform resources have methods generated on the fly # for inspec check to work we need to skip these train errors raise unless test_backend && e.receiver.class == Train::Transports::Mock::Connection skip_resource(e.message) end end def self.desc(description = nil) return @description if description.nil? @description = description end def self.example(example = nil) return @example if example.nil? @example = example end def check_supports status = inspec.platform.supported?(@supports) fail_msg = "Resource #{@__resource_name__.capitalize} is not supported on platform #{inspec.platform.name}/#{inspec.platform.release}." fail_resource(fail_msg) unless status status end def skip_resource(message) @resource_skipped = true @resource_exception_message = message end def resource_skipped? @resource_skipped end def fail_resource(message) @resource_failed = true @resource_exception_message = message end def resource_failed? @resource_failed end def inspec @__backend_runner__ end end # rubocop:enable Lint/NestedMethodDefinition if __resource_registry.key?(name) Inspec::Log.warn("Overwriting resource #{name}. To reference a specific version of #{name} use the resource() method") end __resource_registry[name] = cl end
def __resource_registry
def __resource_registry Inspec::Resource.registry end
def check_supports
def check_supports status = inspec.platform.supported?(@supports) fail_msg = "Resource #{@__resource_name__.capitalize} is not supported on platform #{inspec.platform.name}/#{inspec.platform.release}." fail_resource(fail_msg) unless status status end
def desc(description = nil)
def desc(description = nil) return if description.nil? __resource_registry[@name].desc(description) end
def example(example = nil)
def example(example = nil) return if example.nil? __resource_registry[@name].example(example) end
def fail_resource(message)
def fail_resource(message) @resource_failed = true @resource_exception_message = message end
def initialize(backend, name, *args)
def initialize(backend, name, *args) @resource_skipped = false @resource_failed = false @supports = Inspec::Resource.supports[name] # attach the backend to this instance @__backend_runner__ = backend @__resource_name__ = name # check resource supports supported = true supported = check_supports unless @supports.nil? test_backend = defined?(Train::Transports::Mock::Connection) && backend.backend.class == Train::Transports::Mock::Connection # do not return if we are supported, or for tests return unless supported || test_backend # call the resource initializer begin super(*args) rescue Inspec::Exceptions::ResourceSkipped => e skip_resource(e.message) rescue Inspec::Exceptions::ResourceFailed => e fail_resource(e.message) rescue NoMethodError => e # The new platform resources have methods generated on the fly # for inspec check to work we need to skip these train errors raise unless test_backend && e.receiver.class == Train::Transports::Mock::Connection skip_resource(e.message) end end
def inspec
def inspec @__backend_runner__ end
def name(name = nil)
def name(name = nil) return if name.nil? @name = name __register(name, self) end
def resource_failed?
def resource_failed? @resource_failed end
def resource_skipped?
def resource_skipped? @resource_skipped end
def skip_resource(message)
def skip_resource(message) @resource_skipped = true @resource_exception_message = message end
def supports(criteria = nil)
def supports(criteria = nil) return if criteria.nil? Inspec::Resource.supports[@name] ||= [] Inspec::Resource.supports[@name].push(criteria) end