Skip to content

Commit

Permalink
Cleanup: Bulk Migration from boto3_conn to module.client() (ansible-c…
Browse files Browse the repository at this point in the history
…ollections#188)

* Migrate from boto3_conn to module.client
* Simplify error handling when creating connections
* Simplify Region handling
* Remove unused imports
* Changelog

This commit was initially merged in https://github.com/ansible-collections/community.aws
See: ansible-collections@6bdf00d
  • Loading branch information
tremble authored and alinabuzachis committed Oct 6, 2023
1 parent 38f6e64 commit 8d35a03
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions plugins/modules/iam_mfa_device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,13 @@

try:
import boto3
import botocore
from botocore.exceptions import ClientError
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_conn
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_aws_connection_info


def list_mfa_devices(connection, module):
Expand All @@ -96,11 +95,10 @@ def main():
if module._name == 'iam_mfa_device_facts':
module.deprecate("The 'iam_mfa_device_facts' module has been renamed to 'iam_mfa_device_info'", date='2021-12-01', collection_name='community.aws')

region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
if region:
connection = boto3_conn(module, conn_type='client', resource='iam', region=region, endpoint=ec2_url, **aws_connect_kwargs)
else:
module.fail_json(msg="region must be specified")
try:
connection = module.client('iam')
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
module.fail_json_aws(e, msg='Failed to connect to AWS')

list_mfa_devices(connection, module)

Expand Down

0 comments on commit 8d35a03

Please sign in to comment.