aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jonas Gunz <himself@jonasgunz.de> 2020-12-31 17:01:51 +0100
committerGravatar Jonas Gunz <himself@jonasgunz.de> 2020-12-31 17:01:51 +0100
commit07855324daecd8a2569f70cbef42ba4194472a79 (patch)
tree2a5321dc1eefd4cc88917a60f605cf85c632e284
parentfb2ac002b24684acc99db786135181ab78de5e9f (diff)
downloadoctodns-custom-provider-07855324daecd8a2569f70cbef42ba4194472a79.tar.gz
phpipam implemented
-rwxr-xr-xoctodns-custom-providers/source/phpipam.py77
-rwxr-xr-xsetup.py2
2 files changed, 68 insertions, 11 deletions
diff --git a/octodns-custom-providers/source/phpipam.py b/octodns-custom-providers/source/phpipam.py
index aacebb8..e3d4c28 100755
--- a/octodns-custom-providers/source/phpipam.py
+++ b/octodns-custom-providers/source/phpipam.py
@@ -1,13 +1,15 @@
-import octodns.zone.Zone
-import octodns.source.base.BaseSource
+#import octodns.zone.Zone
+import octodns.record
+import octodns.source.base
import phpipam
import logging
+import re
-class PhpipamSource(BaseSource):
+class PhpipamSource(octodns.source.base.BaseSource):
SUPPORTS_GEO=False
SUPPORTS=set(('A', 'AAAA'))
- def __init__(self, id, url, user="", token, appid, section):
+ def __init__(self, id, url, user, token, appid, cidrs, tag = 'Used', default_ttl='3600'):
'''
Arguments
=========
@@ -16,18 +18,73 @@ class PhpipamSource(BaseSource):
user: str
token: str
appid: str
- section: str
- phpipam section id to search for addresses
+ cidrs: str[]
+ list of cidrs to search
+ tag: str
+ Address tag name
'''
self.log = logging.getLogger('PhpipamSource[{}]'.format(id))
self.log.debug('__init__: id=%s url=%s appid=%s', id, url, appid)
- super(PhpipamSource).__init__(id)
+ super(PhpipamSource, self).__init__(id)
- self._ipam = PhpipamAPI( url, appid, user, token )
+ self._default_ttl = default_ttl
+ self._tag = tag
+ self._cidrs = cidrs
+ self._ipam = phpipam.PhpipamAPI( url, appid, user, token )
def populate(self, zone, target=False, lenient=False):
+ ipam = self._ipam
+ tag = self._tag
+ cidrs = self._cidrs
domain = zone.name
- # Do the *MAGIC* Here
- return False
+ tags = ipam.addresses.getTags()
+ for _tag in tags:
+ if _tag['type'] == tag:
+ tag_id=_tag['id']
+ if len(tag_id) == 0:
+ self.log.error(f'populate(): tag {tag} was not found.')
+ return False
+
+ self.log.debug(f'populate(): tag {tag} has id {tag_id}')
+
+ selected_addresses = {}
+
+ for _cidr in cidrs:
+ subnets = ipam.subnets.search(search=_cidr)
+
+ if not len(subnets) == 1:
+ self.log.warning(f'populate(): CIDR {_cidr} has no or no exact match. Ignoring.')
+ continue
+ subnet = subnets[0]
+
+ addresses = ipam.subnets.getAddresses(subnet_id=subnet['id'])
+ for _address in addresses:
+ hostname = _address['hostname']
+ ip = _address['ip']
+
+ 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 )
+
+ return True
diff --git a/setup.py b/setup.py
index b1f862c..eaa4011 100755
--- a/setup.py
+++ b/setup.py
@@ -3,7 +3,7 @@ import setuptools
setup(
name='octodns-custom-providers',
- version='0.1.0',
+ version='0.2.0',
author="Jonas Gunz",
description="Custom sources for OctoDNS",
packages=setuptools.find_packages(),