Skip to content

Commit c35e770

Browse files
committed
Attempt to parse json hash or array when called with hiera()
Attempt to parse a string as json when called by hiera() to improve hiera-etcd compatibility with yaml backend. Fall back to native string if the string does not contain json. Use case: It should be possible to use both etcd and yaml backends with a given puppet repo and the hiera() function to return a complex data structure. * hiera() returns first match and can be an array, hash, or string * hiera_array() does a concat and may not be desired * hiera_hash() does a deep merge and may not be desired
1 parent b92c42f commit c35e770

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

lib/hiera/backend/etcd_backend.rb

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def parse_result(res, type, scope)
4242
answer = nil
4343
case type
4444
when :array
45+
# Called with hiera_array().
4546
answer ||= []
4647
begin
4748
data = Backend.parse_answer(JSON[res], scope)
@@ -56,6 +57,7 @@ def parse_result(res, type, scope)
5657
Hiera.warn("Data is in json format, but this is not an array")
5758
end
5859
when :hash
60+
# Called with hiera_hash().
5961
answer ||= {}
6062
begin
6163
data = Backend.parse_answer(JSON[res], scope)
@@ -69,6 +71,8 @@ def parse_result(res, type, scope)
6971
Hiera.warn("Data is in json format, but this is not an hash")
7072
end
7173
else
74+
# Called with hiera(), which can return an array, hash, or string.
75+
res = JSON[res] rescue res
7276
answer = Backend.parse_answer(res, scope)
7377
end
7478
answer

0 commit comments

Comments
 (0)