Use helper function for new module and rename (#2277)

pull/18777/head
Rob 9 years ago committed by Matt Clay
parent f420fa0eaa
commit 7e70ef2223

@ -16,7 +16,7 @@
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: aws_mfa_devices module: iam_mfa_device_facts
short_description: List the MFA (Multi-Factor Authentication) devices registered for a user short_description: List the MFA (Multi-Factor Authentication) devices registered for a user
description: description:
- List the MFA (Multi-Factor Authentication) devices registered for a user - List the MFA (Multi-Factor Authentication) devices registered for a user
@ -37,7 +37,7 @@ requirements:
''' '''
RETURN = """ RETURN = """
devices: mfa_devices:
description: The MFA devices registered for the given user description: The MFA devices registered for the given user
returned: always returned: always
type: list type: list
@ -48,22 +48,18 @@ devices:
- enable_date: "2016-03-11T23:25:37+00:00" - enable_date: "2016-03-11T23:25:37+00:00"
serial_number: arn:aws:iam::085120003702:mfa/pwnall serial_number: arn:aws:iam::085120003702:mfa/pwnall
user_name: pwnall user_name: pwnall
changed:
description: True if listing the devices succeeds
type: bool
returned: always
""" """
EXAMPLES = ''' EXAMPLES = '''
# Note: These examples do not set authentication details, see the AWS Guide for details. # Note: These examples do not set authentication details, see the AWS Guide for details.
# List MFA devices (more details: http://docs.aws.amazon.com/IAM/latest/APIReference/API_ListMFADevices.html) # List MFA devices (more details: http://docs.aws.amazon.com/IAM/latest/APIReference/API_ListMFADevices.html)
aws_mfa_devices: iam_mfa_device_facts:
register: mfa_devices register: mfa_devices
# Assume an existing role (more details: http://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html) # Assume an existing role (more details: http://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html)
sts_assume_role: sts_assume_role:
mfa_serial_number: "{{ mfa_devices.devices[0].serial_number }}" mfa_serial_number: "{{ mfa_devices.mfa_devices[0].serial_number }}"
role_arn: "arn:aws:iam::123456789012:role/someRole" role_arn: "arn:aws:iam::123456789012:role/someRole"
role_session_name: "someRoleSession" role_session_name: "someRoleSession"
register: assumed_role register: assumed_role
@ -77,16 +73,6 @@ except ImportError:
HAS_BOTO3 = False HAS_BOTO3 = False
def normalize_mfa_device(mfa_device):
serial_number = mfa_device.get('SerialNumber', None)
user_name = mfa_device.get('UserName', None)
enable_date = mfa_device.get('EnableDate', None)
return {
'serial_number': serial_number,
'user_name': user_name,
'enable_date': enable_date
}
def list_mfa_devices(connection, module): def list_mfa_devices(connection, module):
user_name = module.params.get('user_name') user_name = module.params.get('user_name')
changed = False changed = False
@ -96,27 +82,24 @@ def list_mfa_devices(connection, module):
args['UserName'] = user_name args['UserName'] = user_name
try: try:
response = connection.list_mfa_devices(**args) response = connection.list_mfa_devices(**args)
changed = True except ClientError as e:
except ClientError, e: module.fail_json(msg=e.message, **camel_dict_to_snake_dict(e.response))
module.fail_json(msg=e)
mfa_devices = response.get('MFADevices', []) module.exit_json(changed=changed, **camel_dict_to_snake_dict(response))
devices = [normalize_mfa_device(mfa_device) for mfa_device in mfa_devices]
module.exit_json(changed=changed, devices=devices)
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = ec2_argument_spec()
argument_spec.update( argument_spec.update(
dict( dict(
user_name = dict(required=False, default=None) user_name=dict(required=False, default=None)
) )
) )
module = AnsibleModule(argument_spec=argument_spec) module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO3: if not HAS_BOTO3:
module.fail_json(msg='boto3 and botocore are required.') module.fail_json(msg='boto3 required for this module')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
if region: if region:
Loading…
Cancel
Save