From f69718a20dc691d91984aecae273d02b670de20a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nicolas=20B=C3=A9jean?= <nicolas@bejean.fr>
Date: Sun, 12 Dec 2021 21:16:05 +0100
Subject: [PATCH] Create Bash script for PSC

---
 .gitlab-ci.yml               | 16 ++++------------
 manifests/psc/setting-psc.sh | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+), 12 deletions(-)
 create mode 100644 manifests/psc/setting-psc.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index c0c3d97..53aefa3 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -5,6 +5,7 @@ image: busybox:latest
 variables:
   TF_ROOT_ELASTIC: ${CI_PROJECT_DIR}/manifests/elastic
   TF_ROOT_MAGENTO: ${CI_PROJECT_DIR}/manifests/magento
+  GCLOUD_ROOT_PSC: ${CI_PROJECT_DIR}/manifests/psc
 
 cache:
   - key: elastic
@@ -135,15 +136,6 @@ gcloud-psc:
     echo "Activate Service Account: OK"
     gcloud config set project $GCP_PROJECT_ID
     echo "Set Project: OK"
-    echo " "
-    echo "Private IP Google Access is enabled on subnet: $(gcloud compute networks subnets describe $GCP_SUBNETWORK_NAME --region=$GCP_REGION --format="get(privateIpGoogleAccess)")"
-    gcloud compute addresses create $GCP_INTERNAL_IP_NAME --region=$GCP_REGION --subnet=$GCP_SUBNETWORK_NAME
-    echo "Internal IP: $(gcloud compute addresses describe $GCP_INTERNAL_IP_NAME --region=$GCP_REGION --format="get(address)")"
-    echo " "
-    gcloud compute forwarding-rules create $GCP_PSC_NAME --region=$GCP_REGION --network=$GCP_NETWORK_NAME --address=$GCP_INTERNAL_IP_NAME --target-service-attachment=$ELASTIC_SERVICE_ATTACHMENT_URI
-    echo "Is connection accepted? $(gcloud compute forwarding-rules describe $GCP_PSC_NAME --region=$GCP_REGION --format="get(pscConnectionStatus)")"
-    echo " "
-    gcloud dns managed-zones create $GCP_DNS_ZONE_MAGENTO_ELASTIC --description="Managed by GitLab CI" --dns-name=$ELASTIC_DNS_ZONE. --visibility=private --networks=$GCP_NETWORK_NAME
-    gcloud dns record-sets transaction start --zone="$GCP_DNS_ZONE_MAGENTO_ELASTIC"
-    gcloud dns record-sets transaction add $(gcloud compute addresses describe $GCP_INTERNAL_IP_NAME --region=$GCP_REGION --format="get(address)") --name="*.$ELASTIC_DNS_ZONE." --ttl="30" --type="A" --zone="$GCP_DNS_ZONE_MAGENTO_ELASTIC"
-    gcloud dns record-sets transaction execute --zone="$GCP_DNS_ZONE_MAGENTO_ELASTIC"
+    cd $GCLOUD_ROOT_PSC
+    chmod +x ./setting-psc.sh
+    ./setting-psc.sh
diff --git a/manifests/psc/setting-psc.sh b/manifests/psc/setting-psc.sh
new file mode 100644
index 0000000..cc0686e
--- /dev/null
+++ b/manifests/psc/setting-psc.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+
+PRIVATE_IP_ENABLED=$(gcloud compute networks subnets describe $GCP_SUBNETWORK_NAME --region=$GCP_REGION --format="get(privateIpGoogleAccess)")
+
+if [[ $PRIVATE_IP_ENABLED == "True" ]]; then
+  echo 'PRIVATE_IP_ENABLED'
+else
+  gcloud compute networks subnets update $GCP_SUBNETWORK_NAME --region=$GCP_REGION --enable-private-ip-google-access
+fi
+
+
+ADDRESS_CREATED=$(gcloud compute addresses describe $GCP_INTERNAL_IP_NAME --region=$GCP_REGION --format="get(status)")
+
+if [[ $ADDRESS_CREATED == "IN_USE" ]]; then
+  echo 'ADDRESS_CREATED'
+else
+  gcloud compute addresses create $GCP_INTERNAL_IP_NAME --region=$GCP_REGION --subnet=$GCP_SUBNETWORK_NAME
+fi
+
+
+FORWARDING_RULE_ACCEPTED=$(gcloud compute forwarding-rules describe $GCP_PSC_NAME --region=$GCP_REGION --format="get(pscConnectionStatus)")
+
+if [[ $FORWARDING_RULE_ACCEPTED == "ACCEPTED" ]]; then
+  echo 'FORWARDING_RULE_ACCEPTED'
+else
+  gcloud compute forwarding-rules create $GCP_PSC_NAME --region=$GCP_REGION --network=$GCP_NETWORK_NAME --address=$GCP_INTERNAL_IP_NAME --target-service-attachment=$ELASTIC_SERVICE_ATTACHMENT_URI
+fi
+
+#DNS_ZONE_CREATED=$(gcloud)
+
+gcloud dns managed-zones create $GCP_DNS_ZONE_MAGENTO_ELASTIC --description="Managed by GitLab CI" --dns-name=$ELASTIC_DNS_ZONE. --visibility=private --networks=$GCP_NETWORK_NAME
+gcloud dns record-sets transaction start --zone="$GCP_DNS_ZONE_MAGENTO_ELASTIC"
+gcloud dns record-sets transaction add $(gcloud compute addresses describe $GCP_INTERNAL_IP_NAME --region=$GCP_REGION --format="get(address)") --name="*.$ELASTIC_DNS_ZONE." --ttl="30" --type="A" --zone="$GCP_DNS_ZONE_MAGENTO_ELASTIC"
+gcloud dns record-sets transaction execute --zone="$GCP_DNS_ZONE_MAGENTO_ELASTIC"
-- 
GitLab