Skip to content

Commit

Permalink
Add parameter for expected HTTP code
Browse files Browse the repository at this point in the history
Allow user to specify the expected HTTP result code.  Defaults to 200.
  • Loading branch information
Clayton O'Neill committed Sep 24, 2015
1 parent 5bdfecc commit afd0d1d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ def create
# If `#create` is called, that means that `#exists?` returned false, which
# means that the connection could not be established... so we need to
# cause a failure here.
raise Puppet::Error, "Unable to connect to the HTTP server! (#{@validator.http_server}:#{@validator.http_port})"
raise Puppet::Error, "Unable to connect to the HTTP server! (#{@validator.http_server}:#{@validator.http_port} with HTTP code #{@validator.expected_code})"
end

# Returns the existing validator, if one exists otherwise creates a new object
# from the class.
#
# @api private
def validator
@validator ||= PuppetX::PuppetCommunity::HttpValidator.new(resource[:name], resource[:server], resource[:port], resource[:use_ssl], resource[:test_url])
@validator ||= PuppetX::PuppetCommunity::HttpValidator.new(resource[:name], resource[:server], resource[:port], resource[:use_ssl], resource[:test_url], resource[:expected_code])
end

end
Expand Down
14 changes: 14 additions & 0 deletions lib/puppet/type/http_conn_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@
defaultto '/'
end

newparam(:expected_code) do
desc 'The HTTP status code that should be expected; defaults to 200.'
defaultto 200

validate do |value|
# This will raise an error if the string is not convertible to an integer
Integer(value)
end

munge do |value|
Integer(value)
end
end

newparam(:timeout) do
desc 'The max number of seconds that the validator should wait before giving up and deciding that the HTTP server is not running; defaults to 15 seconds.'
defaultto 15
Expand Down
8 changes: 5 additions & 3 deletions lib/puppet_x/puppet-community/http_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class HttpValidator
attr_reader :use_ssl
attr_reader :test_path
attr_reader :test_headers
attr_reader :expected_code

def initialize(http_resource_name, http_server, http_port, use_ssl, test_path, expected_code)
if http_resource_name =~ /\A#{URI::regexp}\z/
Expand All @@ -22,6 +23,7 @@ def initialize(http_resource_name, http_server, http_port, use_ssl, test_path, e
@use_ssl = use_ssl
@test_path = test_path
end
@expected_code = expected_code
@test_headers = { "Accept" => "application/json" }
end

Expand All @@ -34,13 +36,13 @@ def attempt_connection
conn = Puppet::Network::HttpPool.http_instance(http_server, http_port, use_ssl)

response = conn.get(test_path, test_headers)
unless response.kind_of?(Net::HTTPSuccess)
Puppet.notice "Unable to connect to the server (http#{use_ssl ? "s" : ""}://#{http_server}:#{http_port}): [#{response.code}] #{response.msg}"
unless response.code.to_i == @expected_code
Puppet.notice "Unable to connect to the server or wrong HTTP code (expected #{@expected_code}) (http#{@use_ssl ? "s" : ""}://#{@http_server}:#{@http_port}): [#{@response.code}] #{@response.msg}"
return false
end
return true
rescue Exception => e
Puppet.notice "Unable to connect to the server (http#{use_ssl ? "s" : ""}://#{http_server}:#{http_port}): #{e.message}"
Puppet.notice "Unable to connect to the server (http#{@use_ssl ? "s" : ""}://#{@http_server}:#{@http_port}): #{e.message}"
return false
end
end
Expand Down

0 comments on commit afd0d1d

Please sign in to comment.