diff options
author | Luke Kanies <luke@madstop.com> | 2009-03-13 01:38:47 -0500 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-03-20 18:27:07 +1100 |
commit | 8b13390819f9aec9f344a72b3c0b3b6609f6209d (patch) | |
tree | 6861f1f6648076cffd0a0b0f30e3d00e83e96667 /lib | |
parent | edf00dba76b65108b443af8d0636073cb3d9d975 (diff) | |
download | puppet-8b13390819f9aec9f344a72b3c0b3b6609f6209d.tar.gz puppet-8b13390819f9aec9f344a72b3c0b3b6609f6209d.tar.xz puppet-8b13390819f9aec9f344a72b3c0b3b6609f6209d.zip |
Adding REST::Handler methods for converting between indirection and uris
This is the first main phase to having a common module for
handling the REST api - this Handler module will be included
by all of the web server REST modules and the Indirector
Request class, so there's a common place that understands
how the URI consists.
Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/network/http/handler.rb | 46 | ||||
-rw-r--r-- | lib/puppet/node/environment.rb | 4 |
2 files changed, 50 insertions, 0 deletions
diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb index 7f6bd4111..1e5281695 100644 --- a/lib/puppet/network/http/handler.rb +++ b/lib/puppet/network/http/handler.rb @@ -2,6 +2,22 @@ module Puppet::Network::HTTP end module Puppet::Network::HTTP::Handler + + # How we map http methods and the indirection name in the URI + # to an indirection method. + METHOD_MAP = { + "GET" => { + :plural => :search, + :singular => :find + }, + "PUT" => { + :singular => :save + }, + "DELETE" => { + :singular => :destroy + } + } + attr_reader :model, :server, :handler # Retrieve the accept header from the http request. @@ -43,6 +59,36 @@ module Puppet::Network::HTTP::Handler return do_exception(response, e) end + def uri2indirection(http_method, uri, params) + environment, indirection, key = uri.split("/", 4)[1..-1] # the first field is always nil because of the leading slash + + raise ArgumentError, "The environment must be purely alphanumeric, not '%s'" % environment unless environment =~ /^\w+$/ + raise ArgumentError, "The indirection name must be purely alphanumeric, not '%s'" % indirection unless indirection =~ /^\w+$/ + + plurality = (indirection == handler.to_s + "s") ? :plural : :singular + + unless METHOD_MAP[http_method] + raise ArgumentError, "No support for http method %s" % http_method + end + + unless method = METHOD_MAP[http_method][plurality] + raise ArgumentError, "No support for plural %s operations" % http_method + end + + indirection.sub!(/s$/, '') if plurality == :plural + + params[:environment] = environment + + key = URI.unescape(key) + + Puppet::Indirector::Request.new(indirection, method, key, params) + end + + def indirection2uri(request) + indirection = request.method == :search ? request.indirection_name.to_s + "s" : request.indirection_name.to_s + "/#{request.environment.to_s}/#{indirection}/#{request.escaped_key}#{request.query_string}" + end + # Are we interacting with a singular instance? def singular?(request) %r{/#{handler.to_s}$}.match(path(request)) diff --git a/lib/puppet/node/environment.rb b/lib/puppet/node/environment.rb index 57dc23e9d..445439aa3 100644 --- a/lib/puppet/node/environment.rb +++ b/lib/puppet/node/environment.rb @@ -61,4 +61,8 @@ class Puppet::Node::Environment end result end + + def to_s + name.to_s + end end |