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

Accept instances hashes on apache and http_check integrations #691

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
18 changes: 18 additions & 0 deletions functions/clean_empty.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# For cleaning up empty elements on hashes and arrays
function datadog_agent::clean_empty (
Any $var
) {
if $var.is_a(Array) {
$_var = $var.map |$item| { datadog_agent::clean_empty($item) }
$result = $_var.filter |$item| { !$item.empty }

} elsif $var.is_a(Hash) {
$_var = Hash($var.map |$key, $value| { Tuple([$key, datadog_agent::clean_empty($value)]) })
$result = $_var.filter |$key, $value| { !$value.empty }

} else {
$result = $var
}

$result
}
25 changes: 20 additions & 5 deletions manifests/integrations/apache.pp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,29 @@
# }
#
class datadog_agent::integrations::apache (
String $url = 'http://localhost/server-status?auto',
Optional[String] $username = undef,
Optional[String] $password = undef,
Array $tags = [],
Boolean $disable_ssl_validation = false
String $url = 'http://localhost/server-status?auto',
Optional[String] $username = undef,
Optional[String] $password = undef,
Array $tags = [],
Optional[Boolean] $disable_ssl_validation = undef,
Optional[Hash] $init_config = undef,
Optional[Array] $instances = undef,
Optional[Array] $logs = undef,
) inherits datadog_agent::params {
include datadog_agent

if !$instances {
$_instances = datadog_agent::clean_empty([{
'apache_status_url' => $url,
'apache_user' => $username,
'apache_password' => $password,
'tags' => $tags,
'disable_ssl_validation' => $disable_ssl_validation,
}])
} else {
$_instances = $instances
}

$legacy_dst = "${datadog_agent::params::legacy_conf_dir}/apache.yaml"
if $::datadog_agent::_agent_major_version > 5 {
$dst_dir = "${datadog_agent::params::conf_dir}/apache.d"
Expand Down
93 changes: 65 additions & 28 deletions manifests/integrations/http_check.pp
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,37 @@


class datadog_agent::integrations::http_check (
$sitename = undef,
$url = undef,
$username = undef,
$password = undef,
$timeout = 1,
$method = 'get',
$data = undef,
$threshold = undef,
$window = undef,
$content_match = undef,
$reverse_content_match = false,
$include_content = false,
$http_response_status_code = undef,
$collect_response_time = true,
$disable_ssl_validation = false,
$ignore_ssl_warning = false,
$skip_event = true,
$no_proxy = false,
$check_certificate_expiration = true,
$days_warning = undef,
$days_critical = undef,
$sitename = undef,
$url = undef,
$username = undef,
$password = undef,
$timeout = 1,
$method = 'get',
$data = undef,
$threshold = undef,
$window = undef,
$content_match = undef,
$reverse_content_match = false,
$include_content = false,
$http_response_status_code = undef,
$collect_response_time = true,
$disable_ssl_validation = false,
$ignore_ssl_warning = false,
$skip_event = true,
$no_proxy = false,
$check_certificate_expiration = true,
$days_warning = undef,
$days_critical = undef,
Optional[Boolean] $check_hostname = undef,
Optional[String] $ssl_server_name = undef,
$headers = [],
$allow_redirects = true,
$tags = [],
$contact = [],
Optional[Array] $instances = undef,
$ca_certs = undef,
$headers = [],
$allow_redirects = true,
$tags = [],
$contact = [],
Optional[Hash] $init_config = undef,
Optional[Array] $instances = undef,
Optional[Array] $logs = undef,
$ca_certs = undef,
) inherits datadog_agent::params {
include datadog_agent

Expand Down Expand Up @@ -227,12 +229,47 @@
'contact' => $contact,
'ca_certs' => $ca_certs,
}]
} elsif !$instances{
} elsif !$instances {
$_instances = []
} else {
$_instances = $instances
}

