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


29
30
31
32
33
# File 'lib/puppet/pal/json_catalog_encoder.rb', line 29

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.


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

def exclude_virtual
  @exclude_virtual
end

#prettyObject (readonly)

Is the resulting Json pretty printed or not.


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

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


39
40
41
# File 'lib/puppet/pal/json_catalog_encoder.rb', line 39

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)

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

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