diff --git a/code/ddns-lambda.py b/code/ddns-lambda.py index f3cdce8..b2af36a 100755 --- a/code/ddns-lambda.py +++ b/code/ddns-lambda.py @@ -73,7 +73,7 @@ LOGGER = logging.getLogger() account_id = None region = None -VERSION = '1.2.0b61' +VERSION = '1.2.0b63' # Read Env variables DEBUG_LOG_LEVEL = os.environ.get('DebugLogLevel', 'INFO') @@ -403,14 +403,11 @@ def lambda_handler( instance = get_item_from_dynamodb_table(dynamodb_client, DDBNAME, instance_id) LOGGER.info("instance attributes: %s", str(instance) + lineno()) try: - # LOGGER.info(f"instance_data type {type(instance)} {lineno()}") - # instance_data = json.loads(instance) - # LOGGER.info(f"getting _DnsEntries {lineno()}") dns_data_raw = instance.get('_DnsEntries', []) - LOGGER.info( + LOGGER.debug( f"got _DnsEntries type {type(dns_data_raw)} value {dns_data_raw} {lineno()}") dns_data = [dns_data_tuple(**item) for item in dns_data_raw] - LOGGER.info(f"converted _DnsEntries to namdtuples {dns_data} {lineno()}") + LOGGER.debug(f"converted _DnsEntries to namdtuples {dns_data} {lineno()}") LOGGER.info( f"Found entries from DDB for DNS records: {str(dns_data)} {lineno()}") except Exception as err: @@ -887,8 +884,21 @@ def lambda_handler( LOGGER.info( f"flags=noreverse, not adding PTR and heritage TXT for host {tag_data['ptr_entry'].hostname} zone {tag_data['ptr_entry'].zonename} value {final_private_dns_name}") - else: # not running so delete the records. Note this may leave orphans around if the flags are set and then the host is shut down. We may want to remove no matter what. +# else: # not running so delete the records. Note this may leave orphans around if the flags are set and then the host is shut down. We may want to remove no matter what. +# go through the dns_data records, and delete them. dns_data contains the records that were added. It is possible the tags have changed +# so using existing tag data will not be valid + + if state != 'running': if not flags['noforward']: + heritage_records = {} + for entry in dns_data: + if entry.rr_type == 'TXT' and entry.rr_value.startswith('heritage='): + heritage_records[entry.rr_name] = entry.rr_value + for entry in dns_data: + if not (entry.rr_type == 'TXT' and entry.rr_value.startswith('heritage=')): + process_response = new_process_delete_records( + instance_id, entry.zone_id, entry.rr_name, entry.zone_name, entry.rr_type, entry.rr_value, heritage_records.get(entry.rr_name, '')) + # Process and delete A record and associated TXT record process_response = process_delete_records( route53, @@ -2734,6 +2744,12 @@ def publish_to_sns(client, message): LOGGER.debug("No SNS Topic specified, ignoring") +def new_process_delete_records(instance_id, zone_id, record_name, zone_name, record_type, record_value, heritage_value): + LOGGER.info( + f"new delete records: instance {instance_id}, zone_id {zone_id}, name {record_name} zone_name {zone_name} type {record_type} value {record_value} heritage {heritage_value}") + return + + def process_delete_records(route53, instance_id, zone_id, record_name, zone_name, record_type, record_value, heritage_value): """ @@ -2745,6 +2761,7 @@ def process_delete_records(route53, instance_id, zone_id, :param zone_name: :param record_type: :param record_value: + :param heritage_value: :return response: # dictionary of 'delete_success' and 'msg' """ diff --git a/code/ddns-lambda.zip b/code/ddns-lambda.zip index e9d4d11..d56b7a2 100644 Binary files a/code/ddns-lambda.zip and b/code/ddns-lambda.zip differ