|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# generic script for removing proxygen deployed apis where the pull request is closed |
| 4 | + |
| 5 | +# set the repo name to be the name of the repo this is running in |
| 6 | +REPO_NAME=prescriptionsforpatients |
| 7 | + |
| 8 | +# this should be customised to delete relevant proxygen deployments if they are used |
| 9 | +main() { |
| 10 | + echo "Checking prescriptions for patients deployments" |
| 11 | + PULL_REQUEST_PROXYGEN_REGEX="prescriptions-for-patients-v2-pr-" |
| 12 | + delete_apigee_deployments "internal-dev" "prescriptions-for-patients-v2" "PrescriptionsForPatientsProxygenPrivateKey" "2026-01-22-PROD-prescriptions-for-patients-v2" |
| 13 | + delete_apigee_deployments "internal-dev-sandbox" "prescriptions-for-patients-v2" "PrescriptionsForPatientsProxygenPrivateKey" "2026-01-22-PROD-prescriptions-for-patients-v2" |
| 14 | +} |
| 15 | + |
| 16 | +delete_apigee_deployments() { |
| 17 | + APIGEE_ENVIRONMENT=$1 |
| 18 | + APIGEE_API=$2 |
| 19 | + PROXYGEN_PRIVATE_KEY_NAME=$3 |
| 20 | + PROXYGEN_KID=$4 |
| 21 | + proxygen_private_key_arn=$(aws cloudformation list-exports --query "Exports[?Name=='secrets:${PROXYGEN_PRIVATE_KEY_NAME}'].Value" --output text) |
| 22 | + |
| 23 | + echo |
| 24 | + echo "checking apigee deployments on ${APIGEE_ENVIRONMENT}" |
| 25 | + echo |
| 26 | + |
| 27 | + jq -n --arg apiName "${APIGEE_API}" \ |
| 28 | + --arg environment "${APIGEE_ENVIRONMENT}" \ |
| 29 | + --arg kid "${PROXYGEN_KID}" \ |
| 30 | + --arg proxygenSecretName "${proxygen_private_key_arn}" \ |
| 31 | + '{apiName: $apiName, environment: $environment, kid, $kid, proxygenSecretName: $proxygenSecretName}' > payload.json |
| 32 | + |
| 33 | + aws lambda invoke --function-name "lambda-resources-ProxygenPTLInstanceGet" --cli-binary-format raw-in-base64-out --payload file://payload.json out.json > response.json |
| 34 | + |
| 35 | + if eval "cat response.json | jq -e '.FunctionError' >/dev/null"; then |
| 36 | + echo 'Error calling lambda' |
| 37 | + cat out.json |
| 38 | + exit 1 |
| 39 | + fi |
| 40 | + |
| 41 | + jq -r '.[].name' "out.json" | while read -r i; do |
| 42 | + echo "Checking if apigee deployment $i has open pull request" |
| 43 | + PULL_REQUEST=${i//${PULL_REQUEST_PROXYGEN_REGEX}/} |
| 44 | + echo "Checking pull request id ${PULL_REQUEST}" |
| 45 | + URL="https://api.github.com/repos/NHSDigital/${REPO_NAME}/pulls/${PULL_REQUEST}" |
| 46 | + RESPONSE=$(curl "${URL}" -H "Authorization: token ${GITHUB_TOKEN}" 2>/dev/null) |
| 47 | + STATE=$(echo "${RESPONSE}" | jq -r .state) |
| 48 | + if [ "$STATE" == "closed" ]; then |
| 49 | + echo "** going to delete apigee deployment $i as state is ${STATE} **" |
| 50 | + jq -n --arg apiName "${APIGEE_API}" \ |
| 51 | + --arg environment "${APIGEE_ENVIRONMENT}" \ |
| 52 | + --arg instance "${i}" \ |
| 53 | + --arg kid "${PROXYGEN_KID}" \ |
| 54 | + --arg proxygenSecretName "${proxygen_private_key_arn}" \ |
| 55 | + '{apiName: $apiName, environment: $environment, kid, $kid, proxygenSecretName: $proxygenSecretName, instance: $instance}' > payload.json |
| 56 | + |
| 57 | + aws lambda invoke --function-name "lambda-resources-ProxygenPTLInstanceDelete" --cli-binary-format raw-in-base64-out --payload file://payload.json out.txt > response.json |
| 58 | + if eval "cat response.json | jq -e '.FunctionError' >/dev/null"; then |
| 59 | + echo 'Error calling lambda' |
| 60 | + cat out.txt |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + |
| 64 | + |
| 65 | + else |
| 66 | + echo "not going to delete apigee deployment $i as state is ${STATE}" |
| 67 | + fi |
| 68 | + done |
| 69 | +} |
| 70 | + |
| 71 | +main |
0 commit comments