Class: OctocatalogDiff::Facts::Yaml

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/facts/yaml.rb

Overview

Deal with facts in YAML files

Class Method Summary collapse

Class Method Details

.fact_retriever(options = {}, node = '') ⇒ Hash

Manipulate a YAML file so it can be parsed and return the facts as a hash. If we leave it as Puppet::Node::Facts then it will require us to load puppet gems in order to parse it, and that’s too heavy for simple fact retrieval.

Parameters:

  • options (Hash) (defaults to: {})

    Options hash specifically for this fact type.

    • :fact_file_string [String] => Fact data as a string

  • node (String) (defaults to: '')

    Node name (overrides node name from fact data)

Returns:

  • (Hash)

    Facts



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/octocatalog-diff/facts/yaml.rb', line 17

def self.fact_retriever(options = {}, node = '')
  fact_file_string = options.fetch(:fact_file_string)

  # Touch up the first line before parsing.
  fact_file_data = fact_file_string.split(/\n/)
  fact_file_data[0] = '---' if fact_file_data[0] =~ /^---/

  # Load the parsed fact file.
  parsed = YAML.load(fact_file_data.join("\n"))

  # This is a handler for a YAML file that has just the facts and none of the
  # structure. For example if you saved the output of `facter -y` to a file and
  # are passing that in, this will work.
  result = if parsed.key?('name') && parsed.key?('values')
    parsed
  else
    { 'name' => node || parsed['fqdn'] || '', 'values' => parsed }
  end

  result['name'] = node unless node == ''
  result
end