$instances_array = $_instances.map |$instance| {
Hash($instance.map |$key, $value| {
case $key {
'sitename': {
Tuple(['name', $value])
}

'data', 'headers': {
$_value = datadog_agent::clean_empty($value)
if !$_value.is_a(Array) {
Tuple([$key, $_value]).next
asenci marked this conversation as resolved.
Show resolved Hide resolved
}

$value_array = $_value.filter |$item| { $item =~ /:/ }
asenci marked this conversation as resolved.
Show resolved Hide resolved
$value_hash = Hash($_value.map |$item| {
$_item = $item.split(':')
$i_key = $_item[0].rstrip
$i_value = $_item[1, - 1].join(':').lstrip

Tuple([$i_key, $i_value])
})

Tuple([$key, datadog_agent::clean_empty($value_hash)])
}

'tags': {
Tuple([$key, datadog_agent::clean_empty($value)])
}

default: {
Tuple([$key, $value]) }
asenci marked this conversation as resolved.
Show resolved Hide resolved
}
})
}

$legacy_dst = "${datadog_agent::params::legacy_conf_dir}/http_check.yaml"
if $::datadog_agent::_agent_major_version > 5 {
$dst_dir = "${datadog_agent::params::conf_dir}/http_check.d"
Expand Down
19 changes: 19 additions & 0 deletions spec/classes/datadog_agent_integrations_apache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@

skip('doubly undefined behavior')
end

context 'with instances hash' do
let(:params) do
{
instances: [
{
'apache_status_url' => 'http://foobar',
'apache_user' => 'userfoo',
'apache_password' => 'passfoo',
'tags' => ['foo', 'bar', 'baz'],
},
],
}
end

it { is_expected.to contain_file(conf_file).with_content(%r{apache_status_url: http://foobar}) }
it { is_expected.to contain_file(conf_file).with_content(%r{apache_user: userfoo}) }
it { is_expected.to contain_file(conf_file).with_content(%r{apache_password: passfoo}) }
end
end
end
end
Expand Down
108 changes: 59 additions & 49 deletions spec/classes/datadog_agent_integrations_http_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
collect_response_time: false,
disable_ssl_validation: true,
skip_event: true,
http_response_status_code: '503',
http_response_status_code: 503,
no_proxy: true,
check_certificate_expiration: true,
days_warning: 14,
Expand All @@ -83,7 +83,7 @@
it { is_expected.to contain_file(conf_file).with_content(%r{data: key=value}) }
it { is_expected.to contain_file(conf_file).with_content(%r{threshold: 456}) }
it { is_expected.to contain_file(conf_file).with_content(%r{window: 789}) }
it { is_expected.to contain_file(conf_file).with_content(%r{content_match: 'foomatch'}) }
it { is_expected.to contain_file(conf_file).with_content(%r{content_match: foomatch}) }
it { is_expected.to contain_file(conf_file).with_content(%r{reverse_content_match: true}) }
it { is_expected.to contain_file(conf_file).with_content(%r{include_content: true}) }
it { is_expected.to contain_file(conf_file).without_content(%r{collect_response_time: true}) }
Expand All @@ -95,7 +95,7 @@
it { is_expected.to contain_file(conf_file).with_content(%r{days_warning: 14}) }
it { is_expected.to contain_file(conf_file).with_content(%r{days_critical: 7}) }
it { is_expected.to contain_file(conf_file).with_content(%r{allow_redirects: true}) }
it { is_expected.to contain_file(conf_file).with_content(%r{ca_certs: /dev/null}) }
it { is_expected.to contain_file(conf_file).with_content(%r{ca_certs: "/dev/null"}) }
end

context 'with json post data' do
Expand All @@ -114,52 +114,6 @@
it { is_expected.to contain_file(conf_file).with_content(%r{headers:\s+Content-Type:\s+application/json}) }
end

context 'with headers parameter array' do
let(:params) do
{
sitename: 'foo.bar.baz',
url: 'http://foo.bar.baz:4096',
headers: ['foo', 'bar', 'baz'],
}
end

it { is_expected.to contain_file(conf_file).with_content(%r{headers:\s+foo\s+bar\s+baz\s*?[^-]}m) }
end

context 'with headers parameter empty values' do
context 'mixed in with other headers' do
let(:params) do
{
sitename: 'foo.bar.baz',
url: 'http://foo.bar.baz:4096',
headers: ['foo', '', 'baz'],
}
end

it { is_expected.to contain_file(conf_file).with_content(%r{headers:\s+foo\s+baz\s*?[^-]}m) }
end

context 'single element array of an empty string' do
let(:params) do
{
headers: [''],
}
end

skip('undefined behavior')
end

context 'single value empty string' do
let(:params) do
{
headers: '',
}
end

skip('doubly undefined behavior')
end
end

context 'with tags parameter array' do
let(:params) do
{
Expand Down Expand Up @@ -218,6 +172,62 @@
it { is_expected.to contain_file(conf_file).with_content(%r{notify:\s+- alice\s+bob\s+carlo}) }
end
end

context 'with instances hash' do
let(:params) do
{
instances: [
{
'name' => 'foo.bar.baz',
'url' => 'http://foo.bar.baz:4096',
'username' => 'foouser',
'password' => 'barpassword',
'timeout' => 123,
'method' => 'post',
'data' => 'key=value',
'threshold' => 456,
'window' => 789,
'content_match' => 'foomatch',
'reverse_content_match' => true,
'include_content' => true,
'collect_response_time' => false,
'disable_ssl_validation' => true,
'skip_event' => true,
'http_response_status_code' => 503,
'no_proxy' => true,
'check_certificate_expiration' => true,
'days_warning' => 14,
'days_critical' => 7,
'allow_redirects' => true,
'ca_certs' => '/dev/null',
},
],
}
end

it { is_expected.to contain_file(conf_file).with_content(%r{name: foo.bar.baz}) }
it { is_expected.to contain_file(conf_file).with_content(%r{url: http://foo.bar.baz:4096}) }
it { is_expected.to contain_file(conf_file).with_content(%r{username: foouser}) }
it { is_expected.to contain_file(conf_file).with_content(%r{password: barpassword}) }
it { is_expected.to contain_file(conf_file).with_content(%r{timeout: 123}) }
it { is_expected.to contain_file(conf_file).with_content(%r{method: post}) }
it { is_expected.to contain_file(conf_file).with_content(%r{data: key=value}) }
it { is_expected.to contain_file(conf_file).with_content(%r{threshold: 456}) }
it { is_expected.to contain_file(conf_file).with_content(%r{window: 789}) }
it { is_expected.to contain_file(conf_file).with_content(%r{content_match: foomatch}) }
it { is_expected.to contain_file(conf_file).with_content(%r{reverse_content_match: true}) }
it { is_expected.to contain_file(conf_file).with_content(%r{include_content: true}) }
it { is_expected.to contain_file(conf_file).without_content(%r{collect_response_time: true}) }
it { is_expected.to contain_file(conf_file).with_content(%r{disable_ssl_validation: true}) }
it { is_expected.to contain_file(conf_file).with_content(%r{skip_event: true}) }
it { is_expected.to contain_file(conf_file).with_content(%r{http_response_status_code: 503}) }
it { is_expected.to contain_file(conf_file).with_content(%r{no_proxy: true}) }
it { is_expected.to contain_file(conf_file).with_content(%r{check_certificate_expiration: true}) }
it { is_expected.to contain_file(conf_file).with_content(%r{days_warning: 14}) }
it { is_expected.to contain_file(conf_file).with_content(%r{days_critical: 7}) }
it { is_expected.to contain_file(conf_file).with_content(%r{allow_redirects: true}) }
it { is_expected.to contain_file(conf_file).with_content(%r{ca_certs: "/dev/null"}) }
end
end
end
end
25 changes: 2 additions & 23 deletions templates/agent-conf.d/apache.yaml.erb
Original file line number Diff line number Diff line change
@@ -1,24 +1,3 @@
### MANAGED BY PUPPET

init_config:

instances:
- apache_status_url: <%= @url %>
<% if @disable_ssl_validation -%>
disable_ssl_validation: <%= @disable_ssl_validation %>
<% end -%>
<% if @username -%>
apache_user: <%= @username %>
<% end -%>
<% if @password -%>
apache_password: <%= @password %>
<% end -%>
<% if @tags and ! @tags.empty? -%>
tags:
<%- Array(@tags).each do |tag| -%>
<%- if tag != '' -%>
- <%= tag %>
<%- end -%>
<%- end -%>
<% end -%>

<% require 'yaml' -%>
<%= {'init_config'=>@init_config, 'instances'=>@_instances, 'logs'=>@logs}.to_yaml %>
Loading