Oracle Profitability and Cost Management Cloud Common Helper Functions for cURL

#!/bin/sh
SERVER_URL="https://<SERVICE_NAME>-<TENANT_NAME>.<SERVICE_TYPE>.<dcX>.oraclecloud.com"
USERNAME="<USER_NAME>"
PASSWORD="<PASSWORD>"
APP_NAME="BksML30"
API_VERSION="v1"
POV_GROUP_MEMBER="2014_January_Actual"
POV_GROUP_MEMBER1="2016_January_Actual"
funcRemoveTempFiles()
{
	for var in "$@"
	do
		if [ -f $var ]; then
			rm $var
		fi
	done
}
funcPrintErrorDetails()
{
	contentType=`echo $(grep 'Content-Type:' respHeader.txt) | tr -d [:space:]`
	if [ ! -z $contentType ] && [[ $contentType = *"application/json"* ]]; then
		output=`cat $1`
		error=`echo $output | jq '.details'`
		echo "Error details: " $error
	fi
}
funcGetStatus()
{
	output=`cat response.txt`
	count=`echo $output | jq '.links | length'`
	i=0
	pingUrl=""
	while [ $i -lt $count ]; do
		rel=`echo $output | jq '.links['$i'].rel'`
		rel=`echo "$rel" | tr -d "\""`
		if [ "$rel" == "Job Status" ]; then
				pingUrl=`echo $output | jq '.links['$i'].href'`
				pingUrl=`echo "$pingUrl" | tr -d "\""`
		fi
		i=`expr $i + 1`
	done
	echo $pingUrl
	completed="false"
	while [ $completed != "true" ]; do
		statusCode2=`curl -X $1 -s -w "%{http_code}" -u "$USERNAME:$PASSWORD" -o "pingResponse.txt"  -H "Content-Type: application/x-www-form-urlencoded" "$pingUrl"`
		if [ $statusCode2 == 200 ]; then
			status2=`jq '.status' pingResponse.txt`
			if [ $status2 != -1 ]; then
				if [ $status2 == 0 ]; then
                                    completed="true"
                                    echo "Job completed Successfully"
                                else
                                    completed="true"
                                    error=`jq '.details' pingResponse.txt`
                                    echo "Error occurred while executing request"
                                    echo "Error Details : " $error                                
                                fi
			else
				echo "Please wait..."
				sleep 20
			fi
			
		else
			echo "Please wait..."
			sleep 20
		fi
		funcRemoveTempFiles "pingResponse.txt"
	done
}
funcExecuteRequest()
{
	if [ ! -z "$4" ]; then
		statusCode=`curl -X $1 -s -w "%{http_code}" -u "$USERNAME:$PASSWORD" -o "response.txt" -D "respHeader.txt" -H "Content-Type: $4" -d "$3" $2`
	else
		statusCode=`curl -X $1 -s -w "%{http_code}" -u "$USERNAME:$PASSWORD" -o "response.txt" -D "respHeader.txt" -H "Content-Type: $3" $2`
	fi
	if [ $statusCode != 200 ]; then
		echo "Error executing request"
		if [ $statusCode != 000 ]; then
			echo "Response error code : " $statusCode
			funcPrintErrorDetails "response.txt"
			funcRemoveTempFiles "respHeader.txt" "response.txt"
		fi
		exit 0
	fi
	return $statusCode
}
funcGetRestAPIVersionsInfo()
{
	url=$SERVER_URL/epm/rest
	funcExecuteRequest "GET" $url "application/x-www-form-urlencoded"
	status=$?
	output=`cat response.txt`
	if [ $status == 200 ]; then
		echo "Version $API_VERSION details :"
		count=`echo $output | jq '.links | length'`
		i=0
		while [ $i -lt $count ]; do
			echo "Service : " `echo $output | jq '.links['$i'].rel'`
			echo "URL :" `echo $output | jq '.links['$i'].href'`
			echo "Action :" `echo $output | jq '.links['$i'].action'`
			echo ""
			i=`expr $i + 1`
		done
	else
		error=`echo $output`
		echo "Error occurred. " $error
	fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}
