Skip to content

Commit bc43dac

Browse files
authored
Add trusted_facts_to_tags argument to tag by trusted facts (#658)
Adds an argument trusted_facts_to_tags that behaves much like facts_to_tags but for trusted facts.
1 parent 1572ae0 commit bc43dac

3 files changed

Lines changed: 51 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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,45 @@
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: ['osfamily'],
2184+
trusted_facts_to_tags: ['extensions.trusted_fact', 'extensions.facts_array', 'extensions.facts_hash.actor.first_name'],
2185+
}
2186+
end
2187+
let(:facts) do
2188+
{
2189+
'operatingsystem' => 'CentOS',
2190+
'osfamily' => 'redhat',
2191+
}
2192+
end
2193+
let(:trusted_facts) do
2194+
{
2195+
'trusted_fact' => 'test',
2196+
'facts_array' => ['one', 'two'],
2197+
'facts_hash' => {
2198+
'actor' => {
2199+
'first_name' => 'Macaulay',
2200+
'last_name' => 'Culkin',
2201+
},
2202+
},
2203+
}
2204+
end
2205+
2206+
it do
2207+
is_expected.to contain_file('/etc/datadog-agent/datadog.yaml')
2208+
.with_content(%r{tags:\n- osfamily:redhat\n- extensions.trusted_fact:test\n- extensions.facts_array:one\n- extensions.facts_array:two\n- extensions.facts_hash.actor.first_name:Macaulay})
2209+
end
2210+
end
2211+
end
2212+
21742213
context 'with facts to tags set' do
21752214
describe 'a6 ensure facts_array outputs a list of tags' do
21762215
let(:params) do

0 commit comments

Comments
 (0)