Example #1
0
def set_users_groups(module, iam, name, groups, updated, new_name):
    """ Sets groups for a user, will purge groups not explictly passed, while
        retaining pre-existing groups that also are in the new list.
    """
    changed = False

    if updated:
        name = new_name

    try:
        orig_users_groups = [og['group_name'] for og in iam.get_groups_for_user(
            name).list_groups_for_user_result.groups]
        remove_groups = [
            rg for rg in frozenset(orig_users_groups).difference(groups)]
        new_groups = [
            ng for ng in frozenset(groups).difference(orig_users_groups)]
    except boto.exception.BotoServerError, err:
        module.fail_json(changed=changed, msg=str(err))
Example #2
0
def set_users_groups(module, iam, name, groups, updated=None,
new_name=None):
    """ Sets groups for a user, will purge groups not explictly passed, while
        retaining pre-existing groups that also are in the new list.
    """
    changed = False

    if updated:
        name = new_name

    try:
        orig_users_groups = [og['group_name'] for og in iam.get_groups_for_user(
            name).list_groups_for_user_result.groups]
        remove_groups = [
            rg for rg in frozenset(orig_users_groups).difference(groups)]
        new_groups = [
            ng for ng in frozenset(groups).difference(orig_users_groups)]
    except boto.exception.BotoServerError, err:
        module.fail_json(changed=changed, msg=str(err))
Example #3
0
def set_users_groups(module, iam, name, groups, updated=None, new_name=None):
    """ Sets groups for a user, will purge groups not explicitly passed, while
        retaining pre-existing groups that also are in the new list.
    """
    changed = False

    if updated:
        name = new_name

    try:
        orig_users_groups = [
            og['group_name'] for og in iam.get_groups_for_user(
                name).list_groups_for_user_result.groups
        ]
        remove_groups = [
            rg for rg in frozenset(orig_users_groups).difference(groups)
        ]
        new_groups = [
            ng for ng in frozenset(groups).difference(orig_users_groups)
        ]
    except boto.exception.BotoServerError as err:
        module.fail_json(changed=changed, msg=str(err))
    else:
        if len(orig_users_groups) > 0:
            for new in new_groups:
                iam.add_user_to_group(new, name)
            for rm in remove_groups:
                iam.remove_user_from_group(rm, name)
        else:
            for group in groups:
                try:
                    iam.add_user_to_group(group, name)
                except boto.exception.BotoServerError as err:
                    error_msg = boto_exception(err)
                    if ('The group with name %s cannot be found.' %
                            group) in error_msg:
                        module.fail_json(changed=False,
                                         msg="Group %s doesn't exist" % group)

    if len(remove_groups) > 0 or len(new_groups) > 0:
        changed = True

    return (groups, changed)
Example #4
0
File: iam.py Project: likewg/DevOps
def set_users_groups(module, iam, name, groups, updated=None,
new_name=None):
    """ Sets groups for a user, will purge groups not explicitly passed, while
        retaining pre-existing groups that also are in the new list.
    """
    changed = False

    if updated:
        name = new_name

    try:
        orig_users_groups = [og['group_name'] for og in iam.get_groups_for_user(
            name).list_groups_for_user_result.groups]
        remove_groups = [
            rg for rg in frozenset(orig_users_groups).difference(groups)]
        new_groups = [
            ng for ng in frozenset(groups).difference(orig_users_groups)]
    except boto.exception.BotoServerError as err:
        module.fail_json(changed=changed, msg=str(err))
    else:
        if len(orig_users_groups) > 0:
            for new in new_groups:
                iam.add_user_to_group(new, name)
            for rm in remove_groups:
                iam.remove_user_from_group(rm, name)
        else:
            for group in groups:
                try:
                    iam.add_user_to_group(group, name)
                except boto.exception.BotoServerError as err:
                    error_msg = boto_exception(err)
                    if ('The group with name %s cannot be found.' % group) in error_msg:
                        module.fail_json(changed=False, msg="Group %s doesn't exist" % group)


    if len(remove_groups) > 0 or len(new_groups) > 0:
        changed = True

    return (groups, changed)
Example #5
0
def set_users_groups(iam, name, groups):
    """ Sets groups for a user, will purge groups not explictly passed, while
        retaining pre-existing groups that also are in the new list.
    """
    changed = False
    orig_users_groups = [og['group_name'] for og in iam.get_groups_for_user(
        name).list_groups_for_user_result.groups]
    remove_groups = [
        rg for rg in frozenset(orig_users_groups).difference(groups)]
    new_groups = [
        ng for ng in frozenset(groups).difference(orig_users_groups)]
    if len(orig_users_groups) > 0:
        for new in new_groups:
            iam.add_user_to_group(new, name)
        for rm in remove_groups:
            iam.remove_user_from_group(rm, name)
    else:
        for group in groups:
            iam.add_user_to_group(group, name)

    if len(remove_groups) > 0 or len(new_groups) > 0:
        changed = True

    return (groups, changed)