From f28f20b675737741a98b1b87b4791f736a996d40 Mon Sep 17 00:00:00 2001 From: Rick Bradley Date: Wed, 2 Apr 2008 00:48:54 -0500 Subject: Added support for destroy/DELETE over REST (including units & integrations on both webrick & mongrel). Added pending specs for the trivialities in the REST network_fetch and network_delete methods. Refactored YAML exception detection out into a private helper method. --- lib/puppet/indirector/rest.rb | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/puppet/indirector/rest.rb b/lib/puppet/indirector/rest.rb index 0c86b2706..076e96356 100644 --- a/lib/puppet/indirector/rest.rb +++ b/lib/puppet/indirector/rest.rb @@ -4,19 +4,38 @@ require 'uri' # Access objects via REST class Puppet::Indirector::REST < Puppet::Indirector::Terminus def network_fetch(path) - # TODO: url_encode path, set proper server + port Net::HTTP.get(URI.parse("http://127.0.0.1:34343/#{path}")) end + def network_delete(path) + Net::HTTP.start("127.0.0.1", 34343) {|x| x.delete("/#{path}").body } # weird-ass net/http library + end + def find(name, options = {}) - network_result = network_fetch("#{indirection.name}/#{name}") - raise YAML.load(network_result) if network_result =~ %r{--- !ruby/exception} - decoded_result = indirection.model.from_yaml(network_result) + network_result = network_fetch("#{indirection.name}/#{name}") + raise YAML.load(network_result) if exception?(network_result) + decoded_result = indirection.model.from_yaml(network_result) + end + + def search(name, options = {}) + network_results = network_fetch("#{indirection.name}s/#{name}") + raise YAML.load(network_results) if exception?(network_results) + decoded_results = YAML.load(network_results.to_s).collect {|result| indirection.model.from_yaml(result) } + end + + def destroy(name, options = {}) + network_result = network_delete("#{indirection.name}/#{name}") + raise YAML.load(network_result) if exception?(network_result) + decoded_result = YAML.load(network_result.to_s) + end + + def save + end - def search(key, options = {}) - network_results = network_fetch("#{indirection.name}s/#{key}") - raise YAML.load(network_results) if network_results =~ %r{--- !ruby/exception} - decoded_results = YAML.load(network_results.to_s).collect {|result| indirection.model.from_yaml(result) } + private + + def exception?(yaml_string) + yaml_string =~ %r{--- !ruby/exception} end end -- cgit