class Chef::Compliance::InputCollection

Constants

HIDDEN_IVARS

Attributes

events[R]

Event dispatcher for this run.

@return [Chef::EventDispatch::Dispatcher]

Public Class Methods

new(events) click to toggle source
# File lib/chef/compliance/input_collection.rb, line 29
def initialize(events)
  @events = events
end

Public Instance Methods

from_file(filename, cookbook_name) click to toggle source

Add a input to the input collection. The cookbook_name needs to be determined by the caller and is used in the `include_input` API to match on. The path should be the complete path on the host of the yml file, including the filename.

@param path [String] @param cookbook_name [String]

# File lib/chef/compliance/input_collection.rb, line 40
def from_file(filename, cookbook_name)
  new_input = Input.from_file(events, filename, cookbook_name)
  self << new_input
  events.compliance_input_loaded(new_input)
end
from_hash(hash) click to toggle source

Add a input from a raw hash. This input will be enabled by default.

@param path [String] @param cookbook_name [String]

# File lib/chef/compliance/input_collection.rb, line 51
def from_hash(hash)
  new_input = Input.from_hash(events, hash)
  new_input.enable!
  self << new_input
end
include_input(arg) click to toggle source

DSL method to enable input files. This matches on the filename of the input file. If the specific input is omitted then it uses the default input. The string supports regular expression matching.

@example Specific input file in a cookbook

include_input “acme_cookbook::ssh-001”

@example The compliance/inputs/default.yml input in a cookbook

include_input “acme_cookbook”

@example Every input file in a cookbook

include_input “acme_cookbook::.*”

@example Matching inputs by regexp in a cookbook

include_input “acme_cookbook::ssh.*”

@example Matching inputs by regexp in any cookbook in the cookbook collection

include_input “.::ssh.

@example Adding an arbitrary hash of data (not from any file in a cookbook)

include_input({ “ssh_custom_path”: “/usr/local/bin” })

# File lib/chef/compliance/input_collection.rb, line 91
def include_input(arg)
  raise "include_input was given a nil value" if arg.nil?

  # if we're given a hash argument just shove it in the raw_hash
  if arg.is_a?(Hash)
    from_hash(arg)
    return
  end

  matching_inputs(arg).each(&:enable!)
end
inspec_data() click to toggle source

@return [Array<Input>] inspec inputs which are enabled in a form suitable to pass to inspec

# File lib/chef/compliance/input_collection.rb, line 59
def inspec_data
  select(&:enabled?).each_with_object({}) { |input, hash| hash.merge(input.inspec_data) }
end
inspect() click to toggle source

Omit the event object from error output

# File lib/chef/compliance/input_collection.rb, line 111
def inspect
  ivar_string = (instance_variables.map(&:to_sym) - HIDDEN_IVARS).map do |ivar|
    "#{ivar}=#{instance_variable_get(ivar).inspect}"
  end.join(", ")
  "#<#{self.class}:#{object_id} #{ivar_string}>"
end
valid?(arg) click to toggle source
# File lib/chef/compliance/input_collection.rb, line 103
def valid?(arg)
  !matching_inputs(arg).empty?
end

Private Instance Methods

matching_inputs(arg, should_raise: false) click to toggle source
# File lib/chef/compliance/input_collection.rb, line 120
def matching_inputs(arg, should_raise: false)
  (cookbook_name, input_name) = arg.split("::")

  input_name = "default" if input_name.nil?

  inputs = select { |input| /^#{cookbook_name}$/.match?(input.cookbook_name) && /^#{input_name}$/.match?(input.pathname) }

  if inputs.empty? && should_raise
    raise "No inspec inputs matching '#{input_name}' found in cookbooks matching '#{cookbook_name}'"
  end

  inputs
end
matching_inputs!(arg) click to toggle source
# File lib/chef/compliance/input_collection.rb, line 134
def matching_inputs!(arg)
  matching_inputs(arg, should_raise: true)
end