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.

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.



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

def initialize(catalog, relationship_graph, prioritizer)
  @catalog = catalog
  @relationship_graph = relationship_graph
  @prioritizer = prioritizer
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.



8
9
10
# File 'lib/puppet/transaction/additional_resource_generator.rb', line 8

def relationship_graph=(value)
  @relationship_graph = value
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:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/puppet/transaction/additional_resource_generator.rb', line 44

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
    resource.log_exception(detail, "Failed to generate additional resources using 'eval_generate': #{detail}")
    return false
  end
  add_resources(generated, resource)

  made = Hash[generated.map(&:name).zip(generated)]
  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.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppet/transaction/additional_resource_generator.rb', line 16

def generate_additional_resources(resource)
  return unless resource.respond_to?(:generate)
  begin
    generated = resource.generate
  rescue => detail
    resource.log_exception(detail, "Failed to generate additional resources using 'generate': #{detail}")
  end
  return unless generated
  generated = [generated] unless generated.is_a?(Array)
  generated.collect do |res|
    @catalog.resource(res.ref) || res
  end.reverse_each do |res|
    # This is reversed becuase 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.
    add_resource(res, resource)

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