Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add trailing dot to record values if it's missing #45

Merged
merged 7 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion octodns_googlecloud/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
__version__ = __VERSION__ = '0.0.3'


def add_trailing_dot(value):
"""
Required function to handle cases where records being pushed
to Google Cloud DNS do not end with a dot.

:param value: Contains the record value
:type value: str

:type return: str
"""
if value[-1] != '.':
value = f'{value}.'
return value


def _batched_iterator(iterable, batch_size):
n = len(iterable)
for i in range(0, n, batch_size):
Expand Down Expand Up @@ -347,6 +362,7 @@ def _rrset_for_CAA(self, gcloud_zone, record):
)

def _rrset_for_CNAME(self, gcloud_zone, record):
record.value = add_trailing_dot(record.value)
return gcloud_zone.resource_record_set(
record.fqdn, record._type, record.ttl, [record.value]
)
Expand All @@ -356,7 +372,10 @@ def _rrset_for_MX(self, gcloud_zone, record):
record.fqdn,
record._type,
record.ttl,
[f'{v.preference} {v.exchange}' for v in record.values],
[
f'{v.preference} {add_trailing_dot(v.exchange)}'
for v in record.values
],
)

def _rrset_for_NAPTR(self, gcloud_zone, record):
Expand Down
10 changes: 9 additions & 1 deletion tests/test_octodns_provider_googlecloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from octodns.record import Create, Delete, Record, Update
from octodns.zone import Zone

from octodns_googlecloud import GoogleCloudProvider, _batched_iterator
from octodns_googlecloud import (
GoogleCloudProvider,
_batched_iterator,
add_trailing_dot,
)

zone = Zone(name='unit.tests.', sub_zones=[])
octo_records = []
Expand Down Expand Up @@ -280,6 +284,10 @@ def __next__(self):


class TestGoogleCloudProvider(TestCase):
def test_trailing_dot(self):
self.assertEqual(add_trailing_dot('unit.tests'), 'unit.tests.')
yugandhar91 marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(add_trailing_dot('unit.tests.'), 'unit.tests.')

@patch('octodns_googlecloud.dns')
def _get_provider(*args):
'''Returns a mock GoogleCloudProvider object to use in testing.
Expand Down
Loading