summaryrefslogtreecommitdiffstats
path: root/lib/puppet
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-08-21 23:09:27 -0500
committerLuke Kanies <luke@madstop.com>2008-08-26 22:40:38 -0700
commita215abaef97ea1fb0187f46c5e6a880ff1d29036 (patch)
tree1f320446b228626ff1be86baf2b74102afd7f1f1 /lib/puppet
parentd1746054b8617271941e7307b2ecee0b61976818 (diff)
downloadpuppet-a215abaef97ea1fb0187f46c5e6a880ff1d29036.tar.gz
puppet-a215abaef97ea1fb0187f46c5e6a880ff1d29036.tar.xz
puppet-a215abaef97ea1fb0187f46c5e6a880ff1d29036.zip
Dividing server/port configuration responsibility between the REST terminus and the indirection request.
Previously, the REST terminus did all of the configuration, but it required rewriting the request key if it was a URI because you can't have a uri in a uri (i.e., you can't use http://host/puppet://host/dist/file). Now the request parses the URI and sets host/port/key/protocol appropriately, and the REST terminus has its own overrides and defaults so that subclasses like certificate classes can provide specific values. Signed-off-by: Luke Kanies <luke@madstop.com>
Diffstat (limited to 'lib/puppet')
-rw-r--r--lib/puppet/indirector/request.rb36
-rw-r--r--lib/puppet/indirector/rest.rb40
2 files changed, 61 insertions, 15 deletions
diff --git a/lib/puppet/indirector/request.rb b/lib/puppet/indirector/request.rb
index 98fa38885..f1d02cead 100644
--- a/lib/puppet/indirector/request.rb
+++ b/lib/puppet/indirector/request.rb
@@ -5,6 +5,8 @@ require 'puppet/indirector'
class Puppet::Indirector::Request
attr_accessor :indirection_name, :key, :method, :options, :instance, :node, :ip, :authenticated
+ attr_accessor :server, :port, :uri, :protocol
+
# Is this an authenticated request?
def authenticated?
# Double negative, so we just get true or false
@@ -28,7 +30,15 @@ class Puppet::Indirector::Request
end
if key.is_a?(String) or key.is_a?(Symbol)
- @key = key
+ # If the request key is a URI, then we need to treat it specially,
+ # because it rewrites the key. We could otherwise strip server/port/etc
+ # info out in the REST class, but it seemed bad design for the REST
+ # class to rewrite the key.
+ if key.to_s =~ /^\w+:\/\// # it's a URI
+ set_uri_key(key)
+ else
+ @key = key
+ end
else
@instance = key
@key = @instance.name
@@ -39,4 +49,28 @@ class Puppet::Indirector::Request
def indirection
Puppet::Indirector::Indirection.instance(@indirection_name)
end
+
+ private
+
+ # Parse the key as a URI, setting attributes appropriately.
+ def set_uri_key(key)
+ @uri = key
+ begin
+ uri = URI.parse(URI.escape(key))
+ rescue => detail
+ raise ArgumentError, "Could not understand URL %s: %s" % [source, detail.to_s]
+ end
+ @server = uri.host if uri.host
+
+ # If the URI class can look up the scheme, it will provide a port,
+ # otherwise it will default to '0'.
+ if uri.port.to_i == 0 and uri.scheme == "puppet"
+ @port = Puppet.settings[:masterport].to_i
+ else
+ @port = uri.port.to_i
+ end
+
+ @protocol = uri.scheme
+ @key = uri.path.sub(/^\//, '')
+ end
end
diff --git a/lib/puppet/indirector/rest.rb b/lib/puppet/indirector/rest.rb
index 4389dfb7e..a2bfc5d9a 100644
--- a/lib/puppet/indirector/rest.rb
+++ b/lib/puppet/indirector/rest.rb
@@ -1,8 +1,33 @@
require 'net/http'
require 'uri'
+require 'puppet/network/http_pool'
+
# Access objects via REST
class Puppet::Indirector::REST < Puppet::Indirector::Terminus
+
+ class << self
+ attr_reader :server_setting, :port_setting
+ end
+
+ # Specify the setting that we should use to get the server name.
+ def self.use_server_setting(setting)
+ @server_setting = setting
+ end
+
+ def self.server
+ return Puppet.settings[server_setting || :server]
+ end
+
+ # Specify the setting that we should use to get the port.
+ def self.use_port_setting(setting)
+ @port_setting = setting
+ end
+
+ def self.port
+ return Puppet.settings[port_setting || :masterport].to_i
+ end
+
# Figure out the content type, turn that into a format, and use the format
# to extract the body of the response.
def deserialize(response, multiple = false)
@@ -33,20 +58,7 @@ class Puppet::Indirector::REST < Puppet::Indirector::Terminus
end
def network(request)
- if request.key =~ /^\w+:\/\// # it looks like a URI
- begin
- uri = URI.parse(URI.escape(request.key))
- rescue => detail
- raise ArgumentError, "Could not understand URL %s: %s" % [source, detail.to_s]
- end
- server = uri.host || Puppet[:server]
- port = uri.port.to_i == 0 ? Puppet[:masterport].to_i : uri.port.to_i
- else
- server = Puppet[:server]
- port = Puppet[:masterport].to_i
- end
-
- Puppet::Network::HttpPool.http_instance(server, port)
+ Puppet::Network::HttpPool.http_instance(request.server || self.class.server, request.port || self.class.port)
end
def find(request)