Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
badra001 committed Apr 25, 2023
1 parent d64b226 commit 86dac12
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 50 deletions.
103 changes: 53 additions & 50 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_id = None
region = None
VERSION = '1.2.0b46'
VERSION = '1.2.0b48'

# Read Env variables
DEBUG_LOG_LEVEL = os.environ.get('DebugLogLevel', 'INFO')
Expand Down Expand Up @@ -459,7 +459,7 @@ def lambda_handler(
vpc_id = instance['Reservations'][0]['Instances'][0]['VpcId']

# Are DNS Hostnames and DNS Support enabled?
if is_dns_hostnames_enabled(compute, instance_id, vpc_id):
fif is_dns_hostnames_enabled(compute, instance_id, vpc_id):
LOGGER.debug("DNS hostnames enabled for %s", str(vpc_id) + lineno())
else:
LOGGER.error(
Expand Down Expand Up @@ -626,54 +626,56 @@ def lambda_handler(
tag_data['name'] = tag_data_tuple(*process_tags_name(tags))
LOGGER.debug("New tag_data structure: %s", str(pformat(tag_data)) + lineno())

if tag_data['option_name'].valid and tag_data['option_zone'].valid:
LOGGER.info("instance: %s, using custom hostname tag %s and custom zone tag %s.",
instance_id, tag_data['option_name'].hostname, tag_data['option_zone'].zonename)
f_hostname = tag_data['option_name'].hostname
f_zonename = tag_data['option_zone'].zonename

elif tag_data['option_name'].valid and not tag_data['option_zone'].valid and tag_data['dhcp_options'].valid:
LOGGER.info("instance: %s, using custom hostname %s and not custom zone %s (not-valid) and dhcp_options zone %s.",
instance_id, tag_data['option_name'].hostname, tag_data['option_zone'].zonename, tag_data['dhcp_options'].zonename)
f_hostname = tag_data['option_name'].hostname
f_zonename = tag_data['dhcp_options'].zonename

elif tag_data['name'].valid and tag_data['option_zone'].valid:
LOGGER.info("instance: %s, using hostname %s from Name and custom zone %s.",
instance_id, tag_data['name'].hostname, tag_data['option_zone'].zonename)
f_hostname = tag_data['name'].hostname
f_zonename = tag_data['option_zone'].zonename

elif tag_data['name'].valid:
LOGGER.info("instance: %s, using hostname %s and zone %s from Name tag.",
instance_id, tag_data['name'].hostname, tag_data['name'].zonename)
f_hostname = tag_data['name'].hostname
f_zonename = tag_data['name'].zonename

elif tag_data['name'].valid and tag_data['dhcp_options'].valid:
LOGGER.info("instance: %s, using hostname %s and dhcp_options zone %s.",
instance_id, tag_data['name'].hostname, tag_data['dhcp_options'].zonename)
f_hostname = tag_data['name'].hostname
f_zonename = tag_data['dhcp_options'].zonename

elif tag_data['option_zone'].valid and not tag_data['option_name'].valid and not tag_data['name'].valid:
LOGGER.info("instance: %s, not using hostname %s or %s, using custom zone %s, using ip-address %s for hostname.",
instance_id, tag_data['option_name'].hostname, tag_data['name'].hostname, tag_data['option_zone'].zonename, private_ip)
f_hostname = '-'.join(['ip'] + private_ip.split('.'))
f_zonename = tag_data['option_zone'].zonename

elif tag_data['dhcp_options'].valid:
LOGGER.info("instance: %s, using ip-address %s for hostname and zone %s.",
instance_id, private_ip, tag_data['dhcp_options'].zonename)
f_hostname = '-'.join(['ip'] + private_ip.split('.'))
f_zonename = tag_data['dhcp_options'].zonename
default_hostname = '-'.join(['ip'] + private_ip.split('.'))
if tag_data['option_zone'].valid:
if tag_data['option_name'].valid:
LOGGER.info("instance: %s, using tag_option hostname tag %s and tag_option zone tag %s.",
instance_id, tag_data['option_name'].hostname, tag_data['option_zone'].zonename)
f_hostname = tag_data['option_name'].hostname
f_zonename = tag_data['option_zone'].zonename
elif tag_data['name'].valid:
LOGGER.info("instance: %s, using tag_key Name hostname tag %s and tag_option zone tag %s.",
instance_id, tag_data['name'].hostname, tag_data['option_zone'].zonename)
f_hostname = tag_data['name'].hostname
f_zonename = tag_data['option_zone'].zonename
else:
LOGGER.info("instance: %s, using default ip-address %s for hostname and tag_option zone %s.",
instance_id, default_hostname, tag_data['option_zone'].zonename)
f_hostname = default_hostname
f_zonename = tag_data['option_zone'].zonename

else:
LOGGER.error(
"instance: %s, No DHCP Associated for VPC and no custom tags. Exiting Script", instance_id)
caller_response.append(
'No DHCP Associated for VPC and no custom tags. Exiting Script')
return caller_response
if tag_data['option_name'].valid:
LOGGER.info("instance: %s, using tag_option hostname %s and and tag_option zone %s.",
instance_id, tag_data['option_name'].hostname, tag_data['option_name'].zonename)
f_hostname = tag_data['option_name'].hostname
f_zonename = tag_data['option_name'].zonename
if not tag_data['option_name'].valid and tag_data['option_name'].hostname != '' and tag_data['dhcp_options'].valid:
LOGGER.info("instance: %s, using tag_option hostname %s and and dhcp_options zone %s.",
instance_id, tag_data['option_name'].hostname, tag_data['dhcp_options'].zonename)
f_hostname = tag_data['option_name'].hostname
f_zonename = tag_data['dhcp_options'].zonename
elif tag_data['name'].valid:
LOGGER.info("instance: %s, using tag_key Name hostname %s and tag_key Name zone %s.",
instance_id, tag_data['name'].hostname, tag_data['name'].zonename)
f_hostname = tag_data['name'].hostname
f_zonename = tag_data['name'].zonename
elif not tag_data['name'].valid and tag_data['dhcp_options'].valid:
LOGGER.info("instance: %s, using default ip-addresss hostname %s and dhcp_options zone %s.",
instance_id, default_hostname, tag_data['dhcp_options'].zonename)
f_hostname = default_hostname
f_zonename = tag_data['dhcp_options'].zonename
# elif not tag_data['name'].valid and not tag_data['dhcp_options'].valid:
else:
LOGGER.info("instance: %s, no valid hostname or zone found", instance_id)
f_hostname = None
f_zonename = None
LOGGER.error(
"instance: %s, No DHCP Associated for VPC and no custom tags. Exiting Script", instance_id)
caller_response.append(
'No DHCP Associated for VPC and no custom tags. Exiting Script')
return caller_response
# note this will not continue and set a cname

final_private_hostname = f_hostname
final_hosted_zone_name = f_zonename
Expand Down Expand Up @@ -2917,7 +2919,7 @@ def process_tags_flags(tags):

def process_tags_value(name):
"""
Process the name from one of the tags and return vald (true|false), hosname, domainname, if the domainname exists in a PHZ
Process the name from one of the tags and return vald (true|false), hostname, domainname, if the domainname exists in a PHZ
:param str: name (hostname or domainname) to check
:return tuple(bool,str,str): true|false if vaid, hostname, domainname
Expand Down Expand Up @@ -3043,7 +3045,8 @@ def parse_hostname_to_components(name):
domain = '.'.join(names[i:]) + '.'
if phz_collection_by_vpc.get(domain):
return (host, domain)
LOGGER.error('No PHZ found for any domain components of %s: %s', name, str(lineno))
LOGGER.error(
f"No PHZ found for any domain components of {name} associated with this vpc: {lineno()}")
return None


Expand Down
Binary file modified code/ddns-lambda.zip
Binary file not shown.

0 comments on commit 86dac12

Please sign in to comment.