Skip to main content

Register Multiple Components Together

Using the Catalog API

Another effective approach is leveraging the catalog API. By running a custom script as shown in the example below, you can automate the registration of components, providing a more programmatic method for bulk registrations.

#!/bin/bash

set -e

function usage {
echo "usage: $0 [-a accountIdentifier] [-x xApiKey] [-u bearerAuthorization] [-l catalogLocations]"
echo " -a Harness Account Identifier"
echo " -x Harness X-API-KEY for the given account"
echo " -u Harness Bearer Authorization from the logged in session"
echo " -l Catalog locations to be registered in Harness IDP. Comma seperated list of locations"
exit 1
}

while getopts a:x:u:l:h flag
do
case "${flag}" in
a) ACCOUNT_IDENTIFIER=${OPTARG};;
x) X_API_KEY=${OPTARG};;
u) BEARER_AUTHORIZATION=${OPTARG};;
l) CATALOG_LOCATIONS=${OPTARG};;
h | ?) usage
esac
done

echo -e "\nStarting catalog location registration for given location in Harness IDP...";

CATALOG_LOCATION_REGISTER_DATA='{"type":"url","target":"CATALOG_LOCATION_TARGET"}'

for LOCATION in ${CATALOG_LOCATIONS//,/ }
do
echo -e "\n--------"
echo "Registering $LOCATION catalog location in Harness IDP account $ACCOUNT_IDENTIFIER"

POST_DATA=${CATALOG_LOCATION_REGISTER_DATA/CATALOG_LOCATION_TARGET/$LOCATION}

RESULT_HTTP_CODE=$(curl --write-out %{http_code} -s --output /dev/null -H "Content-Type: application/json" -H "Harness-Account: ${ACCOUNT_IDENTIFIER}" -H "x-api-key: ${X_API_KEY}" -H "Authorization: Bearer ${BEARER_AUTHORIZATION}" -X POST --data "${POST_DATA}" "https://idp.harness.io/${ACCOUNT_IDENTIFIER}/idp/api/catalog/locations")

if [[ "$RESULT_HTTP_CODE" -ne 201 ]] ; then
echo "Failed registering $LOCATION catalog location in Harness IDP account $ACCOUNT_IDENTIFIER"
else
echo "Successfully registerd $LOCATION catalog location in Harness IDP account $ACCOUNT_IDENTIFIER"
fi

echo "--------"
done

echo -e "\nCompleted catalog location registration for given location in Harness IDP...";