From 5e00467f95e3eff6a2aee1c9beb0bf3f8b01d5ac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicolas=20B=C3=A9jean?= <nicolas@bejean.fr>
Date: Tue, 14 Dec 2021 18:33:23 +0100
Subject: [PATCH] feat: Destroy PSC

---
 manifests/psc/destroy-psc.sh | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/manifests/psc/destroy-psc.sh b/manifests/psc/destroy-psc.sh
index aa22192..cf267a7 100644
--- a/manifests/psc/destroy-psc.sh
+++ b/manifests/psc/destroy-psc.sh
@@ -1,3 +1,31 @@
 #!/bin/bash
 
 echo "GCP_PSC_CONNECTION_ID=$(gcloud compute forwarding-rules describe $GCP_PSC_NAME --region=$GCP_REGION --format="get(pscConnectionId)")" > ${CI_PROJECT_DIR}/pscConnectionId.env
+
+gcloud dns managed-zones describe $GCP_DNS_ZONE_GKE_ELASTIC > /dev/null 2>&1
+DNS_ZONE_CREATED=$?
+
+if [[ $DNS_ZONE_CREATED == 0 ]]; then
+  gcloud dns record-sets delete "*.$EC_DNS_ZONE." --type="A" --zone="$GCP_DNS_ZONE_GKE_ELASTIC"
+  gcloud dns managed-zones delete $GCP_DNS_ZONE_GKE_ELASTIC
+else
+  echo 'DNS_ZONE_ALREADY_DELETED'
+fi
+
+
+FORWARDING_RULE_ACCEPTED=$(gcloud compute forwarding-rules describe $GCP_PSC_NAME --region=$GCP_REGION --format="get(pscConnectionStatus)")
+
+if [[ $FORWARDING_RULE_ACCEPTED == "ACCEPTED" ]]; then
+  gcloud compute forwarding-rules delete $GCP_PSC_NAME --region=$GCP_REGION --quiet
+else
+  echo 'FORWARDING_RULE_ALREADY_DELETED'
+fi
+
+
+ADDRESS_CREATED=$(gcloud compute addresses describe $GCP_INTERNAL_IP_NAME --region=$GCP_REGION --format="get(status)")
+
+if [[ $ADDRESS_CREATED == "RESERVED" ]]; then
+  gcloud compute addresses delete $GCP_INTERNAL_IP_NAME --region=$GCP_REGION --quiet
+else
+  echo 'ADDRESS_ALREADY_DELETED'
+fi
-- 
GitLab