Beispiel #1
0
def _list_pritunl_user(module, organization_id, user_id=None, filters=None):
    users = []

    response = pritunl_auth_request(module, 'GET',
                                    "/user/%s" % organization_id)

    if response.getcode() != 200:
        module.fail_json(msg='Could not retrive users from Pritunl')
    else:
        for user in json.loads(response.read()):
            # No filtering
            if filters is None:
                users.append(user)

            else:
                filtered_flag = False

                for filter_key, filter_val in iteritems(filters):
                    if filter_val != user[filter_key]:
                        filtered_flag = True

                if not filtered_flag:
                    users.append(user)

    return users
Beispiel #2
0
def delete_pritunl_organization(module):
    result = {}

    org_name = module.params.get('name')

    if org_name is None:
        module.fail_json(
            msg='Please provide an organization name using name=<OrgName>')

    # Grab existing orgs
    orgs = _list_pritunl_organization(module, {"name": org_name})

    # Check if the pritunl org exists, if not, do nothing
    if len(orgs) == 0:
        result['changed'] = False
        result['response'] = {}

    # Otherwise remove the org from Pritunl
    else:
        response = pritunl_auth_request(module, 'DELETE',
                                        "/organization/%s" % orgs[0]['id'])

        if response.getcode() != 200:
            module.fail_json(
                msg="Could not remove organization %s from Pritunl" %
                (org_name))
        else:
            result['changed'] = True
            result['response'] = json.loads(response.read())

    module.exit_json(**result)
Beispiel #3
0
def post_pritunl_organization(module):
    result = {}

    org_name = module.params.get('name')

    if org_name is None:
        module.fail_json(msg=("Please provide an organization name "
                              "using name=<OrgName>"))

    # Grab existing orgs
    orgs = _list_pritunl_organization(module, {"name": org_name})

    # Check if the pritunl org already exists
    # If yes do nothing
    if len(orgs) > 0:
        result['changed'] = False
        result['response'] = orgs

    # Otherwise add the org to Pritunl
    else:
        response = pritunl_auth_request(
            module,
            'POST',
            '/organization',
            headers={'Content-Type': 'application/json'},
            data=json.dumps({'name': org_name}))

        if response.getcode() != 200:
            module.fail_json(msg="Could not add organization %s to Pritunl" %
                             (org_name))
        else:
            result['changed'] = True
            result['response'] = json.loads(response.read())

    module.exit_json(**result)
Beispiel #4
0
def delete_pritunl_user(module):
    result = {}

    org_name = module.params.get('organization')
    user_name = module.params.get('user_name')

    org_obj_list = _list_pritunl_organization(module, {"name": org_name})

    if len(org_obj_list) == 0:
        module.fail_json(
            msg=
            "Can not remove user from the organization '%s' which does not exist"
            % org_name)

    org_id = org_obj_list[0]['id']

    # Grab existing users from this org
    users = _list_pritunl_user(module, org_id, filters={"name": user_name})

    # Check if the pritunl user exists, if not, do nothing
    if len(users) == 0:
        result['changed'] = False
        result['response'] = {}

    # Otherwise remove the org from Pritunl
    else:
        response = pritunl_auth_request(
            module, 'DELETE', "/user/%s/%s" % (org_id, users[0]['id']))

        if response.getcode() != 200:
            module.fail_json(
                msg="Could not remove user %s from organization %s from Pritunl"
                % (users[0]['name'], org_name))
        else:
            result['changed'] = True
            result['response'] = json.loads(response.read())

    module.exit_json(**result)
Beispiel #5
0
def _list_pritunl_organization(module, filters=None):
    orgs = []

    response = pritunl_auth_request(module, 'GET', '/organization')

    if response.getcode() != 200:
        module.fail_json(msg='Could not retrive organizations from Pritunl')
    else:
        for org in json.loads(response.read()):
            # No filtering
            if filters is None:
                orgs.append(org)

            else:
                filtered_flag = False

                for filter_key, filter_val in iteritems(filters):
                    if filter_val != org[filter_key]:
                        filtered_flag = True

                if not filtered_flag:
                    orgs.append(org)

    return orgs
Beispiel #6
0
def post_pritunl_user(module):
    result = {}

    org_name = module.params.get('organization')
    user_name = module.params.get('user_name')

    if user_name is None:
        module.fail_json(
            msg='Please provide a user name using user_name=<username>')

    user_params = {
        'name': user_name,
        'email': module.params.get('user_email'),
        'groups': module.params.get('user_groups'),
        'disabled': module.params.get('user_disabled'),
        'gravatar': module.params.get('user_gravatar'),
        'type': module.params.get('user_type'),
    }

    org_obj_list = _list_pritunl_organization(module, {"name": org_name})

    if len(org_obj_list) == 0:
        module.fail_json(
            msg="Can not add user to organization '%s' which does not exist" %
            org_name)

    org_id = org_obj_list[0]['id']

    # Grab existing users from this org
    users = _list_pritunl_user(module, org_id, filters={"name": user_name})

    # Check if the pritunl user already exists
    # If yes do nothing
    if len(users) > 0:
        # Compare remote user params with local user_params and trigger update if needed
        user_params_changed = False
        for key in user_params.keys():
            # When a param is not specified grab the existing one to prevent from changing it with the PUT request
            if user_params[key] is None:
                user_params[key] = users[0][key]

            # groups is a list comparison
            if key == 'groups':
                if set(users[0][key]) != set(user_params[key]):
                    user_params_changed = True

            # otherwise it is either a boolean or a string
            else:
                if users[0][key] != user_params[key]:
                    user_params_changed = True

        # Trigger a PUT on the API to update the current user if settings have changed
        if user_params_changed:
            response = pritunl_auth_request(
                module,
                'PUT',
                "/user/%s/%s" % (org_id, users[0]['id']),
                headers={'Content-Type': 'application/json'},
                data=json.dumps(user_params))

            if response.getcode() != 200:
                module.fail_json(
                    msg="Could not update Pritunl user %s from %s organization"
                    % (user_name, org_name))
            else:
                result['changed'] = True
                result['response'] = json.loads(response.read())
        else:
            result['changed'] = False
            result['response'] = users
    else:
        response = pritunl_auth_request(
            module,
            'POST',
            "/user/%s" % org_id,
            headers={'Content-Type': 'application/json'},
            data=json.dumps(user_params))

        if response.getcode() != 200:
            module.fail_json(
                msg="Could not add Pritunl user %s to %s organization" %
                (user_params['name'], org_name))
        else:
            result['changed'] = True
            result['response'] = json.loads(response.read())

    module.exit_json(**result)