diff --git a/code/ddns-lambda.py b/code/ddns-lambda.py index 6b23b48..1ecccb1 100755 --- a/code/ddns-lambda.py +++ b/code/ddns-lambda.py @@ -39,22 +39,24 @@ LOGGER = logging.getLogger() ACCOUNT = None REGION = None -VERSION = '0.0.5' +VERSION = '0.0.6' # Adjust the logging level [logging.INFO, logging.DEBUG, logging.WARNING, etc] LOGGER.setLevel(logging.DEBUG) # SNS_CLIENT = None -# Read Env variables -SLEEPTIME = int(os.environ.get('SleepTime','60')) -DDBNAME = os.environ.get('DynamoDBName','inf-dynamic-route53') -TAGKEY_CNAME = os.environ.get('TagKeyCname','boc:dns:cname') -TAGKEY_ZONE = os.environ.get('TagKeyZone','boc:dns:zone') -TAGKEY_HOSTNAME = os.environ.get('TagKeyHostName','') -DNS_RR_TTL = int(os.environ.get('DNS_RR_TimeToLive','60')) -DNS_RR_TTL = 60 if DNS_RR_TTL==0 else DNS_RR_TTL +# Read Env variables +SLEEPTIME = int(os.environ.get('SleepTime', '60')) +DDBNAME = os.environ.get('DynamoDBName', 'inf-dynamic-route53') +TAGKEY_CNAME = os.environ.get('TagKeyCname', 'boc:dns:cname') +TAGKEY_ZONE = os.environ.get('TagKeyZone', 'boc:dns:zone') +TAGKEY_HOSTNAME = os.environ.get('TagKeyHostName', '') +DNS_RR_TTL = int(os.environ.get('DNS_RR_TimeToLive', '60')) +DNS_RR_TTL = 60 if DNS_RR_TTL == 0 else DNS_RR_TTL + +print('Loading function v%s: %s'.format( + VERSION, datetime.datetime.now().time().isoformat())) -print('Loading function v%s: %s'.format(VERSION,datetime.datetime.now().time().isoformat())) def lineno(): # pragma: no cover """ @@ -73,6 +75,7 @@ def lineno(): # pragma: no cover # except ClientError as err: # print("Unexpected error: %s" % err) + def get_route53_client(): """ Get route53 client @@ -83,6 +86,7 @@ def get_route53_client(): except ClientError as err: print("Unexpected error: %s" % err) + def get_ec2_client(): """ Get ec2 client @@ -93,6 +97,7 @@ def get_ec2_client(): except ClientError as err: print("Unexpected error: %s" % err) + def get_dynamodb_client(): """ Get dynamodb client @@ -103,6 +108,7 @@ def get_dynamodb_client(): except ClientError as err: print("Unexpected error: %s" % err) + def lambda_handler( event, context, @@ -155,7 +161,7 @@ def lambda_handler( # Only doing something if the state is running if state == 'running': - LOGGER.debug("sleeping for {} seconds {}".format(SLEEPTIME,lineno())) + LOGGER.debug("sleeping for {} seconds {}".format(SLEEPTIME, lineno())) if "pytest" in sys.modules: # called from within a test run @@ -178,7 +184,8 @@ def lambda_handler( LOGGER.debug("instance_attributes: %s", str(instance_attributes) + lineno()) LOGGER.debug("trying to put instance information in " "dynamo table %s", str(instance_attributes) + lineno()) - put_item_in_dynamodb_table(dynamodb_client, DDBNAME, instance_id, instance_attributes) + put_item_in_dynamodb_table(dynamodb_client, DDBNAME, + instance_id, instance_attributes) LOGGER.debug("done putting item in dynamo table %s", lineno()) else: # Fetch item from DynamoDB @@ -195,14 +202,16 @@ def lambda_handler( LOGGER.debug("tags are: %s", str(tags) + lineno()) # tag_type = determine_tag_type(tags) - tag_type = determine_tag_type(tags)[0] # changed to return a list, so read the 1st value - has_custom_hostname = determine_tag_type(tags)[1] # if hostname is found in the + # changed to return a list, so read the 1st value + tag_type = determine_tag_type(tags)[0] + has_custom_hostname = determine_tag_type(tags)[1] # if hostname is found in the LOGGER.debug("tag type: %s", str(tag_type) + lineno()) LOGGER.debug("has custom hostname tag: %s", str(has_custom_hostname) + lineno()) if tag_type == 'invalid': - LOGGER.info("Must have either CNAME or ZONE in tags, can not have both tags" + lineno()) + LOGGER.info( + "Must have either CNAME or ZONE in tags, can not have both tags" + lineno()) exit(-1) LOGGER.debug("Get instance attributes %s", lineno()) @@ -211,7 +220,8 @@ def lambda_handler( if instance and 'Reservations' in instance: LOGGER.debug("reservations: %s", str(instance['Reservations']) + lineno()) LOGGER.debug("reservations: %s", str(instance['Reservations'][0]) + lineno()) - LOGGER.debug("reservations: %s", str(instance['Reservations'][0]['Instances']) + lineno()) + LOGGER.debug("reservations: %s", str( + instance['Reservations'][0]['Instances']) + lineno()) LOGGER.debug("reservations:" " %s", str(instance['Reservations'][0]['Instances'][0]) + lineno()) @@ -260,10 +270,12 @@ def lambda_handler( reversed_domain_prefix = get_reversed_domain_prefix(subnet_mask, private_ip) reversed_domain_prefix = reverse_list(reversed_domain_prefix) - LOGGER.debug("reversed_domain_prefix is: %s", str(reversed_domain_prefix) + lineno()) + LOGGER.debug("reversed_domain_prefix is: %s", + str(reversed_domain_prefix) + lineno()) # Set the reverse lookup zone reversed_lookup_zone = reversed_domain_prefix + 'in-addr.arpa.' - LOGGER.info("The reverse lookup zone for this instance is: %s", str(reversed_lookup_zone)) + LOGGER.info("The reverse lookup zone for this instance is: %s", + str(reversed_lookup_zone)) # Get VPC id vpc_id = instance['Reservations'][0]['Instances'][0]['VpcId'] @@ -272,11 +284,13 @@ def lambda_handler( if is_dns_hostnames_enabled(compute, vpc_id): LOGGER.debug("DNS hostnames enabled for %s", str(vpc_id) + lineno()) else: - LOGGER.debug("DNS hostnames disabled for %s. You have to enable DNS hostnames to use Route 53 private hosted zones. %s", vpc_id, lineno()) + LOGGER.debug( + "DNS hostnames disabled for %s. You have to enable DNS hostnames to use Route 53 private hosted zones. %s", vpc_id, lineno()) if is_dns_support_enabled(compute, vpc_id): LOGGER.debug("DNS support enabled for %s", str(vpc_id) + lineno()) else: - LOGGER.debug("DNS support disabled for %s. You have to enabled DNS support to use Route 53 private hosted zones. %s", str(vpc_id), lineno()) + LOGGER.debug("DNS support disabled for %s. You have to enabled DNS support to use Route 53 private hosted zones. %s", str( + vpc_id), lineno()) exit() # Create the public and private hosted zone collections. @@ -285,7 +299,8 @@ def lambda_handler( LOGGER.debug("hosted_zones: %s", str(hosted_zones) + lineno()) private_hosted_zones = get_private_hosted_zones(hosted_zones) LOGGER.debug("private_hosted_zones: %s", str(list(private_hosted_zones)) + lineno()) - private_hosted_zone_collection = get_private_hosted_zone_collection(private_hosted_zones) + private_hosted_zone_collection = get_private_hosted_zone_collection( + private_hosted_zones) LOGGER.debug("private_hosted_zone_collection: %s", str(list(private_hosted_zone_collection)) + lineno()) @@ -310,19 +325,24 @@ def lambda_handler( reverse_zone = record['Name'] break if reverse_zone: - LOGGER.debug("Reverse lookup zone found: %s", str(reversed_lookup_zone) + lineno()) + LOGGER.debug("Reverse lookup zone found: %s", + str(reversed_lookup_zone) + lineno()) reverse_lookup_zone_id = get_zone_id(route53, reversed_lookup_zone) - LOGGER.debug("reverse_lookup_zone_id: %s", str(reverse_lookup_zone_id) + lineno()) + LOGGER.debug("reverse_lookup_zone_id: %s", str( + reverse_lookup_zone_id) + lineno()) - reverse_hosted_zone_properties = get_hosted_zone_properties(route53, reverse_lookup_zone_id) + reverse_hosted_zone_properties = get_hosted_zone_properties( + route53, reverse_lookup_zone_id) LOGGER.debug("reverse_hosted_zone_properties:" " %s", str(reverse_hosted_zone_properties) + lineno()) if vpc_id in map(lambda x: x['VPCId'], reverse_hosted_zone_properties['VPCs']): - LOGGER.info("Reverse lookup zone %s is associated with VPC %s %s", reverse_lookup_zone_id, vpc_id, lineno()) + LOGGER.info("Reverse lookup zone %s is associated with VPC %s %s", + reverse_lookup_zone_id, vpc_id, lineno()) reverse_zone_associated = True else: - LOGGER.info("Reverse lookup zone %s is NOT associated with VPC %s %s", reverse_lookup_zone_id, vpc_id, lineno()) + LOGGER.info("Reverse lookup zone %s is NOT associated with VPC %s %s", + reverse_lookup_zone_id, vpc_id, lineno()) reverse_zone_associated = False # awspeter - commmeted out vpc association @@ -332,7 +352,8 @@ def lambda_handler( # except BaseException as err: # LOGGER.debug("%s", str(err)+lineno()) else: - LOGGER.info("No matching reverse lookup zone, PTR record will not be created %s", lineno()) + LOGGER.info( + "No matching reverse lookup zone, PTR record will not be created %s", lineno()) # LOGGER.info("No matching reverse lookup zone, so we will create one %s", lineno()) # # create private hosted zone for reverse lookups # if state == 'running': @@ -374,13 +395,17 @@ def lambda_handler( # " %s", str(list(public_hosted_zones_collection)) + lineno()) if tag.get('Value').lstrip().lower() in private_hosted_zone_collection: - LOGGER.debug("Private zone found: %s", str(tag.get('Value')) + lineno()) + LOGGER.debug("Private zone found: %s", + str(tag.get('Value')) + lineno()) private_hosted_zone_name = tag.get('Value').lstrip().lower() - LOGGER.debug("private_zone_name: %s", str(private_hosted_zone_name) + lineno()) - private_hosted_zone_id = get_zone_id(route53, private_hosted_zone_name) + LOGGER.debug("private_zone_name: %s", str( + private_hosted_zone_name) + lineno()) + private_hosted_zone_id = get_zone_id( + route53, private_hosted_zone_name) LOGGER.debug("private_hosted_zone_id:" " %s", str(private_hosted_zone_id) + lineno()) - private_hosted_zone_properties = get_hosted_zone_properties(route53, private_hosted_zone_id) + private_hosted_zone_properties = get_hosted_zone_properties( + route53, private_hosted_zone_id) LOGGER.debug("private_hosted_zone_properties:" " %s", str(private_hosted_zone_properties) + lineno()) fqdn = private_host_name + '.' + private_hosted_zone_name @@ -391,9 +416,11 @@ def lambda_handler( if vpc['VPCId'] == vpc_id: found_vpc_id = True if found_vpc_id: - LOGGER.info("Private hosted zone %s is associated with VPC %s %s", private_hosted_zone_id, vpc_id, lineno()) + LOGGER.info("Private hosted zone %s is associated with VPC %s %s", + private_hosted_zone_id, vpc_id, lineno()) else: - LOGGER.info("Private hosted zone %s is NOT associated with VPC %s %s", private_hosted_zone_id, vpc_id, lineno()) + LOGGER.info("Private hosted zone %s is NOT associated with VPC %s %s", + private_hosted_zone_id, vpc_id, lineno()) # LOGGER.info("Associating zone %s with VPC %s %s", private_hosted_zone_id, vpc_id, lineno()) # try: # associate_zone(route53, private_hosted_zone_id, region, vpc_id) @@ -411,16 +438,17 @@ def lambda_handler( private_ip ) - LOGGER.debug("appending to caller response %s", lineno()) + LOGGER.debug( + "appending to caller response %s", lineno()) caller_response.append('Created A record in zone id: ' + - str(private_hosted_zone_id) + - ' for hosted zone ' + - str(private_host_name) + '.' + - str(private_hosted_zone_name) + - ' with value: ' + - str(private_ip)) - + str(private_hosted_zone_id) + + ' for hosted zone ' + + str(private_host_name) + '.' + + str(private_hosted_zone_name) + + ' with value: ' + + str(private_ip)) + if reverse_zone_associated: create_resource_record( route53, @@ -429,19 +457,19 @@ def lambda_handler( 'in-addr.arpa', 'PTR', fqdn -# private_dns_name + # private_dns_name ) caller_response.append('Created PTR record in zone id: ' + - str(reverse_lookup_zone_id) + - ' for hosted zone ' + - str(reversed_ip_address) + - 'in-addr.arpa with value: ' + - str(fqdn)) + str(reverse_lookup_zone_id) + + ' for hosted zone ' + + str(reversed_ip_address) + + 'in-addr.arpa with value: ' + + str(fqdn)) # str(private_dns_name)) except BaseException as err: - LOGGER.debug("%s", str(err)+lineno()) + LOGGER.debug("%s", str(err) + lineno()) else: try: delete_resource_record( @@ -468,7 +496,7 @@ def lambda_handler( 'in-addr.arpa', 'PTR', fqdn -# private_dns_name + # private_dns_name ) caller_response.append('Deleted PTR record in zone id: ' + @@ -481,7 +509,7 @@ def lambda_handler( # str(private_dns_name)) except BaseException as err: - LOGGER.debug("%s", str(err)+lineno()) + LOGGER.debug("%s", str(err) + lineno()) # create PTR record # awspeter - commneted out public dns # elif tag.get('Value').lstrip().lower() in public_hosted_zones_collection: @@ -535,17 +563,20 @@ def lambda_handler( else: LOGGER.info("No matching zone found for %s", tag.get('Value')) else: - LOGGER.info("%s is not a valid host name %s", tag.get('Value'), lineno()) + LOGGER.info("%s is not a valid host name %s", + tag.get('Value'), lineno()) # Consider making this an elif CNAME else: - LOGGER.debug("The tag \'%s\' is not a zone tag %s", str(tag.get('Key')), lineno()) + LOGGER.debug("The tag \'%s\' is not a zone tag %s", + str(tag.get('Key')), lineno()) if TAGKEY_CNAME.upper() in tag.get('Key', {}).lstrip().upper(): # Simple hostname check if is_valid_hostname(tag.get('Value')): - LOGGER.debug("CNAME hostname of %s is valid %s", str(tag.get('Value')), lineno()) + LOGGER.debug("CNAME hostname of %s is valid %s", + str(tag.get('Value')), lineno()) # convert the cname value to lower case and strip whitespace and newline characters icname = tag.get('Value').lstrip().lower() @@ -556,7 +587,8 @@ def lambda_handler( # Gets suffix cname_domain_suffix = icname[icname.find('.') + 1:] - LOGGER.debug("cname_domain_suffix: %s", str(cname_domain_suffix) + lineno()) + LOGGER.debug("cname_domain_suffix: %s", str( + cname_domain_suffix) + lineno()) # Try and find the hosted zone with the cname suffix cname_domain_suffix_id = get_zone_id(route53, cname_domain_suffix) @@ -566,9 +598,11 @@ def lambda_handler( LOGGER.debug("Iterating over private hosted zones %s", lineno()) for cname_private_hosted_zone in private_hosted_zone_collection: - LOGGER.debug("cname for private hosted zone in private hosted zone collection: %s", str(cname_private_hosted_zone) + lineno()) + LOGGER.debug("cname for private hosted zone in private hosted zone collection: %s", str( + cname_private_hosted_zone) + lineno()) - cname_private_hosted_zone_id = get_zone_id(route53, cname_private_hosted_zone) + cname_private_hosted_zone_id = get_zone_id( + route53, cname_private_hosted_zone) LOGGER.debug("cname_private_hosted_zone_id:" " %s", str(cname_private_hosted_zone_id) + lineno()) LOGGER.debug("cname_domain_suffix_id:" @@ -585,7 +619,8 @@ def lambda_handler( # create CNAME record in private zone if state == 'running': try: - LOGGER.debug("creating resource record %s", lineno()) + LOGGER.debug( + "creating resource record %s", lineno()) LOGGER.debug("private_dns_name:" " %s", str(private_dns_name) + lineno()) create_resource_record( @@ -609,7 +644,8 @@ def lambda_handler( LOGGER.debug("%s", str(err) + lineno()) else: try: - LOGGER.debug("deleting resource record %s", lineno()) + LOGGER.debug( + "deleting resource record %s", lineno()) delete_resource_record( route53, cname_private_hosted_zone_id, @@ -703,7 +739,7 @@ def lambda_handler( LOGGER.debug("dhcp_configurations: %s", str(get_dhcp_configurations) + lineno()) except BaseException as err: - LOGGER.info("No DHCP option set assigned to this VPC %s\n", str(err)+lineno()) + LOGGER.info("No DHCP option set assigned to this VPC %s\n", str(err) + lineno()) exit() # Look to see whether there's a DHCP option set assigned to @@ -715,14 +751,17 @@ def lambda_handler( for configuration in dhcp_configurations: LOGGER.debug("configuration: %s", str(configuration) + lineno()) - LOGGER.debug("private hosted zones: %s", str(private_hosted_zone_collection) + lineno()) + LOGGER.debug("private hosted zones: %s", str( + private_hosted_zone_collection) + lineno()) if configuration in private_hosted_zone_collection: private_hosted_zone_name = configuration - LOGGER.debug("Private zone found %s", str(private_hosted_zone_name) + lineno()) + LOGGER.debug("Private zone found %s", str( + private_hosted_zone_name) + lineno()) private_hosted_zone_id = get_zone_id(route53, private_hosted_zone_name) - LOGGER.debug("Private_hosted_zone_id: %s", str(private_hosted_zone_id) + lineno()) + LOGGER.debug("Private_hosted_zone_id: %s", str( + private_hosted_zone_id) + lineno()) private_hosted_zone_properties = get_hosted_zone_properties( route53, private_hosted_zone_id @@ -736,10 +775,12 @@ def lambda_handler( if state == 'running': if vpc_id in map(lambda x: x['VPCId'], private_hosted_zone_properties['VPCs']): vpc_associated = True - LOGGER.info("Private hosted zone %s is associated with VPC %s %s", private_hosted_zone_id, vpc_id, lineno()) + LOGGER.info("Private hosted zone %s is associated with VPC %s %s", + private_hosted_zone_id, vpc_id, lineno()) else: vpc_associated = False - LOGGER.info("Private hosted zone %s is NOT associated with VPC %s %s", private_hosted_zone_id, vpc_id, lineno()) + LOGGER.info("Private hosted zone %s is NOT associated with VPC %s %s", + private_hosted_zone_id, vpc_id, lineno()) exit() # LOGGER.info("Associating zone %s with VPC" # " %s %s", private_hosted_zone_id, vpc_id, lineno()) @@ -763,14 +804,15 @@ def lambda_handler( ) caller_response.append('Created A record in zone id: ' + - str(private_hosted_zone_id) + - ' for hosted zone ' + - str(private_host_name) + '.' + - str(private_hosted_zone_name) + - ' with value: ' + - str(private_ip)) + str(private_hosted_zone_id) + + ' for hosted zone ' + + str(private_host_name) + '.' + + str(private_hosted_zone_name) + + ' with value: ' + + str(private_ip)) else: - LOGGER.debug("No forward zone associated with VPC - skipping creating resource records %s", lineno()) + LOGGER.debug( + "No forward zone associated with VPC - skipping creating resource records %s", lineno()) if reverse_zone_associated: create_resource_record( route53, @@ -779,20 +821,21 @@ def lambda_handler( 'in-addr.arpa', 'PTR', fqdn -# private_dns_name + # private_dns_name ) caller_response.append('Created PTR record in zone id: ' + - str(reverse_lookup_zone_id) + - ' for hosted zone ' + - str(reversed_ip_address) + - 'in-addr.arpa with value: ' + - str(fqdn)) + str(reverse_lookup_zone_id) + + ' for hosted zone ' + + str(reversed_ip_address) + + 'in-addr.arpa with value: ' + + str(fqdn)) # str(private_dns_name)) else: - LOGGER.debug("No reverse zone associated with VPC - skipping creating resource records %s", lineno()) + LOGGER.debug( + "No reverse zone associated with VPC - skipping creating resource records %s", lineno()) - else: + else: if vpc_associated: LOGGER.debug("Creating resource records %s", lineno()) create_resource_record( @@ -805,12 +848,13 @@ def lambda_handler( ) caller_response.append('Created A record in zone id: ' + - str(private_hosted_zone_id) + ' for hosted zone ' + - str(cname_prefix) + '.' + - str(private_hosted_zone_name) + ' with value: ' + - str(private_ip)) + str(private_hosted_zone_id) + ' for hosted zone ' + + str(cname_prefix) + '.' + + str(private_hosted_zone_name) + ' with value: ' + + str(private_ip)) else: - LOGGER.debug("No forward zone associated with VPC - skipping creating resource records %s", lineno()) + LOGGER.debug( + "No forward zone associated with VPC - skipping creating resource records %s", lineno()) if reverse_zone_associated: create_resource_record( route53, @@ -822,13 +866,14 @@ def lambda_handler( ) caller_response.append('Created PTR record in zone id: ' + - str(reverse_lookup_zone_id) + - ' for hosted zone ' + - str(reversed_ip_address) + - 'in-addr.arpa with value: ' + - str(cname)) + str(reverse_lookup_zone_id) + + ' for hosted zone ' + + str(reversed_ip_address) + + 'in-addr.arpa with value: ' + + str(cname)) else: - LOGGER.debug("No reverse zone associated with VPC - skipping creating resource records %s", lineno()) + LOGGER.debug( + "No reverse zone associated with VPC - skipping creating resource records %s", lineno()) except BaseException as err: LOGGER.info("unexpected error. %s\n", str(err) + lineno()) @@ -846,10 +891,10 @@ def lambda_handler( private_ip ) caller_response.append('Deleted A record in zone id: ' + - str(private_hosted_zone_id) + ' for hosted zone ' + - str(private_host_name) + '.' + - str(private_hosted_zone_name) + ' with value: ' + - str(private_ip)) + str(private_hosted_zone_id) + ' for hosted zone ' + + str(private_host_name) + '.' + + str(private_hosted_zone_name) + ' with value: ' + + str(private_ip)) delete_resource_record( route53, @@ -858,16 +903,16 @@ def lambda_handler( 'in-addr.arpa', 'PTR', fqdn -# private_dns_name + # private_dns_name ) caller_response.append('Deleted PTR record in zone id: ' + - str(reverse_lookup_zone_id) + - ' for hosted zone ' + - str(reversed_ip_address) + - 'in-addr.arpa with value: ' + - str(fqdn)) + str(reverse_lookup_zone_id) + + ' for hosted zone ' + + str(reversed_ip_address) + + 'in-addr.arpa with value: ' + + str(fqdn)) # str(private_dns_name)) - else: + else: delete_resource_record( route53, private_hosted_zone_id, @@ -877,10 +922,10 @@ def lambda_handler( private_ip ) caller_response.append('Deleted A record in zone id: ' + - str(private_hosted_zone_id) + ' for hosted zone ' + - str(cname_prefix) + '.' + - str(private_hosted_zone_name) + ' with value: ' + - str(private_ip)) + str(private_hosted_zone_id) + ' for hosted zone ' + + str(cname_prefix) + '.' + + str(private_hosted_zone_name) + ' with value: ' + + str(private_ip)) delete_resource_record( route53, reverse_lookup_zone_id, @@ -891,11 +936,11 @@ def lambda_handler( ) caller_response.append('Deleted PTR record in zone id: ' + - str(reverse_lookup_zone_id) + - ' for hosted zone ' + - str(reversed_ip_address) + - 'in-addr.arpa with value: ' + - str(cname)) + str(reverse_lookup_zone_id) + + ' for hosted zone ' + + str(reversed_ip_address) + + 'in-addr.arpa with value: ' + + str(cname)) except BaseException as err: LOGGER.info("unexpected error. %s\n", str(err) + lineno()) @@ -913,6 +958,7 @@ def lambda_handler( return caller_response + def determine_tag_type(tags): """ Determine tag type - CNAME or ZONE @@ -941,6 +987,7 @@ def determine_tag_type(tags): return ['zone_selected', has_custom_hostname] return 'invalid' + def get_cname_from_tags(tags): """ Get the cname prefix from tags @@ -958,7 +1005,8 @@ def get_cname_from_tags(tags): return cname return None except: - LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0])) + LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0])) + def get_instances(client, instance_id): """ @@ -970,6 +1018,7 @@ def get_instances(client, instance_id): except ClientError as err: LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + def list_hosted_zones(client): """ Get route53 hosted zones @@ -980,7 +1029,8 @@ def list_hosted_zones(client): return client.list_hosted_zones() except ClientError as err: LOGGER.info("unexpected error. %s\n", str(err) + lineno()) - + + def list_tables(client): """ List the dynamodb tables @@ -990,7 +1040,8 @@ def list_tables(client): try: return client.list_tables() except ClientError as err: - LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + def delete_item_from_dynamodb_table(client, table, instance_id): """ @@ -1007,7 +1058,8 @@ def delete_item_from_dynamodb_table(client, table, instance_id): 'InstanceId': {'S': instance_id} }) except ClientError as err: - LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + def put_item_in_dynamodb_table(client, table, instance_id, instance_attributes): """ @@ -1030,7 +1082,8 @@ def put_item_in_dynamodb_table(client, table, instance_id, instance_attributes): } ) except ClientError as err: - LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + def get_item_from_dynamodb_table(client, table, instance_id): """ @@ -1064,7 +1117,8 @@ def get_item_from_dynamodb_table(client, table, instance_id): return json.loads(item) return None except ClientError as err: - LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + def get_private_hosted_zone_collection(private_hosted_zones): """ @@ -1121,6 +1175,7 @@ def get_private_hosted_zone_collection(private_hosted_zones): # except: # LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def get_private_hosted_zones(hosted_zones): """ Get private hosted zones @@ -1140,6 +1195,7 @@ def get_private_hosted_zones(hosted_zones): except: LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def get_dhcp_option_set_id_for_vpc(client, vpc_id): """ Get the dhcp option set from vpc @@ -1162,7 +1218,7 @@ def get_dhcp_option_set_id_for_vpc(client, vpc_id): return option_sets[vpc_id] except ClientError as err: - LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + LOGGER.info("unexpected error. %s\n", str(err) + lineno()) # def create_dynamodb_table(client, table_name): # """ @@ -1194,6 +1250,7 @@ def get_dhcp_option_set_id_for_vpc(client, vpc_id): # except ClientError as err: # LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + def get_dynamodb_table(client, table_name): """ Get the dynamodb table @@ -1230,6 +1287,7 @@ def get_dynamodb_table(client, table_name): # except: # LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def change_resource_recordset(client, zone_id, host_name, hosted_zone_name, record_type, value): """ Change resource recordset @@ -1270,15 +1328,18 @@ def change_resource_recordset(client, zone_id, host_name, hosted_zone_name, reco error_message = str(err) if "conflicts with other records" in error_message: - LOGGER.debug("Can not create dns record because of duplicates: %s", str(err) + lineno()) + LOGGER.debug( + "Can not create dns record because of duplicates: %s", str(err) + lineno()) return 'Duplicate resource record' elif "conflicting RRSet" in error_message: - LOGGER.debug("Can not create dns record because of duplicates: %s", str(err) + lineno()) + LOGGER.debug( + "Can not create dns record because of duplicates: %s", str(err) + lineno()) return 'Conflicting resource record' else: LOGGER.info("unexpected error. %s\n", str(err) + lineno()) return 'Unexpected error: ' + str(err) + def create_resource_record(client, zone_id, host_name, hosted_zone_name, record_type, value): """ This function creates resource records in the hosted zone passed by the calling function. @@ -1322,6 +1383,7 @@ def create_resource_record(client, zone_id, host_name, hosted_zone_name, record_ LOGGER.debug("Can not create dns record because " "of duplicates: %s", str(err) + lineno()) + 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. @@ -1365,15 +1427,16 @@ def delete_resource_record(client, zone_id, host_name, hosted_zone_name, record_ except ClientError as err: if 'Not Found' in str(err): - LOGGER.debug("Record not found error: %s", str(err)+lineno()) + LOGGER.debug("Record not found error: %s", str(err) + lineno()) return if 'InvalidChangeBatch' in str(err) and 'it was not found' in str(err): - LOGGER.debug("Record not found error: %s", str(err)+lineno()) + LOGGER.debug("Record not found error: %s", str(err) + lineno()) return LOGGER.info("unexpected error. %s\n", str(err) + lineno()) + def get_zone_id(client, zone_name, private_zone=True): """ This function returns the zone id for the zone name that's passed into the function. @@ -1406,6 +1469,7 @@ def get_zone_id(client, zone_name, private_zone=True): except: LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def is_valid_hostname(hostname): """ This function checks to see whether the hostname entered @@ -1423,7 +1487,8 @@ def is_valid_hostname(hostname): return all(allowed.match(x) for x in hostname.split(".")) except: LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) - + + def get_dhcp_configurations(client, dhcp_options_id): """ This function returns the names of the zones/domains that are in the option set. @@ -1451,6 +1516,7 @@ def get_dhcp_configurations(client, dhcp_options_id): except: LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def reverse_list(ip_list): """ Reverses the order of the instance's IP address and @@ -1486,6 +1552,7 @@ def reverse_list(ip_list): except: LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def get_reversed_domain_prefix(subnet_mask, private_ip): """ Uses the mask to get the zone prefix for the reverse lookup zone @@ -1532,6 +1599,7 @@ def get_reversed_domain_prefix(subnet_mask, private_ip): # except: # LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def json_serial(obj): """ JSON serializer for objects not serializable by default json code @@ -1545,6 +1613,7 @@ def json_serial(obj): except: LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def remove_empty_from_dict(dictionary): """ Removes empty keys from dictionary @@ -1554,7 +1623,7 @@ def remove_empty_from_dict(dictionary): try: if isinstance(dictionary, dict): - return dict((k, remove_empty_from_dict(v)) for k, v in dictionary.items() \ + return dict((k, remove_empty_from_dict(v)) for k, v in dictionary.items() if v and remove_empty_from_dict(v)) if isinstance(dictionary, list): return [remove_empty_from_dict(v) for v in dictionary @@ -1583,7 +1652,8 @@ def remove_empty_from_dict(dictionary): # Comment='Updated by Lambda DDNS' # ) # except: -# LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) +# LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def is_dns_hostnames_enabled(client, vpc_id): """ @@ -1603,6 +1673,7 @@ def is_dns_hostnames_enabled(client, vpc_id): except: LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def is_dns_support_enabled(client, vpc_id): """ Whether dns support is enabled @@ -1619,7 +1690,8 @@ def is_dns_support_enabled(client, vpc_id): LOGGER.debug('response2: %s', str(response) + lineno()) return response['EnableDnsSupport']['Value'] except: - LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def get_hosted_zone_properties(client, zone_id): """ @@ -1629,14 +1701,17 @@ def get_hosted_zone_properties(client, zone_id): :return: """ try: - LOGGER.debug('getting hosted zone properties: zone_id: %s', str(zone_id) + lineno()) + LOGGER.debug('getting hosted zone properties: zone_id: %s', + str(zone_id) + lineno()) hosted_zone_properties = client.get_hosted_zone(Id=zone_id) - LOGGER.debug('hosted_zone_properties: %s', str(hosted_zone_properties) + lineno()) + LOGGER.debug('hosted_zone_properties: %s', str( + hosted_zone_properties) + lineno()) if 'ResponseMetadata' in hosted_zone_properties: hosted_zone_properties.pop('ResponseMetadata') return hosted_zone_properties except: - LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + LOGGER.info("unexpected error. %s\n", str(sys.exc_info()[0]) + lineno()) + def get_subnet_cidr_block(client, subnet_id): """ @@ -1672,4 +1747,3 @@ def get_subnet_cidr_block(client, subnet_id): # ) # except ClientError as err: # LOGGER.debug("Unexpected error: %s", str(err)+lineno()) -