From 279cb6f83086e5c1d2ba39674a976cc86f7d1dd3 Mon Sep 17 00:00:00 2001
From: swwolf <wolf@shapeways.com>
Date: Fri, 21 Jul 2017 10:30:08 +0200
Subject: [PATCH] datadog integration for twemproxy, options for haproxy (#326)

* add instances parameter

* building config from instances

* spec test for instances set

* adding datadog integration for twemproxy

* add options to haproxy integration

* following instances pattern for twemproxy integration

* fix twemproxy template typo

* fix twemproxy template typo

* fix twemproxy param error

* fix spec test
---
 manifests/integrations/haproxy.pp             | 15 +++---
 manifests/integrations/twemproxy.pp           | 53 +++++++++++++++++++
 ...datadog_agent_integrations_haproxy_spec.rb | 29 +++++++---
 ...tadog_agent_integrations_twemproxy_spec.rb | 46 ++++++++++++++++
 templates/agent-conf.d/haproxy.yaml.erb       |  5 ++
 templates/agent-conf.d/twemproxy.yaml.erb     | 11 ++++
 6 files changed, 147 insertions(+), 12 deletions(-)
 create mode 100644 manifests/integrations/twemproxy.pp
 create mode 100644 spec/classes/datadog_agent_integrations_twemproxy_spec.rb
 create mode 100644 templates/agent-conf.d/twemproxy.yaml.erb

diff --git a/manifests/integrations/haproxy.pp b/manifests/integrations/haproxy.pp
index cd4f5bdc..79fe9a19 100644
--- a/manifests/integrations/haproxy.pp
+++ b/manifests/integrations/haproxy.pp
@@ -9,23 +9,26 @@
 # Sample Usage:
 #
 #   class { 'datadog_agent::integrations::haproxy' :
-#     url   => 'http://localhost:8080',
-#     creds => { username => 'admin',
-#                password => 'password',
-#              },
+#     url     => 'http://localhost:8080',
+#     creds   => { username => 'admin',
+#                  password => 'password',
+#                },
+#     options => { collect_aggregates_only => 'False' },
 #   }
 #
 class datadog_agent::integrations::haproxy(
   $creds     = {},
   $url       = "http://${::ipaddress}:8080",
+  $options   = {},
   $instances = undef,
 ) inherits datadog_agent::params {
   include datadog_agent
 
   if !$instances and $url {
     $_instances = [{
-      'creds' => $creds,
-      'url'   => $url,
+      'creds'   => $creds,
+      'url'     => $url,
+      'options' => $options,
     }]
   } elsif !$instances {
     $_instances = []
diff --git a/manifests/integrations/twemproxy.pp b/manifests/integrations/twemproxy.pp
new file mode 100644
index 00000000..f1280c3e
--- /dev/null
+++ b/manifests/integrations/twemproxy.pp
@@ -0,0 +1,53 @@
+# Class: datadog_agent::integrations::twemproxy
+#
+# This class will install the necessary configuration for the twemproxy aka nutcracker integration
+#
+# Parameters:
+#   $host:
+#       The host twemproxy is running on. Defaults to '127.0.0.1'
+#   $port
+#       The twemproxy password for the datadog user. Defaults to 22222
+#
+# Sample Usage:
+#
+#  class { 'datadog_agent::integrations::twemproxy' :
+#    instances => [
+#      {
+#        'host' => 'localhost',
+#        'port' => '22222',
+#      },
+#      {
+#        'host' => 'localhost',
+#        'port' => '22223',
+#      },
+#    ]
+#  }
+#
+class datadog_agent::integrations::twemproxy(
+  $host = 'localhost',
+  $port = '22222',
+  $instances = undef,
+) inherits datadog_agent::params {
+  include datadog_agent
+
+  if !$instances and $host {
+    $_instances = [{
+      'host' => $host,
+      'port' => $port,
+    }]
+  } elsif !$instances{
+    $_instances = []
+  } else {
+    $_instances = $instances
+  }
+
+  file { "${datadog_agent::params::conf_dir}/twemproxy.yaml":
+    ensure  => file,
+    owner   => $datadog_agent::params::dd_user,
+    group   => $datadog_agent::params::dd_group,
+    mode    => '0600',
+    content => template('datadog_agent/agent-conf.d/twemproxy.yaml.erb'),
+    require => Package[$datadog_agent::params::package_name],
+    notify  => Service[$datadog_agent::params::service_name]
+  }
+}
diff --git a/spec/classes/datadog_agent_integrations_haproxy_spec.rb b/spec/classes/datadog_agent_integrations_haproxy_spec.rb
index 90c98672..eb082ef0 100644
--- a/spec/classes/datadog_agent_integrations_haproxy_spec.rb
+++ b/spec/classes/datadog_agent_integrations_haproxy_spec.rb
@@ -55,30 +55,47 @@
     end
   end
 
+  context 'with options set' do
+    let(:params) {{
+      options: {
+        'optionk' => 'optionv',
+      },
+    }}
+    it { should contain_file(conf_file).with_content(%r{optionk: optionv}) }
+  end
+
   context 'with instances set' do
     let(:params) {{
       instances: [
         {
-          'url'   => 'http://foo.bar:8421',
-          'creds' => {
+          'url'     => 'http://foo.bar:8421',
+          'creds'   => {
             'username' => 'foo',
             'password' => 'bar',
-          }
+          },
+          'options' => {
+            'optionk1' => 'optionv1',
+          },
         },
         {
-          'url'   => 'http://shoe.baz:1248',
-          'creds' => {
+          'url'     => 'http://shoe.baz:1248',
+          'creds'   => {
             'username' => 'shoe',
             'password' => 'baz',
-          }
+          },
+          'options' => {
+            'optionk2' => 'optionv2',
+          },
         },
       ]
     }}
     it { should contain_file(conf_file).with_content(%r{url: http://foo.bar:8421}) }
     it { should contain_file(conf_file).with_content(%r{username: foo}) }
     it { should contain_file(conf_file).with_content(%r{password: bar}) }
+    it { should contain_file(conf_file).with_content(%r{optionk1: optionv1}) }
     it { should contain_file(conf_file).with_content(%r{url: http://shoe.baz:1248}) }
     it { should contain_file(conf_file).with_content(%r{username: shoe}) }
     it { should contain_file(conf_file).with_content(%r{password: baz}) }
+    it { should contain_file(conf_file).with_content(%r{optionk2: optionv2}) }
   end
 end
diff --git a/spec/classes/datadog_agent_integrations_twemproxy_spec.rb b/spec/classes/datadog_agent_integrations_twemproxy_spec.rb
new file mode 100644
index 00000000..bb7b58fe
--- /dev/null
+++ b/spec/classes/datadog_agent_integrations_twemproxy_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe 'datadog_agent::integrations::twemproxy' do
+  let(:facts) {{
+    operatingsystem: 'Ubuntu',
+  }}
+  let(:conf_dir) { '/etc/dd-agent/conf.d' }
+  let(:dd_user) { 'dd-agent' }
+  let(:dd_group) { 'root' }
+  let(:dd_package) { 'datadog-agent' }
+  let(:dd_service) { 'datadog-agent' }
+  let(:conf_file) { "#{conf_dir}/twemproxy.yaml" }
+
+  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}]") }
+
+  context 'with default parameters' do
+    it { should contain_file(conf_file).with_content(%r{host: localhost}) }
+    it { should contain_file(conf_file).with_content(%r{port: 22222}) }
+  end
+
+  context 'with parameters set' do
+    let(:params) {{
+      instances: [
+        {
+          'host' => 'twemproxy1',
+          'port' => '1234',
+        },
+        {
+          'host' => 'twemproxy2',
+          'port' => '4567',
+        }
+      ]
+    }}
+    it { should contain_file(conf_file).with_content(%r{host: twemproxy1}) }
+    it { should contain_file(conf_file).with_content(%r{port: 1234}) }
+    it { should contain_file(conf_file).with_content(%r{host: twemproxy2}) }
+    it { should contain_file(conf_file).with_content(%r{port: 4567}) }
+  end
+end
diff --git a/templates/agent-conf.d/haproxy.yaml.erb b/templates/agent-conf.d/haproxy.yaml.erb
index fb0e08ec..1d07c663 100644
--- a/templates/agent-conf.d/haproxy.yaml.erb
+++ b/templates/agent-conf.d/haproxy.yaml.erb
@@ -13,4 +13,9 @@ instances:
       <%= k %>: <%= v %>
       <%- end -%>
     <%- end -%>
+    <%- if instance['options'] -%>
+      <%- instance['options'].each do |k, v| -%>
+      <%= k %>: <%= v %>
+      <%- end -%>
+    <%- end -%>
 <% end -%>
diff --git a/templates/agent-conf.d/twemproxy.yaml.erb b/templates/agent-conf.d/twemproxy.yaml.erb
new file mode 100644
index 00000000..9c4fdaa0
--- /dev/null
+++ b/templates/agent-conf.d/twemproxy.yaml.erb
@@ -0,0 +1,11 @@
+#
+# MANAGED BY PUPPET
+#
+
+init_config:
+
+instances:
+<%- (Array(@_instances)).each do |instance| -%>
+  - host: <%= instance['host'] %>
+    port: <%= instance['port'] %>
+<% end -%>