Skip to content

Commit

Permalink
Merge pull request #233 from DataDog/jaime/generic-integration
Browse files Browse the repository at this point in the history
Make a generic define for creating modules
  • Loading branch information
truthbk authored Sep 20, 2016
2 parents 8218721 + 9d77765 commit 7326b14
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/puppet/parser/functions/to_instances_yaml.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'yaml'

module Puppet::Parser::Functions
newfunction(:to_instances_yaml, :type => :rvalue) do |args|
init_config = args[0]
instances = args[1]
default_values = {
'init_config' => init_config.is_a?(String) ? nil: init_config,
'instances' => instances
}
YAML::dump(default_values)
end
end
5 changes: 5 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@
$pup_log_file = '',
$syslog_host = '',
$syslog_port = '',
$conf_dir = $datadog_agent::params::conf_dir,
$service_name = $datadog_agent::params::service_name,
$package_name = $datadog_agent::params::package_name,
$dd_user = $datadog_agent::params::dd_user,
$dd_group = $datadog_agent::params::dd_group,
) inherits datadog_agent::params {

validate_string($dd_url)
Expand Down
24 changes: 24 additions & 0 deletions manifests/integration.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
define datadog_agent::integration (
$instances,
$init_config = undef,
$integration = $title,
){

include datadog_agent

validate_array($instances)
if $init_config != undef {
validate_hash($init_config)
}
validate_string($integration)

file { "${datadog_agent::conf_dir}/${integration}.yaml":
ensure => file,
owner => $datadog_agent::dd_user,
group => $datadog_agent::dd_group,
mode => '0600',
content => to_instances_yaml($init_config, $instances),
notify => Service[$datadog_agent::service_name]
}

}
25 changes: 25 additions & 0 deletions spec/defines/datadog_agent__integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'spec_helper'

describe "datadog_agent::integration" do
let (:title) { "test" }
let(:facts) do
{
operatingsystem: 'CentOS',
osfamily: 'redhat'
}
end
let (:params) {{
:instances => [
{ 'one' => "two" }
]
}}
it { should compile }
it { should contain_file('/etc/dd-agent/conf.d/test.yaml').with_content(/init_config: /) }
gem_spec = Gem.loaded_specs['puppet']
if gem_spec.version >= Gem::Version.new('4.0.0')
it { should contain_file('/etc/dd-agent/conf.d/test.yaml').with_content(/---\ninit_config: \ninstances:\n- one: two\n/) }
else
it { should contain_file('/etc/dd-agent/conf.d/test.yaml').with_content(/--- \n init_config: \n instances: \n - one: two/) }
end
it { should contain_file('/etc/dd-agent/conf.d/test.yaml').that_notifies("Service[datadog-agent]") }
end

0 comments on commit 7326b14

Please sign in to comment.