Class: JsonCatalogEncoder

Inherits:
Object show all
Defined in:
lib/puppet/pal/json_catalog_encoder.rb

Overview

The JsonCatalogEncoder is a wrapper around a catalog produced by the Pal::CatalogCompiler.with_json_encoding method. It allows encoding the entire catalog or an individual resource as Rich Data Json.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(catalog, pretty: true, exclude_virtual: true) ⇒ JsonCatalogEncoder

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Do not instantiate this class directly! Use the ‘Pal::CatalogCompiler#with_json_encoding` method instead.

Parameters:

  • catalog (Puppet::Resource::Catalog)

    the internal catalog that this class wraps

  • pretty (Boolean) (defaults to: true)

    (true), if the resulting JSON should be pretty printed or not

  • exclude_virtual (Boolean) (defaults to: true)

    (true), if the resulting catalog should contain unrealzed virtual resources or not



27
28
29
30
31
# File 'lib/puppet/pal/json_catalog_encoder.rb', line 27

def initialize(catalog, pretty: true, exclude_virtual: true)
  @catalog = catalog
  @pretty = pretty
  @exclude_virtual = exclude_virtual
end

Instance Attribute Details

#exclude_virtualObject (readonly)

Should unrealized virtual resources be included in the result or not.



12
13
14
# File 'lib/puppet/pal/json_catalog_encoder.rb', line 12

def exclude_virtual
  @exclude_virtual
end

#prettyObject (readonly)

Is the resulting Json pretty printed or not.



9
10
11
# File 'lib/puppet/pal/json_catalog_encoder.rb', line 9

def pretty
  @pretty
end

Instance Method Details

#encodeObject

Encodes the entire catalog as a rich-data Json catalog.

Returns:

  • String The catalog in Json format using rich data format



37
38
39
# File 'lib/puppet/pal/json_catalog_encoder.rb', line 37

def encode
  possibly_filtered_catalog.to_json(:pretty => pretty)
end

#encode_resource(type, title) ⇒ String

Returns one particular resource as a Json string, or returns nil if resource was not found.

Parameters:

  • type (String)

    the name of the puppet type (case independent)

  • title (String)

    the title of the wanted resource

Returns:

  • (String)

    the resulting Json text

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
# File 'lib/puppet/pal/json_catalog_encoder.rb', line 47

def encode_resource(type, title)
  # Ensure that both type and title are given since the underlying API will do mysterious things
  # if 'title' is nil. (Other assertions are made by the catalog when looking up the resource).
  #
  # TRANSLATORS 'type' and 'title' are internal parameter names - do not translate
  raise ArgumentError, _("Both type and title must be given") if type.nil? or title.nil?
  r = possibly_filtered_catalog.resource(type, title)
  return nil if r.nil?
  r.to_data_hash.to_json(:pretty => pretty)
end