Esempio n. 1
0
def main():
    """
    Main function
    :returns: IP Address List Information
    """
    module = AnsibleModule(argument_spec=dict(
        auth=dict(type='dict'),
        region=dict(default='na', type='str'),
        my_user=dict(default=False, type='bool'),
        username=dict(default=None, type='str'),
        firstname=dict(default=None, type='str'),
        lastname=dict(default=None, type='str'),
        email=dict(default=None, type='str'),
        phone_country_code=dict(default=None, type='str'),
        phone=dict(default=None, type='str'),
        state=dict(default=None, type='str'),
        department=dict(default=None, type='str'),
    ),
                           supports_check_mode=True)
    return_data = return_object('user')
    user = None

    try:
        credentials = get_credentials(module)
    except ImportError as e:
        module.fail_json(msg='{0}'.format(e))

    # Check the region supplied is valid
    regions = get_regions()
    if module.params.get('region') not in regions:
        module.fail_json(
            msg='Invalid region. Regions must be one of {0}'.format(regions))

    if credentials is False:
        module.fail_json(msg='Could not load the user credentials')

    try:
        client = NTTMCPClient(credentials, module.params.get('region'))
    except NTTMCPAPIException as e:
        module.fail_json(msg=e.msg)

    try:
        if module.params.get('my_user'):
            user = client.get_my_user()
            if user is not None:
                return_data['user'].append(user)
        elif module.params.get('username') is not None:
            user = client.get_user(username=module.params.get('username'))
            if user is not None:
                return_data['user'].append(user)
        else:
            return_data['user'] = client.list_users(
                firstname=module.params.get('firstname'),
                lastname=module.params.get('lastname'),
                email=module.params.get('email'),
                phone_country_code=module.params.get('phone_country_code'),
                phone=module.params.get('phone'),
                state=module.params.get('state'),
                department=module.params.get('department'))
        return_data['count'] = len(return_data.get('user'))
        module.exit_json(data=return_data)
    except (KeyError, IndexError, AttributeError, NTTMCPAPIException) as e:
        module.fail_json(
            msg='Could not retrieve a list of users - {0}'.format(e))
Esempio n. 2
0
def main():
    """
    Main function
    :returns: IP Address List Information
    """
    module = AnsibleModule(argument_spec=dict(
        auth=dict(type='dict'),
        region=dict(default='na', type='str'),
        username=dict(required=True, type='str'),
        my_user=dict(default=False, type='bool'),
        password=dict(required=False, type='str'),
        new_password=dict(required=False, type='str'),
        roles=dict(required=False, type='list', elements='str'),
        fullname=dict(default=None, type='str'),
        firstname=dict(default=None, type='str'),
        lastname=dict(default=None, type='str'),
        email=dict(default=None, type='str'),
        phone_country_code=dict(default=None, type='int'),
        phone=dict(default=None, type='int'),
        remove_phone=dict(default=False, type='bool'),
        department=dict(default=None, type='str'),
        custom_1=dict(default=None, type='str'),
        custom_2=dict(default=None, type='str'),
        state=dict(default='present', choices=['present', 'absent']),
    ),
                           supports_check_mode=True)
    user = None
    username = module.params.get('username')
    state = module.params.get('state')
    try:
        credentials = get_credentials(module)
    except ImportError as e:
        module.fail_json(msg='{0}'.format(e))

    # Check the region supplied is valid
    regions = get_regions()
    if module.params.get('region') not in regions:
        module.fail_json(
            msg='Invalid region. Regions must be one of {0}'.format(regions))

    if credentials is False:
        module.fail_json(msg='Could not load the user credentials')

    try:
        client = NTTMCPClient(credentials, module.params.get('region'))
    except NTTMCPAPIException as e:
        module.fail_json(msg=e.msg)

    # Search for the user
    try:
        if module.params.get('my_user'):
            user = client.get_my_user()
        else:
            user = client.get_user(username=username)
    except NTTMCPAPIException as e:
        module.fail_json(msg=str(e).replace('"', '\''))

    if state == 'present':
        if user is None:
            if module.check_mode:
                module.exit_json(
                    msg='A new user with the username {0} will be created'.
                    format(username))
            # Create User
            create_user(module, client)
        else:
            try:
                changed = False
                if module.params.get('new_password'):
                    change_password(module, client, user)
                    changed = True
                # Check for role changes and for checkmode
                compare_user(module, user, True)
                if compare_user_roles(module, user.get('role')).get('changes'):
                    user = update_user_roles(module, client)
                    changed = True
                compare_result = compare_user(module, user, False)
                if compare_result.get('changes'):
                    update_user(module, client, user)
                module.exit_json(changed=changed, data=user)
            except NTTMCPAPIException as e:
                module.fail_json(
                    msg='Failed to update the User - {0}'.format(e))
    elif state == 'absent':
        if user is None:
            module.exit_json(msg='User not found')
        # Implement Check Mode
        if module.check_mode:
            module.exit_json(
                msg=
                'An existing user was found for username {0} and will be removed'
                .format(username))
        delete_user(module, client, username)