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