Class: Puppet::Transaction::AdditionalResourceGenerator Private

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/transaction/additional_resource_generator.rb

Overview

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

Adds additional resources to the catalog and relationship graph that are generated by existing resources. There are two ways that a resource can generate additional resources, either through the #generate method or the #eval_generate method.

API:

  • private

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(catalog, relationship_graph, prioritizer) ⇒ AdditionalResourceGenerator

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.

Returns a new instance of AdditionalResourceGenerator.

API:

  • private



14
15
16
17
18
19
# File 'lib/puppet/transaction/additional_resource_generator.rb', line 14

def initialize(catalog, relationship_graph, prioritizer)
  @catalog = catalog
  @relationship_graph = relationship_graph
  @prioritizer = prioritizer
  @resources_failed_to_generate = false
end

Instance Attribute Details

#relationship_graph=(value) ⇒ Object (writeonly)

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.

API:

  • private



10
11
12
# File 'lib/puppet/transaction/additional_resource_generator.rb', line 10

def relationship_graph=(value)
  @relationship_graph = value
end

#resources_failed_to_generateObject (readonly)

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.

boolean

true if any resource has attempted and failed to generate resources

API:

  • private



12
13
14
# File 'lib/puppet/transaction/additional_resource_generator.rb', line 12

def resources_failed_to_generate
  @resources_failed_to_generate
end

Instance Method Details

#eval_generate(resource) ⇒ Object

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.

Raises:

API:

  • private



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/puppet/transaction/additional_resource_generator.rb', line 56

def eval_generate(resource)
  return false unless resource.respond_to?(:eval_generate)
  raise Puppet::DevError, _("Depthfirst resources are not supported by eval_generate") if resource.depthfirst?

  begin
    generated = replace_duplicates_with_catalog_resources(resource.eval_generate)
    return false if generated.empty?
  rescue => detail
    @resources_failed_to_generate = true
    # TRANSLATORS eval_generate is a method name and should be left untranslated
    resource.log_exception(detail, _("Failed to generate additional resources using 'eval_generate': %{detail}") % { detail: detail })
    return false
  end
  add_resources(generated, resource)

  made = generated.map(&:name).zip(generated).to_h
  contain_generated_resources_in(resource, made)
  connect_resources_to_ancestors(resource, made)

  true
end

#generate_additional_resources(resource) ⇒ Object

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.

API:

  • private



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/puppet/transaction/additional_resource_generator.rb', line 21

def generate_additional_resources(resource)
  return unless resource.respond_to?(:generate)

  begin
    generated = resource.generate
  rescue => detail
    @resources_failed_to_generate = true
    resource.log_exception(detail, _("Failed to generate additional resources using 'generate': %{detail}") % { detail: detail })
  end
  return unless generated

  generated = [generated] unless generated.is_a?(Array)
  generated.collect! do |res|
    @catalog.resource(res.ref) || res
  end
  unless resource.depthfirst?
    # This is reversed because PUP-1963 changed how generated
    # resources were added to the catalog. It exists for backwards
    # compatibility only, and can probably be removed in Puppet 5
    #
    # Previously, resources were given sequential priorities in the
    # relationship graph. Post-1963, resources are added to the
    # catalog one by one adjacent to the parent resource. This
    # causes an implicit reversal of their application order from
    # the old code. The reverse makes it all work like it did.
    generated.reverse!
  end
  generated.each do |res|
    add_resource(res, resource)

    add_generated_directed_dependency(resource, res)
    generate_additional_resources(res)
  end
end