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

Support array and hash merging from multiple matched keys #4

Closed
wants to merge 3 commits into from
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hiera-regex.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |gem|
gem.name = "hiera-regex"
gem.version = "0.2.1"
gem.version = "0.2.2"
gem.description = "Hiera backend for looking up keys using the power of regular expressions"
gem.summary = "Hiera regex backend"
gem.author = "John Julien"
Expand Down
109 changes: 57 additions & 52 deletions lib/hiera/backend/regex_backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,62 +12,67 @@ def initialize(cache=nil)

# TODO: Support multiple resolution_types, currently only supports :priority
def lookup(key, scope, order_override, resolution_type)
answer = nil
datasourcefiles(scope) do |source|
scope_key = File.basename(source, ".regex")
scope_key = "::#{scope_key}" if ! scope[scope_key]
if ! scope[scope_key]
Hiera.debug("Could not find key #{scope_key} within scope, skipping check for source #{source}")
next
end
Hiera.debug("Checking #{source} for #{scope_key} regex matching #{scope[scope_key]}")
answer = nil
datasourcefiles(scope) do |source|
scope_key = File.basename(source, ".regex")
scope_key = "::#{scope_key}" if ! scope[scope_key]
if ! scope[scope_key]
Hiera.debug("Could not find key #{scope_key} within scope, skipping check for source #{source}")
next
end
Hiera.debug("Checking #{source} for #{scope_key} regex matching #{scope[scope_key]}")

# TODO: Add support for more than just YAML format
data = YAML.load(@cache.read(source))
# TODO: Add support for more than just YAML format
data = YAML.load(@cache.read(source))

next if ! data
next if data.empty?
next if ! data
next if data.empty?

lineno = 0
data.each do |item|
lineno = lineno + 1
item.each_key do |regex_key|
if scope[scope_key] =~ /#{regex_key}/ and item[regex_key][key]
Hiera.debug("#{scope_key} with value of '#{scope[scope_key]}' matched regex /#{regex_key}/ at #{source}:#{lineno}")
answer = Backend.parse_string(item[regex_key][key], scope)
break
end
end
break if answer
end
end
return answer
end

def datasources(scope)
datadir = Backend.datadir(:regex, scope)
Config[:hierarchy].flatten.map do |source|
# We only support data sources that end in a key. Keys found
# in the middle of a datasource's name are impossible to support since
# standard file names do not do well containing regular experssion patterns
if source =~ /\}$/
# We need to strip out the ending key's %{::} characters so that they do not
# get interpolated when we call parse_string
yield(Backend.parse_string(source.gsub(/%\{:*([^\{]*)\}$/, '\1'), scope))
end
end
end
lineno = 0
data.each do |item|
lineno = lineno + 1
item.each_key do |regex_key|
if scope[scope_key] =~ /#{regex_key}/ and item[regex_key][key]
Hiera.debug("#{scope_key} with value of '#{scope[scope_key]}' matched regex /#{regex_key}/ at #{source}:#{lineno}")
new_answer = Backend.parse_answer(item[regex_key][key], scope)
if new_answer.is_a?(Array)
answer << new_answer
elsif new_answer.is_a?(Hash)
answer = Backend.merge_answer(new_answer,answer)
else
answer = new_answer
return answer
end
end
end
end
end
return answer
end

def datasourcefiles(scope)
datadir = Backend.datadir(:regex, scope)
datasources(scope) do |source|
file = Backend.datafile(:regex, scope, source, :regex)
if file
yield(file)
end
end
end
def datasources(scope)
datadir = Backend.datadir(:regex, scope)
Config[:hierarchy].flatten.map do |source|
# We only support data sources that end in a key. Keys found
# in the middle of a datasource's name are impossible to support since
# standard file names do not do well containing regular experssion patterns
if source =~ /\}$/
# We need to strip out the ending key's %{::} characters so that they do not
# get interpolated when we call parse_string
yield(Backend.parse_string(source.gsub(/%\{:*([^\{]*)\}$/, '\1'), scope))
end
end
end

def datasourcefiles(scope)
datadir = Backend.datadir(:regex, scope)
datasources(scope) do |source|
file = Backend.datafile(:regex, scope, source, :regex)
if file
yield(file)
end
end
end
end
end
end
end