Skip to content

Instantly share code, notes, and snippets.

@dangtrinhnt
Created May 3, 2020 15:19
Show Gist options
  • Save dangtrinhnt/2f75490f46bf2425246a29832bf99871 to your computer and use it in GitHub Desktop.
Save dangtrinhnt/2f75490f46bf2425246a29832bf99871 to your computer and use it in GitHub Desktop.
Delete all Kong targets
#! /bin/bash
# Run this script inside a bastion instance
# $1: kong address and administration port, e.g., kong.dangtrinh.com:8001
KONG_URL=$1
# get all hosts of the services
latest_hosts=( $(curl ${KONG_URL}/services | jq -r '.["data"][] | .host') )
# list all upstream names
all_upstreams=( $(curl ${KONG_URL}/upstreams | jq -r '.["data"][] | .name') )
for upstream in ${all_upstreams[@]}
do
if [[ ! " ${latest_hosts[@]} " =~ " ${upstream} " ]]; then
echo "-Upstream ${upstream} is not the latest one."
targets=( $(curl ${KONG_URL}/upstreams/${upstream}/targets/all/ | jq -r '.["data"][] | .id') )
for target in ${targets[@]}
do
echo "Deleting target $target of upstream $upstream"
curl -X DELETE ${KONG_URL}/upstreams/${upstream}/targets/${target}
done
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment