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 27 def initialize(source, target, type) 28 @source, @target, @type = source, target, type 29 end
Public Instance Methods
arrayify(resources, left)
click to toggle source
# File lib/puppet/parser/relationship.rb 7 def arrayify(resources, left) 8 case resources 9 when Puppet::Pops::Evaluator::Collectors::AbstractCollector 10 # on the LHS, go as far left as possible, else whatever the collected result is 11 left ? leftmost_alternative(resources) : resources.collected.values 12 when Array 13 resources 14 else 15 [resources] 16 end 17 end
evaluate(catalog)
click to toggle source
# File lib/puppet/parser/relationship.rb 19 def evaluate(catalog) 20 arrayify(source, true).each do |s| 21 arrayify(target, false).each do |t| 22 mk_relationship(s, t, catalog) 23 end 24 end 25 end
mk_relationship(source, target, catalog)
click to toggle source
# File lib/puppet/parser/relationship.rb 35 def mk_relationship(source, target, catalog) 36 source_ref = canonical_ref(source) 37 target_ref = canonical_ref(target) 38 rel_param = param_name 39 40 source_resource = catalog.resource(*source_ref) 41 unless source_resource 42 raise ArgumentError, _("Could not find resource '%{source}' for relationship on '%{target}'") % { source: source.to_s, target: target.to_s } 43 end 44 unless catalog.resource(*target_ref) 45 raise ArgumentError, _("Could not find resource '%{target}' for relationship from '%{source}'") % { target: target.to_s, source: source.to_s } 46 end 47 Puppet.debug {"Adding relationship from #{source} to #{target} with '#{param_name}'"} 48 if source_resource[rel_param].class != Array 49 source_resource[rel_param] = [source_resource[rel_param]].compact 50 end 51 source_resource[rel_param] << (target_ref[1].nil? ? target_ref[0] : "#{target_ref[0]}[#{target_ref[1]}]") 52 end
param_name()
click to toggle source
# File lib/puppet/parser/relationship.rb 31 def param_name 32 PARAM_MAP[type] || raise(ArgumentError, _("Invalid relationship type %{relationship_type}") % { relationship_type: type }) 33 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 75 def canonical_ref(ref) 76 case ref 77 when Puppet::Pops::Types::PResourceType 78 [ref.type_name, ref.title] 79 when Puppet::Pops::Types::PClassType 80 ['class', ref.class_name] 81 else 82 [ref.to_s, nil] 83 end 84 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 59 def leftmost_alternative(x) 60 if x.is_a?(Puppet::Pops::Evaluator::Collectors::AbstractCollector) 61 collected = x.collected 62 return collected.values unless collected.empty? 63 adapter = Puppet::Pops::Adapters::EmptyAlternativeAdapter.get(x) 64 adapter.nil? ? [] : leftmost_alternative(adapter.empty_alternative) 65 elsif x.is_a?(Array) && x.size == 1 && x[0].is_a?(Puppet::Pops::Evaluator::Collectors::AbstractCollector) 66 leftmost_alternative(x[0]) 67 else 68 x 69 end 70 end