Please review pull request #46: (#14256) Adding save API opened by (kelseyhightower)

Description:

This patch adds a new API for saving data by delegating to a specific
backend. The backend is responsible for saving data based on a key,
value, and source. The backend must return a status response of true
or false depending on the success or failure of the save operation.

Example:

If we have the following Hiera configuration:

:hierarchy:
- %{environment}
- common

:backends:
- backend_with_save_support

We can save data to the backend_with_save_support like this:

require 'hiera'

hiera = Hiera.new
status = hiera.save('key, 'value', 'backend_with_save_support', 'common')

This patch includes tests.

  • Opened: Tue May 01 16:23:57 UTC 2012
  • Based on: puppetlabs:master (6bd7122cf4f6d3a97168acc0adf7f0613ab7b8a5)
  • Requested merge: kelseyhightower:feature/master/14256_add_save_api (77b0ca4faff09801e3b6ee046b06a94eb07c1a1d)

Diff follows:

diff --git a/lib/hiera.rb b/lib/hiera.rb
index 0cd5d0e..b990ea8 100644
--- a/lib/hiera.rb
+++ b/lib/hiera.rb
@@ -64,4 +64,25 @@ def initialize(options={})
   def lookup(key, default, scope, order_override=nil, resolution_type=:priority)
     Backend.lookup(key, default, scope, order_override, resolution_type)
   end
+
+  # Calls the backend to do the actual save.
+  #
+  # The backend should match one of the configured backend plugins by name.
+  #
+  # The source can be any of the items in the hierarchy. For example, if you
+  # have the following hierarchy:
+  #
+  #   :hierarchy:
+  #     - %{environment}
+  #     - common
+  #
+  # And the following scope:
+  #
+  #   { 'environment' => 'production' }
+  #
+  # The source should be either 'common', 'production', or any value you
+  # expect to be matched by %{environment}.
+  def save(key, value, backend, source)
+    Backend.save(key, value, backend, source)
+  end
 end
diff --git a/lib/hiera/backend.rb b/lib/hiera/backend.rb
index 0f2f77f..274e707 100644
--- a/lib/hiera/backend.rb
+++ b/lib/hiera/backend.rb
@@ -176,6 +176,26 @@ def lookup(key, default, scope, order_override, resolution_type)
         return default if answer == empty_answer(resolution_type)
         return answer
       end
+
+      # Calls out to the specified backend to save the data.
+      def save(key, value, backend, source)
+        status = false
+
+        if constants.include?("#{backend.capitalize}_backend") ||
+           constants.include?("#{backend.capitalize}_backend".to_sym)
+          dest = Backend.const_get("#{backend.capitalize}_backend").new
+
+          if dest.respond_to?(:save)
+            status = dest.save(key, value, source)
+          else
+            Hiera.warn("Cannot save data, #{backend} backend does not support saving")
+          end
+        else
+          Hiera.warn("Cannot save data, #{backend} is not a valid backend")
+        end
+
+        return status
+      end
     end
   end
 end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index f64c64e..f9fd430 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -17,3 +17,12 @@ class Functions
   end
 end
 
+class Hiera
+  module Backend
+    class Test_backend
+      def initialize
+        Hiera.debug("Hiera Test backend starting")
+      end
+    end
+  end
+end
diff --git a/spec/unit/backend_spec.rb b/spec/unit/backend_spec.rb
index 85d1239..ad22378 100644
--- a/spec/unit/backend_spec.rb
+++ b/spec/unit/backend_spec.rb
@@ -284,5 +284,31 @@ class Hiera
         Backend.lookup("key", {"test" => "value"}, {}, nil, :hash).should == {"test" => "value"}
       end
     end
+
+    describe "#save" do
+      before do
+        Hiera.stubs(:debug)
+        Hiera.stubs(:warn)
+      end
+
+      it "should warn if the backend is not valid" do
+        Config.load({})
+        Hiera.expects(:warn).with("Cannot save data, nobackend is not a valid backend")
+        Backend.save("key", "value", "nobackend", "source")
+      end
+
+      it "should warn if the backend does not support save" do
+        Config.load({})
+        Hiera.expects(:warn).with("Cannot save data, test backend does not support saving")
+        Backend.save("key", "value", "test", "source").should be_false
+      end
+
+      it "should return the status from the backend" do
+        Config.load({})
+        Hiera::Backend::Test_backend.any_instance.expects(:save).
+          with("key", "value", "source").returns(true)
+        Backend.save("key", "value", "test", "source").should be_true
+      end
+    end
   end
 end
diff --git a/spec/unit/hiera_spec.rb b/spec/unit/hiera_spec.rb
index 86c5992..671cc2b 100644
--- a/spec/unit/hiera_spec.rb
+++ b/spec/unit/hiera_spec.rb
@@ -57,4 +57,13 @@
       Hiera.new.lookup(:key, :default, :scope, :order_override, :resolution_type)
     end
   end
+
+  describe "#save" do
+    it "should proxy to the Backend#save method" do
+      Hiera::Config.stubs(:load)
+      Hiera::Config.stubs(:load_backends)
+      Hiera::Backend.expects(:save).with(:key, :value, :backend, :source)
+      Hiera.new.save(:key, :value, :backend, :source)
+    end
+  end
 end

    

--
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