Skip to content

Commit 1e89c5e

Browse files
committed
Add trusted_facts_to_tags argument to tag by trusted facts
Note it's not possible to do the same for report tags, since there is no Puppet API to access trusted facts from outside Puppet code.
1 parent 1572ae0 commit 1e89c5e

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

functions/tag6.pp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
function datadog_agent::tag6(
66
Variant[Array, String] $tag_names,
77
Variant[String, Boolean] $lookup_fact = false,
8+
Variant[Hash, Undef] $lookup_table = $facts,
89
) {
910
if $tag_names =~ Array {
1011
$tags = $tag_names.reduce([]) |$_tags , $tag| {
11-
concat($_tags, datadog_agent::tag6($tag, $lookup_fact))
12+
concat($_tags, datadog_agent::tag6($tag, $lookup_fact, $lookup_table))
1213
}
1314
} else {
1415
if $lookup_fact =~ String {
@@ -18,9 +19,9 @@ function datadog_agent::tag6(
1819
}
1920

2021
if $lookup {
21-
$match_as_string = $facts[$tag_names]
22+
$match_as_string = $lookup_table[$tag_names]
2223
if $match_as_string == undef {
23-
$value = $facts.dig(*split($tag_names, '[.]'))
24+
$value = $lookup_table.dig(*split($tag_names, '[.]'))
2425
} else {
2526
$value = $match_as_string
2627
}

manifests/init.pp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
# $facts_to_tags
3131
# Optional array of facts' names that you can use to define tags following
3232
# the scheme: "fact_name:fact_value".
33+
# $trusted_facts_to_tags
34+
# Optional array of trusted facts' names that you can use to define tags following
35+
# the scheme: "fact_name:fact_value".
3336
# $puppet_run_reports
3437
# Will send results from your puppet agent runs back to the datadog service.
3538
# $manage_dogapi_gem
@@ -243,6 +246,7 @@
243246
$hiera_integrations = false,
244247
Boolean $hiera_tags = false,
245248
Array $facts_to_tags = [],
249+
Array $trusted_facts_to_tags = [],
246250
Boolean $puppet_run_reports = false,
247251
String $puppetmaster_user = $settings::user,
248252
String $puppet_gem_provider = $datadog_agent::params::gem_provider,
@@ -689,8 +693,9 @@
689693
notify => Service[$datadog_agent::params::service_name]
690694
}
691695

692-
$_local_tags = datadog_agent::tag6($local_tags, false)
693-
$_facts_tags = datadog_agent::tag6($facts_to_tags, true)
696+
$_local_tags = datadog_agent::tag6($local_tags, false, undef)
697+
$_facts_tags = datadog_agent::tag6($facts_to_tags, true, $facts)
698+
$_trusted_facts_tags = datadog_agent::tag6($trusted_facts_to_tags, true, $trusted)
694699

695700
$_agent_config = {
696701
'api_key' => $api_key,
@@ -707,7 +712,7 @@
707712
'dogstatsd_non_local_traffic' => $non_local_traffic,
708713
'log_file' => $agent_log_file,
709714
'log_level' => $log_level,
710-
'tags' => unique(flatten(union($_local_tags, $_facts_tags))),
715+
'tags' => unique(flatten(union($_local_tags, $_facts_tags, $_trusted_facts_tags))),
711716
}
712717

713718
$agent_config = deep_merge($_agent_config, $extra_config)

spec/classes/datadog_agent_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,44 @@
21712171
end
21722172
end
21732173

2174+
context 'with trusted_facts to tags set' do
2175+
Puppet::Util::Log.level = :debug
2176+
Puppet::Util::Log.newdestination(:console)
2177+
2178+
describe 'a6 ensure facts_array outputs a list of tags' do
2179+
let(:params) do
2180+
{
2181+
agent_major_version: 6,
2182+
puppet_run_reports: true,
2183+
facts_to_tags: ['operatingsystem'],
2184+
trusted_facts_to_tags: ['extensions.osfamily', 'extensions.facts_array', 'extensions.facts_hash.actor.first_name'],
2185+
}
2186+
end
2187+
let(:facts) do
2188+
{
2189+
'operatingsystem' => 'rhel',
2190+
}
2191+
end
2192+
let(:trusted_facts) do
2193+
{
2194+
'osfamily' => 'redhat',
2195+
'facts_array' => ['one', 'two'],
2196+
'facts_hash' => {
2197+
'actor' => {
2198+
'first_name' => 'Macaulay',
2199+
'last_name' => 'Culkin',
2200+
},
2201+
},
2202+
}
2203+
end
2204+
2205+
it do
2206+
is_expected.to contain_file('/etc/datadog-agent/datadog.yaml')
2207+
.with_content(%r{tags:\n- operatingsystem:rhel\n- extensions.osfamily:redhat\n- extensions.facts_array:one\n- extensions.facts_array:two\n- extensions.facts_hash.actor.first_name:Macaulay})
2208+
end
2209+
end
2210+
end
2211+
21742212
context 'with facts to tags set' do
21752213
describe 'a6 ensure facts_array outputs a list of tags' do
21762214
let(:params) do

0 commit comments

Comments
 (0)