#!/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
