summaryrefslogtreecommitdiffstats
path: root/lib/puppet/resource.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/puppet/resource.rb')
-rw-r--r--lib/puppet/resource.rb56
1 files changed, 48 insertions, 8 deletions
diff --git a/lib/puppet/resource.rb b/lib/puppet/resource.rb
index e47501791..010cd956e 100644
--- a/lib/puppet/resource.rb
+++ b/lib/puppet/resource.rb
@@ -1,16 +1,20 @@
require 'puppet'
require 'puppet/util/tagging'
-#require 'puppet/resource/reference'
require 'puppet/util/pson'
# The simplest resource class. Eventually it will function as the
# base class for all resource-like behaviour.
class Puppet::Resource
+ require 'puppet/resource/reference'
include Puppet::Util::Tagging
+
+ require 'puppet/resource/type_collection_helper'
+ include Puppet::Resource::TypeCollectionHelper
+
extend Puppet::Util::Pson
include Enumerable
- attr_accessor :file, :line, :catalog, :exported, :virtual
- attr_writer :type, :title
+ attr_accessor :file, :line, :catalog, :exported, :virtual, :namespace, :validate_parameters
+ attr_writer :type, :title, :environment
require 'puppet/indirector'
extend Puppet::Indirector
@@ -81,6 +85,7 @@ class Puppet::Resource
# Set a given parameter. Converts all passed names
# to lower-case symbols.
def []=(param, value)
+ validate_parameter(param) if validate_parameters
@parameters[parameter_name(param)] = value
end
@@ -117,7 +122,13 @@ class Puppet::Resource
# Create our resource.
def initialize(type, title, attributes = {})
+ # Doing this, instead of including it in the class,
+ # is the only way I could get the load order to work
+ # here.
+ extend Puppet::Node::Environment::Helper
+
@parameters = {}
+ @namespace = ""
(attributes[:parameters] || {}).each do |param, value|
self[param] = value
@@ -139,6 +150,15 @@ class Puppet::Resource
@reference.to_s
end
+ def resource_type
+ case type.to_s.downcase
+ when "class"; find_hostclass
+ when "node"; find_node
+ else
+ find_builtin_resource_type || find_defined_resource_type
+ end
+ end
+
# Get our title information from the reference, since it will canonize it for us.
def title
@reference.title
@@ -246,8 +266,33 @@ class Puppet::Resource
self
end
+ def valid_parameter?(name)
+ resource_type.valid_parameter?(name)
+ end
+
+ def validate_parameter(name)
+ raise ArgumentError, "Invalid parameter #{name}" unless valid_parameter?(name)
+ end
+
private
+ def find_node
+ known_resource_types.node(title)
+ end
+
+ def find_hostclass
+ name = title == :main ? "" : title
+ known_resource_types.find_hostclass(namespace, name)
+ end
+
+ def find_builtin_resource_type
+ Puppet::Type.type(type.to_s.downcase.to_sym)
+ end
+
+ def find_defined_resource_type
+ known_resource_types.find_definition(namespace, type.to_s.downcase)
+ end
+
# Produce a canonical method name.
def parameter_name(param)
param = param.to_s.downcase.to_sym
@@ -267,11 +312,6 @@ class Puppet::Resource
end
end
- # Retrieve the resource type.
- def resource_type
- Puppet::Type.type(type)
- end
-
# Create an old-style TransBucket instance, for non-builtin resource types.
def to_transbucket
bucket = Puppet::TransBucket.new([])