D CSS Common Helper Functions for cURL

This appendix shows the CSS common helper functions for cURL for the EPM Cloud REST APIs.

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="<USERNAME>"
PASSWORD="<PASSWORD>"
API_VERSION="v1"

# To avoid SSL connection issue in the environment please add -k option for below curl commands.
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." #"$restStatus"
                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
}

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

funcRemoveUserFromGroups() {
        url="$SERVER_URL/interop/rest/security/$API_VERSION/groups"
        params="filename=$1&jobtype=REMOVE_USER_FROM_GROUPS&username=$2"
        header="Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
        cssRESTAPI="RemoveUserFromGroups"
        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
}

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

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

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

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

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

funcUpdateUsers() {
        url="$SERVER_URL/interop/rest/security/$API_VERSION/users"
        params="filename=$1&jobtype=UPDATE_USERS"
        header="Content-Type: application/x-www-form-urlencoded;charset=UTF-8"
        cssRESTAPI="UpdateUsers"
        statusMessage=$(funcCSSRESTHelper "PUT" "$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
#funcGenerateUserGroupReport UserGroupReport.csv
#funcAddUserToGroups groups.csv joe
#funcRemoveUserFromGroups groups.csv joe
#funcAddGroups CreateGroup1.csv
#funcRemoveGroups DeleteGroup1.csv
#funcGenerateInvalidLoginReport 2021-06-01 2021-06-10 invalidLoginReport.csv
#funcGenerateRoleAssignmentAuditReport 2021-06-01 2021-06-10 roleAssignmentAuditReport.csv
#funcUpdateUsers updateuser.csv