class Puppet::Parser::Resource

The primary difference between this class and its parent is that this class has rules on who can set parameters

Attributes

catalog[RW]
collector_id[RW]
evaluated[RW]
exported[R]
file[RW]
line[RW]
override[RW]
parameters[R]
scope[RW]
source[RW]
translated[RW]
virtual[RW]

Public Class Methods

new(*args) click to toggle source
Calls superclass method ::new
# File lib/puppet/parser/resource.rb, line 117
def initialize(*args)
  raise ArgumentError, "Resources require a hash as last argument" unless args.last.is_a? Hash
  raise ArgumentError, "Resources require a scope" unless args.last[:scope]
  super

  @source ||= scope.source
end
relationship_parameter?(name) click to toggle source

Determine whether the provided parameter name is a relationship parameter.

# File lib/puppet/parser/resource.rb, line 31
def self.relationship_parameter?(name)
  @relationship_names ||= Puppet::Type.relationship_params.collect { |p| p.name }
  @relationship_names.include?(name)
end

Public Instance Methods

[](param) click to toggle source
# File lib/puppet/parser/resource.rb, line 41
def [](param)
  param = param.intern
  if param == :title
    return self.title
  end
  if @parameters.has_key?(param)
    @parameters[param].value
  else
    nil
  end
end
[]=(param, value = nil)
Alias for: set_parameter
add_edge_to_stage() click to toggle source

Process the stage metaparameter for a class. A containment edge is drawn from the class to the stage. The stage for containment defaults to main, if none is specified.

# File lib/puppet/parser/resource.rb, line 64
def add_edge_to_stage
  return unless self.class?

  unless stage = catalog.resource(:stage, self[:stage] || (scope && scope.resource && scope.resource[:stage]) || :main)
    raise ArgumentError, "Could not find stage #{self[:stage] || :main} specified by #{self}"
  end

  self[:stage] ||= stage.title unless stage.title == :main
  catalog.add_edge(stage, self)
end
eachparam() { |param| ... } click to toggle source
# File lib/puppet/parser/resource.rb, line 53
def eachparam
  @parameters.each do |name, param|
    yield param
  end
end
evaluate() click to toggle source

Retrieve the associated definition and evaluate it.

# File lib/puppet/parser/resource.rb, line 76
def evaluate
  return if evaluated?
  @evaluated = true
  if klass = resource_type and ! builtin_type?
    finish
    evaluated_code = klass.evaluate_code(self)

    return evaluated_code
  elsif builtin?
    devfail "Cannot evaluate a builtin type (#{type})"
  else
    self.fail "Cannot find definition #{type}"
  end
end
evaluated?() click to toggle source
# File lib/puppet/parser/resource.rb, line 39
def evaluated?;  !!@evaluated;  end
exported=(value) click to toggle source

Mark this resource as both exported and virtual, or remove the exported mark.

# File lib/puppet/parser/resource.rb, line 93
def exported=(value)
  if value
    @virtual = true
    @exported = value
  else
    @exported = value
  end
end
finish() click to toggle source

Do any finishing work on this object, called before evaluation or before storage/translation.

# File lib/puppet/parser/resource.rb, line 104
def finish
  return if finished?
  @finished = true
  add_defaults
  add_scope_tags
  validate
end
finished?() click to toggle source

Has this resource already been finished?

# File lib/puppet/parser/resource.rb, line 113
def finished?
  @finished
end
isomorphic?() click to toggle source

Is this resource modeling an isomorphic resource type?

# File lib/puppet/parser/resource.rb, line 126
def isomorphic?
  if builtin_type?
    return resource_type.isomorphic?
  else
    return true
  end
end
merge(resource) click to toggle source

Merge an override resource in. This will throw exceptions if any overrides aren’t allowed.

# File lib/puppet/parser/resource.rb, line 136
def merge(resource)
  # Test the resource scope, to make sure the resource is even allowed
  # to override.
  unless self.source.object_id == resource.source.object_id || resource.source.child_of?(self.source)
    raise Puppet::ParseError.new("Only subclasses can override parameters", resource.line, resource.file)
  end
  # Some of these might fail, but they'll fail in the way we want.
  resource.parameters.each do |name, param|
    override_parameter(param)
  end
end
metaparam_compatibility_mode?() click to toggle source

This only mattered for clients < 0.25, which we don’t support any longer. …but, since this hasn’t been deprecated, and at least some functions used it, deprecate now rather than just eliminate. –daniel 2012-07-15

# File lib/puppet/parser/resource.rb, line 151
def metaparam_compatibility_mode?
  Puppet.deprecation_warning "metaparam_compatibility_mode? is obsolete since < 0.25 clients are really, really not supported any more"
  false
end
name() click to toggle source
# File lib/puppet/parser/resource.rb, line 156
def name
  self[:name] || self.title
end
override?() click to toggle source
# File lib/puppet/parser/resource.rb, line 38
def override?;   !!@override;   end
set_parameter(param, value = nil) click to toggle source

Define a parameter in our resource. if we ever receive a parameter named ‘tag’, set the resource tags with its value.

# File lib/puppet/parser/resource.rb, line 166
def set_parameter(param, value = nil)
  if ! value.nil?
    param = Puppet::Parser::Resource::Param.new(
      :name => param, :value => value, :source => self.source
    )
  elsif ! param.is_a?(Puppet::Parser::Resource::Param)
    raise ArgumentError, "Received incomplete information - no value provided for parameter #{param}"
  end

  tag(*param.value) if param.name == :tag

  # And store it in our parameter hash.
  @parameters[param.name] = param
end
Also aliased as: []=
to_hash() click to toggle source
# File lib/puppet/parser/resource.rb, line 182
def to_hash
  @parameters.inject({}) do |hash, ary|
    param = ary[1]
    # Skip "undef" values.
    hash[param.name] = param.value if param.value != :undef
    hash
  end
end
to_resource() click to toggle source

Create a Puppet::Resource instance from this parser resource. We plan, at some point, on not needing to do this conversion, but it’s sufficient for now.

# File lib/puppet/parser/resource.rb, line 195
def to_resource
  result = Puppet::Resource.new(type, title)

  to_hash.each do |p, v|
    if v.is_a?(Puppet::Resource)
      v = Puppet::Resource.new(v.type, v.title)
    elsif v.is_a?(Array)
      # flatten resource references arrays
      v = v.flatten if v.flatten.find { |av| av.is_a?(Puppet::Resource) }
      v = v.collect do |av|
        av = Puppet::Resource.new(av.type, av.title) if av.is_a?(Puppet::Resource)
        av
      end
    end

    # If the value is an array with only one value, then
    # convert it to a single value.  This is largely so that
    # the database interaction doesn't have to worry about
    # whether it returns an array or a string.
    result[p] = if v.is_a?(Array) and v.length == 1
                  v[0]
                else
                  v
                end
  end

  result.file = self.file
  result.line = self.line
  result.exported = self.exported
  result.virtual = self.virtual
  result.tag(*self.tags)

  result
end
translated?() click to toggle source

Set up some boolean test methods

# File lib/puppet/parser/resource.rb, line 37
def translated?; !!@translated; end