class Kitchen::Terraform::InSpecOptionsFactory

InSpecOptionsMapper is the class of objects which build Inspec options.

Attributes

options[RW]
system_bastion_host_resolver[RW]
system_inspec_map[RW]

Public Class Methods

inputs_key() click to toggle source

inputs_key provides a key for InSpec profile inputs which depends on the version of InSpec.

@return [Symbol] if the version is less than 4.3.2, :attributes; else, :inputs.

# File lib/kitchen/terraform/inspec_options_factory.rb, line 30
def inputs_key
  if ::Gem::Requirement.new("< 4.3.2").satisfied_by? ::Gem::Version.new ::Inspec::VERSION
    :attributes
  else
    :inputs
  end
end
new(outputs:) click to toggle source

initialize prepares a new instance of the class.

@param outputs [Hash] the Terraform output variables. @return [Kitchen::Terraform::InSpecOptionsFactory]

# File lib/kitchen/terraform/inspec_options_factory.rb, line 58
def initialize(outputs:)
  self.options = { "distinct_exit" => false }
  self.system_bastion_host_resolver = ::Kitchen::Terraform::SystemBastionHostResolver.new outputs: outputs
  self.system_inspec_map = ::Kitchen::Terraform::SYSTEM_INSPEC_MAP.dup
end

Public Instance Methods

build(attributes:, system_configuration_attributes:) click to toggle source

build creates a mapping of InSpec options. Most key-value pairs are derived from the configuration attributes of a system; some key-value pairs are hard-coded.

@param attributes [Hash] the attributes to be added to the InSpec options. @param system_configuration_attributes [Hash] the configuration attributes of a system. @raise [Kitchen::ClientError] if the system bastion host fails to be resolved. @return [Hash] a mapping of InSpec options.

# File lib/kitchen/terraform/inspec_options_factory.rb, line 46
def build(attributes:, system_configuration_attributes:)
  map_system_to_inspec system_configuration_attributes: system_configuration_attributes
  options.store self.class.inputs_key, attributes
  resolve_bastion_host system_configuration_attributes: system_configuration_attributes

  options
end

Private Instance Methods

map_system_to_inspec(system_configuration_attributes:) click to toggle source
# File lib/kitchen/terraform/inspec_options_factory.rb, line 68
def map_system_to_inspec(system_configuration_attributes:)
  system_configuration_attributes.lazy.select do |attribute_name, _|
    system_inspec_map.key?(attribute_name)
  end.each do |attribute_name, attribute_value|
    options.store system_inspec_map.fetch(attribute_name), attribute_value
  end
end
resolve_bastion_host(system_configuration_attributes:) click to toggle source
# File lib/kitchen/terraform/inspec_options_factory.rb, line 76
def resolve_bastion_host(system_configuration_attributes:)
  system_bastion_host_resolver.resolve(
    bastion_host: system_configuration_attributes.fetch(:bastion_host, ""),
    bastion_host_output: system_configuration_attributes.fetch(:bastion_host_output, ""),
  ) do |bastion_host:|
    options.store :bastion_host, bastion_host
  end
end