google.cloud/scripts/cleanup-project.sh
Yusuke Tsutsumi ebf095d22d tests: fix gcp_compute_vpn_tunnel
- vpn_gateway required minimal changes (stop using legacy load
  balancers)
- compute_vpn_tunnel requires a valid vpn configuration
  (several dependent forwarding rules and configuration to set up a
  proper tunnel).
2022-11-19 18:40:44 -08:00

50 lines
No EOL
1.9 KiB
Bash
Executable file

#!/usr/bin/env bash
# cleanup-project cleans up an ansible testing project
#
# WARNING: do not run tests against a project while
# this is running, or else your tests will fail.
#
# dependencies:
# - google-cloud-sdk (gcloudgcloud )
set -e
PROJECT_ID="${1}"
SERVICE_ACCOUNT_NAME="${2}"
ZONE="us-central1-a"
main() {
# note: the ordering here is deliberate, to start with
# leaf resources and work upwards to parent resources.
cleanup_resource_per_region "compute vpn-tunnels"
cleanup_resource "compute instances" "" "--zone=$ZONE"
cleanup_resource_per_region "compute addresses"
cleanup_resource "compute target-http-proxies" "" "--global"
cleanup_resource "compute forwarding-rules" "--global" "--global"
cleanup_resource "compute forwarding-rules" \
"--regions=us-central1" "--region=us-central1"
cleanup_resource "compute url-maps" "--global" "--global"
cleanup_resource "compute url-maps" \
"--regions=us-central1" "--region=us-central1"
cleanup_resource "compute backend-services" "--global" "--global"
cleanup_resource "compute backend-services" \
"--regions=us-central1" "--region=us-central1"
}
cleanup_resource() {
resource_group="$1"
extra_list_args="$2"
extra_delete_args="$3"
for resource in $(gcloud $resource_group list --project="${PROJECT_ID}" --format="csv[no-heading](name)" $extra_list_args); do
gcloud $resource_group delete "${resource}" --project="${PROJECT_ID}" -q $extra_delete_args
done
}
cleanup_resource_per_region() {
resource_group="$1"
for resource_and_region in $(gcloud $resource_group list --project="${PROJECT_ID}" --format="csv[no-heading](name,region)"); do
read -r resource region < <(echo "$resource_and_region" | tr "," " ")
gcloud $resource_group delete "${resource}" --project="${PROJECT_ID}" -q --region="${region}"
done
}
main