#!/bin/bash
#
# This script is an "out of the box" script that goes through
# Login and configure the enclosure escape character using PUT /file-metadata API
# with the authentication token from Login.
# User need to set DOUBLE_ENCLOSURE, CUSTOM_ENCLOSURE_ESCAPE_CHARACTER and RULESET_ID accordingly
#
source apiHostInfo
eval $(cat loginCredentials)
source helpers
login
# Set DOUBLE_ENCLOSURE=true if you want to set enclosure escape character same as enclosure character,
# and if DOUBLE_ENCLOSURE=true then CUSTOM_ENCLOSURE_ESCAPE_CHARACTER value will be ignored.
DOUBLE_ENCLOSURE=true
# Replace * with your custom escape character if you want to set custom enclosure escape character
# and also DOUBLE_ENCLOSURE=false need to set
CUSTOM_ENCLOSURE_ESCAPE_CHARACTER="\"*\""
# Comment this RULESET_ID if you want to update for all delimited file ruleset for which enclosure is defined.
#RULESET_ID=1
echo "Calling GET /file-metadata API"
if [[ -z "$RULESET_ID" ]] || [ "$RULESET_ID" = "null" ] || [ "$RULESET_ID" = "" ]; then
FILE_METADATA_RESPONSE=$(curl $SSL_CERT -X GET -H ''"$AUTH_HEADER"'' -H 'Accept: application/json' ''"$MASKING_ENGINE/file-metadata"'')
else
FILE_METADATA_RESPONSE=$(curl $SSL_CERT -X GET -H ''"$AUTH_HEADER"'' -H 'Accept: application/json' ''"$MASKING_ENGINE/file-metadata?ruleset_id=$RULESET_ID"'')
fi
i=0
while true; do
ENCLOSURE=$(jq '.responseList['$i'] .enclosure' <<<"$FILE_METADATA_RESPONSE")
if [ "$DOUBLE_ENCLOSURE" = true ]; then
CUSTOM_ENCLOSURE_ESCAPE_CHARACTER=$ENCLOSURE
fi
UPDATED_FILE_METADATA_RESPONSE=$(jq '.responseList['$i'] .enclosureEscapeCharacter='$CUSTOM_ENCLOSURE_ESCAPE_CHARACTER'' <<<"$FILE_METADATA_RESPONSE")
FILE_METADATA_RESPONSE=$UPDATED_FILE_METADATA_RESPONSE
FILE_METADATA_OBJECT=$(jq '.responseList['$i']' <<<"$FILE_METADATA_RESPONSE")
FILE_METADATA_ID=$(jq '.responseList['$i'] .fileMetadataId' <<<"$FILE_METADATA_RESPONSE")
if [[ -z "$FILE_METADATA_ID" ]] || [ "$FILE_METADATA_ID" = "null" ]; then
break
else
if [[ ! -z "$ENCLOSURE" ]] && [ ! "$ENCLOSURE" = "null" ] && [ ! "$ENCLOSURE" = "" ]; then
echo "Calling $MASKING_ENGINE/file-metadata/$FILE_METADATA_ID API to update enclosure escape character=$CUSTOM_ENCLOSURE_ESCAPE_CHARACTER"
UPDATE_RESPONSE=$(curl $SSL_CERT -X PUT -H ''"$AUTH_HEADER"'' -H 'Content-Type: application/json' -H 'Accept: application/json' -d ''"$FILE_METADATA_OBJECT"'' ''"$MASKING_ENGINE/file-metadata/$FILE_METADATA_ID"'')
check_error "$UPDATE_RESPONSE"
fi
fi
((i++))
done
echo