def test_create_and_delete_health_check(self): """ Will create, tag health check, search for it via a tag, then delete it. :return: """ zone_domain = 'unit-test-zone.com' hostname = 'test-unittest' port = 80 protocol_type = 'HTTP' route53 = modules.route53.Route53() health_check_config_dict = { 'Port': port, 'Type': protocol_type, 'ResourcePath': '/', 'FullyQualifiedDomainName': hostname+'.'+zone_domain, 'RequestInterval': 10, 'FailureThreshold': 3 } # Create health check response = route53.create_health_check(health_check_config_dict) print "XXXXX Create" print response self.assertEqual(response['HealthCheck']['HealthCheckConfig']['FullyQualifiedDomainName'], hostname+'.'+zone_domain) self.assertEquals(response['HealthCheck']['HealthCheckConfig']['ResourcePath'], '/') health_check_id = response['HealthCheck']['Id'] #health_check_creation_status_code = response['ResponseMetadata']['HTTPStatusCode'] # Tag health check tag_name = 'unit-test-name' tag_value = 'test_create_and_delete_health_check-'+str(random.randrange(100000, 999999999)) route53.change_tags_for_resource_health_check(health_check_id, 'Name', 'unit-test') route53.change_tags_for_resource_health_check(health_check_id, tag_name, tag_value) # Search for health check via tag searched_health_check_id = route53.get_health_check_by_tag(tag_name, tag_value) self.assertEqual(health_check_id, searched_health_check_id) # Delete the health check time.sleep(.5) delete_response = self.delete_health_check(health_check_id) #print "XXXXX Delete" #print response self.assertEquals(delete_response, None)
def ec2_terminate_event(ec2_instance_id): """ When an ec2 instance is terminated, the DNS record and health check for this server is removed """ # config settings = configparser.ConfigParser() settings.read('config.ini') logging.info("Event: ec2_termination_event") logging.info("Working on ec2-instance id: " + ec2_instance_id) logging.info("Using route53 hosted zone id: " + settings.get('route53', 'hosted_zone')) logging.info("Domain name: " + settings.get('route53', 'domain_name')) # Get the DNS name to a simple or weighted dns_name = '' if settings.get('dns_record_type', 'type') == 'simple': dns_name = ec2_instance_id + '.' + settings.get( 'route53', 'domain_name') elif settings.get('dns_record_type', 'type') == 'weighted': dns_name = settings.get('dns_record_type', 'dns_name') + '.' + settings.get( 'route53', 'domain_name') # init route53 object route53 = modules.route53.Route53() route53.set_hosted_zone_id(settings.get('route53', 'hosted_zone')) health_check_id = route53.get_health_check_by_tag('instance-id', ec2_instance_id) instance_public_ip = route53.get_health_check_tag_value( ec2_instance_id, 'instance-public-ip') # Delete DNS record resource_record_set_dict = { 'Name': dns_name, 'Type': settings.get('dns_record_set', 'type'), 'SetIdentifier': ec2_instance_id, 'Weight': int(settings.get('dns_record_set', 'Weight')), 'TTL': int(settings.get('dns_record_set', 'TTL')), 'ResourceRecords': [ { 'Value': instance_public_ip }, ], 'HealthCheckId': health_check_id } logging.debug(resource_record_set_dict) try: response_delete_resource_record_sets = route53.create_resource_record_sets( 'DELETE', resource_record_set_dict, '') logging.debug(response_delete_resource_record_sets) except: logging.info("Unable to delete the record set") logging.info(resource_record_set_dict) # Search for health check via tag searched_health_check_id = route53.get_health_check_by_tag( 'instance-id', ec2_instance_id) # Delete health check try: delete_response = route53.delete_health_check(searched_health_check_id) except: logging.info("Unable to delete the health check")
def ec2_terminate_event(ec2_instance_id): """ When an ec2 instance is terminated, the DNS record and health check for this server is removed """ # config settings = configparser.ConfigParser() settings.read('config.ini') logging.info("Event: ec2_termination_event") logging.info("Working on ec2-instance id: "+ec2_instance_id) logging.info("Using route53 hosted zone id: "+settings.get('route53', 'hosted_zone')) logging.info("Domain name: "+settings.get('route53', 'domain_name')) # Get the DNS name to a simple or weighted dns_name = '' if settings.get('dns_record_type', 'type') == 'simple': dns_name = ec2_instance_id+'.'+settings.get('route53', 'domain_name') elif settings.get('dns_record_type', 'type') == 'weighted': dns_name = settings.get('dns_record_type', 'dns_name')+'.'+settings.get('route53', 'domain_name') # init route53 object route53 = modules.route53.Route53() route53.set_hosted_zone_id(settings.get('route53', 'hosted_zone')) health_check_id = route53.get_health_check_by_tag('instance-id', ec2_instance_id) instance_public_ip = route53.get_health_check_tag_value(ec2_instance_id, 'instance-public-ip') # Delete DNS record resource_record_set_dict = { 'Name': dns_name, 'Type': settings.get('dns_record_set', 'type'), 'SetIdentifier': ec2_instance_id, 'Weight': int(settings.get('dns_record_set', 'Weight')), 'TTL': int(settings.get('dns_record_set', 'TTL')), 'ResourceRecords': [ { 'Value': instance_public_ip }, ], 'HealthCheckId': health_check_id } logging.debug(resource_record_set_dict) try: response_delete_resource_record_sets = route53.create_resource_record_sets('DELETE', resource_record_set_dict, '') logging.debug(response_delete_resource_record_sets) except: logging.info("Unable to delete the record set") logging.info(resource_record_set_dict) # Search for health check via tag searched_health_check_id = route53.get_health_check_by_tag('instance-id', ec2_instance_id) # Delete health check try: delete_response = route53.delete_health_check(searched_health_check_id) except: logging.info("Unable to delete the health check")