Debian Codegraph @

ansible_collections/hetzner/hcloud/plugins/modules/rdns.py

raw
1 #!/usr/bin/python
2
3 # Copyright: (c) 2019, 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: rdns
12
13 short_description: Create and manage reverse DNS entries on the Hetzner Cloud.
14
15
16 description:
17 - Create, update and delete reverse DNS entries on the Hetzner Cloud.
18
19 author:
20 - Lukas Kaemmerling (@lkaemmerling)
21
22 options:
23 server:
24 description:
25 - Name or ID of the Hetzner Cloud server you want to add the reverse DNS entry to.
26 type: str
27 floating_ip:
28 description:
29 - Name or ID of the Hetzner Cloud Floating IP you want to add the reverse DNS entry to.
30 type: str
31 primary_ip:
32 description:
33 - Name or ID of the Hetzner Cloud Primary IP you want to add the reverse DNS entry to.
34 type: str
35 load_balancer:
36 description:
37 - Name or ID of the Hetzner Cloud Load Balancer you want to add the reverse DNS entry to.
38 type: str
39 ip_address:
40 description:
41 - The IP address that should point to I(dns_ptr).
42 type: str
43 required: true
44 dns_ptr:
45 description:
46 - The DNS address the I(ip_address) should resolve to.
47 - Omit the param to reset the reverse DNS entry to the default value.
48 type: str
49 state:
50 description:
51 - State of the reverse DNS entry.
52 default: present
53 choices: [ absent, present ]
54 type: str
55
56 extends_documentation_fragment:
57 - hetzner.hcloud.hcloud
58 """
59
60 EXAMPLES = """
61 - name: Create a reverse DNS entry for a server
62 hetzner.hcloud.rdns:
63 server: my-server
64 ip_address: 123.123.123.123
65 dns_ptr: example.com
66 state: present
67
68 - name: Create a reverse DNS entry for a Floating IP
69 hetzner.hcloud.rdns:
70 floating_ip: my-floating-ip
71 ip_address: 123.123.123.123
72 dns_ptr: example.com
73 state: present
74
75 - name: Create a reverse DNS entry for a Primary IP
76 hetzner.hcloud.rdns:
77 primary_ip: my-primary-ip
78 ip_address: 123.123.123.123
79 dns_ptr: example.com
80 state: present
81
82 - name: Create a reverse DNS entry for a Load Balancer
83 hetzner.hcloud.rdns:
84 load_balancer: my-load-balancer
85 ip_address: 123.123.123.123
86 dns_ptr: example.com
87 state: present
88
89 - name: Ensure the reverse DNS entry is absent (remove if needed)
90 hetzner.hcloud.rdns:
91 server: my-server
92 ip_address: 123.123.123.123
93 dns_ptr: example.com
94 state: absent
95 """
96
97 RETURN = """
98 hcloud_rdns:
99 description: The reverse DNS entry
100 returned: always
101 type: dict
102 contains:
103 server:
104 description: Name of the server
105 type: str
106 returned: always
107 sample: my-server
108 floating_ip:
109 description: Name of the Floating IP
110 type: str
111 returned: always
112 sample: my-floating-ip
113 primary_ip:
114 description: Name of the Primary IP
115 type: str
116 returned: always
117 sample: my-primary-ip
118 load_balancer:
119 description: Name of the Load Balancer
120 type: str
121 returned: always
122 sample: my-load-balancer
123 ip_address:
124 description: The IP address that point to the DNS ptr
125 type: str
126 returned: always
127 sample: 123.123.123.123
128 dns_ptr:
129 description: The DNS that resolves to the IP
130 type: str
131 returned: always
132 sample: example.com
133 """
134
135 import ipaddress
136 from typing import Any
137
138 from ansible.module_utils.basic import AnsibleModule
139
140 from ..module_utils._base import AnsibleHCloud
141 from ..module_utils._vendor.hcloud import HCloudException
142 from ..module_utils._vendor.hcloud.floating_ips import BoundFloatingIP
143 from ..module_utils._vendor.hcloud.load_balancers import BoundLoadBalancer
144 from ..module_utils._vendor.hcloud.primary_ips import BoundPrimaryIP
145 from ..module_utils._vendor.hcloud.servers import BoundServer
146
147
148 class AnsibleHCloudReverseDNS(AnsibleHCloud):
149 represent = "hcloud_rdns"
150
151 hcloud_resource: BoundServer | BoundFloatingIP | BoundLoadBalancer | BoundPrimaryIP | None = None
152 hcloud_rdns: dict[str, Any] | None = None
153
154 def _prepare_result(self):
155 result = {
156 "server": None,
157 "floating_ip": None,
158 "load_balancer": None,
159 "ip_address": self.hcloud_rdns["ip_address"],
160 "dns_ptr": self.hcloud_rdns["dns_ptr"],
161 }
162
163 if self.module.params.get("server"):
164 result["server"] = self.hcloud_resource.name
165 elif self.module.params.get("floating_ip"):
166 result["floating_ip"] = self.hcloud_resource.name
167 elif self.module.params.get("load_balancer"):
168 result["load_balancer"] = self.hcloud_resource.name
169 elif self.module.params.get("primary_ip"):
170 result["primary_ip"] = self.hcloud_resource.name
171 return result
172
173 def _get_resource(self):
174 try:
175 if self.module.params.get("server"):
176 self.hcloud_resource = self._client_get_by_name_or_id(
177 "servers",
178 self.module.params.get("server"),
179 )
180 elif self.module.params.get("floating_ip"):
181 self.hcloud_resource = self._client_get_by_name_or_id(
182 "floating_ips",
183 self.module.params.get("floating_ip"),
184 )
185 elif self.module.params.get("primary_ip"):
186 self.hcloud_resource = self._client_get_by_name_or_id(
187 "primary_ips",
188 self.module.params.get("primary_ip"),
189 )
190 elif self.module.params.get("load_balancer"):
191 self.hcloud_resource = self._client_get_by_name_or_id(
192 "load_balancers",
193 self.module.params.get("load_balancer"),
194 )
195 except HCloudException as exception:
196 self.fail_json_hcloud(exception)
197
198 def _get_rdns(self):
199 ip_address = self.module.params.get("ip_address")
200
201 try:
202 ip_address_obj = ipaddress.ip_address(ip_address)
203 except ValueError:
204 self.module.fail_json(msg=f"The given IP address is not valid: {ip_address}")
205
206 if ip_address_obj.version == 4:
207 if self.module.params.get("server"):
208 if self.hcloud_resource.public_net.ipv4.ip == ip_address:
209 self.hcloud_rdns = {
210 "ip_address": self.hcloud_resource.public_net.ipv4.ip,
211 "dns_ptr": self.hcloud_resource.public_net.ipv4.dns_ptr,
212 }
213 else:
214 self.module.fail_json(msg="The selected server does not have this IP address")
215 elif self.module.params.get("floating_ip"):
216 if self.hcloud_resource.ip == ip_address:
217 self.hcloud_rdns = {
218 "ip_address": self.hcloud_resource.ip,
219 "dns_ptr": self.hcloud_resource.dns_ptr[0]["dns_ptr"],
220 }
221 else:
222 self.module.fail_json(msg="The selected Floating IP does not have this IP address")
223 elif self.module.params.get("primary_ip"):
224 if self.hcloud_resource.ip == ip_address:
225 self.hcloud_rdns = {
226 "ip_address": self.hcloud_resource.ip,
227 "dns_ptr": self.hcloud_resource.dns_ptr[0]["dns_ptr"],
228 }
229 else:
230 self.module.fail_json(msg="The selected Primary IP does not have this IP address")
231 elif self.module.params.get("load_balancer"):
232 if self.hcloud_resource.public_net.ipv4.ip == ip_address:
233 self.hcloud_rdns = {
234 "ip_address": self.hcloud_resource.public_net.ipv4.ip,
235 "dns_ptr": self.hcloud_resource.public_net.ipv4.dns_ptr,
236 }
237 else:
238 self.module.fail_json(msg="The selected Load Balancer does not have this IP address")
239
240 elif ip_address_obj.version == 6:
241 if self.module.params.get("server"):
242 for ipv6_address_dns_ptr in self.hcloud_resource.public_net.ipv6.dns_ptr:
243 if ipv6_address_dns_ptr["ip"] == ip_address:
244 self.hcloud_rdns = {
245 "ip_address": ipv6_address_dns_ptr["ip"],
246 "dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
247 }
248 elif self.module.params.get("floating_ip"):
249 for ipv6_address_dns_ptr in self.hcloud_resource.dns_ptr:
250 if ipv6_address_dns_ptr["ip"] == ip_address:
251 self.hcloud_rdns = {
252 "ip_address": ipv6_address_dns_ptr["ip"],
253 "dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
254 }
255 elif self.module.params.get("primary_ip"):
256 for ipv6_address_dns_ptr in self.hcloud_resource.dns_ptr:
257 if ipv6_address_dns_ptr["ip"] == ip_address:
258 self.hcloud_rdns = {
259 "ip_address": ipv6_address_dns_ptr["ip"],
260 "dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
261 }
262 elif self.module.params.get("load_balancer"):
263 for ipv6_address_dns_ptr in self.hcloud_resource.public_net.ipv6.dns_ptr:
264 if ipv6_address_dns_ptr["ip"] == ip_address:
265 self.hcloud_rdns = {
266 "ip_address": ipv6_address_dns_ptr["ip"],
267 "dns_ptr": ipv6_address_dns_ptr["dns_ptr"],
268 }
269
270 def _create_rdns(self):
271 self.module.fail_on_missing_params(required_params=["dns_ptr"])
272 params = {
273 "ip": self.module.params.get("ip_address"),
274 "dns_ptr": self.module.params.get("dns_ptr"),
275 }
276
277 if not self.module.check_mode:
278 try:
279 action = self.hcloud_resource.change_dns_ptr(**params)
280 action.wait_until_finished()
281 except HCloudException as exception:
282 self.fail_json_hcloud(exception)
283 self._mark_as_changed()
284 self._get_resource()
285 self._get_rdns()
286
287 def _update_rdns(self):
288 dns_ptr = self.module.params.get("dns_ptr")
289 if dns_ptr != self.hcloud_rdns["dns_ptr"]:
290 params = {
291 "ip": self.module.params.get("ip_address"),
292 "dns_ptr": dns_ptr,
293 }
294
295 if not self.module.check_mode:
296 try:
297 action = self.hcloud_resource.change_dns_ptr(**params)
298 action.wait_until_finished()
299 except HCloudException as exception:
300 self.fail_json_hcloud(exception)
301 self._mark_as_changed()
302 self._get_resource()
303 self._get_rdns()
304
305 def present_rdns(self):
306 self._get_resource()
307 self._get_rdns()
308 if self.hcloud_rdns is None:
309 self._create_rdns()
310 else:
311 self._update_rdns()
312
313 def delete_rdns(self):
314 self._get_resource()
315 self._get_rdns()
316 if self.hcloud_rdns is not None:
317 if not self.module.check_mode:
318 try:
319 self.hcloud_resource.change_dns_ptr(ip=self.hcloud_rdns["ip_address"], dns_ptr=None)
320 except HCloudException as exception:
321 self.fail_json_hcloud(exception)
322 self._mark_as_changed()
323 self.hcloud_rdns = None
324
325 @classmethod
326 def define_module(cls):
327 return AnsibleModule(
328 argument_spec=dict(
329 server={"type": "str"},
330 floating_ip={"type": "str"},
331 load_balancer={"type": "str"},
332 primary_ip={"type": "str"},
333 ip_address={"type": "str", "required": True},
334 dns_ptr={"type": "str"},
335 state={
336 "choices": ["absent", "present"],
337 "default": "present",
338 },
339 **super().base_module_arguments(),
340 ),
341 required_one_of=[["server", "floating_ip", "load_balancer", "primary_ip"]],
342 mutually_exclusive=[["server", "floating_ip", "load_balancer", "primary_ip"]],
343 supports_check_mode=True,
344 )
345
346
347 def main():
348 module = AnsibleHCloudReverseDNS.define_module()
349
350 hcloud = AnsibleHCloudReverseDNS(module)
351 state = module.params["state"]
352 if state == "absent":
353 hcloud.delete_rdns()
354 elif state == "present":
355 hcloud.present_rdns()
356
357 module.exit_json(**hcloud.get_result())
358
359
360 if __name__ == "__main__":
361 main()