Commit 0b38aace authored by Chris Price's avatar Chris Price

Support for configurable URL in terminus

parent c7628d23
......@@ -26,7 +26,7 @@ Puppet::Face.define(:node, '0.0.1') do
args.map do |node|
begin
response = http.get("/v3/nodes/#{CGI.escape(node)}", headers)
response = http.get(Puppet::Util::Puppetdb.url_path("/v3/nodes/#{CGI.escape(node)}"), headers)
if response.is_a? Net::HTTPSuccess
result = JSON.parse(response.body)
elsif response.is_a? Net::HTTPNotFound
......
......@@ -36,7 +36,7 @@ class Puppet::Node::Facts::Puppetdb < Puppet::Indirector::REST
def find(request)
profile "facts#find" do
begin
url = "/v3/nodes/#{CGI.escape(request.key)}/facts"
url = Puppet::Util::Puppetdb.url_path("/v3/nodes/#{CGI.escape(request.key)}/facts")
response = profile "Query for nodes facts: #{url}" do
http_get(request, url, headers)
end
......@@ -105,7 +105,7 @@ class Puppet::Node::Facts::Puppetdb < Puppet::Indirector::REST
query_param = CGI.escape(query.to_json)
begin
url = "/v3/nodes?query=#{query_param}"
url = Puppet::Util::Puppetdb.url_path("/v3/nodes?query=#{query_param}")
response = profile "Fact query request: #{URI.unescape(url)}" do
http_get(request, url, headers)
end
......
......@@ -31,7 +31,7 @@ class Puppet::Resource::Puppetdb < Puppet::Indirector::REST
query_param = CGI.escape(expr.to_json)
begin
url = "/v3/resources?query=#{query_param}"
url = Puppet::Util::Puppetdb.url_path("/v3/resources?query=#{query_param}")
response = profile "Resources query: #{URI.unescape(url)}" do
http_get(request, url, headers)
end
......
......@@ -19,6 +19,13 @@ module Puppet::Util::Puppetdb
config.port
end
def self.url_path(path)
unless path.start_with?("/")
path = "/" + path
end
config.url_prefix + path
end
def self.config
@config ||= Puppet::Util::Puppetdb::Config.load
@config
......
......@@ -9,7 +9,7 @@ class Puppet::Util::Puppetdb::Command
include Puppet::Util::Puppetdb
include Puppet::Util::Puppetdb::CommandNames
Url = "/v3/commands"
CommandsUrl = "/v3/commands"
# Public instance methods
......@@ -45,7 +45,8 @@ class Puppet::Util::Puppetdb::Command
begin
response = profile "Submit command HTTP post" do
http = Puppet::Network::HttpPool.http_instance(config.server, config.port)
http.post(Url + "?checksum=#{checksum}", payload, headers)
http.post(Puppet::Util::Puppetdb.url_path(CommandsUrl + "?checksum=#{checksum}"),
payload, headers)
end
Puppet::Util::Puppetdb.log_x_deprecation_header(response)
......
......@@ -11,6 +11,7 @@ class Config
defaults = {
:server => "puppetdb",
:port => 8081,
:url_prefix => "",
:soft_write_failure => false,
:ignore_blacklisted_events => true,
}
......@@ -52,11 +53,12 @@ class Config
main_section = main_section.inject({}) {|h, (k,v)| h[k.to_sym] = v ; h}
# merge with defaults but filter out anything except the legal settings
config_hash = defaults.merge(main_section).reject do |k, v|
!([:server, :port, :ignore_blacklisted_events, :soft_write_failure].include?(k))
!([:server, :port, :url_prefix, :ignore_blacklisted_events, :soft_write_failure].include?(k))
end
config_hash[:server] = config_hash[:server].strip
config_hash[:port] = config_hash[:port].to_i
config_hash[:url_prefix] = normalize_url_prefix(config_hash[:url_prefix].strip)
config_hash[:ignore_blacklisted_events] =
Puppet::Util::Puppetdb.to_bool(config_hash[:ignore_blacklisted_events])
config_hash[:soft_write_failure] =
......@@ -84,6 +86,10 @@ class Config
config[:port]
end
def url_prefix
config[:url_prefix]
end
def ignore_blacklisted_events?
config[:ignore_blacklisted_events]
end
......@@ -96,6 +102,17 @@ class Config
config[:soft_write_failure]
end
# @!group Private class methods
def self.normalize_url_prefix(prefix)
if prefix == ""
prefix
elsif prefix.start_with?("/")
prefix
else
"/" + prefix
end
end
# @!group Private instance methods
# @!attribute [r] count
......
......@@ -36,7 +36,7 @@ describe processor do
Puppet::Network::HttpPool.expects(:http_instance).returns(http)
http.expects(:post).with {|path, body, headers|
expect(path).to include(Puppet::Util::Puppetdb::Command::Url)
expect(path).to include(Puppet::Util::Puppetdb::Command::CommandsUrl)
expect(body).to eq(payload)
}.returns(httpok)
......
......@@ -54,6 +54,23 @@ describe Puppet::Util::Puppetdb::Command do
subject.submit
end
end
context "when an alternate url_prefix is configured" do
let(:httpok) { Net::HTTPOK.new('1.1', 200, '') }
it "should make a request to the correct url" do
config = Puppet::Util::Puppetdb.config
config.stubs(:url_prefix).returns "/puppetdb"
Puppet::Util::Puppetdb.expects(:config).at_least_once.returns(config)
httpok.stubs(:body).returns '{"uuid": "a UUID"}'
http.expects(:post).with { |path|
path.start_with?("/puppetdb" + Puppet::Util::Puppetdb::Command::CommandsUrl)
}.returns httpok
subject.submit
end
end
end
end
......@@ -32,6 +32,7 @@ describe Puppet::Util::Puppetdb::Config do
config.server.should == 'puppetdb'
config.port.should == 8081
config.ignore_blacklisted_events?.should == true
config.url_prefix.should == ""
end
end
......@@ -62,12 +63,14 @@ server = main_server
port = 1234
ignore_blacklisted_events = false
soft_write_failure = true
url_prefix = /puppetdb
CONF
config = described_class.load
config.server.should == 'main_server'
config.port.should == 1234
config.ignore_blacklisted_events?.should == false
config.soft_write_failure.should be_true
config.url_prefix.should == "/puppetdb"
end
it "should use the default if no value is specified" do
......@@ -78,6 +81,17 @@ CONF
config.port.should == 8081
config.ignore_blacklisted_events?.should == true
config.soft_write_failure.should be_false
config.url_prefix.should == ""
end
it "should add a leading slash to url_prefix if needed" do
write_config <<CONF
[main]
url_prefix = puppetdb
CONF
config = described_class.load
config.url_prefix.should == "/puppetdb"
end
it "should be insensitive to whitespace" do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment