|
| 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() |
0 commit comments