Class: Puppet::Util::MongodbValidator

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mongodb_resource_name, mongodb_server, mongodb_port) ⇒ MongodbValidator

Returns a new instance of MongodbValidator.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/puppet/util/mongodb_validator.rb', line 13

def initialize(mongodb_resource_name, mongodb_server, mongodb_port)
  # NOTE: (spredzy) : By relying on the uri module
  # we rely on its well tested interface to parse
  # both IPv4 and IPv6 based URL with a port specified.
  # Unfortunately URI needs a scheme, hence the http
  # string here to make the string URI compliant.
  uri = URI("http://#{mongodb_resource_name}")
  @mongodb_server = IPAddr.new(uri.host).to_s
  @mongodb_port = uri.port
rescue StandardError
  @mongodb_server = mongodb_server.to_s
  @mongodb_port   = mongodb_port
end

Instance Attribute Details

#mongodb_portObject (readonly)

Returns the value of attribute mongodb_port.



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

def mongodb_port
  @mongodb_port
end

#mongodb_serverObject (readonly)

Returns the value of attribute mongodb_server.



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

def mongodb_server
  @mongodb_server
end

Instance Method Details

#attempt_connectionObject

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



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppet/util/mongodb_validator.rb', line 32

def attempt_connection
  Timeout.timeout(Puppet[:http_connect_timeout]) do
    TCPSocket.new(@mongodb_server, @mongodb_port).close
    true
  rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH => e
    Puppet.debug "Unable to connect to mongodb server (#{@mongodb_server}:#{@mongodb_port}): #{e.message}"
    false
  end
rescue Timeout::Error
  false
end