diff --git a/lib/puppet/functions/encode_url_queries_for_python.rb b/lib/puppet/functions/encode_url_queries_for_python.rb new file mode 100644 index 000000000..5c20b9885 --- /dev/null +++ b/lib/puppet/functions/encode_url_queries_for_python.rb @@ -0,0 +1,19 @@ +Puppet::Functions.create_function(:encode_url_queries_for_python) do + + def encode_url_queries_for_python(*args) + require 'uri' + + if (args.size != 1) then + raise Puppet::ParseError, 'encode_url_queries_for_python(): Wrong number of arguments' + end + queries = args[0] + if queries.class != Hash + raise Puppet::ParseError, "encode_url_queries_for_python(): Requires a Hash, got #{queries.class}" + end + + if queries.empty? + return '' + end + return '?' + URI.encode_www_form(queries).gsub(/%/, '%%') + end +end diff --git a/manifests/migration/libvirt.pp b/manifests/migration/libvirt.pp index de107bd3d..b450fa3eb 100644 --- a/manifests/migration/libvirt.pp +++ b/manifests/migration/libvirt.pp @@ -233,13 +233,7 @@ class nova::migration::libvirt( $postfix = '' } - if empty($client_extraparams) { - $extra_params ='' - } else { - $extra_params_before_python_escape = join(uriescape(join_keys_to_values($client_extraparams, '=')), '&') - # Must escape % as nova interprets it incorrectly. - $extra_params = sprintf('?%s', regsubst($extra_params_before_python_escape, '%', '%%', 'G')) - } + $extra_params = encode_url_queries_for_python($client_extraparams) $live_migration_uri = "qemu+${transport}://${prefix}%s${postfix}/system${extra_params}" diff --git a/spec/unit/functions/encode_url_queries_for_python_spec.rb b/spec/unit/functions/encode_url_queries_for_python_spec.rb new file mode 100644 index 000000000..39df9c979 --- /dev/null +++ b/spec/unit/functions/encode_url_queries_for_python_spec.rb @@ -0,0 +1,9 @@ +require 'spec_helper' +require 'puppet' + +describe 'encode_url_queries_for_python', :type => :puppet_function do + + it { is_expected.to run.with_params({}).and_return('') } + it { is_expected.to run.with_params({'a' => 1, 'b' => 2}).and_return('?a=1&b=2') } + it { is_expected.to run.with_params({'a' => 'b%c'}).and_return('?a=b%%25c') } +end