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
This commit is contained in:
Jonas L 2023-12-12 11:50:45 +01:00 committed by GitHub
parent b0cb43fe5a
commit 4cfdf50b26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 22 deletions

View file

@ -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.

View file

@ -10,8 +10,6 @@ tags:
- hetzner - hetzner
- cloud - cloud
- hcloud - hcloud
dependencies:
ansible.netcommon: ">=0.0.1"
repository: https://github.com/ansible-collections/hetzner.hcloud repository: https://github.com/ansible-collections/hetzner.hcloud
documentation: https://docs.ansible.com/ansible/latest/collections/hetzner/hcloud documentation: https://docs.ansible.com/ansible/latest/collections/hetzner/hcloud
homepage: https://github.com/ansible-collections/hetzner.hcloud homepage: https://github.com/ansible-collections/hetzner.hcloud

View file

@ -132,13 +132,11 @@ hcloud_rdns:
sample: example.com sample: example.com
""" """
import ipaddress
from typing import Any from typing import Any
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.common.text.converters import to_native 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.hcloud import AnsibleHCloud
from ..module_utils.vendor.hcloud import HCloudException from ..module_utils.vendor.hcloud import HCloudException
@ -200,7 +198,13 @@ class AnsibleHCloudReverseDNS(AnsibleHCloud):
def _get_rdns(self): def _get_rdns(self):
ip_address = self.module.params.get("ip_address") 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.module.params.get("server"):
if self.hcloud_resource.public_net.ipv4.ip == ip_address: if self.hcloud_resource.public_net.ipv4.ip == ip_address:
self.hcloud_rdns = { self.hcloud_rdns = {
@ -234,7 +238,7 @@ class AnsibleHCloudReverseDNS(AnsibleHCloud):
else: else:
self.module.fail_json(msg="The selected Load Balancer does not have this IP address") 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"): if self.module.params.get("server"):
for ipv6_address_dns_ptr in self.hcloud_resource.public_net.ipv6.dns_ptr: for ipv6_address_dns_ptr in self.hcloud_resource.public_net.ipv6.dns_ptr:
if ipv6_address_dns_ptr["ip"] == ip_address: if ipv6_address_dns_ptr["ip"] == ip_address:
@ -263,8 +267,6 @@ class AnsibleHCloudReverseDNS(AnsibleHCloud):
"ip_address": ipv6_address_dns_ptr["ip"], "ip_address": ipv6_address_dns_ptr["ip"],
"dns_ptr": ipv6_address_dns_ptr["dns_ptr"], "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): def _create_rdns(self):
self.module.fail_on_missing_params(required_params=["dns_ptr"]) self.module.fail_on_missing_params(required_params=["dns_ptr"])

View file

@ -15,7 +15,7 @@
- name: Test create with checkmode - name: Test create with checkmode
hetzner.hcloud.rdns: hetzner.hcloud.rdns:
server: "{{ hcloud_server_name }}" 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 dns_ptr: example.com
state: present state: present
check_mode: true check_mode: true
@ -28,7 +28,7 @@
- name: Test create - name: Test create
hetzner.hcloud.rdns: hetzner.hcloud.rdns:
server: "{{ hcloud_server_name }}" 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 dns_ptr: example.com
state: present state: present
register: result register: result
@ -37,13 +37,13 @@
that: that:
- result is changed - result is changed
- result.hcloud_rdns.server == "{{ hcloud_server_name }}" - 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" - result.hcloud_rdns.dns_ptr == "example.com"
- name: Test create idempotency - name: Test create idempotency
hetzner.hcloud.rdns: hetzner.hcloud.rdns:
server: "{{ hcloud_server_name }}" 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 dns_ptr: example.com
state: present state: present
register: result register: result
@ -77,7 +77,7 @@
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- result is changed - 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 - name: Test update reset
hetzner.hcloud.rdns: hetzner.hcloud.rdns:
@ -94,7 +94,7 @@
- name: Test delete - name: Test delete
hetzner.hcloud.rdns: hetzner.hcloud.rdns:
server: "{{ hcloud_server_name }}" 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 state: absent
register: result register: result
- name: Verify delete - name: Verify delete
@ -113,8 +113,8 @@
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- result is changed - result is changed
- result.hcloud_rdns.primary_ip == "{{ hcloud_primary_ip_name }}" - result.hcloud_rdns.primary_ip == hcloud_primary_ip_name
- result.hcloud_rdns.ip_address == "{{ test_primary_ip.hcloud_primary_ip.ip }}" - result.hcloud_rdns.ip_address == test_primary_ip.hcloud_primary_ip.ip
- result.hcloud_rdns.dns_ptr == "example.com" - result.hcloud_rdns.dns_ptr == "example.com"
- name: Test create with floating ip - name: Test create with floating ip
@ -128,8 +128,8 @@
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- result is changed - result is changed
- result.hcloud_rdns.floating_ip == "{{ hcloud_floating_ip_name }}" - result.hcloud_rdns.floating_ip == hcloud_floating_ip_name
- result.hcloud_rdns.ip_address == "{{ test_floating_ip.hcloud_floating_ip.ip }}" - result.hcloud_rdns.ip_address == test_floating_ip.hcloud_floating_ip.ip
- result.hcloud_rdns.dns_ptr == "example.com" - result.hcloud_rdns.dns_ptr == "example.com"
- name: Test create with load balancer - name: Test create with load balancer
@ -143,6 +143,6 @@
ansible.builtin.assert: ansible.builtin.assert:
that: that:
- result is changed - result is changed
- result.hcloud_rdns.load_balancer == "{{ hcloud_load_balancer_name }}" - 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.ip_address == test_load_balancer.hcloud_load_balancer.ipv4_address
- result.hcloud_rdns.dns_ptr == "example.com" - result.hcloud_rdns.dns_ptr == "example.com"

View file

@ -1,5 +1,5 @@
--- ---
collections: collections:
- ansible.netcommon - ansible.utils
- community.crypto - community.crypto
- community.general - community.general