Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a generic define for creating modules #233

Merged
merged 2 commits into from
Sep 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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