summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2009-03-19 23:58:19 -0500
committerJames Turnbull <james@lovedthanlost.net>2009-03-20 18:27:08 +1100
commit0179e945a7d402c90a333c8207243882af362e06 (patch)
treee977d49f50c7907a67799bd8d445f833f9c823ee /lib/puppet
parenta497263d97229489dcc4341cc98ca3c75f116374 (diff)
downloadpuppet-0179e945a7d402c90a333c8207243882af362e06.tar.gz
puppet-0179e945a7d402c90a333c8207243882af362e06.tar.xz
puppet-0179e945a7d402c90a333c8207243882af362e06.zip
Fixing #1557 - Environments are now in REST URIs
This commit includes multiple, related changes, all in one commit because the whole thing was necessary to reach a functional tree again: * The URI starts with the environment, so: /production/certificate/foo /development/file_content/path/to/your/file * All REST handling is done by a single instance mounted at / for webrick and Mongrel, rather than having individual instances mounted at, say, /certificate. * All REST URI translation is done by an API module. Currently only the 'v1' module exists with no support for additional modules, but it's well-separated and will be easy to expand as we need it. Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/application/puppetd.rb2
-rw-r--r--lib/puppet/application/puppetmasterd.rb5
-rw-r--r--lib/puppet/network/http/api/v1.rb7
-rw-r--r--lib/puppet/network/http/handler.rb32
-rw-r--r--lib/puppet/network/http/mongrel.rb13
-rw-r--r--lib/puppet/network/http/mongrel/rest.rb9
-rw-r--r--lib/puppet/network/http/webrick.rb12
-rw-r--r--lib/puppet/network/http/webrick/rest.rb8
8 files changed, 28 insertions, 60 deletions
diff --git a/lib/puppet/application/puppetd.rb b/lib/puppet/application/puppetd.rb
index 3efb0ef0a..cacb84361 100644
--- a/lib/puppet/application/puppetd.rb
+++ b/lib/puppet/application/puppetd.rb
@@ -177,7 +177,7 @@ Puppet::Application.new(:puppetd) do
require 'puppet/network/server'
# No REST handlers yet.
- server = Puppet::Network::Server.new(:handlers => [:facts], :xmlrpc_handlers => handlers, :port => Puppet[:puppetport])
+ server = Puppet::Network::Server.new(:xmlrpc_handlers => handlers, :port => Puppet[:puppetport])
@daemon.server = server
end
diff --git a/lib/puppet/application/puppetmasterd.rb b/lib/puppet/application/puppetmasterd.rb
index 15426ad96..fe92bca7a 100644
--- a/lib/puppet/application/puppetmasterd.rb
+++ b/lib/puppet/application/puppetmasterd.rb
@@ -55,14 +55,11 @@ Puppet::Application.new(:puppetmasterd) do
xmlrpc_handlers = [:Status, :FileServer, :Master, :Report, :Filebucket]
- # Just set up serving to all of the indirected classes.
- rest_handlers = Puppet::Indirector::Indirection.instances
-
if Puppet[:ca]
xmlrpc_handlers << :CA
end
- @daemon.server = Puppet::Network::Server.new(:handlers => rest_handlers, :xmlrpc_handlers => xmlrpc_handlers)
+ @daemon.server = Puppet::Network::Server.new(:xmlrpc_handlers => xmlrpc_handlers)
# Make sure we've got a localhost ssl cert
Puppet::SSL::Host.localhost
diff --git a/lib/puppet/network/http/api/v1.rb b/lib/puppet/network/http/api/v1.rb
index 2ee1a815f..13df7c3d0 100644
--- a/lib/puppet/network/http/api/v1.rb
+++ b/lib/puppet/network/http/api/v1.rb
@@ -51,7 +51,12 @@ module Puppet::Network::HTTP::API::V1
end
def plurality(indirection)
- result = (indirection == handler.to_s + "s") ? :plural : :singular
+ # NOTE This specific hook for facts is ridiculous, but it's a *many*-line
+ # fix to not need this, and our goal is to move away from the complication
+ # that leads to the fix being too long.
+ return :singular if indirection == "facts"
+
+ result = (indirection =~ /s$/) ? :plural : :singular
indirection.sub!(/s$/, '') if result
diff --git a/lib/puppet/network/http/handler.rb b/lib/puppet/network/http/handler.rb
index 76f07ed73..db12a8b67 100644
--- a/lib/puppet/network/http/handler.rb
+++ b/lib/puppet/network/http/handler.rb
@@ -6,7 +6,7 @@ require 'puppet/network/http/api/v1'
module Puppet::Network::HTTP::Handler
include Puppet::Network::HTTP::API::V1
- attr_reader :model, :server, :handler
+ attr_reader :server, :handler
# Retrieve the accept header from the http request.
def accept_header(request)
@@ -30,19 +30,16 @@ module Puppet::Network::HTTP::Handler
raise "No specified acceptable formats (%s) are functional on this machine" % header
end
- def initialize_for_puppet(args = {})
- raise ArgumentError unless @server = args[:server]
- raise ArgumentError unless @handler = args[:handler]
- @model = find_model_for_handler(@handler)
+ def initialize_for_puppet(server)
+ @server = server
end
# handle an HTTP request
def process(request, response)
- indirection_request = uri2indirection(path(request), params(request), http_method(request))
+ indirection_request = uri2indirection(http_method(request), path(request), params(request))
send("do_%s" % indirection_request.method, indirection_request, request, response)
rescue Exception => e
- puts e.backtrace
return do_exception(response, e)
end
@@ -67,8 +64,8 @@ module Puppet::Network::HTTP::Handler
# Execute our find.
def do_find(indirection_request, request, response)
- unless result = model.find(indirection_request.key, indirection_request.options)
- return do_exception(response, "Could not find %s %s" % [model.name, indirection_request.key], 404)
+ unless result = indirection_request.model.find(indirection_request.key, indirection_request.to_hash)
+ return do_exception(response, "Could not find %s %s" % [indirection_request.indirection_name, indirection_request.key], 404)
end
# The encoding of the result must include the format to use,
@@ -82,21 +79,21 @@ module Puppet::Network::HTTP::Handler
# Execute our search.
def do_search(indirection_request, request, response)
- result = model.search(indirection_request.key, indirection_request.options)
+ result = indirection_request.model.search(indirection_request.key, indirection_request.to_hash)
if result.nil? or (result.is_a?(Array) and result.empty?)
- return do_exception(response, "Could not find instances in %s with '%s'" % [model.name, indirection_request.options.inspect], 404)
+ return do_exception(response, "Could not find instances in %s with '%s'" % [indirection_request.indirection_name, indirection_request.to_hash.inspect], 404)
end
format = format_to_use(request)
set_content_type(response, format)
- set_response(response, model.render_multiple(format, result))
+ set_response(response, indirection_request.model.render_multiple(format, result))
end
# Execute our destroy.
def do_destroy(indirection_request, request, response)
- result = model.destroy(indirection_request.key, indirection_request.options)
+ result = indirection_request.model.destroy(indirection_request.key, indirection_request.to_hash)
set_content_type(response, "yaml")
@@ -110,7 +107,7 @@ module Puppet::Network::HTTP::Handler
format = format_to_use(request)
- obj = model.convert_from(format_to_use(request), data)
+ obj = indirection_request.model.convert_from(format_to_use(request), data)
result = save_object(indirection_request, obj)
set_content_type(response, "yaml")
@@ -123,12 +120,7 @@ module Puppet::Network::HTTP::Handler
# LAK:NOTE This has to be here for testing; it's a stub-point so
# we keep infinite recursion from happening.
def save_object(ind_request, object)
- object.save(ind_request.options)
- end
-
- def find_model_for_handler(handler)
- Puppet::Indirector::Indirection.model(handler) ||
- raise(ArgumentError, "Cannot locate indirection [#{handler}].")
+ object.save(ind_request.to_hash)
end
def get?(request)
diff --git a/lib/puppet/network/http/mongrel.rb b/lib/puppet/network/http/mongrel.rb
index 847781cf2..356402545 100644
--- a/lib/puppet/network/http/mongrel.rb
+++ b/lib/puppet/network/http/mongrel.rb
@@ -8,14 +8,12 @@ class Puppet::Network::HTTP::Mongrel
end
def listen(args = {})
- raise ArgumentError, ":handlers must be specified." if !args[:handlers] or args[:handlers].empty?
raise ArgumentError, ":protocols must be specified." if !args[:protocols] or args[:protocols].empty?
raise ArgumentError, ":address must be specified." unless args[:address]
raise ArgumentError, ":port must be specified." unless args[:port]
raise "Mongrel server is already listening" if listening?
@protocols = args[:protocols]
- @handlers = args[:handlers]
@xmlrpc_handlers = args[:xmlrpc_handlers]
@server = Mongrel::HttpServer.new(args[:address], args[:port])
setup_handlers
@@ -38,14 +36,9 @@ class Puppet::Network::HTTP::Mongrel
private
def setup_handlers
- @protocols.each do |protocol|
- next if protocol == :xmlrpc
- klass = class_for_protocol(protocol)
- @handlers.each do |handler|
- @server.register('/' + handler.to_s, klass.new(:server => @server, :handler => handler))
- @server.register('/' + handler.to_s + 's', klass.new(:server => @server, :handler => handler))
- end
- end
+ # Register our REST support at /
+ klass = class_for_protocol(:rest)
+ @server.register('/', klass.new(:server => @server))
if @protocols.include?(:xmlrpc) and ! @xmlrpc_handlers.empty?
setup_xmlrpc_handlers
diff --git a/lib/puppet/network/http/mongrel/rest.rb b/lib/puppet/network/http/mongrel/rest.rb
index 04f413937..d9913dc45 100644
--- a/lib/puppet/network/http/mongrel/rest.rb
+++ b/lib/puppet/network/http/mongrel/rest.rb
@@ -31,13 +31,8 @@ class Puppet::Network::HTTP::MongrelREST < Mongrel::HttpHandler
# what path was requested?
def path(request)
# LAK:NOTE See http://snurl.com/21zf8 [groups_google_com]
- x = '/' + request.params[Mongrel::Const::REQUEST_PATH].split('/')[1]
- end
-
- # return the key included in the request path
- def request_key(request)
- # LAK:NOTE See http://snurl.com/21zf8 [groups_google_com]
- x = request.params[Mongrel::Const::REQUEST_PATH].split('/', 3)[2]
+ #x = '/' + request.params[Mongrel::Const::REQUEST_PATH]
+ request.params[Mongrel::Const::REQUEST_PATH]
end
# return the request body
diff --git a/lib/puppet/network/http/webrick.rb b/lib/puppet/network/http/webrick.rb
index 972ebc2e2..46735c0f5 100644
--- a/lib/puppet/network/http/webrick.rb
+++ b/lib/puppet/network/http/webrick.rb
@@ -19,13 +19,11 @@ class Puppet::Network::HTTP::WEBrick
end
def listen(args = {})
- raise ArgumentError, ":handlers must be specified." if !args[:handlers] or args[:handlers].empty?
raise ArgumentError, ":protocols must be specified." if !args[:protocols] or args[:protocols].empty?
raise ArgumentError, ":address must be specified." unless args[:address]
raise ArgumentError, ":port must be specified." unless args[:port]
@protocols = args[:protocols]
- @handlers = args[:handlers]
@xmlrpc_handlers = args[:xmlrpc_handlers]
arguments = {:BindAddress => args[:address], :Port => args[:port]}
@@ -115,14 +113,8 @@ class Puppet::Network::HTTP::WEBrick
def setup_handlers
# Set up the new-style protocols.
- @protocols.each do |protocol|
- next if protocol == :xmlrpc
- klass = self.class.class_for_protocol(protocol)
- @handlers.each do |handler|
- @server.mount('/' + handler.to_s, klass, handler)
- @server.mount('/' + handler.to_s + 's', klass, handler)
- end
- end
+ klass = self.class.class_for_protocol(:rest)
+ @server.mount('/', klass, :this_value_is_apparently_necessary_but_unused)
# And then set up xmlrpc, if configured.
if @protocols.include?(:xmlrpc) and ! @xmlrpc_handlers.empty?
diff --git a/lib/puppet/network/http/webrick/rest.rb b/lib/puppet/network/http/webrick/rest.rb
index e34f0d2e8..8120c87db 100644
--- a/lib/puppet/network/http/webrick/rest.rb
+++ b/lib/puppet/network/http/webrick/rest.rb
@@ -31,13 +31,7 @@ class Puppet::Network::HTTP::WEBrickREST < WEBrick::HTTPServlet::AbstractServlet
end
def path(request)
- # LAK:NOTE See http://snurl.com/21zf8 [groups_google_com]
- x = '/' + request.path.split('/')[1]
- end
-
- def request_key(request)
- # LAK:NOTE See http://snurl.com/21zf8 [groups_google_com]
- x = request.path.split('/', 3)[2]
+ request.path
end
def body(request)