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
16 changes: 16 additions & 0 deletions functions/clean_empty.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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) }
$_var.filter |$item| { !datadog_agent::is_empty($item) }

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

} elsif !datadog_agent::is_empty($var) {
$var
}
}
10 changes: 10 additions & 0 deletions functions/is_empty.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# For testing if variable is empty
function datadog_agent::is_empty (
Any $var
) {
if $var.is_a(Collection) or $var.is_a(String) or $var.is_a(Numeric) or $var.is_a(Binary) or $var.is_a(Undef) {
$var.empty
} else {
false
}
}
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 {
require ::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
40 changes: 38 additions & 2 deletions manifests/integrations/http_check.pp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@
$allow_redirects = true,
$tags = [],
$contact = [],
Optional[Array] $instances = undef,
Optional[Hash] $init_config = undef,
Optional[Array] $instances = undef,
Optional[Array] $logs = undef,
$ca_certs = undef,
) inherits datadog_agent::params {
require ::datadog_agent
Expand Down Expand Up @@ -233,12 +235,46 @@
'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': {
if !$value.is_a(Array) {
Tuple([$key, $value]).next
}

$value_hash = Hash($value.map |$item| {
$sub_item_array = $item.split(':')
$sub_item_key = $sub_item_array[0].rstrip
$sub_item_value = $sub_item_array[1, - 1].join(':').lstrip

Tuple([$sub_item_key, $sub_item_value])
})

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

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

default: {
Tuple([$key, $value])
}
}
})
}

$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
72 changes: 49 additions & 23 deletions manifests/integrations/mysql.pp
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,25 @@
# }
#
#
class datadog_agent::integrations::mysql(
class datadog_agent::integrations::mysql (
String $host = 'localhost',
Optional[String] $user = 'datadog',
Optional[Variant[String, Integer]] $port = 3306,
Optional[String] $password = undef,
Optional[String] $sock = undef,
Array $tags = [],
Optional[Array] $tags = undef,
$replication = '0',
$galera_cluster = '0',
Boolean $extra_status_metrics = false,
Boolean $extra_innodb_metrics = false,
Boolean $extra_performance_metrics = false,
Boolean $schema_size_metrics = false,
Boolean $disable_innodb_metrics = false,
Optional[Array] $queries = [],
Optional[Array] $queries = undef,
Optional[Hash] $init_config = undef,
Optional[Array] $instances = undef,
Optional[Array] $logs = [],
) inherits datadog_agent::params {
Optional[Array] $logs = undef,
) inherits datadog_agent::params {
require ::datadog_agent

if ($host == undef and $sock == undef) or
Expand All @@ -89,26 +90,51 @@
}

if !$instances and $host {
$_instances = [{
'host' => $host,
'password' => $password,
'user' => $user,
'port' => $port,
'sock' => $sock,
'tags' => $tags,
'replication' => $replication,
'galera_cluster' => $galera_cluster,
'extra_status_metrics' => $extra_status_metrics,
'extra_innodb_metrics' => $extra_innodb_metrics,
'extra_performance_metrics' => $extra_performance_metrics,
'schema_size_metrics' => $schema_size_metrics,
'disable_innodb_metrics' => $disable_innodb_metrics,
'queries' => $queries,
}]
} elsif !$instances{
$_instances = datadog_agent::clean_empty([{
'server' => $host,
'pass' => $password,
'user' => $user,
'port' => $port,
'sock' => $sock,
'tags' => $tags,
'options' => {
'replication' => $replication,
'galera_cluster' => $galera_cluster,
'extra_status_metrics' => $extra_status_metrics,
'extra_innodb_metrics' => $extra_innodb_metrics,
'extra_performance_metrics' => $extra_performance_metrics,
'schema_size_metrics' => $schema_size_metrics,
'disable_innodb_metrics' => $disable_innodb_metrics,
},
'queries' => $queries,
}])
} elsif !$instances {
$_instances = []
} else {
$_instances = $instances
$_instances = datadog_agent::clean_empty($instances.map |$instance| {
if $instance['host'] {
{
'server' => $instance['host'],
'pass' => $instance['password'],
'user' => $instance['user'],
'port' => $instance['port'],
'sock' => $instance['sock'],
'tags' => $instance['tags'],
'options' => {
'replication' => $instance['replication'],
'galera_cluster' => $instance['galera_cluster'],
'extra_status_metrics' => $instance['extra_status_metrics'],
'extra_innodb_metrics' => $instance['extra_innodb_metrics'],
'extra_performance_metrics' => $instance['extra_performance_metrics'],
'schema_size_metrics' => $instance['schema_size_metrics'],
'disable_innodb_metrics' => $instance['disable_innodb_metrics'],
},
'queries' => $queries,
}
} else {
$instance
}
})
}

$legacy_dst = "${datadog_agent::params::legacy_conf_dir}/mysql.yaml"
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 @@ -65,7 +65,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 @@ -85,7 +85,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 @@ -97,7 +97,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 @@ -116,52 +116,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 @@ -220,6 +174,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
Loading