| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright: (c) 2025, Hetzner Cloud GmbH <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = """ |
| 10 |
--- |
| 11 |
module: zone_rrset_info |
| 12 |
|
| 13 |
short_description: Gather infos about your Hetzner Cloud Zone RRSets. |
| 14 |
|
| 15 |
description: |
| 16 |
- Gather infos about your Hetzner Cloud Zone RRSets. |
| 17 |
- See the L(Zone RRSets API documentation,https://docs.hetzner.cloud/reference/cloud#zone-rrsets) for more details. |
| 18 |
|
| 19 |
author: |
| 20 |
- Jonas Lammler (@jooola) |
| 21 |
|
| 22 |
options: |
| 23 |
zone: |
| 24 |
description: |
| 25 |
- Name or ID of the parent Zone. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
id: |
| 29 |
description: |
| 30 |
- ID of the Zone RRSet you want to get. |
| 31 |
type: str |
| 32 |
name: |
| 33 |
description: |
| 34 |
- Name of the Zone RRSets you want to get. |
| 35 |
type: str |
| 36 |
type: |
| 37 |
description: |
| 38 |
- Type of the Zone RRSets you want to get. |
| 39 |
type: str |
| 40 |
label_selector: |
| 41 |
description: |
| 42 |
- Label selector to filter the Zone RRSets you want to get. |
| 43 |
type: str |
| 44 |
|
| 45 |
extends_documentation_fragment: |
| 46 |
- hetzner.hcloud.hcloud |
| 47 |
""" |
| 48 |
|
| 49 |
EXAMPLES = """ |
| 50 |
- name: Gather Zone RRSet infos |
| 51 |
hetzner.hcloud.zone_rrset_info: |
| 52 |
register: output |
| 53 |
- name: Print the gathered infos |
| 54 |
debug: |
| 55 |
var: output.hcloud_zone_rrset_info |
| 56 |
""" |
| 57 |
|
| 58 |
RETURN = """ |
| 59 |
hcloud_zone_rrset_info: |
| 60 |
description: Zone RRSet infos as list. |
| 61 |
returned: always |
| 62 |
type: list |
| 63 |
elements: dict |
| 64 |
contains: |
| 65 |
zone: |
| 66 |
description: ID of the parent Zone. |
| 67 |
type: int |
| 68 |
returned: always |
| 69 |
sample: 42 |
| 70 |
id: |
| 71 |
description: ID of the Zone RRSet. |
| 72 |
returned: always |
| 73 |
type: str |
| 74 |
sample: www/A |
| 75 |
name: |
| 76 |
description: Name of the Zone RRSet. |
| 77 |
returned: always |
| 78 |
type: str |
| 79 |
sample: www |
| 80 |
type: |
| 81 |
description: Mode of the Zone RRSet. |
| 82 |
returned: always |
| 83 |
type: str |
| 84 |
sample: A |
| 85 |
ttl: |
| 86 |
description: TTL of the Zone RRSet. |
| 87 |
returned: always |
| 88 |
type: int |
| 89 |
sample: 10800 |
| 90 |
labels: |
| 91 |
description: User-defined labels (key-value pairs) |
| 92 |
returned: always |
| 93 |
type: dict |
| 94 |
sample: |
| 95 |
key: value |
| 96 |
change_protection: |
| 97 |
description: Protect the Zone RRSet from changes (deletion and updates). |
| 98 |
returned: always |
| 99 |
type: bool |
| 100 |
sample: false |
| 101 |
records: |
| 102 |
description: Record of the Zone RRSet. |
| 103 |
returned: always |
| 104 |
type: list |
| 105 |
elements: dict |
| 106 |
contains: |
| 107 |
value: |
| 108 |
description: Value of the Record. |
| 109 |
returned: always |
| 110 |
type: str |
| 111 |
sample: 203.0.113.1 |
| 112 |
comment: |
| 113 |
description: Comment of the Record. |
| 114 |
returned: always |
| 115 |
type: str |
| 116 |
sample: webserver 1 |
| 117 |
""" |
| 118 |
|
| 119 |
from ansible.module_utils.basic import AnsibleModule |
| 120 |
|
| 121 |
from ..module_utils._base import AnsibleHCloud |
| 122 |
from ..module_utils._vendor.hcloud import APIException, HCloudException |
| 123 |
from ..module_utils._vendor.hcloud.zones import BoundZoneRRSet, Zone, ZoneRecord |
| 124 |
|
| 125 |
|
| 126 |
class AnsibleHCloudZoneRRSetInfo(AnsibleHCloud): |
| 127 |
represent = "hcloud_zone_rrset_info" |
| 128 |
|
| 129 |
hcloud_zone_rrset_info: list[BoundZoneRRSet] | None = None |
| 130 |
|
| 131 |
def _prepare_result(self): |
| 132 |
tmp = [] |
| 133 |
|
| 134 |
for zone_rrset in self.hcloud_zone_rrset_info: |
| 135 |
if zone_rrset is None: |
| 136 |
continue |
| 137 |
|
| 138 |
tmp.append( |
| 139 |
{ |
| 140 |
# Do not use the zone name to prevent a request to the API. |
| 141 |
"zone": zone_rrset.zone.id, |
| 142 |
"id": zone_rrset.id, |
| 143 |
"name": zone_rrset.name, |
| 144 |
"type": zone_rrset.type, |
| 145 |
"ttl": zone_rrset.ttl, |
| 146 |
"labels": zone_rrset.labels, |
| 147 |
"change_protection": zone_rrset.protection["change"], |
| 148 |
"records": [self._prepare_result_record(o) for o in zone_rrset.records or []], |
| 149 |
} |
| 150 |
) |
| 151 |
|
| 152 |
return tmp |
| 153 |
|
| 154 |
def _prepare_result_record(self, record: ZoneRecord): |
| 155 |
return { |
| 156 |
"value": record.value, |
| 157 |
"comment": record.comment or "", |
| 158 |
} |
| 159 |
|
| 160 |
def get_zone_rrsets(self): |
| 161 |
try: |
| 162 |
self.hcloud_zone_rrset_info = [] |
| 163 |
|
| 164 |
# zone name and id are interchangeable |
| 165 |
zone = Zone(self.module.params.get("zone")) |
| 166 |
|
| 167 |
if self.module.params.get("id") is not None: |
| 168 |
# pylint: disable=disallowed-name |
| 169 |
rrset_name, _, rrset_type = self.module.params.get("id").partition("/") |
| 170 |
try: |
| 171 |
self.hcloud_zone_rrset_info = [ |
| 172 |
self.client.zones.get_rrset( |
| 173 |
zone, |
| 174 |
name=rrset_name, |
| 175 |
type=rrset_type, |
| 176 |
) |
| 177 |
] |
| 178 |
except APIException as api_exception: |
| 179 |
if api_exception.code != "not_found": |
| 180 |
raise |
| 181 |
elif self.module.params.get("name") is not None: |
| 182 |
rrset_name, rrset_type = self.module.params.get("name"), self.module.params.get("type") |
| 183 |
try: |
| 184 |
self.hcloud_zone_rrset_info = [ |
| 185 |
self.client.zones.get_rrset( |
| 186 |
zone, |
| 187 |
name=rrset_name, |
| 188 |
type=rrset_type, |
| 189 |
) |
| 190 |
] |
| 191 |
except APIException as api_exception: |
| 192 |
if api_exception.code != "not_found": |
| 193 |
raise |
| 194 |
elif self.module.params.get("label_selector") is not None: |
| 195 |
self.hcloud_zone_rrset_info = self.client.zones.get_rrset_all( |
| 196 |
zone, |
| 197 |
label_selector=self.module.params.get("label_selector"), |
| 198 |
) |
| 199 |
else: |
| 200 |
self.hcloud_zone_rrset_info = self.client.zones.get_rrset_all(zone) |
| 201 |
|
| 202 |
except HCloudException as exception: |
| 203 |
self.fail_json_hcloud(exception) |
| 204 |
|
| 205 |
@classmethod |
| 206 |
def define_module(cls): |
| 207 |
return AnsibleModule( |
| 208 |
argument_spec=dict( |
| 209 |
zone={"type": "str", "required": True}, |
| 210 |
id={"type": "str"}, |
| 211 |
name={"type": "str"}, |
| 212 |
type={"type": "str"}, |
| 213 |
label_selector={"type": "str"}, |
| 214 |
**super().base_module_arguments(), |
| 215 |
), |
| 216 |
required_together=[("name", "type")], |
| 217 |
supports_check_mode=True, |
| 218 |
) |
| 219 |
|
| 220 |
|
| 221 |
def main(): |
| 222 |
module = AnsibleHCloudZoneRRSetInfo.define_module() |
| 223 |
hcloud = AnsibleHCloudZoneRRSetInfo(module) |
| 224 |
|
| 225 |
hcloud.get_zone_rrsets() |
| 226 |
result = hcloud.get_result() |
| 227 |
|
| 228 |
ansible_info = {"hcloud_zone_rrset_info": result["hcloud_zone_rrset_info"]} |
| 229 |
module.exit_json(**ansible_info) |
| 230 |
|
| 231 |
|
| 232 |
if __name__ == "__main__": |
| 233 |
main() |