summaryrefslogtreecommitdiffstats
path: root/ansible_collections/hetzner/hcloud/plugins/modules/rdns.py
blob: 4e21f3e92be4be425b02891abc28e055e19ea75c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/python

# Copyright: (c) 2019, Hetzner Cloud GmbH <info@hetzner-cloud.de>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import annotations

DOCUMENTATION = """
---
module: rdns

short_description: Create and manage reverse DNS entries on the Hetzner Cloud.


description:
    - Create, update and delete reverse DNS entries on the Hetzner Cloud.

author:
    - Lukas Kaemmerling (@lkaemmerling)

options:
    server:
        description:
            - Name or ID of the Hetzner Cloud server you want to add the reverse DNS entry to.
        type: str
    floating_ip:
        description:
            - Name or ID of the Hetzner Cloud Floating IP you want to add the reverse DNS entry to.
        type: str
    primary_ip:
        description:
            - Name or ID of the Hetzner Cloud Primary IP you want to add the reverse DNS entry to.
        type: str
    load_balancer:
        description:
            - Name or ID of the Hetzner Cloud Load Balancer you want to add the reverse DNS entry to.
        type: str
    ip_address:
        description:
            - The IP address that should point to I(dns_ptr).
        type: str
        required: true
    dns_ptr:
        description:
            - The DNS address the I(ip_address) should resolve to.
            - Omit the param to reset the reverse DNS entry to the default value.
        type: str
    state:
        description:
            - State of the reverse DNS entry.
        default: present
        choices: [ absent, present ]
        type: str

extends_documentation_fragment:
- hetzner.hcloud.hcloud
"""

EXAMPLES = """
- name: Create a reverse DNS entry for a server
  hetzner.hcloud.rdns:
    server: my-server
    ip_address: 123.123.123.123
    dns_ptr: example.com
    state: present

- name: Create a reverse DNS entry for a Floating IP
  hetzner.hcloud.rdns:
    floating_ip: my-floating-ip
    ip_address: 123.123.123.123
    dns_ptr: example.com
    state: present

- name: Create a reverse DNS entry for a Primary IP
  hetzner.hcloud.rdns:
    primary_ip: my-primary-ip
    ip_address: 123.123.123.123
    dns_ptr: example.com
    state: present

- name: Create a reverse DNS entry for a Load Balancer
  hetzner.hcloud.rdns:
    load_balancer: my-load-balancer
    ip_address: 123.123.123.123
    dns_ptr: example.com
    state: present

- name: Ensure the reverse DNS entry is absent (remove if needed)
  hetzner.hcloud.rdns:
    server: my-server
    ip_address: 123.123.123.123
    dns_ptr: example.com
    state: absent
"""

RETURN = """
hcloud_rdns:
    description: The reverse DNS entry
    returned: always
    type: complex
    contains:
        server:
            description: Name of the server
            type: str
            returned: always
            sample: my-server
        floating_ip:
            description: Name of the Floating IP
            type: str
            returned: always
            sample: my-floating-ip
        primary_ip:
            description: Name of the Primary IP
            type: str
            returned: always
            sample: my-primary-ip
        load_balancer:
            description: Name of the Load Balancer
            type: str
            returned: always
            sample: my-load-balancer
        ip_address:
            description: The IP address that point to the DNS ptr
            type: str
            returned: always
            sample: 123.123.123.123
        dns_ptr:
            description: The DNS that resolves to the IP
            type: str
            returned: always
            sample: example.com
"""

import ipaddress
from typing import Any

from ansible.module_utils.basic import AnsibleModule

from ..module_utils.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import HCloudException
from ..module_utils.vendor.hcloud.floating_ips import BoundFloatingIP
from ..module_utils.vendor.hcloud.load_balancers import BoundLoadBalancer
from ..module_utils.vendor.hcloud.primary_ips import BoundPrimaryIP
from ..module_utils.vendor.hcloud.servers import BoundServer


