2
2
3
3
# Validator class, for testing that PuppetDB is alive
4
4
class Puppet ::Util ::PuppetdbValidator
5
- attr_reader :puppetdb_server
6
- attr_reader :puppetdb_port
7
- attr_reader :use_ssl
8
- attr_reader :test_path
5
+ attr_reader :test_uri
9
6
attr_reader :test_headers
10
7
11
8
def initialize ( puppetdb_server , puppetdb_port , use_ssl = true , test_path = '/pdb/meta/v1/version' )
12
- @puppetdb_server = puppetdb_server
13
- @puppetdb_port = puppetdb_port
14
- @use_ssl = use_ssl
15
- @test_path = test_path
9
+ @test_uri = URI ( "#{ use_ssl ? 'https' : 'http' } ://#{ puppetdb_server } :#{ puppetdb_port } #{ test_path } " )
16
10
@test_headers = { 'Accept' => 'application/json' }
17
11
end
18
12
@@ -25,16 +19,17 @@ def attempt_connection
25
19
# All that we care about is that we are able to connect successfully via
26
20
# http(s), so here we're simpling hitting a somewhat arbitrary low-impact URL
27
21
# on the puppetdb server.
28
- conn = Puppet :: Network :: HttpPool . http_instance ( puppetdb_server , puppetdb_port , use_ssl )
22
+ conn = Puppet . runtime [ :http ]
29
23
30
- response = conn . get ( test_path , test_headers )
31
- unless response . is_a? ( Net ::HTTPSuccess )
32
- Puppet . notice "Unable to connect to puppetdb server (http#{ use_ssl ? 's' : '' } ://#{ puppetdb_server } :#{ puppetdb_port } ): [#{ response . code } ] #{ response . msg } "
24
+ response = conn . get ( test_uri , headers : test_headers )
25
+ if response . is_a? ( Puppet ::HTTP ::ResponseNetHTTP ) && response . success?
26
+ return true
27
+ else
28
+ Puppet . notice "Unable to connect to puppetdb server (#{ test_uri } ): [#{ response . code } ] #{ response . reason } "
33
29
return false
34
30
end
35
- return true
36
31
rescue StandardError => e
37
- Puppet . notice "Unable to connect to puppetdb server (http #{ use_ssl ? 's' : '' } :// #{ puppetdb_server } : #{ puppetdb_port } ): #{ e . message } "
32
+ Puppet . notice "Unable to connect to puppetdb server (#{ test_uri } ): #{ e . message } "
38
33
return false
39
34
end
40
35
end
0 commit comments