Skip to content

Commit bfab343

Browse files
authored
Merge pull request #45 from T-Systems-MMS/fix_absent
when deleting objects, only the object_name is required
2 parents b7dd5e5 + 9bf90c0 commit bfab343

11 files changed

Lines changed: 46 additions & 37 deletions

hacking/update_examples_and_tests.sh

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,27 @@ for module in ../plugins/modules/*.py; do
44
module_name="$(basename "${module}" .py)"
55

66
# create examples
7-
echo "---" | tee "../examples/${module_name}.yml"
7+
echo "---" | tee "../examples/${module_name}.yml" 1> /dev/null
88
# https://stackoverflow.com/a/22221307
9-
sed -n '/EXAMPLES/,/"""/{/EXAMPLES/b;/"""/b;p}' "${module}" | tee -a "../examples/${module_name}.yml"
9+
sed -n '/EXAMPLES/,/"""/{/EXAMPLES/b;/"""/b;p}' "${module}" | tee -a "../examples/${module_name}.yml" 1> /dev/null
1010

1111
# create tests
12-
echo "---" | tee "../tests/integration/targets/icinga/roles/icinga/tasks/${module_name}.yml"
13-
sed -n '/EXAMPLES/,/"""/{/EXAMPLES/b;/"""/b;p}' "${module}" | tee -a "../tests/integration/targets/icinga/roles/icinga/tasks/${module_name}.yml"
12+
echo "---" | tee "../tests/integration/targets/icinga/roles/icinga/tasks/${module_name}.yml" 1> /dev/null
13+
sed -n '/EXAMPLES/,/"""/{/EXAMPLES/b;/"""/b;p}' "${module}" | tee -a "../tests/integration/targets/icinga/roles/icinga/tasks/${module_name}.yml" 1> /dev/null
1414

15-
# create create working tests deleting the hosts
16-
echo "---" | tee "../tests/integration/targets/icinga/roles/icinga/tasks/absent_${module_name}.yml"
17-
sed -n '/EXAMPLES/,/"""/{/EXAMPLES/b;/"""/b;p}' "${module}" | tee -a "../tests/integration/targets/icinga/roles/icinga/tasks/absent_${module_name}.yml"
15+
# create working tests deleting the hosts
16+
echo "---" | tee "../tests/integration/targets/icinga/roles/icinga/tasks/absent_${module_name}.yml" 1> /dev/null
17+
sed -n '/EXAMPLES/,/"""/{/EXAMPLES/b;/"""/b;p}' "${module}" | tee -a "../tests/integration/targets/icinga/roles/icinga/tasks/absent_${module_name}.yml" 1> /dev/null
1818
sed -i 's/state: present/state: absent/g' "../tests/integration/targets/icinga/roles/icinga/tasks/absent_${module_name}.yml"
1919

20+
# delete imports and command from the tests, because they aren't necessary to delete an object
21+
# regression test for https://github.com/T-Systems-MMS/ansible-collection-icinga-director/issues/44
22+
sed -i '/imports:/,+1d' "../tests/integration/targets/icinga/roles/icinga/tasks/absent_${module_name}.yml"
23+
sed -i '/^\s*command:/d' "../tests/integration/targets/icinga/roles/icinga/tasks/absent_${module_name}.yml"
24+
2025
# create failing tests with wrong password
21-
echo "---" | tee "../tests/integration/targets/icinga/roles/icinga/tasks/wrong_pass_${module_name}.yml"
22-
sed -n '/EXAMPLES/,/"""/{/EXAMPLES/b;/"""/b;p}' "${module}" | tee -a "../tests/integration/targets/icinga/roles/icinga/tasks/wrong_pass_${module_name}.yml"
26+
echo "---" | tee "../tests/integration/targets/icinga/roles/icinga/tasks/wrong_pass_${module_name}.yml" 1> /dev/null
27+
sed -n '/EXAMPLES/,/"""/{/EXAMPLES/b;/"""/b;p}' "${module}" | tee -a "../tests/integration/targets/icinga/roles/icinga/tasks/wrong_pass_${module_name}.yml" 1> /dev/null
2328
# replace password variable with wrong password
2429
sed -i 's/{{ icinga_pass }}/iamwrong/g' "../tests/integration/targets/icinga/roles/icinga/tasks/wrong_pass_${module_name}.yml"
2530

@@ -37,8 +42,8 @@ for module in ../plugins/modules/*.py; do
3742

3843
# create failing tests with wrong host
3944
# add test
40-
echo "---" | tee "../tests/integration/targets/icinga/roles/icinga/tasks/wrong_host_${module_name}.yml"
41-
sed -n '/EXAMPLES/,/"""/{/EXAMPLES/b;/"""/b;p}' "${module}" | tee -a "../tests/integration/targets/icinga/roles/icinga/tasks/wrong_host_${module_name}.yml"
45+
echo "---" | tee "../tests/integration/targets/icinga/roles/icinga/tasks/wrong_host_${module_name}.yml" 1> /dev/null
46+
sed -n '/EXAMPLES/,/"""/{/EXAMPLES/b;/"""/b;p}' "${module}" | tee -a "../tests/integration/targets/icinga/roles/icinga/tasks/wrong_host_${module_name}.yml" 1> /dev/null
4247
# replace url varuable with nonexisting url
4348
sed -i 's/{{ icinga_url }}/http:\/\/nonexistant/g' "../tests/integration/targets/icinga/roles/icinga/tasks/wrong_host_${module_name}.yml"
4449

plugins/modules/icinga_command.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,11 @@
9090
type: str
9191
command:
9292
description:
93-
- The command Icinga should run.
93+
- The command Icinga should run. Required when state is C(present).
9494
- Absolute paths are accepted as provided, relative paths are prefixed with "PluginDir + ", similar Constant prefixes are allowed.
9595
- Spaces will lead to separation of command path and standalone arguments.
9696
- Please note that this means that we do not support spaces in plugin names and paths right now.
9797
type: str
98-
required: True
9998
command_type:
10099
description:
101100
- Plugin Check commands are what you need when running checks agains your infrastructure.
@@ -232,7 +231,7 @@ def main():
232231
type="bool", required=False, default=False, choices=[True, False]
233232
),
234233
vars=dict(type="dict", default={}),
235-
command=dict(required=True),
234+
command=dict(required=False),
236235
command_type=dict(
237236
default="PluginCheck",
238237
choices=["PluginCheck", "PluginNotification", "PluginEvent"],
@@ -242,9 +241,16 @@ def main():
242241
arguments=dict(type="dict", default=None),
243242
)
244243

244+
# When deleting objects, only the name is necessary, so we cannot use
245+
# required=True in the argument_spec. Instead we define here what is
246+
# necessary when state is present
247+
required_if = [("state", "present", ["command"])]
248+
245249
# Define the main module
246250
module = AnsibleModule(
247-
argument_spec=argument_spec, supports_check_mode=True
251+
argument_spec=argument_spec,
252+
supports_check_mode=True,
253+
required_if=required_if,
248254
)
249255

250256
# typ von arguments ist eigentlich dict, also ohne Angabe = {}

plugins/modules/icinga_host.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,7 @@
132132
choices: [True, False]
133133
imports:
134134
description:
135-
- Choose a Host Template
136-
required: true
135+
- Choose a Host Template. Required when state is C(present).
137136
type: list
138137
elements: str
139138
zone:
@@ -198,7 +197,7 @@ def main():
198197
object_name=dict(required=True),
199198
display_name=dict(required=False),
200199
groups=dict(type="list", elements="str", default=[], required=False),
201-
imports=dict(type="list", elements="str", required=True),
200+
imports=dict(type="list", elements="str", required=False),
202201
disabled=dict(type="bool", default=False, choices=[True, False]),
203202
address=dict(required=False),
204203
address6=dict(required=False),
@@ -207,9 +206,16 @@ def main():
207206
check_command=dict(required=False),
208207
)
209208

209+
# When deleting objects, only the name is necessary, so we cannot use
210+
# required=True in the argument_spec. Instead we define here what is
211+
# necessary when state is present
212+
required_if = [("state", "present", ["imports"])]
213+
210214
# Define the main module
211215
module = AnsibleModule(
212-
argument_spec=argument_spec, supports_check_mode=True
216+
argument_spec=argument_spec,
217+
supports_check_mode=True,
218+
required_if=required_if,
213219
)
214220

215221
data = {

plugins/modules/icinga_notification.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@
124124
type: "str"
125125
imports:
126126
description:
127-
- Importable templates, add as many as you want.
127+
- Importable templates, add as many as you want. Required when state is C(present).
128128
Please note that order matters when importing properties from multiple templates - last one wins
129-
required: true
130129
type: "list"
131130
elements: str
132131
"""
@@ -173,17 +172,24 @@ def main():
173172
state=dict(default="present", choices=["absent", "present"]),
174173
url=dict(required=True),
175174
object_name=dict(required=True),
176-
imports=dict(type="list", elements="str", required=True),
175+
imports=dict(type="list", elements="str", required=False),
177176
apply_to=dict(required=True, choices=["service", "host"]),
178177
assign_filter=dict(required=False),
179178
notification_interval=dict(required=False),
180179
types=dict(type="list", elements="str", required=False),
181180
users=dict(type="list", elements="str", required=False),
182181
)
183182

183+
# When deleting objects, only the name is necessary, so we cannot use
184+
# required=True in the argument_spec. Instead we define here what is
185+
# necessary when state is present
186+
required_if = [("state", "present", ["imports"])]
187+
184188
# Define the main module
185189
module = AnsibleModule(
186-
argument_spec=argument_spec, supports_check_mode=True
190+
argument_spec=argument_spec,
191+
supports_check_mode=True,
192+
required_if=required_if,
187193
)
188194

189195
data = {

tests/integration/targets/icinga/roles/icinga/tasks/absent_icinga_command.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,9 @@
4040
set_if: $centreon_verbose$
4141
'--warning':
4242
value: $centreon_warning$
43-
command: "/opt/centreon-plugins/centreon_plugins.pl"
4443
command_type: "PluginCheck"
4544
disabled: false
4645
object_name: centreon-plugins
47-
imports:
48-
- centreon-plugins-template
4946
timeout: "1m"
5047
vars:
5148
centreon_maxrepetitions: 20

tests/integration/targets/icinga/roles/icinga/tasks/absent_icinga_command_template.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
set_if: $centreon_verbose$
4141
'--warning':
4242
value: $centreon_warning$
43-
command: "/opt/centreon-plugins/centreon_plugins.pl"
4443
command_type: "PluginCheck"
4544
object_name: centreon-plugins-template
4645
timeout: "2m"

tests/integration/targets/icinga/roles/icinga/tasks/absent_icinga_host.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
display_name: "foohost"
1313
groups:
1414
- "foohostgroup"
15-
imports:
16-
- "foohosttemplate"
1715
vars:
1816
dnscheck: "no"
1917
check_command: dummy

tests/integration/targets/icinga/roles/icinga/tasks/absent_icinga_host_template.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@
1111
check_command: dummy
1212
groups:
1313
- "foohostgroup"
14-
imports:
15-
- ''

tests/integration/targets/icinga/roles/icinga/tasks/absent_icinga_notification.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
url_password: "{{ icinga_pass }}"
88
apply_to: host
99
assign_filter: 'host.name="foohost"'
10-
imports:
11-
- foonotificationtemplate
1210
notification_interval: '0'
1311
object_name: E-Mail_host
1412
types:

tests/integration/targets/icinga/roles/icinga/tasks/absent_icinga_service_apply.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
assign_filter: 'host.vars.HostOS="Linux"'
1010
apply_for: "host.vars.enabled_notifications"
1111
display_name: "dummy process"
12-
imports:
13-
- fooservicetemplate
1412
groups:
1513
- fooservicegroup
1614
vars:

0 commit comments

Comments
 (0)