Class: Puppet::Parser::Relationship

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/parser/relationship.rb

Constant Summary collapse

PARAM_MAP =
{:relationship => :before, :subscription => :notify}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, target, type) ⇒ Relationship

Returns a new instance of Relationship.



25
26
27
# File 'lib/puppet/parser/relationship.rb', line 25

def initialize(source, target, type)
  @source, @target, @type = source, target, type
end

Instance Attribute Details

#sourceObject



2
3
4
# File 'lib/puppet/parser/relationship.rb', line 2

def source
  @source
end

#targetObject



2
3
4
# File 'lib/puppet/parser/relationship.rb', line 2

def target
  @target
end

#typeObject



2
3
4
# File 'lib/puppet/parser/relationship.rb', line 2

def type
  @type
end

Instance Method Details

#arrayify(resources) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/puppet/parser/relationship.rb', line 6

def arrayify(resources)
  case resources
  when Puppet::Pops::Evaluator::Collectors::AbstractCollector
    resources.collected.values
  when Array
    resources
  else
    [resources]
  end
end

#evaluate(catalog) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/puppet/parser/relationship.rb', line 17

def evaluate(catalog)
  arrayify(source).each do |s|
    arrayify(target).each do |t|
      mk_relationship(s, t, catalog)
    end
  end
end

#mk_relationship(source, target, catalog) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppet/parser/relationship.rb', line 33

def mk_relationship(source, target, catalog)
  # There was once an assumption that this could be an array. These raise
  # assertions are here as a sanity check for 4.0 and can be removed after
  # a release or two
  raise ArgumentError, "source shouldn't be an array" if source.is_a?(Array)
  raise ArgumentError, "target shouldn't be an array" if target.is_a?(Array)
  source = source.to_s
  target = target.to_s

  unless source_resource = catalog.resource(source)
    raise ArgumentError, "Could not find resource '#{source}' for relationship on '#{target}'"
  end
  unless catalog.resource(target)
    raise ArgumentError, "Could not find resource '#{target}' for relationship from '#{source}'"
  end
  Puppet.debug {"Adding relationship from #{source} to #{target} with '#{param_name}'"}
  if source_resource[param_name].class != Array
    source_resource[param_name] = [source_resource[param_name]].compact
  end
  source_resource[param_name] << target
end

#param_nameObject



29
30
31
# File 'lib/puppet/parser/relationship.rb', line 29

def param_name
  PARAM_MAP[type] || raise(ArgumentError, "Invalid relationship type #{type}")
end