Skip to content

Commit bddf8c7

Browse files
author
Lennart Betz
committed
add parameter ensure to objects endpoint and zone
2 parents fe76980 + 221a9fd commit bddf8c7

File tree

7 files changed

+205
-9
lines changed

7 files changed

+205
-9
lines changed

manifests/object.pp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#
55
# === Parameters
66
#
7+
# [*ensure*]
8+
# Set to present enables the object, absent disabled it. Defaults to present.
9+
#
710
# [*object_name*]
811
# Set the icinga2 name of the object. Defaults to title of the define resource.
912
#
@@ -32,6 +35,7 @@
3235
# Icinga Development Team <[email protected]>
3336
#
3437
define icinga2::object(
38+
$ensure = present,
3539
$object_name = $title,
3640
$template = false,
3741
$import = [],
@@ -47,9 +51,11 @@
4751

4852
include ::icinga2::params
4953

50-
$user = $::icinga2::params::user
51-
$group = $::icinga2::params::group
54+
$user = $::icinga2::params::user
55+
$group = $::icinga2::params::group
5256

57+
validate_re($ensure, [ '^present$', '^absent$' ],
58+
"${ensure} isn't supported. Valid values are 'present' and 'absent'.")
5359
validate_string($object_name)
5460
validate_bool($template)
5561
validate_array($import)
@@ -63,11 +69,15 @@
6369
owner => $user,
6470
group => $group,
6571
tag => 'icinga2::config::file',
72+
warn => true,
6673
})
6774

68-
concat::fragment { "icinga2::object::${object_type}::${object_name}":
69-
target => $target,
70-
content => template('icinga2/object.conf.erb'),
71-
order => $order,
75+
if $ensure != 'absent' {
76+
concat::fragment { "icinga2::object::${object_type}::${object_name}":
77+
target => $target,
78+
content => template('icinga2/object.conf.erb'),
79+
order => $order,
80+
}
7281
}
82+
7383
}