class AnsibleHCloudReverseDNS(AnsibleHCloud):
    represent = "hcloud_rdns"

    hcloud_resource: BoundServer | BoundFloatingIP | BoundLoadBalancer | BoundPrimaryIP | None = None
    hcloud_rdns: dict[str, Any] | None = None

    def _prepare_result(self):
        result = {
            "server": None,
            "floating_ip": None,
            "load_balancer": None,
            "ip_address": self.hcloud_rdns["ip_address"],
            "dns_ptr": self.hcloud_rdns["dns_ptr"],
        }

        if self.module.params.get("server"):
            result["server"] = self.hcloud_resource.name
        elif self.module.params.get("floating_ip"):
            result["floating_ip"] = self.hcloud_resource.name
        elif self.module.params.get("load_balancer"):
            result["load_balancer"] = self.hcloud_resource.name
        elif self.module.params.get("primary_ip"):
            result["primary_ip"] = self.hcloud_resource.name
        return result

    def _get_resource(self):
        try:
            if self.module.params.get("server"):
                self.hcloud_resource = self._client_get_by_name_or_id(
                    "servers",
                    self.module.params.get("server"),
                )
            elif self.module.params.get("floating_ip"):
                self.hcloud_resource = self._client_get_by_name_or_id(
                    "floating_ips",
                    self.module.params.get("floating_ip"),
                )
            elif self.module.params.get("primary_ip"):
                self.hcloud_resource = self._client_get_by_name_or_id(
                    "primary_ips",
                    self.module.params.get("primary_ip"),
                )
            elif self.module.params.get("load_balancer"):
                self.hcloud_resource = self._client_get_by_name_or_id(
                    "load_balancers",
                    self.module.params.get("load_balancer"),
                )
        except HCloudException as exception:
            self.fail_json_hcloud(exception)

    def _get_rdns(self):
        ip_address = self.module.params.get("ip_address")

        try:
            ip_address_obj = ipaddress.ip_address(ip_address)
        except ValueError:
            self.module.fail_json(msg=f"The given IP address is not valid: {ip_address}")

        if ip_address_obj.version == 4:
            if self.module.params.get("server"):
                if self.hcloud_resource.public_net.ipv4.ip == ip_address:
                    self.hcloud_rdns = {
                        "ip_address": self.hcloud_resource.public_net.ipv4.ip,
                        "dns_ptr": self.hcloud_resource.public_net.ipv4.dns_ptr,
                    }
                else:
                    self.module.fail_json(msg="The selected server does not have this IP address")
            elif self.module.params.get("floating_ip"):
                if self.hcloud_resource.ip == ip_address:
                    self.hcloud_rdns = {
                        "ip_address": self.hcloud_resource.ip,
                        "dns_ptr": self.hcloud_resource.dns_ptr[0]["dns_ptr"],
                    }
                else:
                    self.module.fail_json(msg="The selected Floating IP does not have this IP address")
            elif self.module.params.get("primary_ip"):
                if self.hcloud_resource.ip == ip_address:
                    self.hcloud_rdns = {
                        "ip_address": self.hcloud_resource.ip,
                        "dns_ptr": self.hcloud_resource.dns_ptr[0]["dns_ptr"],
                    }
                else:
                    self.module.fail_json(msg="The selected Primary IP does not have this IP address")
            elif self.module.params.get("load_balancer"):
                if self.hcloud_resource.public_net.ipv4.ip == ip_address:
                    self.hcloud_rdns = {
                        "ip_address": self.hcloud_resource.public_net.ipv4.ip,
                        "dns_ptr": self.hcloud_resource.public_net.ipv4.dns_ptr,
                    }
                else:
                    self.module.fail_json(msg="The selected Load Balancer does not have this IP address")

        elif ip_address_obj.version == 6:
            if self.module.params.get("server"):
                for ipv6_address_dns_ptr in self.hcloud_resource.public_net.ipv6.dns_ptr:
                    if ipv6_address_dns_ptr["ip"] == ip_address:
                        self.hcloud_rdns = {
                            "ip_address": ipv6_address_dns_ptr["ip"],
                            "dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
                        }
            elif self.module.params.get("floating_ip"):
                for ipv6_address_dns_ptr in self.hcloud_resource.dns_ptr:
                    if ipv6_address_dns_ptr["ip"] == ip_address:
                        self.hcloud_rdns = {
                            "ip_address": ipv6_address_dns_ptr["ip"],
                            "dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
                        }
            elif self.module.params.get("primary_ip"):
                for ipv6_address_dns_ptr in self.hcloud_resource.dns_ptr:
                    if ipv6_address_dns_ptr["ip"] == ip_address:
                        self.hcloud_rdns = {
                            "ip_address": ipv6_address_dns_ptr["ip"],
                            "dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
                        }
            elif self.module.params.get("load_balancer"):
                for ipv6_address_dns_ptr in self.hcloud_resource.public_net.ipv6.dns_ptr:
                    if ipv6_address_dns_ptr["ip"] == ip_address:
                        self.hcloud_rdns = {
                            "ip_address": ipv6_address_dns_ptr["ip"],
                            "dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
                        }

    def _create_rdns(self):
        self.module.fail_on_missing_params(required_params=["dns_ptr"])
        params = {
            "ip": self.module.params.get("ip_address"),
            "dns_ptr": self.module.params.get("dns_ptr"),
        }

        if not self.module.check_mode:
            try:
                action = self.hcloud_resource.change_dns_ptr(**params)
                action.wait_until_finished()
            except HCloudException as exception:
                self.fail_json_hcloud(exception)
        self._mark_as_changed()
        self._get_resource()
        self._get_rdns()

    def _update_rdns(self):
        dns_ptr = self.module.params.get("dns_ptr")
        if dns_ptr != self.hcloud_rdns["dns_ptr"]:
            params = {
                "ip": self.module.params.get("ip_address"),
                "dns_ptr": dns_ptr,
            }

            if not self.module.check_mode:
                try:
                    action = self.hcloud_resource.change_dns_ptr(**params)
                    action.wait_until_finished()
                except HCloudException as exception:
                    self.fail_json_hcloud(exception)
            self._mark_as_changed()
            self._get_resource()
            self._get_rdns()

    def present_rdns(self):
        self._get_resource()
        self._get_rdns()
        if self.hcloud_rdns is None:
            self._create_rdns()
        else:
            self._update_rdns()

    def delete_rdns(self):
        self._get_resource()
        self._get_rdns()
        if self.hcloud_rdns is not None:
            if not self.module.check_mode:
                try:
                    self.hcloud_resource.change_dns_ptr(ip=self.hcloud_rdns["ip_address"], dns_ptr=None)
                except HCloudException as exception:
                    self.fail_json_hcloud(exception)
            self._mark_as_changed()
        self.hcloud_rdns = None

    @classmethod
    def define_module(cls):
        return AnsibleModule(
            argument_spec=dict(
                server={"type": "str"},
                floating_ip={"type": "str"},
                load_balancer={"type": "str"},
                primary_ip={"type": "str"},
                ip_address={"type": "str", "required": True},
                dns_ptr={"type": "str"},
                state={
                    "choices": ["absent", "present"],
                    "default": "present",
                },
                **super().base_module_arguments(),
            ),
            required_one_of=[["server", "floating_ip", "load_balancer", "primary_ip"]],
            mutually_exclusive=[["server", "floating_ip", "load_balancer", "primary_ip"]],
            supports_check_mode=True,
        )


def main():
    module = AnsibleHCloudReverseDNS.define_module()

    hcloud = AnsibleHCloudReverseDNS(module)
    state = module.params["state"]
    if state == "absent":
        hcloud.delete_rdns()
    elif state == "present":
        hcloud.present_rdns()

    module.exit_json(**hcloud.get_result())


if __name__ == "__main__":
    main()