Class: Puppet::Util::RequestManager
- Inherits:
-
Object
- Object
- Puppet::Util::RequestManager
- Defined in:
- lib/puppet/util/request_manager.rb
Overview
Http request manager with expectations checking
Class Method Summary collapse
- .attempt_http_request_with_expectations(uri, parameters, expectations) ⇒ Object
- .matches_expectations?(response, expectations) ⇒ Boolean
- .matches_expected_messages?(response, messages) ⇒ Boolean
- .matches_status_codes?(response, codes) ⇒ Boolean
- .perform_http_request(uri, parameters, expectations, timeout) ⇒ Object
Class Method Details
.attempt_http_request_with_expectations(uri, parameters, expectations) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/puppet/util/request_manager.rb', line 32 def self.attempt_http_request_with_expectations(uri, parameters, expectations) Timeout.timeout(Puppet[:configtimeout]) do response = Puppet::Util::HttpClient.attempt_http_request(uri, parameters) = "Request doesn\'t match expectations\n" unless response.nil? += "Expected status codes: #{expectations[:codes].join(',')}\n" += "Recieved code: #{response.code}\n" if expectations.key?(:messages) += "Expected messages: #{expectations[:messages].join(',')}\n" += "Recieved message: #{response.body}\n" end end raise Puppet::Exceptions::ExpectationsFailError, unless matches_expectations?(response, expectations) end end |
.matches_expectations?(response, expectations) ⇒ Boolean
49 50 51 52 53 54 55 56 57 |
# File 'lib/puppet/util/request_manager.rb', line 49 def self.matches_expectations?(response, expectations) raise ArgumentError, 'You must pass at least one expected status code' unless expectations.key?(:codes) return false if response.nil? return false unless matches_status_codes?(response, expectations[:codes]) return (response, expectations[:messages]) if expectations.key?(:messages) true end |
.matches_expected_messages?(response, messages) ⇒ Boolean
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/puppet/util/request_manager.rb', line 71 def self.(response, ) Puppet.debug 'Expected response messages:' .each do || Puppet.debug end .each do || return true unless response.body.match().nil? end Puppet.debug "The returned message doesn't match any of the expected messages" false end |
.matches_status_codes?(response, codes) ⇒ Boolean
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/puppet/util/request_manager.rb', line 59 def self.matches_status_codes?(response, codes) Puppet.debug 'Expected status codes:' codes.each do |expected_code| Puppet.debug expected_code end return true if codes.include?(Integer(response.code)) Puppet.debug "The returned status code doesn't match any of the expected status codes" false end |
.perform_http_request(uri, parameters, expectations, timeout) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/puppet/util/request_manager.rb', line 14 def self.perform_http_request(uri, parameters, expectations, timeout) start_time = Time.now begin attempt_http_request_with_expectations(uri, parameters, expectations) Puppet.debug("Request marked as passed in #{Time.now - start_time} seconds") rescue Timeout::Error, Puppet::Exceptions::ExpectationsFailError => e Puppet.debug("Request marked as failed: #{e}") if (Time.now - start_time) < timeout Puppet.debug('Sleeping 2 seconds before retry...') sleep 2 retry else raise(Puppet::Exceptions::RequestFailError, "Request marked as failed within timeout window of #{timeout}: #{e} #{e.}") end end end |