From a322efc7d697aaf9ee28eba22cef7269858ad97a Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Mon, 30 Aug 2021 18:43:43 +0200 Subject: phpipam source: create PTR reverse records when in in-addr.arpa zone --- octodns-custom-providers/source/phpipam.py | 78 ++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 15 deletions(-) (limited to 'octodns-custom-providers/source') diff --git a/octodns-custom-providers/source/phpipam.py b/octodns-custom-providers/source/phpipam.py index a509ead..0db4909 100755 --- a/octodns-custom-providers/source/phpipam.py +++ b/octodns-custom-providers/source/phpipam.py @@ -9,7 +9,7 @@ class PhpipamSource(octodns.source.base.BaseSource): SUPPORTS_GEO=False SUPPORTS=set(('A', 'AAAA')) - def __init__(self, id, url, user, token, appid, cidrs, tag = 'Used', default_ttl='3600'): + def __init__(self, id, url, user, token, appid, cidrs, tag = 'Used', default_ttl='3600', reverse = False): ''' Arguments ========= @@ -32,14 +32,72 @@ class PhpipamSource(octodns.source.base.BaseSource): self._tag = tag self._cidrs = cidrs self._ipam = phpipam_api.PhpipamAPI( url, appid, user, token ) + self._reverse = reverse + + @staticmethod + def _ip_in_arpa_zone(zone_name, reverse_parts): + zone_parts = zone_name.strip('.').split('.')[0:-2][::-1] + for i in range(len(zone_parts)): + if not zone_parts[i] == reverse_parts[3-i]: + return False + return True + + def _populate_reverse(self, zone, selected_addresses): + for hostname in selected_addresses: + data={ + 'type':'PTR', + 'ttl':self._default_ttl, + 'value':f'{hostname.strip(".")}.' + } + + ips=[] + if type(selected_addresses[hostname]) == list: + ips = selected_addresses[hostname] + else: + ips.append(selected_addresses[hostname]) + + for ip in ips: + # TODO de-uglify + parts = ip.split('.')[::-1] + if not PhpipamSource._ip_in_arpa_zone(zone.name, parts): + continue + + parts.append('in-addr') + parts.append('arpa') + arpa_name = '.'.join(parts).replace("zone.name","") + + new_record = octodns.record.Record.new( zone, arpa_name, data) + zone.add_record( new_record ) + + def _populate_forward(self, zone, selected_addresses): + for _selected_address in selected_addresses: + if not _selected_address.endswith(zone.name.strip('.')): + continue + + hostname = re.sub( '\.' + zone.name.strip('.').replace('.', '\.') + '$', '', _selected_address) + + data={ + 'type':'A', + 'ttl':self._default_ttl, + 'values':selected_addresses[_selected_address] + } + + new_record = octodns.record.Record.new( zone, hostname, data) + zone.add_record( new_record ) + def populate(self, zone, target=False, lenient=False): ipam = self._ipam tag = self._tag cidrs = self._cidrs domain = zone.name + reverse = self._reverse + + if domain.endswith('in-addr.arpa.'): + reverse = True tags = ipam.addresses.getTags() + tag_id = '' for _tag in tags: if _tag['type'] == tag: tag_id=_tag['id'] @@ -66,25 +124,15 @@ class PhpipamSource(octodns.source.base.BaseSource): if not _address['tag'] == tag_id: continue - if not hostname.endswith(domain.strip('.')): - continue if hostname in selected_addresses: selected_addresses[hostname].append(ip) else: selected_addresses[hostname]=[ip] - for _selected_address in selected_addresses: - # TODO check for IPv6 - hostname = re.sub( '\.' + zone.name.strip('.').replace('.', '\.') + '$', '', _selected_address) - - data={ - 'type':'A', - 'ttl':self._default_ttl, - 'values':selected_addresses[_selected_address] - } - - new_record = octodns.record.Record.new( zone, hostname, data) - zone.add_record( new_record ) + if reverse: + self._populate_reverse(zone, selected_addresses) + else: + self._populate_forward(zone, selected_addresses) return True -- cgit v1.2.3