Skip to content

Commit

Permalink
Cleanup - use is_boto3_error_(message|code) (#268)
Browse files Browse the repository at this point in the history
* Reorder imports
* Make use of is_boto3_error_message
* Mass-migration over to is_boto3_error_code
* Remove unused imports
* unused vars in exception
* Improve consistency around catching BotoCoreError and ClientError
* Remove unused imports
* Remove unused 'PolicyError' from iam_policy_info
* Avoid catching botocore.exceptions.ClientError when we only want some error codes
* Import camel_dict_to_snake_dict/snake_dict_to_camel_dict from ansible.module_utils.common.dict_transformations

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections/community.aws@4cf52ef
  • Loading branch information
tremble authored and GomathiselviS committed Oct 17, 2024
1 parent a3cd5bc commit a3132b7
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions plugins/modules/ec2_placement_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@
'''

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
try:
from botocore.exceptions import (BotoCoreError, ClientError)
import botocore
except ImportError:
pass # caught by AnsibleAWSModule

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry


@AWSRetry.exponential_backoff()
def get_placement_group_details(connection, module):
Expand All @@ -104,7 +106,7 @@ def get_placement_group_details(connection, module):
"Name": "group-name",
"Values": [name]
}])
except (BotoCoreError, ClientError) as e:
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(
e,
msg="Couldn't find placement group named [%s]" % name)
Expand All @@ -128,13 +130,13 @@ def create_placement_group(connection, module):
try:
connection.create_placement_group(
GroupName=name, Strategy=strategy, DryRun=module.check_mode)
except (BotoCoreError, ClientError) as e:
if e.response['Error']['Code'] == "DryRunOperation":
module.exit_json(changed=True, placement_group={
"name": name,
"state": 'DryRun',
"strategy": strategy,
})
except is_boto3_error_code('DryRunOperation'):
module.exit_json(changed=True, placement_group={
"name": name,
"state": 'DryRun',
"strategy": strategy,
})
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(
e,
msg="Couldn't create placement group [%s]" % name)
Expand All @@ -152,7 +154,7 @@ def delete_placement_group(connection, module):
try:
connection.delete_placement_group(
GroupName=name, DryRun=module.check_mode)
except (BotoCoreError, ClientError) as e:
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(
e,
msg="Couldn't delete placement group [%s]" % name)
Expand Down

0 comments on commit a3132b7

Please sign in to comment.