Skip to content

Instantly share code, notes, and snippets.

@debedb
Created March 21, 2023 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debedb/d32ed7d5a5baa0ce402701c1ee979408 to your computer and use it in GitHub Desktop.
Save debedb/d32ed7d5a5baa0ce402701c1ee979408 to your computer and use it in GitHub Desktop.
Primitive resource gathering from GCP
#!/bin/bash
# Parse the output and generate a Deployment Manager configuration file
echo "resources:" > config0.yaml
echo "outputs:" > outputs0.yaml
# List all Cloud Run services and their attributes
services=$(gcloud run services list --format="csv(NAME, REGION, IMAGE)" --quiet | tail -n+2)
while read -r line; do
# Extract the service name, region, and image URL from the CSV output
name=$(echo "$line" | cut -d ',' -f 1)
region=$(echo "$line" | cut -d ',' -f 2)
image=$(echo "$line" | cut -d ',' -f 3)
# Generate a resource configuration for each service
echo "- name: ${name}_cloudrun" >> config0.yaml
echo " type: gcp-types/run-v1:projects.locations.services" >> config0.yaml
echo " properties:" >> config0.yaml
echo " location: $region" >> config0.yaml
echo " serviceId: $name" >> config0.yaml
echo " template:" >> config0.yaml
echo " metadata:" >> config0.yaml
echo " name: $name" >> config0.yaml
echo " annotations:" >> config0.yaml
echo " run.googleapis.com/ingress: all" >> config0.yaml
echo " spec:" >> config0.yaml
echo " containers:" >> config0.yaml
echo " - image: $image" >> config0.yaml
# Generate an output variable for each service
echo "- name: $name-url" >> outputs0.yaml
echo " value: \$(ref.$name.status.address.url)" >> outputs0.yaml
done <<< "$services"
# List all HTTP(S) load balancers and their attributes
load_balancers=$(gcloud compute target-https-proxies list --format="csv(NAME,URL_MAP,REGION)" --quiet | tail -n+2)
while read -r line; do
# Extract the load balancer name, URL map, and region from the CSV output
name=$(echo "$line" | cut -d ',' -f 1)
url_map=$(echo "$line" | cut -d ',' -f 2)
region=$(echo "$line" | cut -d ',' -f 3)
# Generate a resource configuration for each load balancer
echo "- name: ${name}_lb" >> config0.yaml
echo " type: compute.v1.targetHttpsProxy" >> config0.yaml
echo " properties:" >> config0.yaml
echo " urlMap: $url_map" >> config0.yaml
echo " region: $region" >> config0.yaml
# Generate an output variable for each load balancer's IP address
echo "- name: $name-ip" >> outputs0.yaml
echo " value: \$(ref.$name.ipAddress)" >> outputs0.yaml
done <<< "$load_balancers"
# List all Cloud SQL instances and their attributes
sql_instances=$(gcloud sql instances list --format="csv(NAME,REGION,DATABASE_VERSION,SETTINGS.tier)" --quiet | tail -n+2)
# Parse the output and generate a Deployment Manager configuration file
while read -r line; do
# Extract the Cloud SQL instance name, region, version, and tier from the CSV output
name=$(echo "$line" | cut -d ',' -f 1)
region=$(echo "$line" | cut -d ',' -f 2)
version=$(echo "$line" | cut -d ',' -f 3)
tier=$(echo "$line" | cut -d ',' -f 4)
# Generate a resource configuration for each Cloud SQL instance
echo "- name: ${name}_cloudsql" >> config0.yaml
echo " type: sqladmin.v1beta4.instance" >> config0.yaml
echo " properties:" >> config0.yaml
echo " region: $region" >> config0.yaml
echo " instanceType: CLOUD_SQL_INSTANCE" >> config0.yaml
echo " databaseVersion: $version" >> config0.yaml
echo " settings:" >> config0.yaml
echo " tier: $tier" >> config0.yaml
echo " backupConfiguration:" >> config0.yaml
echo " binaryLogEnabled: true" >> config0.yaml
# Generate an output variable for each Cloud SQL instance's connection name
echo "- name: $name-connection" >> outputs0.yaml
echo " value: \$(ref.$name.connectionName)" >> outputs0.yaml
done <<< "$sql_instances"
cat config0.yaml outputs0.yaml > config.yaml
rm config0.yaml outputs0.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment