Skip to content

Commit

Permalink
add timeperiod_template and corresponding info module (#118)
Browse files Browse the repository at this point in the history
* add timeperiod_template and corresponding info module

Co-authored-by: schurzi <[email protected]>
  • Loading branch information
rndmh3ro and schurzi authored May 9, 2021
1 parent d869f1d commit d705f50
Show file tree
Hide file tree
Showing 22 changed files with 502 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
*.tar.gz
test/results
tests/output
.tox
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Currently supported modules:
* `icinga_service_template`
* `icinga_servicegroup`
* `icinga_timeperiod`
* `icinga_timeperiod_template`
* `icinga_user_group`
* `icinga_user_template`
* `icinga_user`
Expand Down
2 changes: 2 additions & 0 deletions examples/icinga_timeperiod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
url_username: "{{ icinga_user }}"
url_password: "{{ icinga_pass }}"
object_name: '24/7'
imports:
- "timeperiod_template"
ranges:
monday: "00:00-23:59"
tuesday: "00:00-23:59"
Expand Down
21 changes: 21 additions & 0 deletions examples/icinga_timeperiod_template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
- name: Create timeperiod template
t_systems_mms.icinga_director.icinga_timeperiod_template:
state: present
url: "{{ icinga_url }}"
url_username: "{{ icinga_user }}"
url_password: "{{ icinga_pass }}"
object_name: "timeperiod_template"
display_name: "timeperiod template"
imports: []
disabled: false
prefer_includes: false
ranges:
monday: "00:00-23:59"
tuesday: "00:00-23:59"
wednesday: "00:00-23:59"
thursday: "00:00-23:59"
friday: "00:00-23:59"
saturday: "00:00-23:59"
sunday: "00:00-23:59"
update_method: "LegacyTimePeriod"
7 changes: 7 additions & 0 deletions examples/icinga_timeperiod_template_info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Query a timeperiod in icinga
t_systems_mms.icinga_director.icinga_timeperiod_template_info:
url: "{{ icinga_url }}"
url_username: "{{ icinga_user }}"
url_password: "{{ icinga_pass }}"
query: "timeperiod_template"
2 changes: 2 additions & 0 deletions plugins/modules/icinga_timeperiod.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
url_username: "{{ icinga_user }}"
url_password: "{{ icinga_pass }}"
object_name: '24/7'
imports:
- "timeperiod_template"
ranges:
monday: "00:00-23:59"
tuesday: "00:00-23:59"
Expand Down
188 changes: 188 additions & 0 deletions plugins/modules/icinga_timeperiod_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 T-Systems Multimedia Solutions GmbH
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#
# This module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import, division, print_function

__metaclass__ = type

DOCUMENTATION = """
---
module: icinga_timeperiod_template
short_description: Manage timeperiod templates in Icinga2
description:
- Add or remove a timeperiod template to Icinga2 through the director API.
author: Sebastian Gumprich (@rndmh3ro)
extends_documentation_fragment:
- ansible.builtin.url
- t_systems_mms.icinga_director.common_options
version_added: '1.17.0'
notes:
- This module supports check mode.
options:
state:
description:
- Apply feature state.
choices: [ "present", "absent" ]
default: present
type: str
object_name:
description:
- Name of the time period.
aliases: ['name']
required: true
type: str
display_name:
description:
- Alternative name for this timeperiod template.
type: str
disabled:
description:
- Disabled objects will not be deployed.
type: bool
default: False
choices: [True, False]
imports:
description:
- Importable templates, add as many as you want.
- Please note that order matters when importing properties from multiple templates - last one wins.
type: list
elements: str
include_period:
description:
- Include other time periods into this.
type: list
elements: str
exclude_period:
description:
- Exclude other time periods from this.
type: list
elements: str
prefer_includes:
description:
- Whether to prefer timeperiods includes or excludes. Default to true.
type: bool
default: true
choices: [True, False]
ranges:
description:
- A dict of days and timeperiods.
type: dict
zone:
description:
- Set the zone.
type: str
update_method:
description:
- Define the update method.
type: str
default: "LegacyTimePeriod"
"""

EXAMPLES = """
- name: Create timeperiod template
t_systems_mms.icinga_director.icinga_timeperiod_template:
state: present
url: "{{ icinga_url }}"
url_username: "{{ icinga_user }}"
url_password: "{{ icinga_pass }}"
object_name: "timeperiod_template"
display_name: "timeperiod template"
imports: []
disabled: false
prefer_includes: false
ranges:
monday: "00:00-23:59"
tuesday: "00:00-23:59"
wednesday: "00:00-23:59"
thursday: "00:00-23:59"
friday: "00:00-23:59"
saturday: "00:00-23:59"
sunday: "00:00-23:59"
update_method: "LegacyTimePeriod"
"""

RETURN = r""" # """

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import url_argument_spec
from ansible_collections.t_systems_mms.icinga_director.plugins.module_utils.icinga import (
Icinga2APIObject,
)


# ===========================================
# Module execution.
#
def main():
# use the predefined argument spec for url
argument_spec = url_argument_spec()
# add our own arguments
argument_spec.update(
state=dict(default="present", choices=["absent", "present"]),
url=dict(required=True),
object_name=dict(required=True, aliases=["name"]),
display_name=dict(required=False),
disabled=dict(type="bool", default=False, choices=[True, False]),
zone=dict(required=False, default=None),
imports=dict(type="list", elements="str", default=[], required=False),
ranges=dict(type="dict", required=False),
prefer_includes=dict(type="bool", default=True, choices=[True, False]),
exclude_period=dict(
type="list", elements="str", default=[], required=False
),
include_period=dict(
type="list", elements="str", default=[], required=False
),
update_method=dict(required=False, default="LegacyTimePeriod"),
)

# Define the main module
module = AnsibleModule(
argument_spec=argument_spec, supports_check_mode=True
)

data = {
"object_name": module.params["object_name"],
"object_type": "template",
"display_name": module.params["display_name"],
"disabled": module.params["disabled"],
"zone": module.params["zone"],
"imports": module.params["imports"],
"ranges": module.params["ranges"],
"prefer_includes": module.params["prefer_includes"],
"excludes": module.params["exclude_period"],
"includes": module.params["include_period"],
"update_method": module.params["update_method"],
}

icinga_object = Icinga2APIObject(
module=module, path="/timeperiod", data=data
)

changed, diff = icinga_object.update(module.params["state"])
module.exit_json(
changed=changed,
diff=diff,
)


# import module snippets
if __name__ == "__main__":
main()
114 changes: 114 additions & 0 deletions plugins/modules/icinga_timeperiod_template_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 T-Systems Multimedia Solutions GmbH
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
#
# This module is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>.

from __future__ import absolute_import, division, print_function

__metaclass__ = type

DOCUMENTATION = """
---
module: icinga_timeperiod_template_info
short_description: Query timeperiod templates in Icinga2
description:
- Get a list of timeperiod template objects from Icinga2 through the director API.
author: Sebastian Gumprich (@rndmh3ro)
extends_documentation_fragment:
- ansible.builtin.url
- t_systems_mms.icinga_director.common_options
version_added: '1.17.0'
notes:
- This module supports check mode.
options:
query:
description:
- Text to filter search results.
- The text is matched on object_name.
- Only objects containing this text will be returned in the resultset.
- Requires Icinga Director 1.8.0+, in earlier versions this parameter is ignored and all objects are returned.
required: false
type: str
default: ""
resolved:
description:
- Resolve all inherited object properties and omit templates in output.
type: bool
default: False
choices: [True, False]
"""

EXAMPLES = """
- name: Query a timeperiod template in icinga
t_systems_mms.icinga_director.icinga_timeperiod_template_info:
url: "{{ icinga_url }}"
url_username: "{{ icinga_user }}"
url_password: "{{ icinga_pass }}"
query: "timeperiod_template"
"""

RETURN = r"""
objects:
description:
- A list of returned Director objects.
- The list contains all objects matching the query filter.
- If the filter does not match any object, the list will be empty.
returned: always
type: list
"""

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import url_argument_spec
from ansible_collections.t_systems_mms.icinga_director.plugins.module_utils.icinga import (
Icinga2APIObject,
)


# ===========================================
# Module execution.
#
def main():
# use the predefined argument spec for url
argument_spec = url_argument_spec()
# add our own arguments
argument_spec.update(
url=dict(required=True),
query=dict(type="str", required=False, default=""),
resolved=dict(type="bool", default=False, choices=[True, False]),
)

# Define the main module
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
)

