Skip to content

Commit

Permalink
Fix v4 and v6 IP address comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedejong committed Dec 19, 2024
1 parent c5e3ec7 commit 9276991
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 6 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ Ultradns Collection Release Notes

.. contents:: Topics

v1.0.2
======

Release Summary
---------------

| Release Date: 2024-12-20
| improved AAAA record handling
Minor Changes
-------------

- CNAME and SOA records now update correctly
- Fix v4 and v6 IP address comparisons

v1.0.1
======

Expand Down
2 changes: 1 addition & 1 deletion changelogs/.plugin-cache.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ plugins:
strategy: {}
test: {}
vars: {}
version: 1.0.1
version: 1.0.2
12 changes: 12 additions & 0 deletions changelogs/changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,15 @@ releases:
fragments:
- 1.0.1.yml
release_date: '2024-12-16'
1.0.2:
changes:
minor_changes:
- CNAME and SOA records now update correctly
- Fix v4 and v6 IP address comparisons
release_summary: '| Release Date: 2024-12-20
| improved AAAA record handling'
fragments:
- 1.0.2.yml
- fix-ipaddress.yml
release_date: '2024-12-20'
3 changes: 3 additions & 0 deletions changelogs/fragments/1.0.2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
release_summary: |
| Release Date: 2024-12-20
| improved AAAA record handling
3 changes: 3 additions & 0 deletions changelogs/fragments/fix-ipaddress.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- Fix v4 and v6 IP address comparisons
- CNAME and SOA records now update correctly
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace: "ultradns"
name: "ultradns"
version: 1.0.1
version: 1.0.2
readme: README.md
authors:
- ultradns
Expand Down
37 changes: 33 additions & 4 deletions plugins/module_utils/ultraapi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, division, print_function
__metaclass__ = type
from ansible.module_utils.basic import env_fallback
from ipaddress import ip_address
from .connection import UltraConnection

PROD = 'api.ultradns.com'
Expand Down Expand Up @@ -84,6 +85,27 @@ def _check_result(self, result):
else:
return self._success()

def data_in_record(self, data, rrset, type):
if not isinstance(rrset, list) or not isinstance(data, str):
return False

if type not in ['A', 'AAAA']:
return data in rrset
else:
ipdata = ip_address(data)
iplist = list(ip_address(ip) for ip in rrset)
return ipdata in iplist

def remove_from_record(self, data, rrset, type):
if not isinstance(rrset, list) or not isinstance(data, str):
return rrset

if type not in ['A', 'AAAA']:
return list(r for r in rrset if r != data)
else:
ipdata = ip_address(data)
return list(r for r in rrset if ip_address(r) != ipdata)

def create(self, path, data):
if self.connection:
return self._check_result(self.connection.post(path, data))
Expand Down Expand Up @@ -280,10 +302,17 @@ def record(self):
# add to the rdata list or replace the entire list
data = {}
if self.params['solo'] or self.params['type'] in ['CNAME', 'SOA']:
data = {'rdata': [self.params['data']]}
if len(result['rrSets'][0]['rdata']) == 1:
if result['rrSets'][0]['rdata'][0] != self.params['data']:
data = {'rdata': [self.params['data']]}
else:
data = {'rdata': [self.params['data']]}
if self.params['ttl']:
data.update({'ttl': self.params['ttl']})
elif self.params['data'] in result['rrSets'][0]['rdata']:

if not data:
return self._no_change()
elif self.data_in_record(self.params['data'], result['rrSets'][0]['rdata'], self.params['type']):
if self.params['ttl'] and self.params['ttl'] != result['rrSets'][0]['ttl']:
data = {'ttl': self.params['ttl'], 'rdata': result['rrSets'][0]['rdata']}
else:
Expand Down Expand Up @@ -319,15 +348,15 @@ def record(self):
res = self._no_change()
elif 'data' not in self.params or not self.params['data']:
res = self.delete(f"{path}/{self.params['name']}")
elif self.params['data'] not in result['rrSets'][0]['rdata']:
elif not self.data_in_record(self.params['data'], result['rrSets'][0]['rdata'], self.params['type']):
res = self._no_change()
else:
if len(result['rrSets'][0]['rdata']) == 1:
res = self.delete(f"{path}/{self.params['name']}")
else:
data = {
'ttl': result['rrSets'][0]['ttl'],
'rdata': list(r for r in result['rrSets'][0]['rdata'] if r != self.params['data'])}
'rdata': self.remove_from_record(self.params['data'], result['rrSets'][0]['rdata'], self.params['type'])}
if 'profile' in result['rrSets'][0] and isinstance(result['rrSets'][0]['profile'], dict) and len(data['rdata']) > 1:
data.update({'profile': result['rrSets'][0]['profile']})
res = self.update(f"{path}/{self.params['name']}", data)
Expand Down

0 comments on commit 9276991

Please sign in to comment.