5
5
6
6
require 'uri'
7
7
8
- # Redefine constants to match PuppetDB defaults.
9
- # This code avoids warnings about redefining constants.
10
- URI ::HTTP . send ( :remove_const , :DEFAULT_PORT ) if URI ::HTTP . const_defined? ( :DEFAULT_PORT )
11
- URI ::HTTP . const_set ( :DEFAULT_PORT , 8080 )
12
- URI ::HTTPS . send ( :remove_const , :DEFAULT_PORT ) if URI ::HTTPS . const_defined? ( :DEFAULT_PORT )
13
- URI ::HTTPS . const_set ( :DEFAULT_PORT , 8081 )
14
-
15
8
module OctocatalogDiff
16
9
# A standard way to connect to PuppetDB from the various scripts in this repository.
17
10
class PuppetDB
11
+ DEFAULT_HTTPS_PORT = 8081
12
+ DEFAULT_HTTP_PORT = 8080
13
+
18
14
# Allow connections to be read (used in tests for now)
19
15
attr_reader :connections
20
16
@@ -54,7 +50,7 @@ def initialize(options = {})
54
50
urls . map { |url | parse_url ( url ) }
55
51
elsif options . key? ( :puppetdb_host )
56
52
is_ssl = options . fetch ( :puppetdb_ssl , true )
57
- default_port = is_ssl ? URI :: HTTPS :: DEFAULT_PORT : URI :: HTTP :: DEFAULT_PORT
53
+ default_port = is_ssl ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT
58
54
port = options . fetch ( :puppetdb_port , default_port ) . to_i
59
55
[ { ssl : is_ssl , host : options [ :puppetdb_host ] , port : port } ]
60
56
elsif ENV [ 'PUPPETDB_URL' ] && !ENV [ 'PUPPETDB_URL' ] . empty?
@@ -64,7 +60,7 @@ def initialize(options = {})
64
60
# This will get the env var and see if it equals 'true'; the result
65
61
# of this == comparison is the true/false boolean we need.
66
62
is_ssl = ENV . fetch ( 'PUPPETDB_SSL' , 'true' ) == 'true'
67
- default_port = is_ssl ? URI :: HTTPS :: DEFAULT_PORT : URI :: HTTP :: DEFAULT_PORT
63
+ default_port = is_ssl ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT
68
64
port = ENV . fetch ( 'PUPPETDB_PORT' , default_port ) . to_i
69
65
[ { ssl : is_ssl , host : ENV [ 'PUPPETDB_HOST' ] , port : port } ]
70
66
else
@@ -152,6 +148,10 @@ def _get(path)
152
148
# @return [Hash] { ssl: true/false, host: <String>, port: <Integer> }
153
149
def parse_url ( url )
154
150
uri = URI ( url )
151
+ if URI . split ( url ) [ 3 ] . nil?
152
+ uri . port = uri . scheme == 'https' ? DEFAULT_HTTPS_PORT : DEFAULT_HTTP_PORT
153
+ end
154
+
155
155
raise ArgumentError , "URL #{ url } has invalid scheme" unless uri . scheme =~ /^https?$/
156
156
{ ssl : uri . scheme == 'https' , host : uri . host , port : uri . port }
157
157
rescue URI ::InvalidURIError => exc
0 commit comments