Issue #21198 has been updated by Charlie Sharpsteen.

Status changed from Unreviewed to Accepted
Assignee set to Andrew Parker
Affected Puppet version changed from 3.2.1 to 3.0.0

Thanks for reporting this Robert. The documentation [does indeed 
claim](http://docs.puppetlabs.com/hiera/1/puppet.html#special-pseudo-variables) 
that these variables are available in both hiera function lookups and data 
binding lookups. However, as you noted, the code to retrieve them is only 
present in `Hiera::Scope` and missing from `Puppet::DataBinding::Variables`.

A quick patch that copies the relevant bits from `Hiera::Scope` resolves the 
problem:

<pre>
diff --git a/lib/puppet/data_binding.rb b/lib/puppet/data_binding.rb
index ce77a9b..34aa1b6 100644
--- a/lib/puppet/data_binding.rb
+++ b/lib/puppet/data_binding.rb
@@ -14,6 +14,10 @@ class Puppet::DataBinding
   # but it does allow people to use the puppet backend with the hiera
   # data bindings withough causing problems.
   class Variables
+    CALLING_CLASS = "calling_class"
+    CALLING_MODULE = "calling_module"
+    MODULE_NAME = "module_name"
+
     FAKE_RESOURCE = Struct.new(:name).new("fake").freeze
     FAKE_CATALOG = Struct.new(:classes).new([].freeze).freeze
 
@@ -22,7 +26,19 @@ class Puppet::DataBinding
     end
 
     def [](name)
-      @variable_bindings[name]
+      if name == CALLING_CLASS
+        ans = find_hostclass(@variable_bindings)
+      elsif name == CALLING_MODULE
+        ans = @variable_bindings[MODULE_NAME]
+      else
+        ans = @variable_bindings[name]
+      end
+
+      if ans.nil? or ans == ""
+        nil
+      else
+        ans
+      end
     end
 
     def resource
@@ -36,5 +52,16 @@ class Puppet::DataBinding
     def function_include(name)
       # noop
     end
+
+    def find_hostclass(scope)
+      if scope.source and scope.source.type == :hostclass
+        return scope.source.name.downcase
+      elsif scope.parent
+        return find_hostclass(scope.parent)
+      else
+        return nil
+      end
+    end
+    private :find_hostclass
   end
 end
</pre>

However, instead of duplicating code, `Hiera::Scope` and `Puppet::DataBinding` 
should probably be re-factored so that they share the same interface to scope 
variables.

----------------------------------------
Bug #21198: hiera calling_module / calling_class not set for automatic 
parameter lookups
https://projects.puppetlabs.com/issues/21198#change-92838

* Author: Robert Frank
* Status: Accepted
* Priority: Normal
* Assignee: Andrew Parker
* Category: 
* Target version: 
* Affected Puppet version: 3.0.0
* Keywords: 
* Branch: 
----------------------------------------
The automatic parameter lookup uses a different scope (Puppet::DataBinding) 
than the explicit lookup with the hiera function (Hiera::Scope). The 
calling_module and calling_class variables are generated in Hiera::Scope and 
therefore are not available when automatic parameter lookup is used. This 
breaks hiera configurations that make use of those variables for lookups.
Including the following class in a node definition on a puppet master
<pre>
class test($myopt = 'default') {
  notify { "auto myopt: ${myopt}": }
  $myvar = hiera('test::myopt', $myopt)
  notify { "hiera myopt: ${myvar}": }
}
</pre>
with a hiera configuration that uses
<pre>
- %{calling_module}
- common
</pre>
and different configuration values for test::myopt in test.yaml and common.yaml 
prints the value defined in test.yaml for the explicit lookup and the value 
defined in common.yaml for the automatic lookup.



-- 
You have received this notification because you have either subscribed to it, 
or are involved in it.
To change your notification preferences, please click here: 
http://projects.puppetlabs.com/my/account

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Bugs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/puppet-bugs?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to