Class: ChefHandlerForeman::ForemanEncFetcher

Inherits:
Chef::EventDispatch::Base
  • Object
show all
Defined in:
lib/chef_handler_foreman/foreman_enc_fetcher.rb

Constant Summary collapse

SUPPORTED_LEVELS =
%w(default force_default normal override force_override automatic)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level) ⇒ ForemanEncFetcher

Returns a new instance of ForemanEncFetcher.



9
10
11
12
13
# File 'lib/chef_handler_foreman/foreman_enc_fetcher.rb', line 9

def initialize(level)
  super()
  raise "Unsupported node attributes level #{level}, use one of #{SUPPORTED_LEVELS.join(', ')}" unless SUPPORTED_LEVELS.include?(level)
  @attributes_level = level
end

Instance Attribute Details

#uploaderObject

Returns the value of attribute uploader.



7
8
9
# File 'lib/chef_handler_foreman/foreman_enc_fetcher.rb', line 7

def uploader
  @uploader
end

Instance Method Details

#node_load_completed(node) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chef_handler_foreman/foreman_enc_fetcher.rb', line 15

def node_load_completed(node)
  client_name = node.name
  result = Chef::Config.foreman_uploader.foreman_request("/api/enc/#{client_name}", client_name, client_name, 'get')
  begin
    enc = JSON.parse(result.body)
  rescue => e
    Chef::Log.error "Foreman ENC could not be fetched because of #{e.class}: #{e.message}"
  return false
  end

  enc['parameters'].each do |parameter, value|
    nested_parts = parameter.split('::')
    nest = nested_parts[0..-2].inject(node.send(@attributes_level)) { |attributes_nest, attribute| attributes_nest[attribute] }
    nest[nested_parts[-1]] = type_cast(value)
  end
end

#type_cast(value) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/chef_handler_foreman/foreman_enc_fetcher.rb', line 32

def type_cast(value)
  case value
  when 'false'
    false
  when 'true'
    true
  else
    value
  end
end