diff --git a/changelogs/fragments/hcloud_rdns-add-support-for-load-balancers.yml b/changelogs/fragments/hcloud_rdns-add-support-for-load-balancers.yml new file mode 100644 index 0000000..3b65e4b --- /dev/null +++ b/changelogs/fragments/hcloud_rdns-add-support-for-load-balancers.yml @@ -0,0 +1,2 @@ +minor_changes: + - hcloud_rdns Add support for load balancer diff --git a/plugins/modules/hcloud_rdns.py b/plugins/modules/hcloud_rdns.py index 3a31a8f..5a8e069 100644 --- a/plugins/modules/hcloud_rdns.py +++ b/plugins/modules/hcloud_rdns.py @@ -30,6 +30,10 @@ options: description: - The name of the Hetzner Cloud Floating IP you want to add the reverse DNS entry to. type: str + load_balancer: + description: + - The name 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). @@ -70,6 +74,13 @@ EXAMPLES = """ dns_ptr: example.com state: present +- name: Create a reverse DNS entry for a Load Balancer + 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) hcloud_rdns: server: my-server @@ -94,6 +105,11 @@ hcloud_rdns: type: str returned: always sample: my-floating-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 @@ -127,6 +143,7 @@ class AnsibleHcloudReverseDNS(Hcloud): result = { "server": None, "floating_ip": None, + "load_balancer": None, "ip_address": to_native(self.hcloud_rdns["ip_address"]), "dns_ptr": to_native(self.hcloud_rdns["dns_ptr"]), } @@ -135,6 +152,8 @@ class AnsibleHcloudReverseDNS(Hcloud): result["server"] = to_native(self.hcloud_resource.name) elif self.module.params.get("floating_ip"): result["floating_ip"] = to_native(self.hcloud_resource.name) + elif self.module.params.get("load_balancer"): + result["load_balancer"] = to_native(self.hcloud_resource.name) return result def _get_resource(self): @@ -151,6 +170,12 @@ class AnsibleHcloudReverseDNS(Hcloud): ) if self.hcloud_resource is None: self.module.fail_json(msg="The selected Floating IP does not exist") + elif self.module.params.get("load_balancer"): + self.hcloud_resource = self.client.load_balancers.get_by_name( + self.module.params.get("load_balancer") + ) + if self.hcloud_resource is None: + self.module.fail_json(msg="The selected Load Balancer does not exist") except Exception as e: self.module.fail_json(msg=e.message) @@ -173,6 +198,14 @@ class AnsibleHcloudReverseDNS(Hcloud): } else: self.module.fail_json(msg="The selected Floating 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 utils.validate_ip_v6_address(ip_address): if self.module.params.get("server"): @@ -189,6 +222,13 @@ class AnsibleHcloudReverseDNS(Hcloud): "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"], + } else: self.module.fail_json(msg="The given IP address is not valid") @@ -253,6 +293,7 @@ class AnsibleHcloudReverseDNS(Hcloud): argument_spec=dict( server={"type": "str"}, floating_ip={"type": "str"}, + load_balancer={"type": "str"}, ip_address={"type": "str", "required": True}, dns_ptr={"type": "str"}, state={ @@ -261,8 +302,8 @@ class AnsibleHcloudReverseDNS(Hcloud): }, **Hcloud.base_module_arguments() ), - required_one_of=[['server', 'floating_ip']], - mutually_exclusive=[["server", "floating_ip"]], + required_one_of=[['server', 'floating_ip', 'load_balancer']], + mutually_exclusive=[["server", "floating_ip", 'load_balancer']], supports_check_mode=True, ) diff --git a/tests/integration/targets/hcloud_rdns/defaults/main.yml b/tests/integration/targets/hcloud_rdns/defaults/main.yml index 420b04d..7993361 100644 --- a/tests/integration/targets/hcloud_rdns/defaults/main.yml +++ b/tests/integration/targets/hcloud_rdns/defaults/main.yml @@ -4,3 +4,4 @@ hcloud_prefix: "tests" hcloud_server_name: "{{hcloud_prefix}}" hcloud_floating_ip_name: "{{hcloud_prefix}}" +hcloud_load_balancer_name: "{{hcloud_prefix}}" diff --git a/tests/integration/targets/hcloud_rdns/tasks/main.yml b/tests/integration/targets/hcloud_rdns/tasks/main.yml index da195ee..d1989fb 100644 --- a/tests/integration/targets/hcloud_rdns/tasks/main.yml +++ b/tests/integration/targets/hcloud_rdns/tasks/main.yml @@ -26,6 +26,18 @@ that: - floatingIP is success +- name: setup Load Balancer + hcloud_load_balancer: + name: "{{ hcloud_load_balancer_name}}" + load_balancer_type: lb11 + network_zone: eu-central + state: present + register: load_balancer +- name: verify setup + assert: + that: + - load_balancer is success + - name: test missing required parameter hcloud_rdns: state: present @@ -130,6 +142,21 @@ - rdns.hcloud_rdns.ip_address == "{{ floatingIP.hcloud_floating_ip.ip}}" - rdns.hcloud_rdns.dns_ptr == "example.com" +- name: test create rdns with load balancer + hcloud_rdns: + load_balancer: "{{ hcloud_load_balancer_name }}" + ip_address: "{{ load_balancer.hcloud_load_balancer.ipv4_address }}" + dns_ptr: "example.com" + state: present + register: rdns +- name: verify create rdns with load balancer + assert: + that: + - rdns is changed + - rdns.hcloud_rdns.load_balancer == "{{ hcloud_load_balancer_name }}" + - rdns.hcloud_rdns.ip_address == "{{ load_balancer.hcloud_load_balancer.ipv4_address }}" + - rdns.hcloud_rdns.dns_ptr == "example.com" + - name: cleanup hcloud_server: name: "{{ hcloud_server_name }}" @@ -149,3 +176,13 @@ assert: that: - result is success + +- name: cleanup + hcloud_load_balancer: + name: "{{ hcloud_load_balancer_name }}" + state: absent + register: result +- name: verify cleanup + assert: + that: + - result is success