Module: PuppetFactset

Defined in:
lib/puppet_factset.rb,
lib/puppet_factset/version.rb

Constant Summary collapse

MERGE_FACTS_DIR =
File.join("spec", "merge_facts")
VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.factset_dirObject

Return the name of the directory holding the factsets



9
10
11
# File 'lib/puppet_factset.rb', line 9

def self.factset_dir
  File.expand_path(File.join(File.dirname(__FILE__), '..', 'res', 'factset'))
end

.factset_hash(factset_name) ⇒ Object

Load factset json file and return a hash



14
15
16
17
18
19
20
21
# File 'lib/puppet_factset.rb', line 14

def self.factset_hash(factset_name)
  data = JSON.parse(File.read(File.join(factset_dir(), "#{factset_name}.json")))

  merge_facts(data["values"])

  # The facts are tucked away inside the 'values' element so just return that
  data["values"]
end

.factsetsObject

List the available factsets, sorted A-Z



24
25
26
27
28
# File 'lib/puppet_factset.rb', line 24

def self.factsets()
  Dir.glob(File.join(factset_dir, '*.json')).map { |f|
    File.basename(f).gsub('.json','')
  }.sort
end

.merge_facts(factset) ⇒ Object

If a directory exists at ‘merge_facts` relative to the current directory then all JSON files present in this directory will be loaded and merged into passed in factset

Parameters:

  • factset

    Hash of facts (will be modified in-place)



35
36
37
38
39
40
# File 'lib/puppet_factset.rb', line 35

def self.merge_facts(factset)
  Dir["#{MERGE_FACTS_DIR}/*.json"].each { |json_file|
    facts = JSON.parse(File.read(json_file))
    factset.merge!(facts)
  }
end