Skip to content

Commit 221a9fd

Browse files
author
Lennart Betz
committed
add feature #12771 add parameter ensure to objects
1 parent 6852387 commit 221a9fd

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
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
}

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',

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)