On 2015-21-09 15:10, jcbollinger wrote:


On Friday, September 18, 2015 at 4:36:32 PM UTC-5, Joao Morais wrote:


    Hello list. I am using the hiera() lookup function in order to query
    some few attributes from a http backend. Something like this:

         common.yaml
         ---
         ...
         class::attr: "%{hiera('other::attr')}"

    This works very well if other::attr is defined, but an empty string is
    returned in the lookup above if the other::attr isn't found. The
    hiera()
    function on the manifest fails the compilation if I do the same query,
    which is what I'd expect from the lookup above.



You have a misconception, albeit an understandable one: the Puppet
function hiera() is not the same as the Hiera interpolation function of
the same name. The two perform very similar work, but there is no
inherent reason to suppose that they must operate identically.  Their
interfaces are not even quite the same (at least as far as their
documentation says), for the Puppet function accepts up to three
arguments, whereas the Hiera function documents only one.


    Is there a way to enforce the declaration of the other::attr when using
    hiera() lookup function inside the hieradata?



No such mechanism is documented.  You could file a feature request, but
inasmuch as there may be sites that knowingly or unknowingly depend on
the current behavior, I judge it unlikely that such a feature request
would be accepted.  Perhaps PL would be more receptive if you structured
the request as an extension rather than as a behavior change -- an
alternative lookup function, for example, or an additional argument to
the existing one.


John is correct, there is nothing that states that interpolation should fail if looked up value does not exist. It will get nil/undef which translates into an empty string.

For now, perhaps you could instead put a throwaway call to the Puppet
hiera() function in an appropriate manifest, so as to force compilation
failure /there/ in the event that the wanted key is not defined.


If you are using 3.x with future parser or 4.x other alternatives are available. You can use the lookup() function instead of hiera() as it supports validating the result against a type which could check that the returned value has a particular pattern (i.e. non empty result from the interpolation).

That also works for automatic data binding by declaring the type of the parameter when using 3.x future parser or 4.x.

Otherwise, John's suggestion with an extra lookup of the interpolated key would be a decent workaround in getting an assertion in place.

- henrik

--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

--
You received this message because you are subscribed to the Google Groups "Puppet 
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/mtp5cd%24bqh%241%40ger.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to