Class: Puppet::Parser::AST::Resource

Inherits:
Branch show all
Defined in:
lib/puppet/parser/ast/resource.rb

Overview

Instruction for Resource instantiation. Instantiates resources of both native and user defined types.

Constant Summary

Constants inherited from Puppet::Parser::AST

AST

Instance Attribute Summary collapse

Attributes inherited from Branch

#children, #pin

Attributes inherited from Puppet::Parser::AST

#file, #line, #parent, #pos, #scope

Instance Method Summary collapse

Methods inherited from Branch

#each

Methods inherited from Puppet::Parser::AST

#inspect, #safeevaluate

Methods included from Util::Errors

#adderrorcontext, #devfail, #error_context, error_location, error_location_with_space, error_location_with_unknowns, #exceptwrap, #fail

Constructor Details

#initialize(argshash) ⇒ Resource

Returns a new instance of Resource.



9
10
11
12
# File 'lib/puppet/parser/ast/resource.rb', line 9

def initialize(argshash)
  Puppet.warn_once('deprecations', 'AST::Resource', _('Use of Puppet::Parser::AST::Resource is deprecated and not fully functional'))
  super(argshash)
end

Instance Attribute Details

#exportedObject

Returns the value of attribute exported.



7
8
9
# File 'lib/puppet/parser/ast/resource.rb', line 7

def exported
  @exported
end

#instancesObject

Returns the value of attribute instances.



7
8
9
# File 'lib/puppet/parser/ast/resource.rb', line 7

def instances
  @instances
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/puppet/parser/ast/resource.rb', line 7

def type
  @type
end

#virtualObject

Returns the value of attribute virtual.



7
8
9
# File 'lib/puppet/parser/ast/resource.rb', line 7

def virtual
  @virtual
end

Instance Method Details

#evaluate(scope) ⇒ Object

Evaluates resources by adding them to the compiler for lazy evaluation and returning the produced resource references.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/puppet/parser/ast/resource.rb', line 17

def evaluate(scope)
  # We want virtual to be true if exported is true.  We can't
  # just set :virtual => self.virtual in the initialization,
  # because sometimes the :virtual attribute is set *after*
  # :exported, in which case it clobbers :exported if :exported
  # is true.  Argh, this was a very tough one to track down.
  virt = virtual || exported

  # First level of implicit iteration: build a resource for each
  # instance.  This handles things like:
  # file { '/foo': owner => blah; '/bar': owner => blah }
  @instances.map do |instance|
    # Evaluate all of the specified params.
    paramobjects = instance.parameters.map { |param| param.safeevaluate(scope) }

    resource_titles = instance.title.safeevaluate(scope)

    # it's easier to always use an array, even for only one name
    resource_titles = [resource_titles] unless resource_titles.is_a?(Array)

    fully_qualified_type, resource_titles = scope.resolve_type_and_titles(type, resource_titles)

    # Second level of implicit iteration; build a resource for each
    # title.  This handles things like:
    # file { ['/foo', '/bar']: owner => blah }
    resource_titles.flatten.map do |resource_title|
      exceptwrap :type => Puppet::ParseError do
        resource = Puppet::Parser::Resource.new(
          fully_qualified_type, resource_title,
          :parameters => paramobjects,
          :file => file,
          :line => line,
          :exported => exported,
          :virtual => virt,
          :source => scope.source,
          :scope => scope,
          :strict => true
        )

        if resource.resource_type.is_a? Puppet::Resource::Type
          resource.resource_type.instantiate_resource(scope, resource)
        end
        scope.compiler.add_resource(scope, resource)
        scope.compiler.evaluate_classes([resource_title], scope, false) if fully_qualified_type == 'class'
        resource
      end
    end
  end.flatten.compact
end