1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
|
# This module adds functionality to a resource to make it
# capable of evaluating the DSL resource type block and also
# hooking into the scope system.
require 'puppet/resource/type_collection_helper'
module Puppet::DSL::ResourceAPI
include Puppet::Resource::TypeCollectionHelper
FUNCTION_MAP = {:acquire => :include}
# Try to convert a missing method into a resource type or a function.
def method_missing(name, *args)
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
end
def set_instance_variables
eachparam do |param|
instance_variable_set("@#{param.name}", param.value)
end
end
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 => type, :title => name, :scope => scope)
arguments.each do |param, value|
resource[param] = value
end
scope.compiler.add_resource(scope, resource)
resource
end
end
def call_function(name, args)
return false unless method = Puppet::Parser::Functions.function(name)
scope.send(method, *args)
end
def valid_type?(name)
return true if [:class, :node].include?(name)
return true if Puppet::Type.type(name)
return true if known_resource_types.definition(name)
return false
end
private
def map_function(name)
return FUNCTION_MAP[name] || name
end
end
|