diff options
author | Luke Kanies <luke@madstop.com> | 2009-03-13 01:22:12 -0500 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-03-20 18:27:07 +1100 |
commit | edf00dba76b65108b443af8d0636073cb3d9d975 (patch) | |
tree | 75951e613a375dca4780d46dde35b1046f64446d | |
parent | a064ed1a6035c44f123e092b2429895fb45bdbd2 (diff) | |
download | puppet-edf00dba76b65108b443af8d0636073cb3d9d975.tar.gz puppet-edf00dba76b65108b443af8d0636073cb3d9d975.tar.xz puppet-edf00dba76b65108b443af8d0636073cb3d9d975.zip |
Adding environment support to the REST URI
Also adding it to the Indirection Request.
Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r-- | lib/puppet/indirector/request.rb | 19 | ||||
-rw-r--r-- | lib/puppet/indirector/rest.rb | 16 | ||||
-rwxr-xr-x | spec/unit/indirector/request.rb | 17 | ||||
-rwxr-xr-x | spec/unit/indirector/rest.rb | 21 |
4 files changed, 56 insertions, 17 deletions
diff --git a/lib/puppet/indirector/request.rb b/lib/puppet/indirector/request.rb index 7fe029597..450b43bd2 100644 --- a/lib/puppet/indirector/request.rb +++ b/lib/puppet/indirector/request.rb @@ -5,20 +5,34 @@ require 'puppet/indirector' # Indirection call, and as a a result also handles REST calls. It's somewhat # analogous to an HTTP Request object, except tuned for our Indirector. class Puppet::Indirector::Request - attr_accessor :indirection_name, :key, :method, :options, :instance, :node, :ip, :authenticated, :ignore_cache, :ignore_terminus + attr_accessor :key, :method, :options, :instance, :node, :ip, :authenticated, :ignore_cache, :ignore_terminus attr_accessor :server, :port, :uri, :protocol + attr_reader :environment, :indirection_name + # Is this an authenticated request? def authenticated? # Double negative, so we just get true or false ! ! authenticated end + def environment=(env) + @environment = if env.is_a?(Puppet::Node::Environment) + env + else + Puppet::Node::Environment.new(env) + end + end + def escaped_key URI.escape(key) end + def indirection_name=(name) + @indirection_name = name.to_sym + end + # LAK:NOTE This is a messy interface to the cache, and it's only # used by the Configurer class. I decided it was better to implement # it now and refactor later, when we have a better design, than @@ -36,7 +50,8 @@ class Puppet::Indirector::Request options ||= {} raise ArgumentError, "Request options must be a hash, not %s" % options.class unless options.is_a?(Hash) - @indirection_name, @method = indirection_name, method + self.indirection_name = indirection_name + self.method = method @options = options.inject({}) do |result, ary| param, value = ary diff --git a/lib/puppet/indirector/rest.rb b/lib/puppet/indirector/rest.rb index e5efb3aa3..6429b22b7 100644 --- a/lib/puppet/indirector/rest.rb +++ b/lib/puppet/indirector/rest.rb @@ -62,14 +62,14 @@ class Puppet::Indirector::REST < Puppet::Indirector::Terminus end def find(request) - deserialize network(request).get("/#{indirection.name}/#{request.escaped_key}#{request.query_string}", headers) + deserialize network(request).get("/#{environment}/#{indirection.name}/#{request.escaped_key}#{request.query_string}", headers) end def search(request) if request.key - path = "/#{indirection.name}s/#{request.escaped_key}#{request.query_string}" + path = "/#{environment}/#{indirection.name}s/#{request.escaped_key}#{request.query_string}" else - path = "/#{indirection.name}s#{request.query_string}" + path = "/#{environment}/#{indirection.name}s#{request.query_string}" end unless result = deserialize(network(request).get(path, headers), true) return [] @@ -79,11 +79,17 @@ class Puppet::Indirector::REST < Puppet::Indirector::Terminus def destroy(request) raise ArgumentError, "DELETE does not accept options" unless request.options.empty? - deserialize network(request).delete("/#{indirection.name}/#{request.escaped_key}", headers) + deserialize network(request).delete("/#{environment}/#{indirection.name}/#{request.escaped_key}", headers) end def save(request) raise ArgumentError, "PUT does not accept options" unless request.options.empty? - deserialize network(request).put("/#{indirection.name}/", request.instance.render, headers) + deserialize network(request).put("/#{environment}/#{indirection.name}/", request.instance.render, headers) + end + + private + + def environment + Puppet::Node::Environment.new end end diff --git a/spec/unit/indirector/request.rb b/spec/unit/indirector/request.rb index 98ecf2fd3..e2edc2641 100755 --- a/spec/unit/indirector/request.rb +++ b/spec/unit/indirector/request.rb @@ -9,6 +9,10 @@ describe Puppet::Indirector::Request do lambda { Puppet::Indirector::Request.new }.should raise_error(ArgumentError) end + it "should always convert the indirection name to a symbol" do + Puppet::Indirector::Request.new("ind", :method, "mykey").indirection_name.should == :ind + end + it "should use provided value as the key if it is a string" do Puppet::Indirector::Request.new(:ind, :method, "mykey").key.should == "mykey" end @@ -178,6 +182,19 @@ describe Puppet::Indirector::Request do Puppet::Indirector::Request.new(:myind, :find, "my key").escaped_key.should == URI.escape("my key") end + it "should have an environment accessor" do + Puppet::Indirector::Request.new(:myind, :find, "my key", :environment => "foo").should respond_to(:environment) + end + + it "should set its environment to an environment instance when a string is specified as its environment" do + Puppet::Indirector::Request.new(:myind, :find, "my key", :environment => "foo").environment.should == Puppet::Node::Environment.new("foo") + end + + it "should use any passed in environment instances as its environment" do + env = Puppet::Node::Environment.new("foo") + Puppet::Indirector::Request.new(:myind, :find, "my key", :environment => env).environment.should equal(env) + end + describe "when building a query string from its options" do before do @request = Puppet::Indirector::Request.new(:myind, :find, "my key") diff --git a/spec/unit/indirector/rest.rb b/spec/unit/indirector/rest.rb index 25b3bfcb2..361412a54 100755 --- a/spec/unit/indirector/rest.rb +++ b/spec/unit/indirector/rest.rb @@ -168,8 +168,8 @@ describe Puppet::Indirector::REST do @searcher.find(@request).should == 'myobject' end - it "should use the indirection name and escaped request key to create the path" do - should_path = "/%s/%s" % [@indirection.name.to_s, "foo"] + it "should use the environment, indirection name, and escaped request key to create the path" do + should_path = "/%s/%s/%s" % [Puppet::Node::Environment.new, @indirection.name.to_s, "foo"] @connection.expects(:get).with { |path, args| path == should_path }.returns(@response) @searcher.find(@request) end @@ -221,15 +221,15 @@ describe Puppet::Indirector::REST do @searcher.search(@request).should == 'myobject' end - it "should use the plural indirection name as the path if there is no request key" do - should_path = "/%ss" % [@indirection.name.to_s] + it "should use the environment and the plural indirection name as the path if there is no request key" do + should_path = "/%s/%ss" % [Puppet::Node::Environment.new, @indirection.name.to_s] @request.stubs(:key).returns nil @connection.expects(:get).with { |path, args| path == should_path }.returns(@response) @searcher.search(@request) end - it "should use the plural indirection name and escaped request key to create the path if the request key is set" do - should_path = "/%ss/%s" % [@indirection.name.to_s, "foo"] + it "should use the envrironment, the plural indirection name, and the escaped request key to create the path if the request key is set" do + should_path = "/%s/%ss/%s" % [Puppet::Node::Environment.new, @indirection.name.to_s, "foo"] @connection.expects(:get).with { |path, args| path == should_path }.returns(@response) @searcher.search(@request) end @@ -286,8 +286,8 @@ describe Puppet::Indirector::REST do @searcher.destroy(@request).should == 'myobject' end - it "should use the indirection name and escaped request key to create the path" do - should_path = "/%s/%s" % [@indirection.name.to_s, "foo"] + it "should use the environment, the indirection name, and the escaped request key to create the path" do + should_path = "/%s/%s/%s" % [Puppet::Node::Environment.new, @indirection.name.to_s, "foo"] @connection.expects(:delete).with { |path, args| path == should_path }.returns(@response) @searcher.destroy(@request) end @@ -337,8 +337,9 @@ describe Puppet::Indirector::REST do lambda { @searcher.save(@request) }.should raise_error(ArgumentError) end - it "should use the indirection name as the path for the request" do - @connection.expects(:put).with { |path, data, args| path == "/#{@indirection.name.to_s}/" }.returns @response + it "should use the environment and the indirection name as the path for the request" do + path = "/%s/%s/" % [Puppet::Node::Environment.new, @indirection.name] + @connection.expects(:put).with { |path, data, args| path == path }.returns @response @searcher.save(@request) end |