Skip to content

Commit d705f50

Browse files
rndmh3roschurzi
andauthored
add timeperiod_template and corresponding info module (#118)
* add timeperiod_template and corresponding info module Co-authored-by: schurzi <Martin.Schurz@t-systems.com>
1 parent d869f1d commit d705f50

22 files changed

Lines changed: 502 additions & 0 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
*.tar.gz
22
test/results
3+
tests/output
34
.tox

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Currently supported modules:
2222
* `icinga_service_template`
2323
* `icinga_servicegroup`
2424
* `icinga_timeperiod`
25+
* `icinga_timeperiod_template`
2526
* `icinga_user_group`
2627
* `icinga_user_template`
2728
* `icinga_user`

examples/icinga_timeperiod.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
url_username: "{{ icinga_user }}"
77
url_password: "{{ icinga_pass }}"
88
object_name: '24/7'
9+
imports:
10+
- "timeperiod_template"
911
ranges:
1012
monday: "00:00-23:59"
1113
tuesday: "00:00-23:59"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
- name: Create timeperiod template
3+
t_systems_mms.icinga_director.icinga_timeperiod_template:
4+
state: present
5+
url: "{{ icinga_url }}"
6+
url_username: "{{ icinga_user }}"
7+
url_password: "{{ icinga_pass }}"
8+
object_name: "timeperiod_template"
9+
display_name: "timeperiod template"
10+
imports: []
11+
disabled: false
12+
prefer_includes: false
13+
ranges:
14+
monday: "00:00-23:59"
15+
tuesday: "00:00-23:59"
16+
wednesday: "00:00-23:59"
17+
thursday: "00:00-23:59"
18+
friday: "00:00-23:59"
19+
saturday: "00:00-23:59"
20+
sunday: "00:00-23:59"
21+
update_method: "LegacyTimePeriod"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
- name: Query a timeperiod in icinga
3+
t_systems_mms.icinga_director.icinga_timeperiod_template_info:
4+
url: "{{ icinga_url }}"
5+
url_username: "{{ icinga_user }}"
6+
url_password: "{{ icinga_pass }}"
7+
query: "timeperiod_template"

plugins/modules/icinga_timeperiod.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
url_username: "{{ icinga_user }}"
7272
url_password: "{{ icinga_pass }}"
7373
object_name: '24/7'
74+
imports:
75+
- "timeperiod_template"
7476
ranges:
7577
monday: "00:00-23:59"
7678
tuesday: "00:00-23:59"
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2021 T-Systems Multimedia Solutions GmbH
5+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6+
#
7+
# This module is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This software is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this software. If not, see <http://www.gnu.org/licenses/>.
19+
20+
from __future__ import absolute_import, division, print_function
21+
22+
__metaclass__ = type
23+
24+
DOCUMENTATION = """
25+
---
26+
module: icinga_timeperiod_template
27+
short_description: Manage timeperiod templates in Icinga2
28+
description:
29+
- Add or remove a timeperiod template to Icinga2 through the director API.
30+
author: Sebastian Gumprich (@rndmh3ro)
31+
extends_documentation_fragment:
32+
- ansible.builtin.url
33+
- t_systems_mms.icinga_director.common_options
34+
version_added: '1.17.0'
35+
notes:
36+
- This module supports check mode.
37+
options:
38+
state:
39+
description:
40+
- Apply feature state.
41+
choices: [ "present", "absent" ]
42+
default: present
43+
type: str
44+
object_name:
45+
description:
46+
- Name of the time period.
47+
aliases: ['name']
48+
required: true
49+
type: str
50+
display_name:
51+
description:
52+
- Alternative name for this timeperiod template.
53+
type: str
54+
disabled:
55+
description:
56+
- Disabled objects will not be deployed.
57+
type: bool
58+
default: False
59+
choices: [True, False]
60+
imports:
61+
description:
62+
- Importable templates, add as many as you want.
63+
- Please note that order matters when importing properties from multiple templates - last one wins.
64+
type: list
65+
elements: str
66+
include_period:
67+
description:
68+
- Include other time periods into this.
69+
type: list
70+
elements: str
71+
exclude_period:
72+
description:
73+
- Exclude other time periods from this.
74+
type: list
75+
elements: str
76+
prefer_includes:
77+
description:
78+
- Whether to prefer timeperiods includes or excludes. Default to true.
79+
type: bool
80+
default: true
81+
choices: [True, False]
82+
ranges:
83+
description:
84+
- A dict of days and timeperiods.
85+
type: dict
86+
zone:
87+
description:
88+
- Set the zone.
89+
type: str
90+
update_method:
91+
description:
92+
- Define the update method.
93+
type: str
94+
default: "LegacyTimePeriod"
95+
"""
96+
97+
EXAMPLES = """
98+
- name: Create timeperiod template
99+
t_systems_mms.icinga_director.icinga_timeperiod_template:
100+
state: present
101+
url: "{{ icinga_url }}"
102+
url_username: "{{ icinga_user }}"
103+
url_password: "{{ icinga_pass }}"
104+
object_name: "timeperiod_template"
105+
display_name: "timeperiod template"
106+
imports: []
107+
disabled: false
108+
prefer_includes: false
109+
ranges:
110+
monday: "00:00-23:59"
111+
tuesday: "00:00-23:59"
112+
wednesday: "00:00-23:59"
113+
thursday: "00:00-23:59"
114+
friday: "00:00-23:59"
115+
saturday: "00:00-23:59"
116+
sunday: "00:00-23:59"
117+
update_method: "LegacyTimePeriod"
118+
119+
"""
120+
121+
RETURN = r""" # """
122+
123+
from ansible.module_utils.basic import AnsibleModule
124+
from ansible.module_utils.urls import url_argument_spec
125+
from ansible_collections.t_systems_mms.icinga_director.plugins.module_utils.icinga import (
126+
Icinga2APIObject,
127+
)
128+
129+
130+
# ===========================================
131+
# Module execution.
132+
#
133+
def main():
134+
# use the predefined argument spec for url
135+
argument_spec = url_argument_spec()
136+
# add our own arguments
137+
argument_spec.update(
138+
state=dict(default="present", choices=["absent", "present"]),
139+
url=dict(required=True),
140+
object_name=dict(required=True, aliases=["name"]),
141+
display_name=dict(required=False),
142+
disabled=dict(type="bool", default=False, choices=[True, False]),
143+
zone=dict(required=False, default=None),
144+
imports=dict(type="list", elements="str", default=[], required=False),
145+
ranges=dict(type="dict", required=False),
146+
prefer_includes=dict(type="bool", default=True, choices=[True, False]),
147+
exclude_period=dict(
148+
type="list", elements="str", default=[], required=False
149+
),
150+
include_period=dict(
151+
type="list", elements="str", default=[], required=False
152+
),
153+
update_method=dict(required=False, default="LegacyTimePeriod"),
154+
)
155+
156+
# Define the main module
157+
module = AnsibleModule(
158+
argument_spec=argument_spec, supports_check_mode=True
159+
)
160+
161+
data = {
162+
"object_name": module.params["object_name"],
163+
"object_type": "template",
164+
"display_name": module.params["display_name"],
165+
"disabled": module.params["disabled"],
166+
"zone": module.params["zone"],
167+
"imports": module.params["imports"],
168+
"ranges": module.params["ranges"],
169+
"prefer_includes": module.params["prefer_includes"],
170+
"excludes": module.params["exclude_period"],
171+
"includes": module.params["include_period"],
172+
"update_method": module.params["update_method"],
173+
}
174+
175+
icinga_object = Icinga2APIObject(
176+
module=module, path="/timeperiod", data=data
177+
)
178+
179+
changed, diff = icinga_object.update(module.params["state"])
180+
module.exit_json(
181+
changed=changed,
182+
diff=diff,
183+
)
184+
185+
186+
# import module snippets
187+
if __name__ == "__main__":
188+
main()
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2021 T-Systems Multimedia Solutions GmbH
5+
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6+
#
7+
# This module is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# This software is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this software. If not, see <http://www.gnu.org/licenses/>.
19+
20+
from __future__ import absolute_import, division, print_function
21+
22+
__metaclass__ = type
23+
24+
DOCUMENTATION = """
25+
---
26+
module: icinga_timeperiod_template_info
27+
short_description: Query timeperiod templates in Icinga2
28+
description:
29+
- Get a list of timeperiod template objects from Icinga2 through the director API.
30+
author: Sebastian Gumprich (@rndmh3ro)
31+
extends_documentation_fragment:
32+
- ansible.builtin.url
33+
- t_systems_mms.icinga_director.common_options
34+
version_added: '1.17.0'
35+
notes:
36+
- This module supports check mode.
37+
options:
38+
query:
39+
description:
40+
- Text to filter search results.
41+
- The text is matched on object_name.
42+
- Only objects containing this text will be returned in the resultset.
43+
- Requires Icinga Director 1.8.0+, in earlier versions this parameter is ignored and all objects are returned.
44+
required: false
45+
type: str
46+
default: ""
47+
resolved:
48+
description:
49+
- Resolve all inherited object properties and omit templates in output.
50+
type: bool
51+
default: False
52+
choices: [True, False]
53+
"""
54+
55+
EXAMPLES = """
56+
- name: Query a timeperiod template in icinga
57+
t_systems_mms.icinga_director.icinga_timeperiod_template_info:
58+
url: "{{ icinga_url }}"
59+
url_username: "{{ icinga_user }}"
60+
url_password: "{{ icinga_pass }}"
61+
query: "timeperiod_template"
62+
"""
63+
64+
RETURN = r"""
65+
objects:
66+
description:
67+
- A list of returned Director objects.
68+
- The list contains all objects matching the query filter.
69+
- If the filter does not match any object, the list will be empty.
70+
returned: always
71+
type: list
72+
"""
73+
74+
from ansible.module_utils.basic import AnsibleModule
75+
from ansible.module_utils.urls import url_argument_spec
76+
from ansible_collections.t_systems_mms.icinga_director.plugins.module_utils.icinga import (
77+
Icinga2APIObject,
78+
)
79+
80+
81+
# ===========================================
82+
# Module execution.
83+
#
84+
def main():
85+
# use the predefined argument spec for url
86+
argument_spec = url_argument_spec()
87+
# add our own arguments
88+
argument_spec.update(
89+
url=dict(required=True),
90+
query=dict(type="str", required=False, default=""),
91+
resolved=dict(type="bool", default=False, choices=[True, False]),
92+
)
93+
94+
# Define the main module
95+
module = AnsibleModule(
96+
argument_spec=argument_spec,
97+
supports_check_mode=True,
98+
)
99+
100+
icinga_object = Icinga2APIObject(
101+
module=module, path="/timeperiods/templates", data=[]
102+
)
103+
104+
object_list = icinga_object.query(
105+
query=module.params["query"], resolved=module.params["resolved"]
106+
)
107+
module.exit_json(
108+
objects=object_list["data"]["objects"],
109+
)
110+
111+
112+
# import module snippets
113+
if __name__ == "__main__":
114+
main()

tests/integration/targets/icinga/checkmode.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- absent_icinga_user.yml
1818
- absent_icinga_user_group.yml
1919
- absent_icinga_user_template.yml
20+
- absent_icinga_timeperiod_template.yml
2021
- absent_icinga_timeperiod.yml
2122
- absent_icinga_command.yml
2223
- absent_icinga_command_template.yml
@@ -28,6 +29,7 @@
2829
- icinga_endpoint.yml
2930
- icinga_command_template.yml
3031
- icinga_command.yml
32+
- icinga_timeperiod_template.yml
3133
- icinga_timeperiod.yml
3234
- icinga_user_template.yml
3335
- icinga_user_group.yml
@@ -54,6 +56,7 @@
5456
- absent_icinga_user_group.yml
5557
- absent_icinga_user_template.yml
5658
- absent_icinga_timeperiod.yml
59+
- absent_icinga_timeperiod_template.yml
5760
- absent_icinga_command.yml
5861
- absent_icinga_command_template.yml
5962
- absent_icinga_endpoint.yml

0 commit comments

Comments
 (0)