Sunday, March 19, 2017

Bash script to call rest endpoint for each id in text file

I needed to write an admin utility script to delete documents from a content management system.  First, the list of document ids were retrieved from the application database; Second, the list of ids were used as input to a bash script to make REST calls to delete the documents.

Environment:
MySql 5.7.10
RHEL 7

STEPS:
  1. Retrieve list of ids from the database and save to uuids.txt, one id on each line. The ids were alfresco cmis unique ids (with unique nodeRef id, ;,  and version number), they look like "workspace://SpacesStore/aa3e765f-c628-44c2-bc66-17d622ca2210;1.0".  I needed documents created after a certain date, and only the unique id portion after the "SpacesStore/" and prior to the ";1.0" of the unique id.  Below is the MySQL select statement:

    select substring_index(substring_index(alf_node_id, 'SpacesStore/', -1),
                           ';1.0', 1), created_dt from cherryshoe_documents where created_dt >= '2017-03-18 00:00:00' order by created_dt desc;

  2. The bash script is below, it incorporates a curl call to a REST DELETE endpoint with basic authentication.

    delete.sh
    
    #!/usr/bin/bash
    # Calls a repository rest endpoint to delete node(s).
    # Takes in an input text file of one id per line.
    
    username=admin
    password=admin
    protocol=http
    #hostname can contain port
    hostname="localhost:8080"
    
    echo username=$username, password=$password, protocol=$protocol, hostname=$hostname
    
    if (( "$#" != 1 ))
    then
        echo "Usage Info: Enter in filename"
    exit 1
    fi
    
    filename="$1"
    
    echo "Starting Node Delete..."
    while read -r line
    do
        uuid="$line"
        endpoint=$protocol://$hostname/alfresco/s/api/node/workspace/SpacesStore/$uuid
        # contains a \r (CR) at the end (0d). Remove it with
        endpoint=${endpoint%$'\r'}
        echo $endpoint
    
        echo "#######START $uuid"
        curl -u $username:$password -i -X DELETE $endpoint
        echo "#######END $uuid"
    
    done < "$filename"
    
    echo "DONE"
    

  3. Sample uuids.txt file below:
    32231a66-649a-4cee-9a16-2c45b639fd94
    08b53382-e120-4100-a093-32b2a57b9bad
    6759904f-48bf-4e02-93e0-6ee9f9885df7
    9931f0e8-1b48-4c3c-a885-94ccf80866ea
    aedef20e-6831-4fdc-ad21-872af333d985

No comments:

Post a Comment

I appreciate your time in leaving a comment!