Class: OctocatalogDiff::Catalog::JSON

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/catalog/json.rb

Overview

Represents a Puppet catalog that is read in directly from a JSON file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ JSON

Constructor

Parameters:

  • :json (String)

    REQUIRED: Content of catalog, will be parsed as JSON

  • :node (String)

    Node name (if not supplied, will be determined from catalog)

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/octocatalog-diff/catalog/json.rb', line 15

def initialize(options)
  raise ArgumentError, 'Usage: OctocatalogDiff::Catalog::JSON.initialize(options_hash)' unless options.is_a?(Hash)
  raise ArgumentError, "Must supply :json as string in options: #{options[:json].class}" unless options[:json].is_a?(String)
  @catalog_json = options[:json]
  begin
    @catalog = ::JSON.parse(@catalog_json)
    @error_message = nil
    @node ||= @catalog['name'] if @catalog.key?('name') # Puppet 4.x
    @node ||= @catalog['data']['name'] if @catalog.key?('data') && @catalog['data'].is_a?(Hash) # Puppet 3.x
  rescue ::JSON::ParserError => exc
    @error_message = "Catalog JSON input failed to parse: #{exc.message}"
    @catalog = nil
    @catalog_json = nil
  end
end

Instance Attribute Details

#catalogObject (readonly)

Returns the value of attribute catalog.



10
11
12
# File 'lib/octocatalog-diff/catalog/json.rb', line 10

def catalog
  @catalog
end

#catalog_jsonObject (readonly)

Returns the value of attribute catalog_json.



10
11
12
# File 'lib/octocatalog-diff/catalog/json.rb', line 10

def catalog_json
  @catalog_json
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



10
11
12
# File 'lib/octocatalog-diff/catalog/json.rb', line 10

def error_message
  @error_message
end

#nodeObject

Returns the value of attribute node.



9
10
11
# File 'lib/octocatalog-diff/catalog/json.rb', line 9

def node
  @node
end