From b3d0590022717c50be02dda0b1283ae3c4312206 Mon Sep 17 00:00:00 2001 From: Carlos Camacho Date: Tue, 21 Feb 2017 11:37:22 +0100 Subject: [PATCH] Allow integer port in os_transport_url Currently we only allow to use strings as the data type for ports. Due to this we need to cast the data type in the puppet modules because from THT this is configured as a Number. This submission allow to use either string or numbers for the port parameter in the os_transport_url function. Change-Id: I9e56f8e2de542b20fe9e6995506cff5bb435e220 Closes-Bug: #1664561 --- .../parser/functions/os_transport_url.rb | 7 +-- .../bugfix-1664561-f5964a3777b9ed93.yaml | 4 ++ spec/functions/os_transport_url_spec.rb | 48 ++++++++++++++++++- 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/bugfix-1664561-f5964a3777b9ed93.yaml diff --git a/lib/puppet/parser/functions/os_transport_url.rb b/lib/puppet/parser/functions/os_transport_url.rb index 28c9c573..7c611a91 100644 --- a/lib/puppet/parser/functions/os_transport_url.rb +++ b/lib/puppet/parser/functions/os_transport_url.rb @@ -10,7 +10,7 @@ Valid hash parameteres: * transport - (string) type of transport, 'rabbit' or 'amqp' * host - (string) single host * hosts - (array) array of hosts to use - * port - (string) port to connect to + * port - (string | integer) port to connect to * username - (string) connection username * password - (string) connection password * virtual_host - (string) virtual host to connect to @@ -68,6 +68,7 @@ EOS # type checking for the parameter hash v.keys.each do |key| + v[key] = v[key].to_s if key == 'port' klass = (key == 'hosts') ? Array : String klass = (key == 'query') ? Hash : klass unless (v[key].class == klass) or (v[key] == :undef) @@ -105,7 +106,7 @@ EOS if v.include?('host') host = function_normalize_ip_for_uri([v['host']]) - host += ":#{v['port']}" if v.include?('port') + host += ":#{v['port'].to_s}" if v.include?('port') if parts.include?(:userinfo) parts[:hostinfo] = "#{parts[:userinfo]}@#{host}" else @@ -118,7 +119,7 @@ EOS # normalize_ip_for_uri may return a string, so check that we still have an # array hosts = [hosts] if hosts.kind_of?(String) - hosts = hosts.map{ |h| "#{h}:#{v['port']}" } if v.include?('port') + hosts = hosts.map{ |h| "#{h}:#{v['port'].to_s}" } if v.include?('port') if parts.include?(:userinfo) parts[:hostinfo] = hosts.map { |h| "#{parts[:userinfo]}@#{h}" }.join(',') else diff --git a/releasenotes/notes/bugfix-1664561-f5964a3777b9ed93.yaml b/releasenotes/notes/bugfix-1664561-f5964a3777b9ed93.yaml new file mode 100644 index 00000000..018015ae --- /dev/null +++ b/releasenotes/notes/bugfix-1664561-f5964a3777b9ed93.yaml @@ -0,0 +1,4 @@ +--- +fixes: + - Bugfix 1664561. Allow to use Integers and Strings + for the port parameter. diff --git a/spec/functions/os_transport_url_spec.rb b/spec/functions/os_transport_url_spec.rb index 327c347b..e23d29c9 100644 --- a/spec/functions/os_transport_url_spec.rb +++ b/spec/functions/os_transport_url_spec.rb @@ -50,6 +50,19 @@ describe 'os_transport_url' do }).and_return('rabbit://guest:s3cr3t@127.0.0.1:5672/virt?read_timeout=60&ssl=1') end + it 'with a single host array for hosts and integer port' do + is_expected.to run.with_params({ + 'transport' => 'rabbit', + 'hosts' => [ '127.0.0.1' ], + 'port' => 5672, + 'username' => 'guest', + 'password' => 's3cr3t', + 'virtual_host' => 'virt', + 'ssl' => '1', + 'query' => { 'read_timeout' => '60' }, + }).and_return('rabbit://guest:s3cr3t@127.0.0.1:5672/virt?read_timeout=60&ssl=1') + end + it 'with all params for a single host' do is_expected.to run.with_params({ 'transport' => 'rabbit', @@ -63,6 +76,19 @@ describe 'os_transport_url' do }).and_return('rabbit://guest:s3cr3t@127.0.0.1:5672/virt?read_timeout=60&ssl=1') end + it 'with all params for a single host and integer port' do + is_expected.to run.with_params({ + 'transport' => 'rabbit', + 'host' => '127.0.0.1', + 'port' => 5672, + 'username' => 'guest', + 'password' => 's3cr3t', + 'virtual_host' => 'virt', + 'ssl' => '1', + 'query' => { 'read_timeout' => '60' }, + }).and_return('rabbit://guest:s3cr3t@127.0.0.1:5672/virt?read_timeout=60&ssl=1') + end + it 'with only required params for a single host' do is_expected.to run.with_params({ 'transport' => 'rabbit', @@ -74,7 +100,15 @@ describe 'os_transport_url' do is_expected.to run.with_params({ 'transport' => 'rabbit', 'host' => 'fe80::ca5b:76ff:fe4b:be3b', - 'port' => '5672' + 'port' => '5672', + }).and_return('rabbit://[fe80::ca5b:76ff:fe4b:be3b]:5672/') + end + + it 'with a single ipv6 address and integer port' do + is_expected.to run.with_params({ + 'transport' => 'rabbit', + 'host' => 'fe80::ca5b:76ff:fe4b:be3b', + 'port' => 5672, }).and_return('rabbit://[fe80::ca5b:76ff:fe4b:be3b]:5672/') end @@ -90,6 +124,18 @@ describe 'os_transport_url' do }).and_return('rabbit://guest:s3cr3t@1.1.1.1:5672,guest:s3cr3t@2.2.2.2:5672/virt?read_timeout=60') end + it 'with all params with multiple hosts and integer port' do + is_expected.to run.with_params({ + 'transport' => 'rabbit', + 'hosts' => ['1.1.1.1', '2.2.2.2'], + 'port' => 5672, + 'username' => 'guest', + 'password' => 's3cr3t', + 'virtual_host' => 'virt', + 'query' => { 'read_timeout' => '60' }, + }).and_return('rabbit://guest:s3cr3t@1.1.1.1:5672,guest:s3cr3t@2.2.2.2:5672/virt?read_timeout=60') + end + it 'with only required params for multiple hosts' do is_expected.to run.with_params({ 'transport' => 'rabbit',