Class: OctocatalogDiff::CatalogUtil::ENC::PE

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/catalog-util/enc/pe.rb,
lib/octocatalog-diff/catalog-util/enc/pe/v1.rb

Overview

Support the Puppet Enterprise classification API. Documentation: docs.puppet.com/pe/latest/nc_index.html

Defined Under Namespace

Classes: V1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ PE

Constructor

Parameters:

  • options (Hash)

    Options - must contain the Puppet Enterprise URL and the node

Raises:

  • (ArgumentError)


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

def initialize(options)
  # Make sure the node is in the options
  raise ArgumentError, 'OctocatalogDiff::CatalogUtil::ENC::PE#new requires :node' unless options.key?(:node)
  @node = options[:node]

  # Retrieve the base URL for the Puppet Enterprise ENC service
  raise ArgumentError, 'OctocatalogDiff::CatalogUtil::ENC::PE#new requires :pe_enc_url' unless options.key?(:pe_enc_url)

  # Save options
  @options = options

  # Get the object corresponding to the version of the API in use.
  # (Right now this is hard-coded at V1 because that is the only version there is. In the future
  # if there are different versions, this will need to be parameterized.)
  @api = OctocatalogDiff::CatalogUtil::ENC::PE::V1.new(@options)

  # Initialize the content and error message
  @content = nil
  @error_message = 'The execute method was never run'
end

Instance Attribute Details

#contentObject (readonly)

Allow the main ENC object to retrieve these values



15
16
17
# File 'lib/octocatalog-diff/catalog-util/enc/pe.rb', line 15

def content
  @content
end

#error_messageObject (readonly)

Allow the main ENC object to retrieve these values



15
16
17
# File 'lib/octocatalog-diff/catalog-util/enc/pe.rb', line 15

def error_message
  @error_message
end

Instance Method Details

#execute(logger) ⇒ Object

Executor

Parameters:

  • logger (Logger)

    Logger object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/octocatalog-diff/catalog-util/enc/pe.rb', line 42

def execute(logger)
  logger.debug "Beginning OctocatalogDiff::CatalogUtil::ENC::PE#execute for #{@node}"

  @options[:facts] ||= facts(logger)
  return unless @options[:facts]

  more_options = { headers: @api.headers, timeout: @options[:timeout] || 10 }
  post_hash = @api.body
  url = @api.url
  response = OctocatalogDiff::Util::HTTParty.post(url, @options.merge(more_options), post_hash, 'pe_enc')

  unless response[:code] == 200
    logger.debug "PE ENC failed: #{response.inspect}"
    logger.error "PE ENC failed: Response from #{url} was #{response[:code]}"
    @error_message = "Response from #{url} was #{response[:code]}"
    return
  end

  logger.debug "Response from #{url} was #{response[:code]}"
  unless response[:parsed].is_a?(Hash)
    logger.error "PE ENC failed: Response from #{url} was not a hash! #{response[:parsed].inspect}"
    @error_message = "PE ENC failed: Response from #{url} was not a hash! #{response[:parsed].class}"
    return
  end

  begin
    @content = @api.result(response[:parsed], logger)
    @error_message = nil
  rescue OctocatalogDiff::Errors::PEClassificationError => exc
    @error_message = exc.message
    logger.error "PE ENC failed: #{exc.message}"
    return
  end

  logger.debug "Completed OctocatalogDiff::CatalogUtil::ENC::PE#execute for #{@node}"
end

#facts(logger) ⇒ OctocatalogDiff::Facts

Facts

Parameters:

  • logger (Logger)

    Logger object

Returns:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/octocatalog-diff/catalog-util/enc/pe.rb', line 82

def facts(logger)
  facts_obj = OctocatalogDiff::CatalogUtil::Facts.new(@options, logger)
  logger.debug "Start retrieving facts for #{@node} from #{self.class}"
  begin
    result = facts_obj.facts
    logger.debug "Success retrieving facts for #{@node} from #{self.class}"
  rescue OctocatalogDiff::Errors::FactRetrievalError, OctocatalogDiff::Errors::FactSourceError => exc
    @content = nil
    @error_message = "Fact retrieval failed: #{exc.class} - #{exc.message}"
    logger.error @error_message
    result = nil
  end
  result
end