Class: Puppet::Pal::JsonCatalogEncoder

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

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



31
32
33
34
35
# File 'lib/puppet/pal/json_catalog_encoder.rb', line 31

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.



16
17
18
# File 'lib/puppet/pal/json_catalog_encoder.rb', line 16

def exclude_virtual
  @exclude_virtual
end

#prettyObject (readonly)

Is the resulting Json pretty printed or not.



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

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



41
42
43
# File 'lib/puppet/pal/json_catalog_encoder.rb', line 41

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)


51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/puppet/pal/json_catalog_encoder.rb', line 51

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