Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#127) Allow escaping %{...} in hiera data so that hiera does not try to... #184

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/hiera/interpolate.rb
Expand Up @@ -3,15 +3,34 @@

class Hiera::Interpolate
class << self
INTERPOLATION = /%\{([^\}]*)\}/
INTERPOLATION = /%+\{([^\}]*)\}/
METHOD_INTERPOLATION = /%\{(scope|hiera)\(['"]([^"']*)["']\)\}/

def interpolate(data, scope, extra_data)
if data.is_a?(String)
# Wrapping do_interpolation in a gsub block ensures we process
# each interpolation site in isolation using separate recursion guards.
data.gsub(INTERPOLATION) do |match|
do_interpolation(match, Hiera::RecursiveGuard.new, scope, extra_data)
#Allow escaping of %{} to support litteral %{some_text} in hiera data.
#Should support escaping of escapes so full list of scenarios looks like:
# %{var} : 'value of var'
# %%{literal} : '%{literal}'
# %%%{var} : '%value of var'
# %%%%{literal} : '%%{literal}'

percents = match.match(/^(%+)/)[1]
if percents.length.even?
match.gsub(/%%/,'%')
else
#Remove a % that represents the interpolation from percents
#Of the remaining %'s sub each '%%' for '%' ('%%' is an escaped '%')
#Remove all but one % from the match to feed into do_intepolation
#add the percents (the escaped % signs back on so %%%{var} becomes %value
percents.chop!
percents.gsub!(/%%/,'%')
match.sub!(/^%+/,'%')
percents + do_interpolation(match, Hiera::RecursiveGuard.new, scope, extra_data).to_s
end
end
else
data
Expand Down
22 changes: 18 additions & 4 deletions spec/unit/backend_spec.rb
Expand Up @@ -5,11 +5,25 @@ class Hiera
describe Backend do
describe "#datadir" do
it "interpolates any values in the configured value" do
Config.load({:rspec => {:datadir => "/tmp/%{interpolate}"}})

dir = Backend.datadir(:rspec, { "interpolate" => "my_data" })
interpolation_tests = {
:test1 => '%{interpolate}',
:test2 => '%%{interpolate}',
:test3 => '%%%{interpolate}',
:test4 => '%%%%{interpolate}',
}

results = {}
interpolation_tests.each do |test,value|
puts "testing #{test} #{value}"
Config.load({:rspec => {:datadir => "/tmp/#{value}"}})
results[test] = Backend.datadir(:rspec, { "interpolate" => "variable" })
end

dir.should == "/tmp/my_data"
puts "Results: #{results}"
results[:test1].should == "/tmp/variable"
results[:test2].should == "/tmp/%{interpolate}"
results[:test3].should == "/tmp/%variable"
results[:test4].should == "/tmp/%%{interpolate}"
end

it "defaults to a directory in var" do
Expand Down