class Puppet::Parser::Relationship
Constants
- PARAM_MAP
Attributes
source[RW]
target[RW]
type[RW]
Public Class Methods
new(source, target, type)
click to toggle source
# File lib/puppet/parser/relationship.rb 26 def initialize(source, target, type) 27 @source, @target, @type = source, target, type 28 end
Public Instance Methods
arrayify(resources, left)
click to toggle source
# File lib/puppet/parser/relationship.rb 6 def arrayify(resources, left) 7 case resources 8 when Puppet::Pops::Evaluator::Collectors::AbstractCollector 9 # on the LHS, go as far left as possible, else whatever the collected result is 10 left ? leftmost_alternative(resources) : resources.collected.values 11 when Array 12 resources 13 else 14 [resources] 15 end 16 end
evaluate(catalog)
click to toggle source
# File lib/puppet/parser/relationship.rb 18 def evaluate(catalog) 19 arrayify(source, true).each do |s| 20 arrayify(target, false).each do |t| 21 mk_relationship(s, t, catalog) 22 end 23 end 24 end
mk_relationship(source, target, catalog)
click to toggle source
# File lib/puppet/parser/relationship.rb 34 def mk_relationship(source, target, catalog) 35 source_ref = canonical_ref(source) 36 target_ref = canonical_ref(target) 37 rel_param = param_name 38 39 source_resource = catalog.resource(*source_ref) 40 unless source_resource 41 raise ArgumentError, _("Could not find resource '%{source}' for relationship on '%{target}'") % { source: source.to_s, target: target.to_s } 42 end 43 unless catalog.resource(*target_ref) 44 raise ArgumentError, _("Could not find resource '%{target}' for relationship from '%{source}'") % { target: target.to_s, source: source.to_s } 45 end 46 Puppet.debug {"Adding relationship from #{source} to #{target} with '#{param_name}'"} 47 if source_resource[rel_param].class != Array 48 source_resource[rel_param] = [source_resource[rel_param]].compact 49 end 50 source_resource[rel_param] << (target_ref[1].nil? ? target_ref[0] : "#{target_ref[0]}[#{target_ref[1]}]") 51 end
param_name()
click to toggle source
# File lib/puppet/parser/relationship.rb 30 def param_name 31 PARAM_MAP[type] || raise(ArgumentError, _("Invalid relationship type %{relationship_type}") % { relationship_type: type }) 32 end
Private Instance Methods
canonical_ref(ref)
click to toggle source
Turns a PResourceType or PClassType into an array [type, title] and all other references to [ref, nil] This is needed since it is not possible to find resources in the catalog based on the type system types :-( (note, the catalog is also used on the agent side)
# File lib/puppet/parser/relationship.rb 74 def canonical_ref(ref) 75 case ref 76 when Puppet::Pops::Types::PResourceType 77 [ref.type_name, ref.title] 78 when Puppet::Pops::Types::PClassType 79 ['class', ref.class_name] 80 else 81 [ref.to_s, nil] 82 end 83 end
leftmost_alternative(x)
click to toggle source
Finds the leftmost alternative for a collector (if it is empty, try its empty alternative recursively until there is either nothing left, or a non empty set is found.
# File lib/puppet/parser/relationship.rb 58 def leftmost_alternative(x) 59 if x.is_a?(Puppet::Pops::Evaluator::Collectors::AbstractCollector) 60 collected = x.collected 61 return collected.values unless collected.empty? 62 adapter = Puppet::Pops::Adapters::EmptyAlternativeAdapter.get(x) 63 adapter.nil? ? [] : leftmost_alternative(adapter.empty_alternative) 64 elsif x.is_a?(Array) && x.size == 1 && x[0].is_a?(Puppet::Pops::Evaluator::Collectors::AbstractCollector) 65 leftmost_alternative(x[0]) 66 else 67 x 68 end 69 end