Skip to content

Commit

Permalink
lookup TXT record
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Feb 24, 2022
1 parent d223181 commit 14b3b55
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
74 changes: 69 additions & 5 deletions code/ddns-lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
LOGGER = logging.getLogger()
ACCOUNT = None
REGION = None
VERSION = '0.1.11'
VERSION = '0.1.12'

# Adjust the logging level [logging.INFO, logging.DEBUG, logging.WARNING, etc]
LOGGER.setLevel(logging.DEBUG)
Expand Down Expand Up @@ -728,9 +728,17 @@ def lambda_handler(
' with value: ' +
str(private_ip))

# pause 1 before deleting to avoid API limit
time.sleep(1)
heritage_value = get_resource_record(
route53,
final_hosted_zone_id,
final_private_hostname,
final_hosted_zone_name,
'TXT',
heritage_value
)
if len(heritage) > 0:
# pause 1 before deleting to avoid API limit
time.sleep(1)
delete_resource_record(
route53,
final_hosted_zone_id,
Expand Down Expand Up @@ -767,9 +775,17 @@ def lambda_handler(
' with value: ' +
str(final_private_dns_name))

# pause 1 before deleting to avoid API limit
time.sleep(1)
heritage_value = delete_resource_record(
route53,
reverse_lookup_zone_id,
reversed_ip_address,
'in-addr.arpa',
'TXT',
heritage_value
)
if len(heritage) > 0:
# pause 1 before deleting to avoid API limit
time.sleep(1)
delete_resource_record(
route53,
reverse_lookup_zone_id,
Expand Down Expand Up @@ -870,6 +886,14 @@ def lambda_handler(
' with value: ' +
str(final_private_dns_name))

heritage_value = get_resource_record(
route53,
cname_domain_suffix_id,
TXT_RR_PREFIX + '.' + cname_host_name,
cname_domain_suffix,
'TXT',
heritage_value
)
if len(heritage) > 0:
delete_resource_record(
route53,
Expand Down Expand Up @@ -1210,6 +1234,46 @@ def create_resource_record(client, zone_id, host_name, hosted_zone_name, record_
"of duplicates: %s", str(err) + lineno())


def get_resource_record(client, zone_id, host_name, hosted_zone_name, record_type, unused=None):
"""
This function getts resource records from the hosted zone passed by the calling function.
:param str client:
:param str zone_id:
:param str host_name:
:param str hosted_zone_name:
:param str record_type:
:param str unused: Placeholder for same calling parameters as delete_resource_record(); unused
:return str value: Value of record if found, None if not
"""
value = None
try:
LOGGER.debug("Getting %s record %s in zone %s"
" %s", record_type, host_name, hosted_zone_name, lineno())
if host_name[-1] != '.':
host_name = host_name + '.'
response = client.list_resource_record_sets(
HostedZoneId=zone_id,
StartRecordName=host_name,
StartRecordType=record_type,
MaxItems=1)

if len(response) > 0:
rr_set = response['ResourceRecordSets'][0]
if rr_set['Name'] == host_name and rr_set['Type'] = record_type:
value = rr_set['ResourceRecords'][0]['Value']

except ClientError as err:
if 'Not Found' in str(err):
LOGGER.debug("Get record not found error: %s", str(err) + lineno())

if 'InvalidChangeBatch' in str(err) and 'it was not found' in str(err):
LOGGER.debug("Get record not found error: %s", str(err) + lineno())

LOGGER.info("Get record unexpected error. %s\n", str(err) + lineno())

return value


def delete_resource_record(client, zone_id, host_name, hosted_zone_name, record_type, value):
"""
This function deletes resource records from the hosted zone passed by the calling function.
Expand Down
Binary file modified code/ddns-lambda.zip
Binary file not shown.

0 comments on commit 14b3b55

Please sign in to comment.