Module: Kitchen::Verifier::Terraform::ConfigureInspecRunnerAttributes

Extended by:
Dry::Monads::Either::Mixin, Dry::Monads::Maybe::Mixin, Dry::Monads::Try::Mixin
Defined in:
lib/kitchen/verifier/terraform/configure_inspec_runner_attributes.rb

Overview

Configures the InSpec profile attributes for the Inspec::Runner used by the verifier to verify a group.

Three different maps are merged to create the profile attributes.

The first map is comprised of attributes that are external to the Terraform state.

{
  "terraform_state" => "/path/to/terraform/state"
}

The second map is comprised of attributes that represent the Terraform output variables of the Terraform state. This map takes precedence in any key conflicts with the first map.

{
  "first_output_variable_name" => "first_output_variable_value",
  "second_output_variable_name" => "second_output_variable_value"
}

The third map is comprised of attributes defined by a group’s :attributes; the keys are converted to strings and the values are assumed to be Terraform output variable names which are resolved. This map takes precedence in any key conflicts with the second map.

{
  first_attribute_name: "second_output_variable_name"
}

# becomes

{
  "first_attribute_name" => "second_output_variable_value"
}

Class Method Summary collapse

Class Method Details

.call(driver:, group:, terraform_state:) ⇒ ::Dry::Monads::Either

Invokes the function

Parameters:

  • driver (::Kitchen::Driver::Terraform)

    a kitchen-terraform driver

  • group (::Hash)

    a kitchen-terraform verifier group

  • terraform_state (::String)

    the path of a Terraform state file

Returns:

  • (::Dry::Monads::Either)

    the result of the function



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
# File 'lib/kitchen/verifier/terraform/configure_inspec_runner_attributes.rb', line 70

def self.call(driver:, group:, terraform_state:)
  Right("terraform_state" => terraform_state).bind do |attributes|
    driver.output.bind do |output|
      Right [
        attributes,
        output
      ]
    end
  end.bind do |attributes, output|
    Try ::KeyError do
      output.each_pair do |output_name, output_body|
        attributes.store output_name, output_body.fetch("value")
      end
      [attributes, output]
    end
  end.bind do |attributes, output|
    Maybe(group[:attributes]).bind do |group_attributes|
      group_attributes.each_pair do |attribute_name, output_name|
        attributes.store attribute_name.to_s, output.fetch(output_name.to_s).fetch("value")
      end
    end
    Right attributes
  end.to_either.or do |error|
    Left "configuring Inspec::Runner attributes failed\n#{error}"
  end
end