Skip to content

Commit

Permalink
get create_time from launch-time
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Feb 25, 2022
1 parent 6917597 commit 61ed460
Showing 1 changed file with 47 additions and 34 deletions.
81 changes: 47 additions & 34 deletions code/ddns-lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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) +
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 61ed460

Please sign in to comment.