Skip to content

Commit

Permalink
feat: replace ansible.netcommon utils with python3 ipaddress modu…
Browse files Browse the repository at this point in the history
…le (#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
  • Loading branch information
jooola authored Dec 12, 2023
1 parent b0cb43f commit 4cfdf50
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 0 additions & 2 deletions galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 9 additions & 7 deletions plugins/modules/rdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,11 @@
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
Expand Down Expand Up @@ -200,7 +198,13 @@ def _get_resource(self):

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 = {
Expand Down Expand Up @@ -234,7 +238,7 @@ def _get_rdns(self):
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:
Expand Down Expand Up @@ -263,8 +267,6 @@ def _get_rdns(self):
"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"])
Expand Down
24 changes: 12 additions & 12 deletions tests/integration/targets/rdns/tasks/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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"
2 changes: 1 addition & 1 deletion tests/requirements.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
collections:
- ansible.netcommon
- ansible.utils
- community.crypto
- community.general

0 comments on commit 4cfdf50

Please sign in to comment.