1 require 'puppet/parser/functions'
3 Puppet::Parser::Functions.newfunction(:os_database_connection,
6 This function builds a os_database_connection string from various parameters.
12 if (arguments.size != 1) then
13 raise(Puppet::ParseError, "os_database_connection(): Wrong number of arguments " +
14 "given (#{arguments.size} for 1)")
21 raise(Puppet::ParseError, "os_database_connection(): Requires an hash, got #{klass}")
25 unless (v[key].class == String) or (v[key] == :undef)
26 raise(Puppet::ParseError, "os_database_connection(): #{key} should be a String")
32 unless v.include?('dialect')
33 raise(Puppet::ParseError, 'os_database_connection(): dialect is required')
37 parts[:host] = v['host']
40 unless v.include?('database')
41 raise(Puppet::ParseError, 'os_database_connection(): database is required')
46 parts[:port] = v['port'].to_i
48 raise(Puppet::ParseError, 'os_database_connection(): host is required with port')
52 if v.include?('username') and (v['username'] != :undef) and (v['username'].to_s != '')
53 parts[:userinfo] = URI.escape(v['username'])
54 if v.include?('password') and (v['password'] != :undef) and (v['password'].to_s != '')
55 parts[:userinfo] += ":#{URI.escape(v['password'])}"
59 if v.include?('charset')
60 parts[:query] = "charset=#{v['charset']}"
63 parts[:scheme] = v['dialect']
66 parts[:path] = "/#{v['database']}"
68 parts[:path] = "///#{v['database']}"
71 URI::Generic.build(parts).to_s