| 1 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 2 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 |
# SPDX-FileCopyrightText: 2026 Christoph Fiehe <[email protected]> |
| 4 |
|
| 5 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 6 |
# Do not use this from other collections or standalone plugins/modules! |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import json |
| 11 |
import typing as t |
| 12 |
|
| 13 |
from ansible.module_utils.common.text.converters import to_bytes |
| 14 |
from ansible.module_utils.urls import fetch_url, url_argument_spec |
| 15 |
|
| 16 |
if t.TYPE_CHECKING: |
| 17 |
from http.client import HTTPResponse |
| 18 |
from urllib.error import HTTPError |
| 19 |
|
| 20 |
from ansible.module_utils.basic import AnsibleModule |
| 21 |
|
| 22 |
|
| 23 |
class Icinga2Client: |
| 24 |
def __init__( |
| 25 |
self, |
| 26 |
module: AnsibleModule, |
| 27 |
url: str, |
| 28 |
ca_path: str | None = None, |
| 29 |
timeout: int | float | None = None, |
| 30 |
) -> None: |
| 31 |
self.module = module |
| 32 |
self.url = url.rstrip("/") |
| 33 |
self.ca_path = ca_path |
| 34 |
self.timeout = timeout |
| 35 |
self.actions = Actions(client=self) |
| 36 |
|
| 37 |
def send_request( |
| 38 |
self, method: str, path: str, data: dict[str, t.Any] | None = None |
| 39 |
) -> tuple[HTTPResponse | HTTPError, dict[str, t.Any]]: |
| 40 |
url = f"{self.url}/{path}" |
| 41 |
headers = { |
| 42 |
"X-HTTP-Method-Override": method.upper(), |
| 43 |
"Accept": "application/json", |
| 44 |
} |
| 45 |
return fetch_url( |
| 46 |
module=self.module, |
| 47 |
url=url, |
| 48 |
ca_path=self.ca_path, |
| 49 |
data=to_bytes(json.dumps(data)), |
| 50 |
headers=headers, |
| 51 |
timeout=self.timeout, |
| 52 |
) |
| 53 |
|
| 54 |
|
| 55 |
class Actions: |
| 56 |
base_path = "v1/actions" |
| 57 |
|
| 58 |
def __init__(self, client: Icinga2Client) -> None: |
| 59 |
self.client = client |
| 60 |
|
| 61 |
def schedule_downtime( |
| 62 |
self, |
| 63 |
object_type: str, |
| 64 |
filter: str, |
| 65 |
author: str, |
| 66 |
comment: str, |
| 67 |
start_time: int, |
| 68 |
end_time: int, |
| 69 |
duration: int, |
| 70 |
filter_vars: dict[str, t.Any] | None = None, |
| 71 |
fixed: bool | None = None, |
| 72 |
all_services: bool | None = None, |
| 73 |
trigger_name: str | None = None, |
| 74 |
child_options: str | None = None, |
| 75 |
) -> tuple[HTTPResponse | HTTPError, dict[str, t.Any]]: |
| 76 |
path = f"{self.base_path}/schedule-downtime" |
| 77 |
|
| 78 |
data: dict[str, t.Any] = { |
| 79 |
"type": object_type, |
| 80 |
"filter": filter, |
| 81 |
"author": author, |
| 82 |
"comment": comment, |
| 83 |
"start_time": start_time, |
| 84 |
"end_time": end_time, |
| 85 |
"duration": duration, |
| 86 |
} |
| 87 |
if filter_vars is not None: |
| 88 |
data["filter_vars"] = filter_vars |
| 89 |
if fixed is not None: |
| 90 |
data["fixed"] = fixed |
| 91 |
if all_services is not None: |
| 92 |
data["all_services"] = all_services |
| 93 |
if trigger_name is not None: |
| 94 |
data["trigger_name"] = trigger_name |
| 95 |
if child_options is not None: |
| 96 |
data["child_options"] = child_options |
| 97 |
|
| 98 |
return self.client.send_request(method="POST", path=path, data=data) |
| 99 |
|
| 100 |
def remove_downtime( |
| 101 |
self, |
| 102 |
object_type: str, |
| 103 |
name: str | None = None, |
| 104 |
filter: str | None = None, |
| 105 |
filter_vars: dict[str, t.Any] | None = None, |
| 106 |
) -> tuple[HTTPResponse | HTTPError, dict[str, t.Any]]: |
| 107 |
path = f"{self.base_path}/remove-downtime" |
| 108 |
|
| 109 |
data: dict[str, t.Any] = {"type": object_type} |
| 110 |
if name is not None: |
| 111 |
data[object_type.lower()] = name |
| 112 |
if filter is not None: |
| 113 |
data["filter"] = filter |
| 114 |
if filter_vars is not None: |
| 115 |
data["filter_vars"] = filter_vars |
| 116 |
|
| 117 |
return self.client.send_request(method="POST", path=path, data=data) |
| 118 |
|
| 119 |
|
| 120 |
def icinga2_argument_spec() -> dict[str, t.Any]: |
| 121 |
argument_spec = url_argument_spec() |
| 122 |
argument_spec.update( |
| 123 |
url=dict(type="str", required=True), |
| 124 |
ca_path=dict(type="path"), |
| 125 |
timeout=dict(type="int", default=10), |
| 126 |
) |
| 127 |
return argument_spec |