Skip to content

Instantly share code, notes, and snippets.

@arbakker
Last active March 22, 2023 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arbakker/3b6661677a4d7f5783523892a7a73a40 to your computer and use it in GitHub Desktop.
Save arbakker/3b6661677a4d7f5783523892a7a73a40 to your computer and use it in GitHub Desktop.
CRUD CLI tool to update metadata with CSW - #geonetwork #bash #NGR #CSW #metadata
#!/usr/bin/env bash
PROGRAM_NAME=$(basename "$0")
function usage {
echo "Medata CRUD CLI tool for CSW services (Catalogue Service for the Web)"
echo ""
echo "usage: $PROGRAM_NAME [options]"
echo " - -o <operation>: allowed values: GET, DELETE, UPSERT"
echo " - -f <md-file-path>: path to metadata file, required when OPERATION=upsert"
echo " - -i <md-identifier>: metadata identifier, required when OPERATION=DELETE|GET"
exit 1
}
function record_in_response() {
response="$1"
if ! (xmlstarlet sel -N gmd="http://www.isotc211.org/2005/gmd" -N gco="http://www.isotc211.org/2005/gco" -t -c "//gmd:MD_Metadata" <<<"$response"); then
echo "false"
return
fi
echo "true"
return
}
OPERATION="GET"
MD_FILE=""
MD_ID=""
while getopts "o:i:f:" o; do
case "${o}" in
f)
MD_FILE="${OPTARG}"
;;
o)
OPERATION="${OPTARG}"
;;
i)
MD_ID="${OPTARG}"
;;
*)
usage
;;
esac
done
shift $((OPTIND - 1))
if test "$#" -gt 0; then
usage
fi
if [[ ($OPERATION == "GET" || $OPERATION == "DELETE") && -z $MD_ID ]]; then
echo "ERROR: option: \`-i <md-identifier>\` should be set when OPERATION=${OPERATION}"
exit 1
elif [[ $OPERATION == "UPSERT" && (-z $MD_FILE || ! -f $MD_FILE) ]]; then
echo "ERROR: option: \`-f <md-file-path>\` should be set and file should exist when OPERATION=${OPERATION}"
exit 1
fi
if [[ -z "${CATALOG}" ]]; then
echo "ERROR: required environment variables CATALOG not set" && exit 1
fi
if [[ -z "${CATALOGUSER}" ]]; then
echo "ERROR: required environment variables CATALOGUSER not set" && exit 1
fi
if [[ -z "${CATALOGPASS}" ]]; then
echo "ERROR: required environment variables CATALOGPASS not set" && exit 1
fi
# see https://www.geocat.net/docs/geonetwork-enterprise/latest/geonetwork/community/api/the-geonetwork-api.html#using-the-api-to-apply-an-xsl-process for
rm -f /tmp/cookie
curl -s -c /tmp/cookie -o /dev/null -X POST "$CATALOG/srv/eng/info?type=me"
TOKEN="$(grep XSRF-TOKEN /tmp/cookie | cut -f 7)"
# curl -X POST -H "X-XSRF-TOKEN: ${TOKEN}" --user "${CATALOGUSER}:${CATALOGPASS}" -b /tmp/cookie \
# "$CATALOG/srv/eng/info?type=me" # MUST return @authenticated = true
if [[ $OPERATION != "GET" ]]; then
if [[ $OPERATION == "UPSERT" ]]; then
MD_ID=$(xmlstarlet sel -N gmd="http://www.isotc211.org/2005/gmd" -N gco="http://www.isotc211.org/2005/gco" --text -t -v "//gmd:fileIdentifier/gco:CharacterString" "$MD_FILE")
MD_RECORD_CONTENT=$(sed '/<\?xml version/d' "$MD_FILE")
RESPONSE=$(curl -s "$CATALOG/srv/dut/csw?service=CSW&request=GetRecordById&version=2.0.2&outputSchema=http://www.isotc211.org/2005/gmd&elementSetName=full&id=${MD_ID}" --user "${CATALOGUSER}:${CATALOGPASS}" -H "X-XSRF-TOKEN: ${TOKEN}" -b /tmp/cookie) # auth not required for request
record_in_response=$(record_in_response "$RESPONSE")
OPERATION="Insert"
if [[ $record_in_response == "true" ]]; then
OPERATION="Update"
fi
BODY="<?xml version=\"1.0\" encoding=\"UTF-8\"?><csw:Transaction xmlns:csw=\"http://www.opengis.net/cat/csw/2.0.2\" service=\"CSW\" version=\"2.0.2\"><csw:${OPERATION}>${MD_RECORD_CONTENT}</csw:${OPERATION}></csw:Transaction>"
elif [[ $OPERATION == "DELETE" ]]; then
BODY="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<csw:Transaction xmlns:csw=\"http://www.opengis.net/cat/csw/2.0.2\" xmlns:ogc=\"http://www.opengis.net/ogc\" version=\"2.0.2\" service=\"CSW\">
<csw:Delete>
<csw:Constraint version=\"1.1.0\">
<ogc:Filter>
<ogc:PropertyIsEqualTo>
<ogc:PropertyName>dc:identifier</ogc:PropertyName>
<ogc:Literal>${MD_ID}</ogc:Literal>
</ogc:PropertyIsEqualTo>
</ogc:Filter>
</csw:Constraint>
</csw:Delete>
</csw:Transaction>"
fi
curl -s -X POST "$CATALOG/srv/dut/csw-publication" --data "$BODY" -H "Content-Type: application/xml" --user "${CATALOGUSER}:${CATALOGPASS}" -H "X-XSRF-TOKEN: ${TOKEN}" -b /tmp/cookie
else
record_url="$CATALOG/srv/dut/csw?service=CSW&request=GetRecordById&version=2.0.2&outputSchema=http://www.isotc211.org/2005/gmd&elementSetName=full&ID=${MD_ID}"
response=$(curl -s -H "Content-Type: application/xml" --user "${CATALOGUSER}:${CATALOGPASS}" -H "X-XSRF-TOKEN: ${TOKEN}" -b /tmp/cookie "$record_url")
# pipe through xmlstarlet to remove GetRecordByIdResponse element
record_in_response=$(record_in_response "$response")
if [[ $record_in_response == "false" ]]; then
echo "ERROR: could not find metadata record in catalog: ${record_url}"
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment