55
66require 'uri'
77
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-
158module OctocatalogDiff
169 # A standard way to connect to PuppetDB from the various scripts in this repository.
1710 class PuppetDB
11+ DEFAULT_HTTPS_PORT = 8081
12+ DEFAULT_HTTP_PORT = 8080
13+
1814 # Allow connections to be read (used in tests for now)
1915 attr_reader :connections
2016
@@ -54,7 +50,7 @@ def initialize(options = {})
5450 urls . map { |url | parse_url ( url ) }
5551 elsif options . key? ( :puppetdb_host )
5652 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
5854 port = options . fetch ( :puppetdb_port , default_port ) . to_i
5955 [ { ssl : is_ssl , host : options [ :puppetdb_host ] , port : port } ]
6056 elsif ENV [ 'PUPPETDB_URL' ] && !ENV [ 'PUPPETDB_URL' ] . empty?
@@ -64,7 +60,7 @@ def initialize(options = {})
6460 # This will get the env var and see if it equals 'true'; the result
6561 # of this == comparison is the true/false boolean we need.
6662 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
6864 port = ENV . fetch ( 'PUPPETDB_PORT' , default_port ) . to_i
6965 [ { ssl : is_ssl , host : ENV [ 'PUPPETDB_HOST' ] , port : port } ]
7066 else
@@ -152,6 +148,10 @@ def _get(path)
152148 # @return [Hash] { ssl: true/false, host: <String>, port: <Integer> }
153149 def parse_url ( url )
154150 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+
155155 raise ArgumentError , "URL #{ url } has invalid scheme" unless uri . scheme =~ /^https?$/
156156 { ssl : uri . scheme == 'https' , host : uri . host , port : uri . port }
157157 rescue URI ::InvalidURIError => exc
0 commit comments