diff options
author | Luke Kanies <luke@reductivelabs.com> | 2010-01-22 00:48:37 -0800 |
---|---|---|
committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
commit | 2fa0a489e26fc2512783c67b1b4579a03f8a20a6 (patch) | |
tree | 0c0157fe3e854fc5fbfa1bd94fc47c528dffef27 /lib/puppet | |
parent | aff59926bb8c8e7a136d6e87359e9857a4512da9 (diff) | |
download | puppet-2fa0a489e26fc2512783c67b1b4579a03f8a20a6.tar.gz puppet-2fa0a489e26fc2512783c67b1b4579a03f8a20a6.tar.xz puppet-2fa0a489e26fc2512783c67b1b4579a03f8a20a6.zip |
Adding parameter validation to Puppet::Resource
This will allow us to remove all of the parameter
validation from the other Resource classes.
This is possible because resource types defined
in the language are visible outside of the parser,
via the environment.
This will enable lots of code removal and simplication.
Signed-off-by: Luke Kanies <luke@reductivelabs.com>
Diffstat (limited to 'lib/puppet')
-rw-r--r-- | lib/puppet/parser/resource.rb | 2 | ||||
-rw-r--r-- | lib/puppet/provider/ldap.rb | 2 | ||||
-rw-r--r-- | lib/puppet/provider/nameservice.rb | 2 | ||||
-rw-r--r-- | lib/puppet/resource.rb | 56 | ||||
-rw-r--r-- | lib/puppet/resource/type.rb | 4 | ||||
-rw-r--r-- | lib/puppet/transportable.rb | 2 | ||||
-rw-r--r-- | lib/puppet/type.rb | 5 | ||||
-rw-r--r-- | lib/puppet/type/component.rb | 4 |
8 files changed, 61 insertions, 16 deletions
diff --git a/lib/puppet/parser/resource.rb b/lib/puppet/parser/resource.rb index 63d028c0c..428b9df50 100644 --- a/lib/puppet/parser/resource.rb +++ b/lib/puppet/parser/resource.rb @@ -396,7 +396,7 @@ class Puppet::Parser::Resource # Now make sure it's a valid argument to our class. These checks # are organized in order of commonhood -- most types, it's a valid # argument and paramcheck is enabled. - if @ref.typeclass.validattr?(param) + if @ref.typeclass.valid_parameter?(param) true elsif %w{name title}.include?(param) # always allow these true diff --git a/lib/puppet/provider/ldap.rb b/lib/puppet/provider/ldap.rb index be6683891..38668e5e5 100644 --- a/lib/puppet/provider/ldap.rb +++ b/lib/puppet/provider/ldap.rb @@ -78,7 +78,7 @@ class Puppet::Provider::Ldap < Puppet::Provider param, values = ary # Skip any attributes we don't manage. - next result unless self.class.resource_type.validattr?(param) + next result unless self.class.resource_type.valid_parameter?(param) paramclass = self.class.resource_type.attrclass(param) diff --git a/lib/puppet/provider/nameservice.rb b/lib/puppet/provider/nameservice.rb index cc517ee5f..57441ddf6 100644 --- a/lib/puppet/provider/nameservice.rb +++ b/lib/puppet/provider/nameservice.rb @@ -44,7 +44,7 @@ class Puppet::Provider::NameService < Puppet::Provider end def options(name, hash) - unless resource_type.validattr?(name) + unless resource_type.valid_parameter?(name) raise Puppet::DevError, "%s is not a valid attribute for %s" % [name, resource_type.name] end 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([]) diff --git a/lib/puppet/resource/type.rb b/lib/puppet/resource/type.rb index 9baf1983e..d47658284 100644 --- a/lib/puppet/resource/type.rb +++ b/lib/puppet/resource/type.rb @@ -144,7 +144,7 @@ class Puppet::Resource::Type set = {} resource.to_hash.each do |param, value| param = param.to_sym - fail Puppet::ParseError, "#{resource.ref} does not accept attribute #{param}" unless validattr?(param) + fail Puppet::ParseError, "#{resource.ref} does not accept attribute #{param}" unless valid_parameter?(param) exceptwrap { scope.setvar(param.to_s, value) } @@ -174,7 +174,7 @@ class Puppet::Resource::Type end # Check whether a given argument is valid. - def validattr?(param) + def valid_parameter?(param) param = param.to_s return true if param == "name" diff --git a/lib/puppet/transportable.rb b/lib/puppet/transportable.rb index 68977dca0..1970d9f1e 100644 --- a/lib/puppet/transportable.rb +++ b/lib/puppet/transportable.rb @@ -49,7 +49,7 @@ module Puppet def to_component trans = TransObject.new(ref, :component) @params.each { |param,value| - next unless Puppet::Type::Component.validattr?(param) + next unless Puppet::Type::Component.valid_parameter?(param) Puppet.debug "Defining %s on %s" % [param, ref] trans[param] = value } diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb index 2fb4abca8..31728c374 100644 --- a/lib/puppet/type.rb +++ b/lib/puppet/type.rb @@ -388,6 +388,11 @@ class Type end end + # This is a forward-compatibility method - it's the validity interface we'll use in Puppet::Resource. + def self.valid_parameter?(name) + validattr?(name) + end + # Return either the attribute alias or the attribute. def attr_alias(name) name = symbolize(name) diff --git a/lib/puppet/type/component.rb b/lib/puppet/type/component.rb index 5fed1760e..bf9007ab4 100644 --- a/lib/puppet/type/component.rb +++ b/lib/puppet/type/component.rb @@ -14,14 +14,14 @@ Puppet::Type.newtype(:component) do # Override how parameters are handled so that we support the extra # parameters that are used with defined resource types. def [](param) - return super if self.class.validattr?(param) + return super if self.class.valid_parameter?(param) @extra_parameters[param.to_sym] end # Override how parameters are handled so that we support the extra # parameters that are used with defined resource types. def []=(param, value) - return super if self.class.validattr?(param) + return super if self.class.valid_parameter?(param) @extra_parameters[param.to_sym] = value end |