D CSS Common Helper Functions for cURL

Note on Proxy Setting: In case of proxies, set the proxy host and port as the system arguments.

#!/bin/sh
#set -x
export PATH=$PATH:<PATH_TO_JQ_BINARY>
SERVER_URL="<SERVICE_URL>"
USERNAME="<DOMAINNAME.USERNAME>"
PASSWORD="<PASSWORD>"
API_VERSION="v1"

funcCallRESTAPI() {
	if [ "$1" == "GET" ] || [ "$1" == "DELETE" ]; then
		if [ "$6" != "" ]; then
			echo `curl -s -u $4:$5 -H "$3" --request $1 -G $2 -d "$6"`
		else
			echo `curl -s -u $4:$5 -H "$3" --request $1 -G $2`
		fi
	else
                if [ "$6" != "" ]; then
                        echo `curl -s -u $4:$5 -H "$3" --request $1 $2 -d "$6"`
                else
                        echo `curl -s -u $4:$5 -H "$3" --request $1 $2`
                fi
	fi
}

funcCSSRESTHelper() {
        jobOutput=$(funcCallRESTAPI "$1" "$2" "$3" "$4" "$5" "$6")
        jobUrl=`echo $jobOutput | jq '.links[1].href'`
        if [ $jobUrl != null ]; then
                jobUrl="${jobUrl%\"}"
                jobUrl="${jobUrl#\"}"
                jobStatus=-1
                while [ $jobStatus == -1 ]; do
                        jobOutput=$(funcCallRESTAPI "GET" "$jobUrl" "$header" "$USERNAME" "$PASSWORD")
                        jobStatus=`echo $jobOutput | jq '.status'`
                done
                restStatus=`echo $jobOutput | jq '.details'`
                restStatus="${restStatus%\"}"
                restStatus="${restStatus#\"}"
                statusMessage=""
                if [ $jobStatus == 0 ]; then
                        statusMessage="$7 completed successfully."
                else
                        statusMessage=$restStatus
                fi
               	echo "$statusMessage"
        else
                failedMessage=`echo $jobOutput | jq '.details'`
		failedMessage="${failedMessage%\"}"
                failedMessage="${failedMessage#\"}"
                echo $failedMessage
        fi
}

funcAddUsers() {
	url="$SERVER_URL/interop/rest/security/$API_VERSION/users"
	params="filename=$1&userpassword=$2&resetpassword=$3"
	header="Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
	cssRESTAPI="AddUsers"
	statusMessage=$(funcCSSRESTHelper "POST" "$url" "$header" "$USERNAME" "$PASSWORD" "$params" "$cssRESTAPI")
	echo $statusMessage
}

funcRemoveUsers() {
        url="$SERVER_URL/interop/rest/security/$API_VERSION/users"
        params="filename=$1"
        header="Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
        cssRESTAPI="RemoveUsers"
        statusMessage=$(funcCSSRESTHelper "DELETE" "$url" "$header" "$USERNAME" "$PASSWORD" "$params" "$cssRESTAPI")
        echo $statusMessage
}

funcAssignRole() {
        url="$SERVER_URL/interop/rest/security/$API_VERSION/users"
        params="filename=$1&jobtype=ASSIGN_ROLE&rolename=$2"
        header="Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
        cssRESTAPI="AssignRole"
        statusMessage=$(funcCSSRESTHelper "PUT" "$url" "$header" "$USERNAME" "$PASSWORD" "$params" "$cssRESTAPI")
        echo $statusMessage
}

funcUnassignRole() {
        url="$SERVER_URL/interop/rest/security/$API_VERSION/users"
        params="filename=$1&jobtype=UNASSIGN_ROLE&rolename=$2"
        header="Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
        cssRESTAPI="UnassignRole"
        statusMessage=$(funcCSSRESTHelper "PUT" "$url" "$header" "$USERNAME" "$PASSWORD" "$params" "$cssRESTAPI")
        echo $statusMessage
}

funcAddUsersToGroup() {
	url="$SERVER_URL/interop/rest/security/$API_VERSION/groups"
	params="filename=$1&jobtype=ADD_USERS_TO_GROUP&groupname=$2"
	header="Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
	cssRESTAPI="AddUsersToGroup"
	statusMessage=$(funcCSSRESTHelper "PUT" "$url" "$header" "$USERNAME" "$PASSWORD" "$params" "$cssRESTAPI")
	echo $statusMessage
}

funcRemoveUsersFromGroup() {
        url="$SERVER_URL/interop/rest/security/$API_VERSION/groups"
        params="filename=$1&jobtype=REMOVE_USERS_FROM_GROUP&groupname=$2"
        header="Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
        cssRESTAPI="RemoveUsersFromGroup"
        statusMessage=$(funcCSSRESTHelper "PUT" "$url" "$header" "$USERNAME" "$PASSWORD" "$params" "$cssRESTAPI")
        echo $statusMessage
}

funcGenerateRoleAssignmentReport() {
        url="$SERVER_URL/interop/rest/security/$API_VERSION/roleassignmentreport"
        params="filename=$1"
        header="Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
        cssRESTAPI="generateRoleAssignmentReport"
        statusMessage=$(funcCSSRESTHelper "POST" "$url" "$header" "$USERNAME" "$PASSWORD" "$params" "$cssRESTAPI")
        echo $statusMessage
}

#funcAddUsers test1.csv password false
#funcRemoveUsers test2.csv
#funcAssignRole test3.csv "Power User"
#funcUnAssignRole test4.csv Viewer
#funcAddUsersToGroup test5.csv TestNativeGroup1
#funcRemoveUsersFromGroup test6.csv TestNativeGroup2
funcGenerateRoleAssignmentReport RoleAssignmentReport.csv