diff options
author | Paul Berry <paul@puppetlabs.com> | 2010-10-08 15:26:28 -0700 |
---|---|---|
committer | Markus Roberts <Markus@reality.com> | 2010-10-27 14:25:38 -0700 |
commit | 65ef24e5c1c33b7d42012891d368917fd6aaf68c (patch) | |
tree | 6ce4f48fafdb10a59a0626fe8afb54e39f135c73 /lib | |
parent | 3b53bfcd0dd20a43c751b2b0d5dbb0e3463ef47b (diff) | |
download | puppet-65ef24e5c1c33b7d42012891d368917fd6aaf68c.tar.gz puppet-65ef24e5c1c33b7d42012891d368917fd6aaf68c.tar.xz puppet-65ef24e5c1c33b7d42012891d368917fd6aaf68c.zip |
(#4534/#4778) -- Normalize parameterized classes
This is a reconciliation/melding of Paul's
(#4534) Class inheritance with parameterized classes is no longer ignored
and Markus's
Fix for #4778 -- evaluate parameterized classes when they are instantiated
Extracted the code from Resource::Type#mk_plain_resource that evaluates
parents and tags the catalog, and moved that into a new method called
instantiate_resource. Instantiate_resource is now also called from
Parser::Ast::Resource#evaluate, so that the notation
"class { classname: }"
now executes this code too. Likewise adds class evaluation so that it behaves
the same (with regard to lazy / strict evaluation) as
include classname
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/parser/ast/resource.rb | 7 | ||||
-rw-r--r-- | lib/puppet/parser/compiler.rb | 4 | ||||
-rw-r--r-- | lib/puppet/resource/type.rb | 31 |
3 files changed, 24 insertions, 18 deletions
diff --git a/lib/puppet/parser/ast/resource.rb b/lib/puppet/parser/ast/resource.rb index 6909c85c2..b019e6aac 100644 --- a/lib/puppet/parser/ast/resource.rb +++ b/lib/puppet/parser/ast/resource.rb @@ -49,10 +49,11 @@ class Resource < AST::ResourceReference :strict => true ) - # And then store the resource in the compiler. - # At some point, we need to switch all of this to return - # resources instead of storing them like this. + 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 }.reject { |resource| resource.nil? } diff --git a/lib/puppet/parser/compiler.rb b/lib/puppet/parser/compiler.rb index e1227e753..c60e1d4fb 100644 --- a/lib/puppet/parser/compiler.rb +++ b/lib/puppet/parser/compiler.rb @@ -144,7 +144,7 @@ class Puppet::Parser::Compiler if klass = scope.find_hostclass(name) found << name and next if scope.class_scope(klass) - resource = klass.mk_plain_resource(scope) + resource = klass.ensure_in_catalog(scope) # If they've disabled lazy evaluation (which the :include function does), # then evaluate our resource immediately. @@ -220,7 +220,7 @@ class Puppet::Parser::Compiler # Create a resource to model this node, and then add it to the list # of resources. - resource = astnode.mk_plain_resource(topscope) + resource = astnode.ensure_in_catalog(topscope) resource.evaluate diff --git a/lib/puppet/resource/type.rb b/lib/puppet/resource/type.rb index 7b21e55dc..d40adc145 100644 --- a/lib/puppet/resource/type.rb +++ b/lib/puppet/resource/type.rb @@ -138,21 +138,15 @@ class Puppet::Resource::Type end end - # Make an instance of our resource type. This is only possible - # for those classes and nodes that don't have any arguments, and is - # only useful for things like the 'include' function. - def mk_plain_resource(scope) + # Make an instance of the resource type, and place it in the catalog + # if it isn't in the catalog already. This is only possible for + # classes and nodes. No parameters are be supplied--if this is a + # parameterized class, then all parameters take on their default + # values. + def ensure_in_catalog(scope) type == :definition and raise ArgumentError, "Cannot create resources for defined resource types" resource_type = type == :hostclass ? :class : :node - # Make sure our parent class has been evaluated, if we have one. - if parent - parent_resource = scope.catalog.resource(resource_type, parent) - unless parent_resource - parent_type(scope).mk_plain_resource(scope) - end - end - # Do nothing if the resource already exists; this makes sure we don't # get multiple copies of the class resource, which helps provide the # singleton nature of classes. @@ -161,11 +155,22 @@ class Puppet::Resource::Type end resource = Puppet::Parser::Resource.new(resource_type, name, :scope => scope, :source => self) + instantiate_resource(scope, resource) scope.compiler.add_resource(scope, resource) - scope.catalog.tag(*resource.tags) resource end + def instantiate_resource(scope, resource) + # Make sure our parent class has been evaluated, if we have one. + if parent && !scope.catalog.resource(resource.type, parent) + parent_type(scope).ensure_in_catalog(scope) + end + + if ['Class', 'Node'].include? resource.type + scope.catalog.tag(*resource.tags) + end + end + def name return @name unless @name.is_a?(Regexp) @name.source.downcase.gsub(/[^-\w:.]/,'').sub(/^\.+/,'') |