Class: Puppet::DSL::ResourceAPI

Inherits:
Object
  • Object
show all
Includes:
Resource::TypeCollectionHelper
Defined in:
lib/vendor/puppet/dsl/resource_api.rb

Constant Summary collapse

FUNCTION_MAP =
{:acquire => :include}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Resource::TypeCollectionHelper

#known_resource_types

Constructor Details

#initialize(resource, scope, block) ⇒ ResourceAPI

Returns a new instance of ResourceAPI.



22
23
24
25
26
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 22

def initialize(resource, scope, block)
  @scope = scope
  @resource = resource
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Try to convert a missing method into a resource type or a function.



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 29

def method_missing(name, *args)
  raise "MethodMissing loop when searching for #{name} with #{args.inspect}" if searching_for_method?
  @searching_for_method = true
  return create_resource(name, args[0], args[1]) if valid_type?(name)

  name = map_function(name)

  return call_function(name, args) if Puppet::Parser::Functions.function(name)

  super
ensure
  @searching_for_method = false
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



11
12
13
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 11

def block
  @block
end

#resourceObject (readonly)

Returns the value of attribute resource.



11
12
13
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 11

def resource
  @resource
end

#scopeObject (readonly)

Returns the value of attribute scope.



11
12
13
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 11

def scope
  @scope
end

Instance Method Details

#call_function(name, args) ⇒ Object



70
71
72
73
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 70

def call_function(name, args)
  return false unless method = Puppet::Parser::Functions.function(name)
  scope.send(method, *args)
end

#create_resource(type, names, arguments = nil) ⇒ Object

Raises:

  • (ArgumentError)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 51

def create_resource(type, names, arguments = nil)
  names = [names] unless names.is_a?(Array)

  arguments ||= {}
  raise ArgumentError, "Resource arguments must be provided as a hash" unless arguments.is_a?(Hash)

  names.collect do |name|
    resource = Puppet::Parser::Resource.new(type, name, :scope => scope)
    arguments.each do |param, value|
      resource[param] = value
    end

    resource.exported = true if exporting?
    resource.virtual = true if virtualizing?
    scope.compiler.add_resource(scope, resource)
    resource
  end
end

#environmentObject



13
14
15
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 13

def environment
  scope.environment
end

#evaluateObject



17
18
19
20
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 17

def evaluate
  set_instance_variables
  instance_eval(&block)
end

#export(resources = nil, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 75

def export(resources = nil, &block)
  if resources
    resources.each { |resource| resource.exported = true }
    return resources
  end
  @exporting = true
  instance_eval(&block)
ensure
  @exporting = false
end

#set_instance_variablesObject



43
44
45
46
47
48
49
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 43

def set_instance_variables
  resource.eachparam do |param|
    instance_variable_set("@#{param.name}", param.value)
  end
  @title = resource.title
  @name ||= resource.title
end

#valid_type?(name) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
101
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 97

def valid_type?(name)
  return true if [:class, :node].include?(name)
  return true if Puppet::Type.type(name)
  return(known_resource_types.definition(name) ? true : false)
end

#virtual(resources = nil, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/vendor/puppet/dsl/resource_api.rb', line 86

def virtual(resources = nil, &block)
  if resources
    resources.each { |resource| resource.virtual = true }
    return resources
  end
  @virtualizing = true
  instance_eval(&block)
ensure
  @virtualizing = false
end