Skip to content

Instantly share code, notes, and snippets.

@AlexAtkinson
Last active May 25, 2023 20:28
Show Gist options
  • Save AlexAtkinson/08703f759dd6f1b1c1ba146efb082395 to your computer and use it in GitHub Desktop.
Save AlexAtkinson/08703f759dd6f1b1c1ba146efb082395 to your computer and use it in GitHub Desktop.
Some Kong MGMT Scripts.

Kong MGMT Scripts

No fancy flags, help, etc. Pure utility.

Generate Kong Report

#!/usr/bin/env bash
# generateKongReport.sh

host="${1}"
port="${2}"

if [[ $# != 2 ]]; then
  echo -e "\nERROR: This tool accepts exactly two args - hostname, port, in that order.\n"
fi

services=()
services=($(curl -s ${host}:${port}/services/ | jq -r '.data[].name'))

echo ''
txt='Service'; printf "$txt"; printf '%*s' "$((${COLUMNS}-$((${COLUMNS}-$(wc -c<<<$txt)+20))))"
printf "ServiceId                               RouteId\n"
txt='-------'; printf -- '-------'; printf '%*s' "$((${COLUMNS}-$((${COLUMNS}-$(wc -c<<<$txt)+20))))"
printf -- '---------                               -------\n'

for service in ${services[*]}; do

  serviceId=$(./getKongServiceId.sh ${host} ${port} ${service})
  routeId=$(./getKongRouteId.sh ${host} ${port} ${service})

  printf "${service}"; printf '%*s' "$((${COLUMNS}-$((${COLUMNS}-$(wc -c<<<$service)+20))))" ; printf "${serviceId}    ${routeId}\n"

done
echo ''

Get Kong Service ID

#!/usr/bin/env bash
# getKongServiceId.sh

host="${1}"
port="${2}"
service="${3}"

if [[ $# != 3 ]]; then
  echo -e "\nERROR: This tool accepts exactly three args - hostname, port, and the name of the service for which you would like the ServiceID for, in that order.\n"
  if [[ ${host} == 'localhost' ]] ; then
    port='8001'
    echo -e "Service List:"
    echo    "-------------"
    curl -s ${host}:${port}/services/ | jq -r '.data[].name'
    echo ''
  fi
  exit
fi

serviceId=$(curl -s ${host}:${port}/services/ | jq -r ".data[] | select (.name | startswith(\"${service}\")) | .id")
echo ${serviceId}

Get Kong Route ID

#!/usr/bin/env bash
# getKongRouteId.sh

host="${1}"
port="${2}"
service="${3}"

if [[ $# != 3 ]]; then
  echo -e "\nERROR: This tool accepts exactly three args - hostname, port, and the name of the service for which you would like the RouteID for.\n"
  if [[ ${host} == 'localhost' ]] ; then
    port='8001'
    echo -e "Service List:"
    echo    "-------------"
    curl -s ${host}:${port}/services/ | jq -r '.data[].name'
    echo ''
  fi
  exit
fi

routeId=$(curl -s ${host}:${port}/routes/ | jq -r ".data[] | select (.service.id | startswith(\"$(./getKongServiceId.sh ${host} ${port} ${service})\")) | .id")
echo ${routeId}

Patch Routes

#!/usr/bin/env bash
# patchRoutes.sh

host="${1}"
port="${2}"
routesFile="${3}"
service="${4}"

if [[ $# != 4 ]]; then
  echo -e "\nERROR: This tool accepts exactly four args - hostname, port, the path to the routes.json file, and the name of the service to patch, in that order.\n"
  echo -e "    Example:"
  echo -e "        ./patchRoutes.sh konghost 8001 conf/20190413Routes/<serviceName>_routes.json <serviceName>\n"
  if [[ ${host} == 'localhost' ]] ; then
    echo -e "Service List:"
    echo    "-------------"
    curl -s ${host}:${port}/services/ | jq -r '.data[].name'
    echo ''
  fi
  exit
fi

routeId=$(./getKongRouteId.sh ${host} ${port} ${service})

echo -e "\nEXECUTING: curl -i -X PATCH --url http://${host}:${port}/routes/${routeId} --header 'Content-Type: application/json' -d @${routesFile}\n"
curl -i -X PATCH --url http://${host}:${port}/routes/${routeId} --header 'Content-Type: application/json' -d @${routesFile}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment