From 7dc6dd0e71f3b7ba8617213f893d5ccdd32b6146 Mon Sep 17 00:00:00 2001 From: Colleen Murphy Date: Sun, 31 May 2015 01:01:38 -0700 Subject: [PATCH] Fix parsing of network gateway id for router The neutron router-show command with shell formating outputs the external_gateway_info with literal backslashes to escape the double quotes. The provider must then match against the literal slashes when extracting the gateway id. If this is not done, the provider cannot detect the gateway id from the external_gateway_info property and will always attempt to change the gateway name from '' to the desired name. Change-Id: I85a18ed08886cebd14f61b65b3681a75a8d41741 --- lib/puppet/provider/neutron_router/neutron.rb | 2 +- .../provider/neutron_router/neutron_spec.rb | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/puppet/provider/neutron_router/neutron.rb b/lib/puppet/provider/neutron_router/neutron.rb index fa5f7f02a..30350fce2 100644 --- a/lib/puppet/provider/neutron_router/neutron.rb +++ b/lib/puppet/provider/neutron_router/neutron.rb @@ -119,7 +119,7 @@ EOT end def parse_gateway_network_id(external_gateway_info_) - match_data = /\{"network_id": "(.*?)"/.match(external_gateway_info_) + match_data = /\{"network_id": "(.*?)"/.match(external_gateway_info_.gsub(/\\"/,'"')) if match_data match_data[1] else diff --git a/spec/unit/provider/neutron_router/neutron_spec.rb b/spec/unit/provider/neutron_router/neutron_spec.rb index 8a2ba4a63..4bd293192 100644 --- a/spec/unit/provider/neutron_router/neutron_spec.rb +++ b/spec/unit/provider/neutron_router/neutron_spec.rb @@ -3,6 +3,7 @@ require 'spec_helper' require 'puppet/provider/neutron_router/neutron' provider_class = Puppet::Type.type(:neutron_router).provider(:neutron) +klass = Puppet::Provider::Neutron describe provider_class do @@ -50,4 +51,33 @@ describe provider_class do end + describe 'when parsing an external gateway info' do + let :resource do + Puppet::Type::Neutron_router.new(router_attrs) + end + + let :provider do + provider_class.new(resource) + end + + after :each do + klass.reset + end + + it 'should detect a gateway net id' do + klass.stubs(:auth_neutron).returns( + 'external_gateway_info="{\"network_id\": \"1b-b1\", \"enable_snat\": true, \"external_fixed_ips\": [{\"subnet_id\": \"1b-b1\", \"ip_address\": \"1.1.1.1\"}]}"' + ) + result = klass.get_neutron_resource_attrs 'foo', nil + expect(provider.parse_gateway_network_id(result['external_gateway_info'])).to eql('1b-b1') + end + + it 'should return empty value, if there is no net id found' do + klass.stubs(:auth_neutron).returns('external_gateway_info="{}"') + result = klass.get_neutron_resource_attrs 'foo', nil + expect(provider.parse_gateway_network_id(result['external_gateway_info'])).to eql('') + end + + end + end