icinga_object = Icinga2APIObject(
module=module, path="/timeperiods/templates", data=[]
)

object_list = icinga_object.query(
query=module.params["query"], resolved=module.params["resolved"]
)
module.exit_json(
objects=object_list["data"]["objects"],
)


# import module snippets
if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions tests/integration/targets/icinga/checkmode.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
- absent_icinga_user.yml
- absent_icinga_user_group.yml
- absent_icinga_user_template.yml
- absent_icinga_timeperiod_template.yml
- absent_icinga_timeperiod.yml
- absent_icinga_command.yml
- absent_icinga_command_template.yml
Expand All @@ -28,6 +29,7 @@
- icinga_endpoint.yml
- icinga_command_template.yml
- icinga_command.yml
- icinga_timeperiod_template.yml
- icinga_timeperiod.yml
- icinga_user_template.yml
- icinga_user_group.yml
Expand All @@ -54,6 +56,7 @@
- absent_icinga_user_group.yml
- absent_icinga_user_template.yml
- absent_icinga_timeperiod.yml
- absent_icinga_timeperiod_template.yml
- absent_icinga_command.yml
- absent_icinga_command_template.yml
- absent_icinga_endpoint.yml
Expand Down
Loading

0 comments on commit d705f50

Please sign in to comment.