Testing changes in Akamai staging environment

I manage the Akamai configuration for one of our customers. We get a lot of service requests from customers that want us to create or update URL redirects (managed as Edge Redirector policies).

The Akamai docs tell you to edit your hosts file to test new configuration versions that have been activated for the staging environment. That’s quite cumbersome so I opted to use curl instead. Once a redirect is configured and pushed to the staging environment I can streamline quality assurance with the following shell script given a URL and redirect target:

#!/bin/bash

help()
{
   echo "Run as: $(basename $0) <URL> [<ExpectedRedirectURL>]"
   echo
   echo "Examples:"
   echo "---------"
   echo "$(basename $0) https://www.uk.myapp.com/welcome-agility"
   echo "$(basename $0) https://www.uk.myapp.com/welcome-agility https://www.myapp.com/gb/en/finance-insurance/finance/overview"
   echo "$(basename $0) https://www.uk.myapp.com/contact-finance https://www.myapp.com/gb/en/contact-us"
   echo
   echo "References:"
   echo "-----------"
   echo "https://learn.akamai.com/en-us/webhelp/ion/web-performance-getting-started-for-http-properties/GUID-9A3C492F-E1CC-468C-8231-95C05DFA6F76.html#GUID-9A3C492F-E1CC-468C-8231-95C05DFA6F76"
   echo "https://for-the-user.tumblr.com/post/163421194700/akamai-staging-wah"
   echo
}

# display help if invoked without parameters
if [[ $# -eq 0 ]] ; then
    help
    exit 0
fi

URL=${1}
EXPECTED_REDIRECT_URL=${2}

# extract domain from URL
DOMAIN=$( echo ${URL} | awk -F'[/:]' '{print $4}' )

# resolve CNAME and trim trailing dot
CNAME=$( dig cname ${DOMAIN} +short | sed 's/\.$//' )

if [ -z "${CNAME}" ]; then
  echo "Domain [${DOMAIN}] does not have a CNAME" >&2
  exit 1
fi

# generate staging domain name
STAGING_CNAME=$( echo ${CNAME} | sed 's/.net$/-staging.net/' )

# query the staging system
echo "Querying: [${URL//${DOMAIN}/${STAGING_CNAME}}]"
RESULT=$( curl -I -s -k -H "Host: ${DOMAIN}" "${URL//${DOMAIN}/${STAGING_CNAME}}" )

if [ -n "${EXPECTED_REDIRECT_URL}" ]; then
  # extract redirect URL from curl output and remove non-printable characters
  ACTUAL_REDIRECT_URL=$( echo "${RESULT}" | awk '/Location/{print $2}' | tr -d '[:cntrl:]' )

  if [ "$ACTUAL_REDIRECT_URL" = "${EXPECTED_REDIRECT_URL}" ]; then
    echo "REDIRECT is correct: [${ACTUAL_REDIRECT_URL}]"
  else
    echo "REDIRECT is incorrect:" >&2
    echo "  expected: [${EXPECTED_REDIRECT_URL}]" >&2
    echo "       got: [${ACTUAL_REDIRECT_URL}]" >&2
    exit 1
  fi

else
  echo "${RESULT}"
fi