Class: Puppet::Util::SensuAPIValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/util/sensu_api_validator.rb

Overview

Validator class, for testing that SensuAPI is alive

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sensu_api_server, sensu_api_port, use_ssl = false, test_path = "/version") ⇒ SensuAPIValidator

Returns a new instance of SensuAPIValidator.



13
14
15
16
17
18
# File 'lib/puppet/util/sensu_api_validator.rb', line 13

def initialize(sensu_api_server, sensu_api_port, use_ssl=false, test_path = "/version")
  @sensu_api_server = sensu_api_server
  @sensu_api_port   = sensu_api_port
  @use_ssl         = use_ssl
  @test_path       = test_path
end

Instance Attribute Details

#sensu_api_portObject (readonly)

Returns the value of attribute sensu_api_port.



8
9
10
# File 'lib/puppet/util/sensu_api_validator.rb', line 8

def sensu_api_port
  @sensu_api_port
end

#sensu_api_serverObject (readonly)

Returns the value of attribute sensu_api_server.



7
8
9
# File 'lib/puppet/util/sensu_api_validator.rb', line 7

def sensu_api_server
  @sensu_api_server
end

#test_headersObject (readonly)

Returns the value of attribute test_headers.



11
12
13
# File 'lib/puppet/util/sensu_api_validator.rb', line 11

def test_headers
  @test_headers
end

#test_pathObject (readonly)

Returns the value of attribute test_path.



10
11
12
# File 'lib/puppet/util/sensu_api_validator.rb', line 10

def test_path
  @test_path
end

#use_sslObject (readonly)

Returns the value of attribute use_ssl.



9
10
11
# File 'lib/puppet/util/sensu_api_validator.rb', line 9

def use_ssl
  @use_ssl
end

Instance Method Details

#attempt_connectionObject

Utility method; attempts to make an http/https connection to the sensu_api server. This is abstracted out into a method so that it can be called multiple times for retry attempts.

Returns:

  • true if the connection is successful, false otherwise.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/puppet/util/sensu_api_validator.rb', line 25

def attempt_connection
  # All that we care about is that we are able to connect successfully via
  # http(s), so here we're simpling hitting a somewhat arbitrary low-impact URL
  # on the sensu_api server.
  http = Net::HTTP.new(@sensu_api_server, @sensu_api_port)
  http.use_ssl = @use_ssl
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Get.new(@test_path)
  request.add_field("Accept", "application/json")
  response = http.request(request)

  unless response.kind_of?(Net::HTTPSuccess) || response.kind_of?(Net::HTTPUnauthorized)
    Puppet.notice "Unable to connect to sensu_api server (http#{use_ssl ? "s" : ""}://#{sensu_api_server}:#{sensu_api_port}): [#{response.code}] #{response.msg}"
    return false
  end
  return true
rescue Exception => e
  Puppet.notice "Unable to connect to sensu_api server (http#{use_ssl ? "s" : ""}://#{sensu_api_server}:#{sensu_api_port}): #{e.message}"
  return false
end