Skip to content

Commit

Permalink
refactor iam_mfa_device_info
Browse files Browse the repository at this point in the history
  • Loading branch information
tremble committed Feb 27, 2024
1 parent 4309c93 commit 629d9d5
Showing 1 changed file with 9 additions and 24 deletions.
33 changes: 9 additions & 24 deletions plugins/modules/iam_mfa_device_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,16 @@
register: assumed_role
"""

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

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ansible_collections.amazon.aws.plugins.module_utils.iam import AnsibleIAMError
from ansible_collections.amazon.aws.plugins.module_utils.iam import list_iam_mfa_devices
from ansible_collections.amazon.aws.plugins.module_utils.iam import normalize_iam_mfa_devices
from ansible_collections.amazon.aws.plugins.module_utils.modules import AnsibleAWSModule


def list_mfa_devices(connection, module):
user_name = module.params.get("user_name")
changed = False

args = {}
if user_name is not None:
args["UserName"] = user_name
try:
response = connection.list_mfa_devices(**args)
except ClientError as e:
module.fail_json_aws(e, msg="Failed to list MFA devices")

module.exit_json(changed=changed, **camel_dict_to_snake_dict(response))
devices = list_iam_mfa_devices(connection, user_name)
module.exit_json(changed=False, mfa_devices=normalize_iam_mfa_devices(devices))


def main():
Expand All @@ -92,12 +78,11 @@ def main():
supports_check_mode=True,
)

connection = module.client("iam")
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)
list_mfa_devices(connection, module)
except AnsibleIAMError as e:
module.fail_json_aws_error(e)


if __name__ == "__main__":
Expand Down

0 comments on commit 629d9d5

Please sign in to comment.