Skip to content

Commit

Permalink
Add support for multiple instances of Elasticsearch. (#333)
Browse files Browse the repository at this point in the history
This follows the same pattern used by 'mysql.pp'. The default values for the
'cluster_stats', 'pshard_stats' or 'pending_task_stats' fields are used if
the parameters are not set in each instance declaration.
  • Loading branch information
stantona authored and truthbk committed Jul 13, 2017
1 parent 143154d commit 5ef1514
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 18 deletions.
33 changes: 32 additions & 1 deletion manifests/integrations/elasticsearch.pp
Expand Up @@ -9,9 +9,20 @@
# Sample Usage:
#
# class { 'datadog_agent::integrations::elasticsearch' :
# url => "http://localhost:9201"
# instances => [{
# url => "http://localhost:9201"
# },
# {
# url => "http://elastic.acme.com:9201"
# }]
# }
#
# Or for a single instance:
#
# class { 'datadog_agent::integrations::elasticsearch' :
# url => "http://localhost:9201"
# }
#
class datadog_agent::integrations::elasticsearch(
$cluster_stats = false,
$password = undef,
Expand All @@ -23,6 +34,7 @@
$tags = [],
$url = 'http://localhost:9200',
$username = undef,
$instances = undef
) inherits datadog_agent::params {
include datadog_agent

Expand All @@ -38,6 +50,25 @@
validate_bool($cluster_stats, $pending_task_stats, $pshard_stats)
validate_string($password, $ssl_cert, $ssl_key, $url, $username)

if !$instances and $url {
$_instances = [{
'cluster_stats' => $cluster_stats,
'password' => $password,
'pending_task_stats' => $pending_task_stats,
'pshard_stats' => $pshard_stats,
'ssl_cert' => $ssl_cert,
'ssl_key' => $ssl_key,
'ssl_verify' => $ssl_verify,
'tags' => $tags,
'url' => $url,
'username' => $username
}]
} elsif !$instances {
$_instances = []
} else {
$_instances = $instances
}

file { "${datadog_agent::params::conf_dir}/elastic.yaml":
ensure => file,
owner => $datadog_agent::params::dd_user,
Expand Down
58 changes: 57 additions & 1 deletion spec/classes/datadog_agent_integrations_elasticsearch_spec.rb
Expand Up @@ -33,7 +33,8 @@
it { should_not contain_file(conf_file).with_content(%r{ ssl_key}) }
it { should_not contain_file(conf_file).with_content(%r{ tags:}) }
end
context 'with parameters set' do

context 'with parameters set' do
let(:params) {{
password: 'password',
pending_task_stats: false,
Expand All @@ -54,4 +55,59 @@
it { should contain_file(conf_file).with_content(%r{ - tag1:key1}) }
end

context 'with multiple instances set' do
let(:params) {
{
instances: [
{
'cluster_stats' => true,
'password' => 'password',
'pending_task_stats' => false,
'pshard_stats' => false,
'url' => 'https://foo:4242',
'username' => 'username',
'ssl_verify' => true,
'ssl_cert' => '/etc/ssl/certs/client.pem',
'ssl_key' => '/etc/ssl/private/client.key',
'tags' => ['tag1:key1'],
},
{
'cluster_stats' => true,
'password' => 'password_2',
'pending_task_stats' => true,
'pshard_stats' => false,
'url' => 'https://bar:2424',
'username' => 'username_2',
'ssl_verify' => false,
'ssl_cert' => '/etc/ssl/certs/client.pem',
'ssl_key' => '/etc/ssl/private/client.key',
'tags' => ['tag2:key2'],
}
]
}
}
it { should contain_file(conf_file).with_content(%r{instances:}) }
it { should contain_file(conf_file).with_content(%r{ - url: https://foo:4242}) }
it { should contain_file(conf_file).with_content(%r{ cluster_stats: true}) }
it { should contain_file(conf_file).with_content(%r{ pending_task_stats: false}) }
it { should contain_file(conf_file).with_content(%r{ username: username}) }
it { should contain_file(conf_file).with_content(%r{ password: password}) }
it { should contain_file(conf_file).with_content(%r{ pshard_stats: false}) }
it { should contain_file(conf_file).with_content(%r{ ssl_verify: true}) }
it { should contain_file(conf_file).with_content(%r{ ssl_cert: /etc/ssl/certs/client.pem}) }
it { should contain_file(conf_file).with_content(%r{ ssl_key: /etc/ssl/private/client.key}) }
it { should contain_file(conf_file).with_content(%r{ tags:}) }
it { should contain_file(conf_file).with_content(%r{ - tag1:key1}) }
it { should contain_file(conf_file).with_content(%r{ - url: https://bar:2424}) }
it { should contain_file(conf_file).with_content(%r{ cluster_stats: true}) }
it { should contain_file(conf_file).with_content(%r{ pending_task_stats: true}) }
it { should contain_file(conf_file).with_content(%r{ username: username_2}) }
it { should contain_file(conf_file).with_content(%r{ password: password_2}) }
it { should contain_file(conf_file).with_content(%r{ pshard_stats: false}) }
it { should contain_file(conf_file).with_content(%r{ ssl_verify: false}) }
it { should contain_file(conf_file).with_content(%r{ ssl_cert: /etc/ssl/certs/client.pem}) }
it { should contain_file(conf_file).with_content(%r{ ssl_key: /etc/ssl/private/client.key}) }
it { should contain_file(conf_file).with_content(%r{ tags:}) }
it { should contain_file(conf_file).with_content(%r{ - tag2:key2}) }
end
end
34 changes: 18 additions & 16 deletions templates/agent-conf.d/elastic.yaml.erb
Expand Up @@ -5,28 +5,30 @@
init_config:

instances:
- url: <%= @url %>
<%- if @username -%>
username: <%= @username %>
<%- (Array(@_instances)).each do |instance| -%>
- url: <%= instance['url'] %>
<%- if instance['username'] && instance['username'] != :undef -%>
username: <%= instance['username'] %>
<%- end -%>
<%- if @password -%>
password: <%= @password %>
<%- if instance['password'] && instance['password'] != :undef -%>
password: <%= instance['password'] %>
<%- end -%>
cluster_stats: <%= @cluster_stats %>
pshard_stats: <%= @pshard_stats %>
pending_task_stats: <%= @pending_task_stats %>
<%- if @url.match(/^https/) -%>
ssl_verify: <%= @ssl_verify %>
cluster_stats: <%= instance['cluster_stats'] %>
pshard_stats: <%= instance['pshard_stats'] %>
pending_task_stats: <%= instance['pending_task_stats'] %>
<%- if instance['url'].match(/^https/) -%>
ssl_verify: <%= instance['ssl_verify'] %>
<%- end -%>
<%- if @ssl_cert -%>
ssl_cert: <%= @ssl_cert %>
<%- if instance['ssl_cert'] && instance['ssl_cert'] != :undef -%>
ssl_cert: <%= instance['ssl_cert'] %>
<%- end -%>
<%- if @ssl_key -%>
ssl_key: <%= @ssl_key %>
<%- if instance['ssl_key'] && instance['ssl_key'] != :undef -%>
ssl_key: <%= instance['ssl_key'] %>
<%- end -%>
<%- unless @tags.empty? -%>
<%- unless instance['tags'].empty? -%>
tags:
<%- @tags.each do |tag| -%>
<%- instance['tags'].each do |tag| -%>
- <%= tag %>
<%- end -%>
<%- end -%>
<%- end -%>

0 comments on commit 5ef1514

Please sign in to comment.