| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', '..')) |
| 4 |
require 'puppet/util/postgresql_validator' |
| 5 |
|
| 6 |
# This file contains a provider for the resource type `postgresql_conn_validator`, |
| 7 |
# which validates the puppetdb connection by attempting an https connection. |
| 8 |
|
| 9 |
Puppet::Type.type(:postgresql_conn_validator).provide(:ruby) do |
| 10 |
desc "A provider for the resource type `postgresql_conn_validator`, |
| 11 |
which validates the PostgreSQL connection by attempting a query |
| 12 |
to the target PostgreSQL server." |
| 13 |
|
| 14 |
# Test to see if the resource exists, returns true if it does, false if it |
| 15 |
# does not. |
| 16 |
# |
| 17 |
# Here we simply monopolize the resource API, to execute a test to see if the |
| 18 |
# database is connectable. When we return a state of `false` it triggers the |
| 19 |
# create method where we can return an error message. |
| 20 |
# |
| 21 |
# @return [bool] did the test succeed? |
| 22 |
def exists? |
| 23 |
validator.attempt_connection(resource[:sleep], resource[:tries]) |
| 24 |
end |
| 25 |
|
| 26 |
# This method is called when the exists? method returns false. |
| 27 |
# |
| 28 |
# @return [void] |
| 29 |
def create |
| 30 |
# If `#create` is called, that means that `#exists?` returned false, which |
| 31 |
# means that the connection could not be established... so we need to |
| 32 |
# cause a failure here. |
| 33 |
raise Puppet::Error, "Unable to connect to PostgreSQL server! (#{resource[:host]}:#{resource[:port]})" |
| 34 |
end |
| 35 |
|
| 36 |
# Returns the existing validator, if one exists otherwise creates a new object |
| 37 |
# from the class. |
| 38 |
# |
| 39 |
# @api private |
| 40 |
def validator |
| 41 |
@validator ||= Puppet::Util::PostgresqlValidator.new(resource) |
| 42 |
end |
| 43 |
end |