From 4cfdf50b26536c468705c729cdb48d4b2d421571 Mon Sep 17 00:00:00 2001 From: Jonas L Date: Tue, 12 Dec 2023 11:50:45 +0100 Subject: [PATCH] feat: replace `ansible.netcommon` utils with python3 `ipaddress` module (#416) ##### SUMMARY Replace `ansible.netcommon` deprecated ipaddr utils with python `ipaddress` module. The `ansible.netcommon` collection is no longer required by the collections. We still use the `ansible.utils` collections for testing --- ...nsible.netcommon-with-python-ipaddress.yml | 4 ++++ galaxy.yml | 2 -- plugins/modules/rdns.py | 16 +++++++------ tests/integration/targets/rdns/tasks/test.yml | 24 +++++++++---------- tests/requirements.yml | 2 +- 5 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 changelogs/fragments/replace-deprecated-ansible.netcommon-with-python-ipaddress.yml diff --git a/changelogs/fragments/replace-deprecated-ansible.netcommon-with-python-ipaddress.yml b/changelogs/fragments/replace-deprecated-ansible.netcommon-with-python-ipaddress.yml new file mode 100644 index 0000000..917d293 --- /dev/null +++ b/changelogs/fragments/replace-deprecated-ansible.netcommon-with-python-ipaddress.yml @@ -0,0 +1,4 @@ +minor_changes: + - > + Replace deprecated `ansible.netcommon` ip utils with python `ipaddress` module. The + `ansible.netcommon` collection is no longer required by the collections. diff --git a/galaxy.yml b/galaxy.yml index 2661a6d..fb4c554 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -10,8 +10,6 @@ tags: - hetzner - cloud - hcloud -dependencies: - ansible.netcommon: ">=0.0.1" repository: https://github.com/ansible-collections/hetzner.hcloud documentation: https://docs.ansible.com/ansible/latest/collections/hetzner/hcloud homepage: https://github.com/ansible-collections/hetzner.hcloud diff --git a/plugins/modules/rdns.py b/plugins/modules/rdns.py index 73279b1..b2decde 100644 --- a/plugins/modules/rdns.py +++ b/plugins/modules/rdns.py @@ -132,13 +132,11 @@ hcloud_rdns: sample: example.com """ +import ipaddress from typing import Any from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.common.text.converters import to_native -from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import ( - utils, -) from ..module_utils.hcloud import AnsibleHCloud from ..module_utils.vendor.hcloud import HCloudException @@ -200,7 +198,13 @@ class AnsibleHCloudReverseDNS(AnsibleHCloud): def _get_rdns(self): ip_address = self.module.params.get("ip_address") - if utils.validate_ip_address(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 = { @@ -234,7 +238,7 @@ class AnsibleHCloudReverseDNS(AnsibleHCloud): else: self.module.fail_json(msg="The selected Load Balancer does not have this IP address") - elif utils.validate_ip_v6_address(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: @@ -263,8 +267,6 @@ class AnsibleHCloudReverseDNS(AnsibleHCloud): "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") def _create_rdns(self): self.module.fail_on_missing_params(required_params=["dns_ptr"]) diff --git a/tests/integration/targets/rdns/tasks/test.yml b/tests/integration/targets/rdns/tasks/test.yml index ed09ccc..00735d6 100644 --- a/tests/integration/targets/rdns/tasks/test.yml +++ b/tests/integration/targets/rdns/tasks/test.yml @@ -15,7 +15,7 @@ - name: Test create with checkmode hetzner.hcloud.rdns: server: "{{ hcloud_server_name }}" - ip_address: "{{ test_server.hcloud_server.ipv6 | ansible.netcommon.ipaddr('next_usable') }}" + ip_address: "{{ test_server.hcloud_server.ipv6 | ansible.utils.ipaddr('next_usable') }}" dns_ptr: example.com state: present check_mode: true @@ -28,7 +28,7 @@ - name: Test create hetzner.hcloud.rdns: server: "{{ hcloud_server_name }}" - ip_address: "{{ test_server.hcloud_server.ipv6 | ansible.netcommon.ipaddr('next_usable') }}" + ip_address: "{{ test_server.hcloud_server.ipv6 | ansible.utils.ipaddr('next_usable') }}" dns_ptr: example.com state: present register: result @@ -37,13 +37,13 @@ that: - result is changed - result.hcloud_rdns.server == "{{ hcloud_server_name }}" - - result.hcloud_rdns.ip_address == "{{ test_server.hcloud_server.ipv6 | ansible.netcommon.ipaddr('next_usable') }}" + - result.hcloud_rdns.ip_address == test_server.hcloud_server.ipv6 | ansible.utils.ipaddr('next_usable') - result.hcloud_rdns.dns_ptr == "example.com" - name: Test create idempotency hetzner.hcloud.rdns: server: "{{ hcloud_server_name }}" - ip_address: "{{ test_server.hcloud_server.ipv6 | ansible.netcommon.ipaddr('next_usable') }}" + ip_address: "{{ test_server.hcloud_server.ipv6 | ansible.utils.ipaddr('next_usable') }}" dns_ptr: example.com state: present register: result @@ -77,7 +77,7 @@ ansible.builtin.assert: that: - result is changed - - result.hcloud_rdns.ip_address == "{{ test_server.hcloud_server.ipv4_address }}" + - result.hcloud_rdns.ip_address == test_server.hcloud_server.ipv4_address - name: Test update reset hetzner.hcloud.rdns: @@ -94,7 +94,7 @@ - name: Test delete hetzner.hcloud.rdns: server: "{{ hcloud_server_name }}" - ip_address: "{{ test_server.hcloud_server.ipv6 | ansible.netcommon.ipaddr('next_usable') }}" + ip_address: "{{ test_server.hcloud_server.ipv6 | ansible.utils.ipaddr('next_usable') }}" state: absent register: result - name: Verify delete @@ -113,8 +113,8 @@ ansible.builtin.assert: that: - result is changed - - result.hcloud_rdns.primary_ip == "{{ hcloud_primary_ip_name }}" - - result.hcloud_rdns.ip_address == "{{ test_primary_ip.hcloud_primary_ip.ip }}" + - result.hcloud_rdns.primary_ip == hcloud_primary_ip_name + - result.hcloud_rdns.ip_address == test_primary_ip.hcloud_primary_ip.ip - result.hcloud_rdns.dns_ptr == "example.com" - name: Test create with floating ip @@ -128,8 +128,8 @@ ansible.builtin.assert: that: - result is changed - - result.hcloud_rdns.floating_ip == "{{ hcloud_floating_ip_name }}" - - result.hcloud_rdns.ip_address == "{{ test_floating_ip.hcloud_floating_ip.ip }}" + - result.hcloud_rdns.floating_ip == hcloud_floating_ip_name + - result.hcloud_rdns.ip_address == test_floating_ip.hcloud_floating_ip.ip - result.hcloud_rdns.dns_ptr == "example.com" - name: Test create with load balancer @@ -143,6 +143,6 @@ ansible.builtin.assert: that: - result is changed - - result.hcloud_rdns.load_balancer == "{{ hcloud_load_balancer_name }}" - - result.hcloud_rdns.ip_address == "{{ test_load_balancer.hcloud_load_balancer.ipv4_address }}" + - result.hcloud_rdns.load_balancer == hcloud_load_balancer_name + - result.hcloud_rdns.ip_address == test_load_balancer.hcloud_load_balancer.ipv4_address - result.hcloud_rdns.dns_ptr == "example.com" diff --git a/tests/requirements.yml b/tests/requirements.yml index 67f003a..3fea3f3 100644 --- a/tests/requirements.yml +++ b/tests/requirements.yml @@ -1,5 +1,5 @@ --- collections: - - ansible.netcommon + - ansible.utils - community.crypto - community.general