class Puppet::Parser::Collector

An object that collects stored objects from the central cache and returns them to the current host, yo.

Attributes

collected[RW]
equery[RW]
form[RW]
overrides[RW]
resources[RW]
scope[RW]
type[RW]
vquery[RW]

Public Class Methods

new(scope, type, equery, vquery, form) click to toggle source
# File lib/puppet/parser/collector.rb, line 68
def initialize(scope, type, equery, vquery, form)
  @scope  = scope
  @vquery = vquery
  @equery = equery

  # initialisation
  @collected = {}

  # Canonize the type
  @type = Puppet::Resource.new(type, "whatever").type

  unless [:exported, :virtual].include?(form)
    raise ArgumentError, "Invalid query form #{form}"
  end
  @form = form
end

Public Instance Methods

add_override(hash) click to toggle source

add a resource override to the soon to be exported/realized resources

# File lib/puppet/parser/collector.rb, line 86
def add_override(hash)
  raise ArgumentError, "Exported resource try to override without parameters" unless hash[:parameters]

  # schedule an override for an upcoming collection
  @overrides = hash
end
evaluate() click to toggle source

Call the collection method, mark all of the returned objects as non-virtual, optionally applying parameter overrides. The collector can also delete himself from the compiler if there is no more resources to collect (valid only for resource fixed-set collector which get their resources from collect_resources and not from the catalog)

# File lib/puppet/parser/collector.rb, line 12
def evaluate
  # Shortcut if we're not using storeconfigs and they're trying to collect
  # exported resources.
  if form == :exported and Puppet[:storeconfigs] != true
    Puppet.warning "Not collecting exported resources without storeconfigs"
    return false
  end

  if self.resources
    unless objects = collect_resources and ! objects.empty?
      return false
    end
  else
    method = "collect_#{@form.to_s}"
    objects = send(method).each do |obj|
      obj.virtual = false
    end
    return false if objects.empty?
  end

  # we have an override for the collected resources
  if @overrides and !objects.empty?
    # force the resource to be always child of any other resource
    overrides[:source].meta_def(:child_of?) do |klass|
      true
    end

    # tell the compiler we have some override for him unless we already
    # overrided those resources
    objects.each do |res|
      unless @collected.include?(res.ref)
        newres = Puppet::Parser::Resource.
          new(res.type, res.title,
              :parameters => overrides[:parameters],
              :file       => overrides[:file],
              :line       => overrides[:line],
              :source     => overrides[:source],
              :scope      => overrides[:scope])

        scope.compiler.add_override(newres)
      end
    end
  end

  # filter out object that this collector has previously found.
  objects.reject! { |o| @collected.include?(o.ref) }

  return false if objects.empty?

  # keep an eye on the resources we have collected
  objects.inject(@collected) { |c,o| c[o.ref]=o; c }

  # return our newly collected resources
  objects
end