Update ec2_asg_lifecycle_hook to meet modern best AWS module practices (#34379)
* Use `AnsibleAWSModule` * Update exception handling to use `fail_json_aws` and check for `BotoCoreError` exceptions associated with bad connection parameters. * Remove connection creation exception handling as it does nothing.
This commit is contained in:
parent
3388999603
commit
4d5533c597
2 changed files with 17 additions and 39 deletions
|
@ -85,6 +85,7 @@ Ansible Changes By Release
|
||||||
* aws_kms_facts
|
* aws_kms_facts
|
||||||
* aws_s3_cors
|
* aws_s3_cors
|
||||||
* aws_ssm_parameter_store
|
* aws_ssm_parameter_store
|
||||||
|
* ec2_asg_lifecycle_hook
|
||||||
* ec2_ami_facts
|
* ec2_ami_facts
|
||||||
* ec2_placement_group
|
* ec2_placement_group
|
||||||
* ec2_placement_group_facts
|
* ec2_placement_group_facts
|
||||||
|
|
|
@ -96,21 +96,13 @@ RETURN = '''
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import traceback
|
from ansible.module_utils.aws.core import AnsibleAWSModule
|
||||||
from ansible.module_utils.basic import AnsibleModule
|
from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info
|
||||||
from ansible.module_utils.ec2 import (boto3_conn, ec2_argument_spec, get_aws_connection_info, HAS_BOTO3)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import botocore
|
import botocore
|
||||||
HAS_BOTOCORE = True
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_BOTOCORE = False
|
pass # handled by AnsibleAWSModule
|
||||||
|
|
||||||
try:
|
|
||||||
import boto3
|
|
||||||
except ImportError:
|
|
||||||
# will be caught by imported HAS_BOTO3
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def create_lifecycle_hook(connection, module):
|
def create_lifecycle_hook(connection, module):
|
||||||
|
@ -151,8 +143,8 @@ def create_lifecycle_hook(connection, module):
|
||||||
AutoScalingGroupName=asg_name,
|
AutoScalingGroupName=asg_name,
|
||||||
LifecycleHookNames=[lch_name]
|
LifecycleHookNames=[lch_name]
|
||||||
)['LifecycleHooks']
|
)['LifecycleHooks']
|
||||||
except botocore.exceptions.ClientError as e:
|
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
|
||||||
module.fail_json(msg="Failed to get Lifecycle Hook %s" % str(e), exception=traceback.format_exc(e))
|
module.fail_json_aws(e, msg="Failed to get Lifecycle Hook")
|
||||||
|
|
||||||
if not existing_hook:
|
if not existing_hook:
|
||||||
changed = True
|
changed = True
|
||||||
|
@ -168,8 +160,8 @@ def create_lifecycle_hook(connection, module):
|
||||||
if changed:
|
if changed:
|
||||||
try:
|
try:
|
||||||
connection.put_lifecycle_hook(**lch_params)
|
connection.put_lifecycle_hook(**lch_params)
|
||||||
except botocore.exceptions.ClientError as e:
|
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
|
||||||
module.fail_json(msg="Failed to create LifecycleHook %s" % str(e), exception=traceback.format_exc(e))
|
module.fail_json_aws(e, msg="Failed to create LifecycleHook")
|
||||||
|
|
||||||
return(changed)
|
return(changed)
|
||||||
|
|
||||||
|
@ -200,8 +192,8 @@ def delete_lifecycle_hook(connection, module):
|
||||||
all_hooks = connection.describe_lifecycle_hooks(
|
all_hooks = connection.describe_lifecycle_hooks(
|
||||||
AutoScalingGroupName=asg_name
|
AutoScalingGroupName=asg_name
|
||||||
)
|
)
|
||||||
except botocore.exceptions.ClientError as e:
|
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
|
||||||
module.fail_json(msg="Failed to get Lifecycle Hooks %s" % str(e), exception=traceback.format_exc(e))
|
module.fail_json_aws(e, msg="Failed to get Lifecycle Hooks")
|
||||||
|
|
||||||
for hook in all_hooks['LifecycleHooks']:
|
for hook in all_hooks['LifecycleHooks']:
|
||||||
if hook['LifecycleHookName'] == lch_name:
|
if hook['LifecycleHookName'] == lch_name:
|
||||||
|
@ -213,8 +205,8 @@ def delete_lifecycle_hook(connection, module):
|
||||||
try:
|
try:
|
||||||
connection.delete_lifecycle_hook(**lch_params)
|
connection.delete_lifecycle_hook(**lch_params)
|
||||||
changed = True
|
changed = True
|
||||||
except botocore.exceptions.ClientError as e:
|
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
|
||||||
module.fail_json(msg="Failed to delete LifecycleHook %s" % str(e), exception=traceback.format_exc(e))
|
module.fail_json_aws(e, msg="Failed to delete LifecycleHook")
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -237,38 +229,23 @@ def main():
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec=argument_spec)
|
module = AnsibleAWSModule(argument_spec=argument_spec,
|
||||||
|
required_if=[['state', 'present', ['transition']]])
|
||||||
state = module.params.get('state')
|
state = module.params.get('state')
|
||||||
|
|
||||||
if not HAS_BOTOCORE:
|
|
||||||
module.fail_json(msg='botocore is required for this module')
|
|
||||||
|
|
||||||
if not HAS_BOTO3:
|
|
||||||
module.fail_json(msg='boto3 is required for this module')
|
|
||||||
|
|
||||||
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True)
|
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True)
|
||||||
|
|
||||||
if not region:
|
connection = boto3_conn(module, conn_type='client', resource='autoscaling', region=region, endpoint=ec2_url, **aws_connect_params)
|
||||||
module.fail_json(msg="region parameter is required")
|
|
||||||
|
|
||||||
try:
|
changed = False
|
||||||
connection = boto3_conn(module, conn_type='client', resource='autoscaling', region=region, endpoint=ec2_url, **aws_connect_params)
|
|
||||||
if not connection:
|
|
||||||
module.fail_json(msg="failed to connect to AWS for the given region: %s" % str(region))
|
|
||||||
except botocore.exceptions.NoCredentialsError as e:
|
|
||||||
module.fail_json(msg=str(e))
|
|
||||||
|
|
||||||
changed = create_changed = replace_changed = False
|
|
||||||
|
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
if not module.params.get('transition'):
|
|
||||||
module.fail_json(msg="transition parameter is required")
|
|
||||||
|
|
||||||
changed = create_lifecycle_hook(connection, module)
|
changed = create_lifecycle_hook(connection, module)
|
||||||
elif state == 'absent':
|
elif state == 'absent':
|
||||||
changed = delete_lifecycle_hook(connection, module)
|
changed = delete_lifecycle_hook(connection, module)
|
||||||
|
|
||||||
module.exit_json(changed=changed)
|
module.exit_json(changed=changed)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in a new issue