Class: Puppet::Util::ScalemgmtValidator

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

Overview

Validator class, for testing that scalemgmt is alive

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scalemgmt_server, scalemgmt_port, api_user = 'admin', api_password = 'admin001', test_path = '/scalemgmt/v2/info') ⇒ ScalemgmtValidator

Returns a new instance of ScalemgmtValidator.



10
11
12
13
14
15
16
17
# File 'lib/puppet/util/scalemgmt_validator.rb', line 10

def initialize(scalemgmt_server, scalemgmt_port, api_user = 'admin', api_password = 'admin001', test_path = '/scalemgmt/v2/info')
  @scalemgmt_server = scalemgmt_server
  @scalemgmt_port   = scalemgmt_port
  @api_user         = api_user
  @api_password     = api_password
  @test_path        = test_path
  @test_headers     = { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
end

Instance Attribute Details

#api_passwordObject (readonly)

Returns the value of attribute api_password.



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

def api_password
  @api_password
end

#api_userObject (readonly)

Returns the value of attribute api_user.



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

def api_user
  @api_user
end

#scalemgmt_portObject (readonly)

Returns the value of attribute scalemgmt_port.



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

def scalemgmt_port
  @scalemgmt_port
end

#scalemgmt_serverObject (readonly)

Returns the value of attribute scalemgmt_server.



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

def scalemgmt_server
  @scalemgmt_server
end

#test_headersObject (readonly)

Returns the value of attribute test_headers.



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

def test_headers
  @test_headers
end

#test_pathObject (readonly)

Returns the value of attribute test_path.



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

def test_path
  @test_path
end

Instance Method Details

#attempt_connectionObject

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



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

def attempt_connection
  # All that we care about is that we are able to connect successfully via
  # https, so here we're simpling hitting a somewhat arbitrary low-impact URL
  # on the scalemgmt server.
  http = Net::HTTP.new(scalemgmt_server, scalemgmt_port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Get.new(test_path, test_headers)
  request.basic_auth(api_user, api_password)

  response = http.request(request)
  unless response.is_a?(Net::HTTPSuccess)
    Puppet.notice "Unable to connect to scalemgmt server (https://#{scalemgmt_server}:#{scalemgmt_port}): [#{response.code}] #{response.msg}"
    return false
  end
  true
rescue Exception => e # rubocop:disable Lint/RescueException
  Puppet.notice "Unable to connect to scalemgmt server (https://#{scalemgmt_server}:#{scalemgmt_port}): #{e.message}"
  false
end