Skip to content

Commit

Permalink
pull zone name encode into a helper, test it, changelog entry
Browse files Browse the repository at this point in the history
  • Loading branch information
ross committed Jul 18, 2024
1 parent 60c464e commit 77c96d1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.0.? - 2024-??-?? - ???

* Support for fully managing zones with special characters in their names, e.g.
128/26.2.0.192.in-addr.arpa. added.

## v0.0.6 - 2024-03-08 - Get port type straight

* DS Record support added
Expand Down
15 changes: 12 additions & 3 deletions octodns_powerdns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#

import logging
import urllib.parse
from operator import itemgetter
from urllib.parse import quote_plus

from requests import HTTPError, Session

Expand All @@ -28,6 +28,15 @@
__version__ = __VERSION__ = '0.0.6'


def _encode_zone_name(name):
# Powerdns uses a special encoding for URLs. Instead of "%2F" for a slash,
# the slash must be encoded with "=2F". (This must be done in version 4.7.3
# from Debian, from version >= 4.8 Powerdns accepts “%2F” and “=2F” as path
# argument. The output of "/api/v1/servers/localhost/zones" still shows the
# zone URL with "=2F")
return quote_plus(name).replace('%', '=')


def _escape_unescaped_semicolons(value):
pieces = value.split(';')
if len(pieces) == 1:
Expand Down Expand Up @@ -454,7 +463,7 @@ def populate(self, zone, target=False, lenient=False):
target,
lenient,
)
encoded_name = urllib.parse.quote_plus(zone.name).replace('%', '=')
encoded_name = _encode_zone_name(zone.name)
resp = None
try:
resp = self._get(f'zones/{encoded_name}')
Expand Down Expand Up @@ -675,7 +684,7 @@ def _get_error(self, http_error):
def _apply(self, plan):
desired = plan.desired
changes = plan.changes
encoded_name = urllib.parse.quote_plus(desired.name).replace('%', '=')
encoded_name = _encode_zone_name(desired.name)
self.log.debug(
'_apply: zone=%s, len(changes)=%d', desired.name, len(changes)
)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_octodns_provider_powerdns.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from octodns_powerdns import (
PowerDnsBaseProvider,
PowerDnsProvider,
_encode_zone_name,
_escape_unescaped_semicolons,
)
from octodns_powerdns.record import PowerDnsLuaRecord, _PowerDnsLuaValue
Expand Down Expand Up @@ -792,3 +793,11 @@ def test_lua_validate(self):
# list w/a bad value
got = _PowerDnsLuaValue.validate([val, {}], PowerDnsLuaRecord._type)
self.assertEqual(['missing type', 'missing script'], got)

def test_encode_zone_name(self):
for expected, value in (
('unit.tests.', 'unit.tests.'),
('another_one.unit.tests.', 'another_one.unit.tests.'),
('128=2F26.2.0.192.in-addr.arpa.', '128/26.2.0.192.in-addr.arpa.'),
):
self.assertEqual(expected, _encode_zone_name(value))

0 comments on commit 77c96d1

Please sign in to comment.