funcGetRestAPIVersionInfo()
{
	url=$SERVER_URL/epm/rest/$API_VERSION
	funcExecuteRequest "GET" $url "application/x-www-form-urlencoded"
	status=$?
	echo "status code :$status"
	output=`cat response.txt`
	if [ $status == 200 ]; then
		echo "Version $API_VERSION details :"
		count=`echo $output | jq '.links | length'`
		i=0
		while [ $i -lt $count ]; do
			echo "Service : " `echo $output | jq '.links['$i'].rel'`
			echo "URL :" `echo $output | jq '.links['$i'].href'`
			echo "Action :" `echo $output | jq '.links['$i'].action'`
			echo ""
			i=`expr $i + 1`
		done
	else
		error=`echo $output`
		echo "Error occurred. " $error
	fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}
funcImportTemplate() {
	description="Import Template through Curl Sample"
	instance="PROFITABILITY_WEB_APP"
	essAppServer="EssbaseCluster-1"
	sharedServicesProject="EssbaseCluster-1"
	applicationType="Management Ledger"
	fileName="PCM_BksML12_20160413_042937.zip"
	isApplicationOverwrite="true"
	param="{\"description\":\"$description\",\"instanceName\":\"$instance\",\"essApplicationServer\":\"$essAppServer\",\"sharedServicesProject\":\"$sharedServicesProject\",\"applicationType\":\"$applicationType\",\"fileName\":\"$fileName\",\"isApplicationOverwrite\":\"$isApplicationOverwrite\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/jobs/templateImportJob
	funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Started importing successfully"
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}

funcDeployCube() {
	comment="Cube deployment Curl"
	param="{\"isKeepData\":\"true\",\"isReplaceCube\":\"true\",\"isRunNow\":\"true\",\"comment\":\"$comment\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/jobs/ledgerDeployCubeJob
	funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Started Deploying Cube successfully"
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}
funcLoadData() {
	dataLoadValue="ADD_EXISTING_VALUES"
	dataFileName="BksML12C.txt"
	param="{\"clearAllDataFlag\":\"false\",\"dataLoadValue\":\"$dataLoadValue\",\"dataFileName\":\"$dataFileName\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/jobs/essbaseDataLoadJob
	funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Started Loading Data successfully"
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}
funcRunCalculation() {
	subsetStart=""
	subsetEnd=""
	ruleName=""
	ruleSetName=""
	comment="Run Calculation Curl"
	exeType="ALL_RULES"
	stringDelimter="_"
	param="{\"isClearCalculated\":\"true\",\"isExecuteCalculations\":\"true\",\"isRunNow\":\"true\",\"comment\":\"$comment\",\"subsetStart\":\"$subsetStart\",\"subsetEnd\":\"$subsetEnd\",\"ruleName\":\"$ruleName\",\"ruleSetName\":\"$ruleSetName\",\"exeType\":\"$exeType\",\"stringDelimter\":\"$stringDelimter\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/povs/$POV_GROUP_MEMBER/jobs/runLedgerCalculationJob
	funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Started Running Calc successfully"
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}
funcRunRuleBalancing() {
	url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/povs/$POV_GROUP_MEMBER/ruleBalance
	funcExecuteRequest "GET" $url "application/x-www-form-urlencoded"
	list=`cat response.txt | jq 'select(.items != null) | .items[].name'`
	if [[ ! -z $list ]]; then
		echo $list
	else
		echo "No Items found"
	fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"

}

funcGeneratePrgDocReport() {
        url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/povs/$POV_GROUP_MEMBER1/programDocumentationReport
        echo $url
        curl -G "$url" --data-urlencode 'queryParameter={"fileType":"PDF","stringDelimter":"_","useAlias":"false"}' -u "$USERNAME:$PASSWORD" -o "response.txt" -D "respHeader.txt"
        output=`cat response.txt`
        status=`echo $output | jq '.status'`
        echo $status
    if [ $status == 0 ]; then
        echo "Program Documentation Report generated successfully"
        message=`echo $output | jq '.details'`
        echo $message
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
    funcRemoveTempFiles "respHeader.txt" "response.txt"

}


