diff --git a/manifests/integrations/pgbouncer.pp b/manifests/integrations/pgbouncer.pp index dcecbadf..9d88fdfc 100644 --- a/manifests/integrations/pgbouncer.pp +++ b/manifests/integrations/pgbouncer.pp @@ -3,36 +3,57 @@ # This class will install the necessary configuration for the pgbouncer integration # # Parameters: -# $password +# $password: # The password for the datadog user # $host: # The host pgbouncer is listening on -# $port +# $port: # The pgbouncer port number -# $username +# $username: # The username for the datadog user -# $tags +# $tags: # Optional array of tags +# $pgbouncers: +# Optional array of pgbouncer hashes. See example # # Sample Usage: # # class { 'datadog_agent::integrations::pgbouncer' : -# host => 'localhost', -# username => 'datadog', -# port => '6432', -# password => 'some_pass', +# host => 'localhost', +# username => 'datadog', +# port => '6432', +# password => 'some_pass', # } # +# class { 'datadog_agent::integrations::pgbouncer' : +# pgbouncers => [ +# { +# 'host' => 'localhost', +# 'username' => 'datadog', +# 'port' => '6432', +# 'password' => 'some_pass', +# 'tags' => ['instance:one'], +# }, +# { +# 'host' => 'localhost', +# 'username' => 'datadog2', +# 'port' => '6433', +# 'password' => 'some_pass2', +# }, +# ], +# } # class datadog_agent::integrations::pgbouncer( - $password, + $password = '', $host = 'localhost', $port = '6432', $username = 'datadog', $tags = [], + $pgbouncers = [], ) inherits datadog_agent::params { validate_array($tags) + validate_array($pgbouncers) if $::datadog_agent::agent6_enable { $dst = "${datadog_agent::conf6_dir}/pgbouncer.yaml" diff --git a/spec/classes/datadog_agent_integrations_pgbouncer_spec.rb b/spec/classes/datadog_agent_integrations_pgbouncer_spec.rb new file mode 100644 index 00000000..86f6f690 --- /dev/null +++ b/spec/classes/datadog_agent_integrations_pgbouncer_spec.rb @@ -0,0 +1,85 @@ +require 'spec_helper' + +describe 'datadog_agent::integrations::pgbouncer' do + context 'supported agents - v5 and v6' do + agents = { '5' => false, '6' => true } + agents.each do |_, enabled| + let(:pre_condition) { "class {'::datadog_agent': agent6_enable => #{enabled}}" } + let(:facts) {{ + operatingsystem: 'Ubuntu', + }} + if !enabled + let(:conf_dir) { '/etc/dd-agent/conf.d' } + else + let(:conf_dir) { '/etc/datadog-agent/conf.d' } + end + let(:dd_user) { 'dd-agent' } + let(:dd_group) { 'root' } + let(:dd_package) { 'datadog-agent' } + let(:dd_service) { 'datadog-agent' } + let(:conf_file) { "#{conf_dir}/pgbouncer.yaml" } + + + context 'with default parameters' do + let(:params) {{ + password: 'foobar', + }} + it { should contain_file(conf_file).with_content(%r{host: localhost}) } + it { should contain_file(conf_file).with_content(%r{port: 6432}) } + it { should contain_file(conf_file).with_content(%r{password: foobar}) } + + it { should compile.with_all_deps } + it { should contain_file(conf_file).with( + owner: dd_user, + group: dd_group, + mode: '0600', + )} + it { should contain_file(conf_file).that_requires("Package[#{dd_package}]") } + it { should contain_file(conf_file).that_notifies("Service[#{dd_service}]") } + end + + context 'with one pgbouncer config parameters' do + let(:params) {{ + host: 'localhost', + username: 'foo', + port: '1234', + password: 'bar', + tags: ['foo:bar'], + }} + it { should contain_file(conf_file).with_content(%r{host: localhost}) } + it { should contain_file(conf_file).with_content(%r{username: foo}) } + it { should contain_file(conf_file).with_content(%r{port: ("|')?1234("|')?}) } + it { should contain_file(conf_file).with_content(%r{password: bar}) } + it { should contain_file(conf_file).with_content(%r{- ("|')?foo:bar("|')?}) } + end + + context 'with multiple pgbouncers configured' do + let(:params) {{ + pgbouncers: [ + { + 'host' => 'localhost', + 'username' => 'datadog', + 'port' => '6432', + 'password' => 'some_pass', + 'tags' => ['instance:one'], + }, + { + 'host' => 'localhost', + 'username' => 'datadog2', + 'port' => '6433', + 'password' => 'some_pass2', + 'tags' => ['instance:two'], + } + ] + }} + it { should contain_file(conf_file).with_content(%r{host: localhost}) } + it { should contain_file(conf_file).with_content(%r{port: ("|')?6432("|')?}) } + it { should contain_file(conf_file).with_content(%r{password: some_pass}) } + it { should contain_file(conf_file).with_content(%r{- ("|')?instance:one("|')?}) } + it { should contain_file(conf_file).with_content(%r{port: ("|')?6433("|')?}) } + it { should contain_file(conf_file).with_content(%r{password: some_pass2}) } + it { should contain_file(conf_file).with_content(%r{- ("|')?instance:two("|')?}) } + end + end + end +end diff --git a/templates/agent-conf.d/pgbouncer.yaml.erb b/templates/agent-conf.d/pgbouncer.yaml.erb index d676099b..1446bdc5 100644 --- a/templates/agent-conf.d/pgbouncer.yaml.erb +++ b/templates/agent-conf.d/pgbouncer.yaml.erb @@ -1,23 +1,44 @@ +<% +require 'yaml' + +if RUBY_VERSION < "1.9" +# Hack to avoid ruby 1.8 always reordering the hashes and +# changing this template resource at every run +# See https://github.com/DataDog/puppet-datadog-agent/issues/43 +# Taken from http://www.dzone.com/snippets/generating-yaml-hashes-sorted +class Hash + # Replacing the to_yaml function so it'll serialize hashes sorted (by their keys) + # + # Original function is in /usr/lib/ruby/1.8/yaml/rubytypes.rb + def to_yaml( opts = {} ) + YAML::quick_emit( object_id, opts ) do |out| + out.map( taguri, to_yaml_style ) do |map| + sort.each do |k, v| # <-- here's my addition (the 'sort') + map.add( k, v ) + end + end + end + end +end +end +%> + +<%- if @pgbouncers.empty? -%> init_config: instances: -# - host: localhost -# port: 6432 -# username: my_username -# password: my_password -# tags: -# - optional_tag1 -# - optional_tag2 -# - host: <%= @host %> port: <%= @port %> username: <%= @username %> password: <%= @password %> -<% if @tags and ! @tags.empty? -%> + <% if @tags and ! @tags.empty? -%> tags: - <%- Array(@tags).each do |tag| -%> - <%- if tag != '' -%> + <%- Array(@tags).each do |tag| -%> + <%- if tag != '' -%> - <%= tag %> + <%- end -%> <%- end -%> - <%- end -%> -<% end -%> + <% end -%> +<%- else -%> +<%= {'init_config'=>nil, 'instances'=>@pgbouncers}.to_yaml %> +<%- end -%>