Skip to content

Configure enclosure escape character

#!/bin/bash

#
# This script uses the Masking Engine APIs to configure the enclosure escape character feature.
# The script uses the /login API to obtain an authentication token and then uses the PUT /file-metadata API.
#
# To use this script, you must set DOUBLE_ENCLOSURE, CUSTOM_ENCLOSURE_ESCAPE_CHARACTER, ESCAPE_ENCLOSURE_ESCAPE_CHARACTER and RULESET_ID accordingly
#

source ./apiHostInfo.bash
eval $(cat ./loginCredentials.bash)
source ./helpers.bash

helpFunction() {
    echo ""
    echo "Usage: $0 -h HELP -d DOUBLE_ENCLOSURE -c CUSTOM_ENCLOSURE_ESCAPE_CHARACTER -e ESCAPE_ENCLOSURE_ESCAPE_CHARACTER -r RULESET_ID"
    echo -e "\t-h Show the usage of the script"
    echo -e "\t-d Set the value for DOUBLE_ENCLOSURE"
    echo -e "\t-c Set the value for CUSTOM_ENCLOSURE_ESCAPE_CHARACTER"
    echo -e "\t-e Set the value for ESCAPE_ENCLOSURE_ESCAPE_CHARACTER"
    echo -e "\t-r Set the value for RULESET_ID"
    echo -e "\n\tAdditional Note:"
    echo -e "\t1: The default value for parameter D=true, no need to set the value if you want to set enclosure escape character same as enclosure character."
    echo -e "\t2: If parameter D=true then custom enclosure escape character value will be ignored."
    echo -e "\t3: The default value for parameter E=false, change accordingly as per the requirement."
    echo -e "\t4: If parameter R is blank, it means changes will be applicable for all rulesets. Pass the R={RULESET_ID} if you want to update the settings only for the given ruleset. Example R=1"
    exit 1 # Exit script after printing help
}

# 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="*"
# Modify ESCAPE_ENCLOSURE_ESCAPE_CHARACTER value accordingly.
ESCAPE_ENCLOSURE_ESCAPE_CHARACTER=false
# Comment this RULESET_ID if you want to update for all delimited file ruleset for which enclosure is defined.
#RULESET_ID=1

while getopts "h:d:c:e:r:" opt; do
    case "$opt" in
    h) helpFunction exit ;;
    d) DOUBLE_ENCLOSURE="$OPTARG" ;;
    c) CUSTOM_ENCLOSURE_ESCAPE_CHARACTER="$OPTARG" ;;
    e) ESCAPE_ENCLOSURE_ESCAPE_CHARACTER="$OPTARG" ;;
    r) RULESET_ID="$OPTARG" ;;
    ?) helpFunction ;; # Print helpFunction in case parameter is non-existent
    esac
done

# Print helpFunction in case parameters are empty
if [ -z "$DOUBLE_ENCLOSURE" ] || [ -z "$CUSTOM_ENCLOSURE_ESCAPE_CHARACTER" ] || [ -z "$ESCAPE_ENCLOSURE_ESCAPE_CHARACTER" ]; then
    echo "Some or all of the parameters are empty"
    helpFunction
fi

login

echo "Calling GET /file-metadata API"
if [[ -z "$RULESET_ID" ]] || [ "$RULESET_ID" = "null" ] || [ "$RULESET_ID" = "" ]; then
    echo "Configuring the enclosure escape character feature for all File Ruleset."
    FILE_METADATA_RESPONSE=$(curl $SSL_CERT -X GET -H ''"$AUTH_HEADER"'' -H 'Accept: application/json' ''"$MASKING_ENGINE/file-metadata"'')
else
    echo "Configuring the enclosure escape character feature for File Ruleset(RULESET_ID=$RULESET_ID)"
    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")
    UPDATED_FILE_METADATA_RESPONSE=$(jq '.responseList['$i'] .escapeEnclosureEscapeCharacter='$ESCAPE_ENCLOSURE_ESCAPE_CHARACTER'' <<<"$UPDATED_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 enclosureEscapeCharacter=$CUSTOM_ENCLOSURE_ESCAPE_CHARACTER and escapeEnclosureEscapeCharacter=$ESCAPE_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