On 02/09/17 13:02, John Baird wrote:
> Henrik,
>
You probably wanted to interpolate #{key} here, not #{filename} since it
will always be nil here.
> # If file is found, return contents of file
> # If file is not found, context.not_found()
> end
>
> def lookup_file(key)
> options = :options
> context = :context
> file_backend(key, :options, :context)
> end
> end
>
That looks like a decent start of that backend. You want to give it a
name that is in your module's namespace.
> I think that's the part I am struggling with... hiera knows about the
> context and options, but my manifest knows (or should know) the proper
> key to look up. How do I combine them so I retrieve the contents of the
> file?
>
You (and the users of your module) interact with the backend via lookup.
The backend function should not be called directly from a manifest -
that is difficult since you have to context in which to do the lookup.
(There are ways, for testing purposes, or when delegating - but that is
not the way it is supposed to be done normally).
Your backend is called from hiera because it is configured to be called
in a hiera.yaml where you also give the path to the root of where the
files are - and you get that in the options parameter (as you already
have figured out).
There are some alternatives how you can support users - which I am
showing below:
(I use a module name of "my" here.)
Alternative 1
---
A user needs to add escapes to all '.' in the filename manually when
looking up. (Try this first to make sure your backend works).
lookup("/tmp/foo'.'bar")
Not so great.
Alternative 2
---
A user calls a function that escapes all '.' in the filename.
lookup(my::escape_dot("/tmp/foo.bar"))
You provide that escape function (see further down)
Alternative 3
---
Make your own lookup wrapping function - tell users to use that as a
convenience.
function my::lookup_file(String $file_name) {
lookup(my::escape_dot($file_name))
}
Users call that instead of lookup()
my::lookup_file('/tmp/foo.bar')
They can also call lookup() directly if they want to use the options
lookup() provides, but then they need to call the my::escape_dot() function.
my::escape_dot function
---
The my::escape_dot is something like:
Puppet::Functions.create_function(:'my::escape_dot') do
dispatch :escape_dot do
param 'String', :key
end
def escape_dot(str)
str.split('.').join("'.'")
end
end
With that escaping in place, your backend function will get the key with
the '.' without the quotes.
There are a couple of corner cases with the quoting function if file
names used as keys can contain single quotes, or if it can contain both
single and double quotes. I hope you do not have to support those cases.
(I suggest you simply document that it is illegal to use file names
containing quotes, and possible check for that and give an error in the
quoting function).
- henrik
> <mailto:
puppet-users%2Bunsu...@googlegroups.com>
> > <mailto:
puppet-users...@googlegroups.com
> <mailto:
puppet-users%2Bunsu...@googlegroups.com>>.
> <mailto:
puppet-users%2Bunsu...@googlegroups.com>.
> To view this discussion on the web visit
>
https://groups.google.com/d/msgid/puppet-users/CACSjNA4Uk7hHP5zbODY4YT9BTboEVuu5WqKSJeAR83sO7ScD6A%40mail.gmail.com
> <
https://groups.google.com/d/msgid/puppet-users/CACSjNA4Uk7hHP5zbODY4YT9BTboEVuu5WqKSJeAR83sO7ScD6A%40mail.gmail.com?utm_medium=email&utm_source=footer>.