Skip to content

Commit 9908a1c

Browse files
committed
dnstap: add print_config functions
Add a new function to print function lines. this is similar to the ruby function in unbound.conf.erb and should allow us to move the other templates to epp.
1 parent 56b192c commit 9908a1c

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

REFERENCE.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
* [`unbound::record`](#unbound--record): Create an unbound static DNS record override
1717
* [`unbound::stub`](#unbound--stub): Create an unbound stub zone for caching upstream name resolvers
1818

19+
### Functions
20+
21+
* [`unbound::print_config`](#unbound--print_config): Print a configuration value if it is defined and the version is supported
22+
1923
### Data types
2024

2125
* [`Unbound::Access_control`](#Unbound--Access_control): custom type for access control lists
@@ -2341,6 +2345,38 @@ Name of the unbound config file
23412345

23422346
Default value: `undef`
23432347

2348+
## Functions
2349+
2350+
### <a name="unbound--print_config"></a>`unbound::print_config`
2351+
2352+
Type: Puppet Language
2353+
2354+
Print a configuration value if it is defined and the version is supported
2355+
2356+
#### `unbound::print_config(String[1] $name, Optional[Variant[Boolean, Integer, String, Array[String, 1]]] $value = undef, Optional[String[1]] $version = undef)`
2357+
2358+
The unbound::print_config function.
2359+
2360+
Returns: `String` the config item as a string or an empty string if the version is not supported
2361+
2362+
##### `name`
2363+
2364+
Data type: `String[1]`
2365+
2366+
the config item name
2367+
2368+
##### `value`
2369+
2370+
Data type: `Optional[Variant[Boolean, Integer, String, Array[String, 1]]]`
2371+
2372+
the config item value
2373+
2374+
##### `version`
2375+
2376+
Data type: `Optional[String[1]]`
2377+
2378+
the version when the config item was introduced
2379+
23442380
## Data types
23452381

23462382
### <a name="Unbound--Access_control"></a>`Unbound::Access_control`

functions/print_config.pp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# @summary Print a configuration value if it is defined and the version is supported
2+
# @param name the config item name
3+
# @param value the config item value
4+
# @param version the version when the config item was introduced
5+
# @return the config item as a string or an empty string if the version is not supported
6+
function unbound::print_config (
7+
String[1] $name,
8+
Optional[Variant[Boolean, Integer, String, Array[String, 1]]] $value = undef,
9+
Optional[String[1]] $version = undef,
10+
) >> String {
11+
$unbound_version = $facts['unbound_version'].lest || { '0.a' }
12+
if ($value =~ Undef or ($version =~ NotUndef and versioncmp($unbound_version, $version) < 0)) {
13+
return ''
14+
}
15+
$value ? {
16+
String => " ${name}: \"${value}\"",
17+
Integer => " ${name}: ${value}",
18+
Boolean => " ${name}: ${value.bool2str('yes', 'no')}",
19+
Array => $value.map |$v| { " ${name}: \"${v}\"" }.join("\n"),
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'unbound::print_config' do
6+
it { is_expected.to run.with_params('string_name', 'string_value').and_return(' string_name: "string_value"') }
7+
it { is_expected.to run.with_params('int_name', 42).and_return(' int_name: 42') }
8+
it { is_expected.to run.with_params('true_name', true).and_return(' true_name: yes') }
9+
it { is_expected.to run.with_params('false_name', false).and_return(' false_name: no') }
10+
11+
it do
12+
is_expected.to run.with_params('list_name', %w[value1 value2]).
13+
and_return(" list_name: \"value1\"\n list_name: \"value2\"")
14+
end
15+
16+
context 'with version' do
17+
let(:facts) { { 'unbound_version' => '1.21.0' } }
18+
19+
it { is_expected.to run.with_params('supported', 42, '1.11.0').and_return(' supported: 42') }
20+
it { is_expected.to run.with_params('supported', 42, '1.21.0').and_return(' supported: 42') }
21+
it { is_expected.to run.with_params('unsupported', 42, '1.22.0').and_return('') }
22+
end
23+
end

0 commit comments

Comments
 (0)