Class: Puppet::Util::GrafanaConnValidator

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

Overview

Validator class, for testing that Grafana is alive

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grafana_url, grafana_api_path) ⇒ GrafanaConnValidator

Returns a new instance of GrafanaConnValidator.



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

def initialize(grafana_url, grafana_api_path)
  @grafana_url      = grafana_url
  @grafana_api_path = grafana_api_path
end

Instance Attribute Details

#grafana_api_pathObject (readonly)

Returns the value of attribute grafana_api_path.



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

def grafana_api_path
  @grafana_api_path
end

#grafana_urlObject (readonly)

Returns the value of attribute grafana_url.



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

def grafana_url
  @grafana_url
end

Instance Method Details

#attempt_connectionObject

Utility method; attempts to make an http/https connection to the Grafana 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.



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

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 Grafana server.
  grafana_host = URI.parse(@grafana_url).host
  grafana_port = URI.parse(@grafana_url).port
  grafana_scheme = URI.parse(@grafana_url).scheme
  http = Net::HTTP.new(grafana_host, grafana_port)
  http.use_ssl = (grafana_scheme == 'https')
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Get.new(@grafana_api_path)
  request.add_field('Accept', 'application/json')
  response = http.request(request)

  unless response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPUnauthorized)
    Puppet.notice "Unable to connect to Grafana server (#{grafana_scheme}://#{grafana_host}:#{grafana_port}): [#{response.code}] #{response.msg}"
    return false
  end
  true
rescue Exception => e # rubocop:disable Lint/RescueException
  Puppet.notice "Unable to connect to Grafana server (#{grafana_scheme}://#{grafana_host}:#{grafana_port}): #{e.message}"
  false
end