Please review pull request #48: Allow hierarchy levels to be expanded by array values opened by (hunner)

Description:

Sometimes array values such as $roles will be used in a given hierarchy
level for Hiera. This commit will expand the level containing %{roles}
into a row for each value of $roles in order of the given array.

  • Opened: Mon May 07 22:12:35 UTC 2012
  • Based on: puppetlabs:master (247c03dcf52dcf355a193935f68652dbb931462f)
  • Requested merge: hunner:feature/parse_array_level_values (1ff4f8b962146e8a48b1cb56cc60ec66e09d705f)

Diff follows:

diff --git a/.gitignore b/.gitignore
index 4bb070a..1377554 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1 @@
-pkg
-test.rb
+*.swp
diff --git a/lib/hiera/backend.rb b/lib/hiera/backend.rb
index 0f2f77f..0a0ee7a 100644
--- a/lib/hiera/backend.rb
+++ b/lib/hiera/backend.rb
@@ -66,8 +66,11 @@ def datasources(scope, override=nil, hierarchy=nil)
         hierarchy.insert(0, override) if override
 
         hierarchy.flatten.map do |source|
-          source = parse_string(source, scope)
-          yield(source) unless source == "" or source =~ /(^\/|\/\/|\/$)/
+          if sources = parse_string(source, scope)
+            sources.each do |source|
+              yield(source) unless source == "" or source =~ /(^\/|\/\/|\/$)/
+            end
+          end
         end
       end
 
@@ -86,7 +89,7 @@ def parse_string(data, scope, extra_data={})
         tdata = data.clone
 
         if tdata.is_a?(String)
-          while tdata =~ /%\{(.+?)\}/
+          while Array(tdata).join('|') =~ /%\{([^|]+?)\}/
             var = $1
 
             val = ""
@@ -100,7 +103,13 @@ def parse_string(data, scope, extra_data={})
                 val = extra_data[var]
             end
 
-            tdata.gsub!(/%\{#{var}\}/, val)
+            if tdata.is_a?(Array)
+              tdata = tdata.map { |elem| parse_string(elem, scope, extra_data) }.flatten
+            elsif val.is_a?(Array)
+              tdata = val.map { |elem| tdata.gsub(/%\{#{var}\}/, elem) }
+            else
+              tdata.gsub!(/%\{#{var}\}/, val)
+            end
           end
         end
 
diff --git a/spec/unit/backend_spec.rb b/spec/unit/backend_spec.rb
index 85d1239..9eaeb44 100644
--- a/spec/unit/backend_spec.rb
+++ b/spec/unit/backend_spec.rb
@@ -86,6 +86,19 @@ class Hiera
         Backend.datasources({:rspec => :tests}) { }
       end
 
+      it "should check all levels returned by parse" do
+        input = "%{roles}/%{tags}"
+        scope = {"roles" => ['web','app','db'], 'tags' => ['local','www']}
+        output = ['web/local','web/www','app/local','app/www','db/local','db/www']
+        Config.load({:hierarchy => [input]})
+        Backend.expects(:parse_string).with(input, scope).returns(output)
+        yielded_values = Array.new
+        Backend.datasources(scope) do |backend|
+          yielded_values << backend
+        end
+        yielded_values.should == output
+      end
+
       it "should not return empty sources" do
         Config.load({})
 
@@ -140,6 +153,11 @@ class Hiera
         Backend.parse_string(input, {"rspec" => :undefined}, {"rspec" => "test"}).should == "test_test_test"
       end
 
+      it "should return each possible answer in an array" do
+        input = "%{roles}/%{tags}"
+        scope = {"roles" => ['web','app','db'], 'tags' => ['local','www']}
+        Backend.parse_string(input, scope, {'rspec' => 'fail'}).should == ['web/local','web/www','app/local','app/www','db/local','db/www']
+      end
     end
 
     describe "#parse_answer" do

    

--
You received this message because you are subscribed to the Google Groups "Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected].
For more options, visit this group at http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to