示例#1
0
def update(context, id, etag, name, password, role):
    result = user.update(context, id=id, etag=etag, name=name, password=password, role=role)

    if result.status_code == 204:
        utils.format_output({"id": id, "etag": etag, "message": "User updated."}, context.format)
    else:
        utils.format_output(result.json(), context.format)
示例#2
0
def update(context, args):
    params = {
        k: getattr(args, k)
        for k in [
            "name",
            "password",
            "email",
            "team_id",
            "fullname",
            "state",
            "id",
            "etag",
        ]
    }
    params["fullname"] = params["fullname"] or params["name"]
    params["state"] = active_string(params["state"])
    return user.update(context, **params)
示例#3
0
def update(context, id, etag, name, password, role):
    """update(context, id, etag, name, password, role)

    Update a user.

    >>> dcictl user-update [OPTIONS]

    :param string id: ID of the user to update [required]
    :param string etag: Entity tag of the user resource [required]
    :param string name: Name of the user
    :param string password: Password of the user
    :param string role: Role of the user (admin or user)
    """
    result = user.update(context, id=id, etag=etag, name=name,
                         password=password, role=role)

    if result.status_code == 204:
        utils.print_json({'id': id, 'message': 'User updated.'})
    else:
        utils.format_output(result, context.format)
示例#4
0
def main():
    module = AnsibleModule(
        argument_spec=dict(
            state=dict(default='present',
                       choices=['present', 'absent'],
                       type='str'),
            # Authentication related parameters
            #
            dci_login=dict(required=False, type='str'),
            dci_password=dict(required=False, type='str'),
            dci_cs_url=dict(required=False, type='str'),
            # Resource related parameters
            #
            id=dict(type='str'),
            name=dict(type='str'),
            password=dict(type='str'),
            role=dict(choices=['user', 'admin'], type='str'),
            team_id=dict(type='str'),
        ), )

    if not dciclient_found:
        module.fail_json(msg='The python dciclient module is required')

    login, password, url = get_details(module)
    if not login or not password:
        module.fail_json(msg='login and/or password have not been specified')

    ctx = dci_context.build_dci_context(url, login, password, 'Ansible')

    # Action required: List all users
    # Endpoint called: /users GET via dci_user.list()
    #
    # List all users
    if module_params_empty(module.params):
        res = dci_user.list(ctx)

    # Action required: Delete the user matching user id
    # Endpoint called: /users/<user_id> DELETE via dci_user.delete()
    #
    # If the user exists and it has been succesfully deleted the changed is
    # set to true, else if the user does not exist changed is set to False
    elif module.params['state'] == 'absent':
        if not module.params['id']:
            module.fail_json(msg='id parameter is required')
        res = dci_user.get(ctx, module.params['id'])
        if res.status_code not in [400, 401, 404, 409]:
            kwargs = {
                'id': module.params['id'],
                'etag': res.json()['user']['etag']
            }
            res = dci_user.delete(ctx, **kwargs)

    # Action required: Retrieve user informations
    # Endpoint called: /user/<user_id> GET via dci_user.get()
    #
    # Get user informations
    elif module.params['id'] and not module.params[
            'name'] and not module.params['password'] and not module.params[
                'role'] and not module.params['team_id']:
        res = dci_user.get(ctx, module.params['id'])

    # Action required: Update an user
    # Endpoint called: /users/<user_id> PUT via dci_user.update()
    #
    # Update the user with the specified characteristics.
    elif module.params['id']:
        res = dci_user.get(ctx, module.params['id'])
        if res.status_code not in [400, 401, 404, 409]:
            kwargs = {
                'id': module.params['id'],
                'etag': res.json()['user']['etag']
            }
            if module.params['name']:
                kwargs['name'] = module.params['name']
            if module.params['password']:
                kwargs['password'] = module.params['password']
            if module.params['role']:
                kwargs['role'] = module.params['role']
            if module.params['team_id']:
                kwargs['team_id'] = module.params['team_id']
            res = dci_user.update(ctx, **kwargs)

    # Action required: Create a user with the specified content
    # Endpoint called: /users POST via dci_user.create()
    #
    # Create the new user.
    else:
        if not module.params['name']:
            module.fail_json(msg='name parameter must be specified')
        if not module.params['password']:
            module.fail_json(msg='password parameter must be specified')
        if not module.params['team_id']:
            module.fail_json(msg='team_id parameter must be specified')
        if not module.params['role']:
            role = 'user'

        kwargs = {
            'name': module.params['name'],
            'password': module.params['password'],
            'role': role,
            'team_id': module.params['team_id'],
        }

        res = dci_user.create(ctx, **kwargs)

    try:
        result = res.json()
        if res.status_code == 404:
            module.fail_json(msg='The resource does not exist')
        if res.status_code == 409:
            result['changed'] = False
        else:
            result['changed'] = True
    except:
        result = {}
        result['changed'] = True

    module.exit_json(**result)