manifests/object/endpoint.pp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
1+
# == Define: icinga2::object::endpoint
2+
#
3+
# Manage Icinga2 endpoint objects.
4+
#
5+
# === Parameters
6+
#
7+
# [*ensure*]
8+
# Set to present enables the endpoint object, absent disabled it. Defaults to present.
9+
#
10+
# [*endpoint*]
11+
# Set the Icinga2 name of the endpoint object. Defaults to title of the define resource.
12+
#
13+
# [*host*]
14+
# Optional. The IP address of the remote Icinga 2 instance.
15+
#
16+
# [*port*]
17+
# The service name/port of the remote Icinga 2 instance. Defaults to 5665.
18+
#
19+
# [*log_duration*]
20+
# Duration for keeping replay logs on connection loss. Defaults to 1d (86400 seconds).
21+
# Attribute is specified in seconds. If log_duration is set to 0, replaying logs is disabled.
22+
# You could also specify the value in human readable format like 10m for 10 minutes
23+
# or 1h for one hour.
24+
#
25+
# [*target*]
26+
# Destination config file to store in this object. File will be declared the
27+
# first time.
28+
#
29+
# [*order*]
30+
# String to set the position in the target file, sorted alpha numeric. Defaults to 10.
31+
#
32+
# === Authors
33+
#
34+
# Icinga Development Team <[email protected]>
35+
#
136
define icinga2::object::endpoint(
37+
$ensure = present,
238
$endpoint = $title,
339
$host = undef,
440
$port = undef,
@@ -12,6 +48,10 @@
1248
$conf_dir = $::icinga2::params::conf_dir
1349

1450
# validation
51+
validate_re($ensure, [ '^present$', '^absent$' ],
52+
"${ensure} isn't supported. Valid values are 'present' and 'absent'.")
53+
validate_integer($order)
54+
1555
if $endpoint { validate_string($endpoint) }
1656
if $host { validate_ip_address($host) }
1757
if $port { validate_integer($port) }
@@ -23,8 +63,6 @@
2363
else {
2464
$_target = "${conf_dir}/zones.conf" }
2565

26-
validate_integer($order)
27-
2866
# compose the attributes
2967
$attrs = {
3068
host => $host,
@@ -34,6 +72,7 @@
3472

3573
# create object
3674
icinga2::object { "icinga2::object::Endpoint::${title}":
75+
ensure => $ensure,
3776
object_name => $endpoint,
3877
object_type => 'Endpoint',
3978
attrs => $attrs,

manifests/object/zone.pp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#
55
# === Parameters
66
#
7+
# [*ensure*]
8+
# Set to present enables the endpoint object, absent disabled it. Defaults to present.
9+
#
710
# [*zone*]
811
# Set the Icinga2 name of the zone object. Defaults to title of the define resource.
912
#
@@ -29,6 +32,7 @@
2932
# Icinga Development Team <[email protected]>
3033
#
3134
define icinga2::object::zone(
35+
$ensure = present,
3236
$zone = $title,
3337
$endpoints = [],
3438
$parent = undef,
@@ -42,6 +46,8 @@
4246
$conf_dir = $::icinga2::params::conf_dir
4347

4448
# validation
49+
validate_re($ensure, [ '^present$', '^absent$' ],
50+
"${ensure} isn't supported. Valid values are 'present' and 'absent'.")
4551
validate_string($zone)
4652
validate_integer($order)
4753

@@ -56,6 +62,8 @@
5662
else {
5763
$_target = "${conf_dir}/zones.conf" }
5864

65+
validate_string($order)
66+
5967
# compose the attributes
6068
if $global {
6169
$attrs = {
@@ -70,6 +78,7 @@
7078

7179
# create object
7280
icinga2::object { "icinga2::object::Zone::${title}":
81+
ensure => $ensure,
7382
object_name => $zone,
7483
object_type => 'Zone',
7584
attrs => $attrs,

spec/defines/endpoint_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323
end
2424

2525

26+
context "#{os} with ensure => absent" do
27+
let(:params) { {:ensure => 'absent', :target => '/bar/baz'} }
28+
29+
it { is_expected.to contain_concat('/bar/baz') }
30+
31+
it { is_expected.not_to contain_concat__fragment('icinga2::object::Endpoint::bar') }
32+
end
33+
34+
35+
context "#{os} with ensure => foo (not a valid value)" do
36+
let(:params) { {:ensure => 'foo', :target => '/bar/baz'} }
37+
38+
it { is_expected.to raise_error(Puppet::Error, /foo isn't supported/) }
39+
end
40+
41+
2642
context "#{os} with target => bar/baz (not valid absolute path)" do
2743
let(:params) { {:target => 'bar/baz'} }
2844

@@ -101,6 +117,36 @@
101117
end
102118

103119

120+
context "Windows 2012 R2 with ensure => absent" do
121+
let(:facts) { {
122+
:kernel => 'Windows',
123+
:architecture => 'x86_64',
124+
:osfamily => 'Windows',
125+
:operatingsystem => 'Windows',
126+
:operatingsystemmajrelease => '2012 R2'
127+
} }
128+
let(:params) { {:ensure => 'absent', :target => 'C:/bar/baz'} }
129+
130+
it { is_expected.to contain_concat('C:/bar/baz') }
131+
132+
it { is_expected.not_to contain_concat__fragment('icinga2::object::Endpoint::bar') }
133+
end
134+
135+
136+
context "Windows 2012 R2 with ensure => foo (not a valid value)" do
137+
let(:facts) { {
138+
:kernel => 'Windows',
139+
:architecture => 'x86_64',
140+
:osfamily => 'Windows',
141+
:operatingsystem => 'Windows',
142+
:operatingsystemmajrelease => '2012 R2'
143+
} }
144+
let(:params) { {:ensure => 'foo', :target => 'C:/bar/baz'} }
145+
146+
it { is_expected.to raise_error(Puppet::Error, /foo isn't supported/) }
147+
end
148+
149+
104150
context "Windows 2012 R2 with target => bar/baz (not valid absolute path)" do
105151
let(:facts) { {
106152
:kernel => 'Windows',

spec/defines/object_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323
end
2424

2525

26+
context "#{os} with ensure => absent" do
27+
let(:params) { {:ensure => 'absent', :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
28+
29+
it { is_expected.to contain_concat('/bar/baz') }
30+
31+
it { is_expected.not_to contain_concat__fragment('icinga2::object::foo::bar') }
32+
end
33+
34+
35+
context "#{os} with ensure => foo (not a valid value)" do
36+
let(:params) { {:ensure => 'foo', :object_type => 'foo', :target => '/bar/baz', :order => '10'} }
37+
38+
it { is_expected.to raise_error(Puppet::Error, /foo isn't supported/) }
39+
end
40+
41+
2642
context "#{os} with object_type => 4247 (not valid string)" do
2743
let(:params) { {:object_type => 4247, :target => '/bar/baz', :order => '10'} }
2844

@@ -102,6 +118,36 @@
102118
end
103119

104120

121+
context "Windows 2012 R2 with ensure => absent" do
122+
let(:facts) { {
123+
:kernel => 'Windows',
124+
:architecture => 'x86_64',
125+
:osfamily => 'Windows',
126+
:operatingsystem => 'Windows',
127+
:operatingsystemmajrelease => '2012 R2'
128+
} }
129+
let(:params) { {:ensure => 'absent', :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
130+
131+
it { is_expected.to contain_concat('C:/bar/baz') }
132+
133+
it { is_expected.not_to contain_concat__fragment('icinga2::object::foo::bar') }
134+
end
135+
136+
137+
context "Windows 2012 R2 with ensure => foo (not a valid value)" do
138+
let(:facts) { {
139+
:kernel => 'Windows',
140+
:architecture => 'x86_64',
141+
:osfamily => 'Windows',
142+
:operatingsystem => 'Windows',
143+
:operatingsystemmajrelease => '2012 R2'
144+
} }
145+
let(:params) { {:ensure => 'foo', :object_type => 'foo', :target => 'C:/bar/baz', :order => '10'} }
146+
147+
it { is_expected.to raise_error(Puppet::Error, /foo isn't supported/) }
148+
end
149+
150+
105151
context "Windows 2012 R2 with object_type => 4247 (not valid string)" do
106152
let(:facts) { {
107153
:kernel => 'Windows',

spec/defines/zone_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@
2323
end
2424

2525

26+
context "#{os} with ensure => absent" do
27+
let(:params) { {:ensure => 'absent', :target => '/bar/baz'} }
28+
29+
it { is_expected.to contain_concat('/bar/baz') }
30+
31+
it { is_expected.not_to contain_concat__fragment('icinga2::object::Zone::bar') }
32+
end
33+
34+
35+
context "#{os} with ensure => foo (not a valid value)" do
36+
let(:params) { {:ensure => 'foo', :target => '/bar/baz'} }
37+
38+
it { is_expected.to raise_error(Puppet::Error, /foo isn't supported/) }
39+
end
40+
41+
2642
context "#{os} with target => bar/baz (not valid absolute path)" do
2743
let(:params) { {:target => 'bar/baz'} }
2844

@@ -101,6 +117,36 @@
101117
end
102118

103119

120+
context "Windows 2012 R2 with ensure => absent" do
121+
let(:facts) { {
122+
:kernel => 'Windows',
123+
:architecture => 'x86_64',
124+
:osfamily => 'Windows',
125+
:operatingsystem => 'Windows',
126+
:operatingsystemmajrelease => '2012 R2'
127+
} }
128+
let(:params) { {:ensure => 'absent', :target => 'C:/bar/baz'} }
129+
130+
it { is_expected.to contain_concat('C:/bar/baz') }
131+
132+
it { is_expected.not_to contain_concat__fragment('icinga2::object::Zone::bar') }
133+
end
134+
135+
136+
context "Windows 2012 R2 with ensure => foo (not a valid value)" do
137+
let(:facts) { {
138+
:kernel => 'Windows',
139+
:architecture => 'x86_64',
140+
:osfamily => 'Windows',
141+
:operatingsystem => 'Windows',
142+
:operatingsystemmajrelease => '2012 R2'
143+
} }
144+
let(:params) { {:ensure => 'foo', :target => 'C:/bar/baz'} }
145+
146+
it { is_expected.to raise_error(Puppet::Error, /foo isn't supported/) }
147+
end
148+
149+
104150
context "Windows 2012 R2 with target => bar/baz (not valid absolute path)" do
105151
let(:facts) { {
106152
:kernel => 'Windows',

templates/object.conf.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
<% if @template %>template<% else %>object<% end %> <%= @object_type -%>
23
<% if scope.lookupvar('icinga2::_constants').include?(@object_name) -%>
34
<%= @object_name -%>
@@ -9,4 +10,3 @@
910
<% unless @import.empty? %><%= "\n" %><% end -%>
1011
<%= scope.function_icinga2_attributes([@attrs]) -%>
1112
}
12-

0 commit comments

Comments
 (0)