From 0536a214f46a7125fc634d359ae22a0cb6f35267 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Gagne=CC=81?= Date: Fri, 4 Jul 2014 00:51:32 -0400 Subject: [PATCH] Add os_database_connection function The os_database_connection function is an helper used to build database connection URI from various parameters. Example: os_database_connection({ dialect => 'mysql', host => '127.0.0.1', port => '3306', username => 'guest', password => 's3cr3t', database => 'test', charset => 'utf-8' }) Result: mysql://guest:s3cr3t@127.0.0.1:3306/test?charset=utf-8 Change-Id: Id0bde33891112e36f13d3f8fdf0ff89820c09c01 --- .../functions/os_database_connection.rb | 72 ++++++++++ spec/functions/os_database_connection_spec.rb | 134 ++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 lib/puppet/parser/functions/os_database_connection.rb create mode 100644 spec/functions/os_database_connection_spec.rb diff --git a/lib/puppet/parser/functions/os_database_connection.rb b/lib/puppet/parser/functions/os_database_connection.rb new file mode 100644 index 00000000..8764f1b8 --- /dev/null +++ b/lib/puppet/parser/functions/os_database_connection.rb @@ -0,0 +1,72 @@ +require 'puppet/parser/functions' + +Puppet::Parser::Functions.newfunction(:os_database_connection, + :type => :rvalue, + :doc => <<-EOS +This function builds a os_database_connection string from various parameters. +EOS +) do |arguments| + + require 'uri' + + if (arguments.size != 1) then + raise(Puppet::ParseError, "os_database_connection(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") + end + + v = arguments[0] + klass = v.class + + unless klass == Hash + raise(Puppet::ParseError, "os_database_connection(): Requires an hash, got #{klass}") + end + + v.keys.each do |key| + unless (v[key].class == String) or (v[key] == :undef) + raise(Puppet::ParseError, "os_database_connection(): #{key} should be a String") + end + end + + parts = {} + + unless v.include?('dialect') + raise(Puppet::ParseError, 'os_database_connection(): dialect is required') + end + + if v.include?('host') + parts[:host] = v['host'] + end + + unless v.include?('database') + raise(Puppet::ParseError, 'os_database_connection(): database is required') + end + + if v.include?('port') + if v.include?('host') + parts[:port] = v['port'].to_i + else + raise(Puppet::ParseError, 'os_database_connection(): host is required with port') + end + end + + if v.include?('username') and (v['username'] != :undef) and (v['username'].to_s != '') + parts[:userinfo] = URI.escape(v['username']) + if v.include?('password') and (v['password'] != :undef) and (v['password'].to_s != '') + parts[:userinfo] += ":#{URI.escape(v['password'])}" + end + end + + if v.include?('charset') + parts[:query] = "charset=#{v['charset']}" + end + + parts[:scheme] = v['dialect'] + + if v.include?('host') + parts[:path] = "/#{v['database']}" + else + parts[:path] = "///#{v['database']}" + end + + URI::Generic.build(parts).to_s +end diff --git a/spec/functions/os_database_connection_spec.rb b/spec/functions/os_database_connection_spec.rb new file mode 100644 index 00000000..6819e7e8 --- /dev/null +++ b/spec/functions/os_database_connection_spec.rb @@ -0,0 +1,134 @@ +require 'spec_helper' + +describe 'os_database_connection' do + + it 'refuses String' do + should run.with_params('foo').\ + and_raise_error(Puppet::ParseError, /Requires an hash/) + end + + it 'refuses Array' do + should run.with_params(['foo']).\ + and_raise_error(Puppet::ParseError, /Requires an hash/) + end + + it 'refuses without at least one argument' do + should run.with_params().\ + and_raise_error(Puppet::ParseError, /Wrong number of arguments/) + end + + it 'refuses too many arguments' do + should run.with_params('foo', 'bar').\ + and_raise_error(Puppet::ParseError, /Wrong number of arguments/) + end + + it 'fails if port is provided with missing host' do + should run.with_params({ + 'dialect' => 'sqlite', + 'database' => '/var/lib/keystone/keystone.db', + 'port' => '3306', + 'charset' => 'utf-8' + }).and_raise_error(Puppet::ParseError, /host is required with port/) + end + + context 'creates the correct connection URI' do + + it 'with all parameters' do + should run.with_params({ + 'dialect' => 'mysql', + 'host' => '127.0.0.1', + 'port' => '3306', + 'database' => 'test', + 'username' => 'guest', + 'password' => 's3cr3t', + 'charset' => 'utf-8' + }).and_return('mysql://guest:s3cr3t@127.0.0.1:3306/test?charset=utf-8') + end + + it 'without port' do + should run.with_params({ + 'dialect' => 'mysql', + 'host' => '127.0.0.1', + 'database' => 'test', + 'username' => 'guest', + 'password' => 's3cr3t', + 'charset' => 'utf-8' + }).and_return('mysql://guest:s3cr3t@127.0.0.1/test?charset=utf-8') + end + + it 'without host and port' do + should run.with_params({ + 'dialect' => 'sqlite', + 'database' => '/var/lib/keystone/keystone.db', + 'charset' => 'utf-8' + }).and_return('sqlite:////var/lib/keystone/keystone.db?charset=utf-8') + end + + it 'without username and password' do + should run.with_params({ + 'dialect' => 'mysql', + 'host' => '127.0.0.1', + 'port' => '3306', + 'database' => 'test', + 'charset' => 'utf-8' + }).and_return('mysql://127.0.0.1:3306/test?charset=utf-8') + end + + it 'with username set to undef' do + should run.with_params({ + 'dialect' => 'mysql', + 'host' => '127.0.0.1', + 'port' => '3306', + 'database' => 'test', + 'username' => :undef, + 'charset' => 'utf-8' + }).and_return('mysql://127.0.0.1:3306/test?charset=utf-8') + end + + it 'with username set to an empty string' do + should run.with_params({ + 'dialect' => 'mysql', + 'host' => '127.0.0.1', + 'port' => '3306', + 'database' => 'test', + 'username' => '', + 'charset' => 'utf-8' + }).and_return('mysql://127.0.0.1:3306/test?charset=utf-8') + end + + it 'without password' do + should run.with_params({ + 'dialect' => 'mysql', + 'host' => '127.0.0.1', + 'port' => '3306', + 'database' => 'test', + 'username' => 'guest', + 'charset' => 'utf-8' + }).and_return('mysql://guest@127.0.0.1:3306/test?charset=utf-8') + end + + it 'with password set to undef' do + should run.with_params({ + 'dialect' => 'mysql', + 'host' => '127.0.0.1', + 'port' => '3306', + 'database' => 'test', + 'username' => 'guest', + 'password' => :undef, + 'charset' => 'utf-8' + }).and_return('mysql://guest@127.0.0.1:3306/test?charset=utf-8') + end + + it 'with password set to an empty string' do + should run.with_params({ + 'dialect' => 'mysql', + 'host' => '127.0.0.1', + 'port' => '3306', + 'database' => 'test', + 'username' => 'guest', + 'password' => '', + 'charset' => 'utf-8' + }).and_return('mysql://guest@127.0.0.1:3306/test?charset=utf-8') + end + end +end