Module: PuppetLanguageServerSidecar::FacterHelper

Defined in:
lib/puppet-languageserver-sidecar/facter_helper.rb

Class Method Summary collapse

Class Method Details

.current_environmentObject



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/puppet-languageserver-sidecar/facter_helper.rb', line 5

def self.current_environment
  begin
    env = Puppet.lookup(:environments).get!(Puppet.settings[:environment])
    return env unless env.nil?
  rescue Puppet::Environments::EnvironmentNotFound
    PuppetLanguageServerSidecar.log_message(:warning, "[FacterHelper::current_environment] Unable to load environment #{Puppet.settings[:environment]}")
  rescue StandardError => e
    PuppetLanguageServerSidecar.log_message(:warning, "[FacterHelper::current_environment] Error loading environment #{Puppet.settings[:environment]}: #{e}")
  end
  Puppet.lookup(:current_environment)
end

.retrieve_facts(_cache, _options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet-languageserver-sidecar/facter_helper.rb', line 17

def self.retrieve_facts(_cache, _options = {})
  require 'puppet/indirector/facts/facter'

  PuppetLanguageServerSidecar.log_message(:debug, '[FacterHelper::retrieve_facts] Starting')
  facts = PuppetLanguageServer::Sidecar::Protocol::FactList.new
  begin
    req = Puppet::Indirector::Request.new(:facts, :find, 'language_server', nil, environment: current_environment)
    result = Puppet::Node::Facts::Facter.new.find(req)
    result.values.each do |key, value|
      # TODO: This isn't strictly correct e.g. fully qualified facts will look a bit odd.
      # Consider a fact called foo.bar.baz = 'Hello'.  Even though the fact name is `foo.bar.baz`
      # it will appear in the facts object as `facts['foo'] = { 'bar' => { 'baz' => 'Hello' }}`
      facts << PuppetLanguageServer::Sidecar::Protocol::Fact.new.from_h!('key' => key, 'value' => value)
    end
  rescue StandardError => e
    PuppetLanguageServerSidecar.log_message(:error, "[FacterHelper::_load_facts] Error loading facts #{e.message} #{e.backtrace}")
  rescue LoadError => e
    PuppetLanguageServerSidecar.log_message(:error, "[FacterHelper::_load_facts] Error loading facts (LoadError) #{e.message} #{e.backtrace}")
  end

  PuppetLanguageServerSidecar.log_message(:debug, "[FacterHelper::retrieve_facts] Finished loading #{facts.count} facts")
  facts
end