From 5c1a9140029525293d87eb87bd8c0260b87feb6c Mon Sep 17 00:00:00 2001 From: Will Thames Date: Wed, 31 May 2017 22:50:32 +1000 Subject: [PATCH] Fix ec2_asg exception handling (#25121) `e.message` is a string, and `camel_dict_to_snake_dict` fails when given a string. The intended code is to run `camel_dict_to_snake_dict` on `e.response`, the result of which includes a `message` key. Make exception handling lines more consistent and wrap for slightly shorter lines. --- lib/ansible/modules/cloud/amazon/ec2_asg.py | 25 ++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/lib/ansible/modules/cloud/amazon/ec2_asg.py b/lib/ansible/modules/cloud/amazon/ec2_asg.py index 23613daf1b..6837523c59 100644 --- a/lib/ansible/modules/cloud/amazon/ec2_asg.py +++ b/lib/ansible/modules/cloud/amazon/ec2_asg.py @@ -381,9 +381,11 @@ def elb_healthy(asg_connection, elb_connection, module, group_name): if e.response['Error']['Code'] == 'InvalidInstance': return None - module.fail_json(msg="Failed to get load balancer.", exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) + module.fail_json(msg="Failed to get load balancer.", + exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) except botocore.exceptions.BotoCoreError as e: - module.fail_json(msg="Failed to get load balancer.", exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.message)) + module.fail_json(msg="Failed to get load balancer.", + exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) for i in lb_instances.get('InstanceStates'): if i['State'] == "InService": @@ -413,9 +415,11 @@ def tg_healthy(asg_connection, elbv2_connection, module, group_name): if e.response['Error']['Code'] == 'InvalidInstance': return None - module.fail_json(msg="Failed to get target group.", exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) + module.fail_json(msg="Failed to get target group.", + exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) except botocore.exceptions.BotoCoreError as e: - module.fail_json(msg="Failed to get target group.", exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.message)) + module.fail_json(msg="Failed to get target group.", + exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) for i in tg_instances.get('TargetHealthDescriptions'): if i['TargetHealth']['State'] == "healthy": @@ -605,7 +609,8 @@ def create_autoscaling_group(connection, module): changed = True return changed, asg_properties except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: - module.fail_json(msg="Failed to create Autoscaling Group.", exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.message)) + module.fail_json(msg="Failed to create Autoscaling Group.", + exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) else: as_group = as_groups['AutoScalingGroups'][0] initial_asg_properties = get_properties(as_group) @@ -646,7 +651,7 @@ def create_autoscaling_group(connection, module): ) except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: module.fail_json(msg="Failed to update Autoscaling Group.", - exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.message)) + exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) # Update load balancers if they are specified and one or more already exists elif as_group['LoadBalancerNames']: @@ -687,7 +692,7 @@ def create_autoscaling_group(connection, module): ) except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: module.fail_json(msg="Failed to update Autoscaling Group.", - exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.message)) + exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) # Update target groups if they are specified and one or more already exists elif target_group_arns and as_group['TargetGroupARNs']: # Get differences @@ -749,7 +754,7 @@ def create_autoscaling_group(connection, module): ) except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: module.fail_json(msg="Failed to update Autoscaling Group notifications.", - exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.message)) + exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) if wait_for_instances: wait_for_new_inst(module, connection, group_name, wait_timeout, desired_capacity, 'viable_instances') # Wait for ELB health if ELB(s)defined @@ -770,7 +775,7 @@ def create_autoscaling_group(connection, module): changed = True except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e: module.fail_json(msg="Failed to read existing Autoscaling Groups.", - exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.message)) + exception=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) return changed, asg_properties @@ -1126,7 +1131,7 @@ def main(): **aws_connect_params) except (botocore.exceptions.NoCredentialsError, botocore.exceptions.ProfileNotFound) as e: module.fail_json(msg="Can't authorize connection. Check your credentials and profile.", - exceptions=traceback.format_exc(), **camel_dict_to_snake_dict(e.message)) + exceptions=traceback.format_exc(), **camel_dict_to_snake_dict(e.response)) changed = create_changed = replace_changed = False if state == 'present':