From 61ed4600c2264070171be2fdc15627b7f86f62f1 Mon Sep 17 00:00:00 2001 From: badra001 Date: Fri, 25 Feb 2022 10:41:07 -0500 Subject: [PATCH] get create_time from launch-time --- code/ddns-lambda.py | 81 ++++++++++++++++++++++++++------------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/code/ddns-lambda.py b/code/ddns-lambda.py index bf4250c..8bf68a7 100755 --- a/code/ddns-lambda.py +++ b/code/ddns-lambda.py @@ -67,13 +67,13 @@ from botocore.exceptions import ClientError from collections import OrderedDict from pprint import pformat - +from dateutil.parser import parse as date_parse # Setting Global Variables LOGGER = logging.getLogger() ACCOUNT = None REGION = None -VERSION = '0.1.13' +VERSION = '0.1.14' # Adjust the logging level [logging.INFO, logging.DEBUG, logging.WARNING, etc] LOGGER.setLevel(logging.DEBUG) @@ -271,10 +271,15 @@ def lambda_handler( try: private_ip = instance['Reservations'][0]['Instances'][0]['PrivateIpAddress'] + launch_time = instance['Reservations'][0]['Instances'][0]['LaunchTime'] + launch_time_date = date_parse(launch_time) private_dns_name = instance['Reservations'][0]['Instances'][0]['PrivateDnsName'] private_host_name = private_dns_name.split('.')[0] LOGGER.debug("private ip: %s", str(private_ip) + lineno()) + LOGGER.debug("launch time: %s", str(launch_time) + lineno()) + LOGGER.debug("launch time_date: %s", str( + int(launch_time_date.timestamp())) + lineno()) LOGGER.debug("private_dns_name: %s", str(private_dns_name) + lineno()) LOGGER.debug("private_host_name: %s", str(private_host_name) + lineno()) except: @@ -623,10 +628,12 @@ def lambda_handler( 'region': region, 'instance_id': instance_id, }) - add_heritage_item_timestamp(heritage, 'create_time') + add_heritage_item_timestamp(heritage, 'create_time', + int(launch_time_date.timestamp())) heritage_value = format_heritage(heritage) heritage_value = '"{}"'.format(heritage_value) if len( heritage_value) else heritage_value + get_rr = False # Create OR Delete the A / PTR Record if state == 'running': @@ -706,7 +713,8 @@ def lambda_handler( except BaseException as err: LOGGER.info("unexpected error. %s\n", str(err) + lineno()) - else: # not running so delete the records +# else: # not running so delete the records + elif state == 'terminating': try: # pause 1 before deleting to avoid API limit @@ -729,15 +737,16 @@ def lambda_handler( 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 get_rr: + 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: delete_resource_record( route53, @@ -775,16 +784,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 get_rr: + # pause 1 before deleting to avoid API limit + time.sleep(1) + heritage_value = get_resource_record( + route53, + reverse_lookup_zone_id, + reversed_ip_address, + 'in-addr.arpa', + 'TXT', + heritage_value + ) if len(heritage) > 0: delete_resource_record( route53, @@ -795,7 +805,7 @@ def lambda_handler( heritage_value ) - caller_response.append('Deleted PTR record in zone id: ' + + caller_response.append('Deleted TXT record in zone id: ' + str(reverse_lookup_zone_id) + ' for hosted zone ' + str(reversed_ip_address) + @@ -886,14 +896,15 @@ 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 get_rr: + 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, @@ -1660,14 +1671,16 @@ def add_heritage_item(data, key, value): data['items'][str(key)] = str(value) -def add_heritage_item_timestamp(data, key): +def add_heritage_item_timestamp(data, key, value=None): """ Add an epoch timestamp to the named field in key. :param dict(string) data: Dictionary containing heritage data :param str key: The key for the key to contain the timestamp + :param int value: The value of the timestamp to use. If missing, now() is assumed. This should be the epoch time desired. :return: This adds the key and current timestamp to the heritage dict items. There is no return value. """ - data['items'][str(key)] = int(datetime.datetime.now().timestamp()) + data['items'][str(key)] = int( + datetime.datetime.now().timestamp()) if value is None else value def format_heritage(data):