funcExportTemplate() {
	fileName=$APP_NAME+"_Template_Export_File"
	param="{\"fileName\":\"$fileName\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/jobs/templateExportJob
	funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Started Exporting successfully"
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}


funcExportQueryResult() {
	queryName="Profitability - Product";
	fileName=$APP_NAME+"_"+$queryName+"_Query_Result"
	param="{\"queryName\":\"$queryName\",\"fileName\":\"$fileName\",\"exportOnlyLevel0Flg\":\"false\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/jobs/exportQueryResultsJob
	funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Started Exporting successfully"
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}


funcCopyPOVData() {
	stringDelimter="_";
	destPovGroupMember="2014_December_Actual";
	param="{\"isManageRule\":\"true\",\"isInputData\":\"true\",\"modelViewName\":\"Balancing - 1 Operating Expenses\",\"createDestPOV\":\"true\",\"stringDelimter\":\"$stringDelimter\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/povs/$POV_GROUP_MEMBER/jobs/copyPOVJob/$destPovGroupMember
	funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Started Copying POV successfully"
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}

funcMergeSlices() {
	stringDelimter="_";
	param="{\"removeZeroCells\":\"true\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/jobs/mergeSlices
	funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Execution of mergeslices completed successfully."
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}

funcClearPOVData() {
	stringDelimter="_";
	param="{\"isManageRule\":\"true\",\"isInputData\":\"true\",\"isAdjustmentValues\":\"true\",\"isAllocatedValues\":\"true\",\"stringDelimter\":\"$stringDelimter\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/povs/$POV_GROUP_MEMBER/jobs/clearPOVJob
	funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Started Clearing POV successfully"
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}


funcCreateFlatFileApplication() {
	description="Flat file based application";
	ruleDimensionName="Rule"
	balanceDimensionName="Balance"
	param="{\"description\":\"$description\",\"ruleDimensionName\":\"$ruleDimensionName\",\"balanceDimensionName\":\"$balanceDimensionName\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/fileApplications/BksML13
	funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == 0 ]; then
        echo "Application created successfully"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}

funcUpdateDimensions() {
	dataFileName="Accounts.txt"
	param="{\"dataFileName\":\"$dataFileName\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/fileApplications/$APP_NAME/updateDimension
	funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == 0 ]; then
        echo "Dimensions updated successfully"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}

funcUpdateDimensionsJob() {
	dataFileName="Account.txt,Activity.txt"
	param="{\"dataFileName\":\"$dataFileName\"}"
	url=$SERVER_URL/epm/rest/$API_VERSION/fileApplications/$APP_NAME/jobs/updateDimension
        funcExecuteRequest "POST" $url "$param" "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Started Update Dimension(s) successfully"
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}

funcApplyDataGrants() {
	
	url=$SERVER_URL/epm/rest/$API_VERSION/applications/$APP_NAME/jobs/applyDataGrants
	funcExecuteRequest "POST" $url "application/json"
        
	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Started Data Grants successfully"
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
	
}


funcEnableApplication() {
	url=$SERVER_URL/epm/rest/$API_VERSION/fileApplications/$APP_NAME/enableApplication
	funcExecuteRequest "POST" $url "application/json"

	output=`cat response.txt`
	status=`echo $output | jq '.status'`
    if [ $status == -1 ]; then
        echo "Started Enabling Application successfully"
		funcGetStatus "GET"
    else
        error=`echo $output | jq '.details'`
        echo "Error occurred. " $error
    fi
	funcRemoveTempFiles "respHeader.txt" "response.txt"
}

echo "Start of script..."
funcGetRestAPIVersionInfo
funcGetRestAPIVersionsInfo
funcImportTemplate
funcDeployCube
funcApplyDataGrants
funcLoadData
funcRunCalculation
funcRunRuleBalancing
funcExportTemplate
funcExportQueryResult
funcCopyPOVData
funcClearPOVData
funcCreateFlatFileApplication
funcUpdateDimensions
funcUpdateDimensionsJob
funcEnableApplication
funcGeneratePrgDocReport
funcMergeSlices
echo "End of script..."