1*55e87721SMatt Gilbride#!/bin/bash 2*55e87721SMatt Gilbride# 3*55e87721SMatt Gilbride# Copyright 2022 Google LLC 4*55e87721SMatt Gilbride# 5*55e87721SMatt Gilbride# Licensed under the Apache License, Version 2.0 (the "License"); 6*55e87721SMatt Gilbride# you may not use this file except in compliance with the License. 7*55e87721SMatt Gilbride# You may obtain a copy of the License at 8*55e87721SMatt Gilbride# 9*55e87721SMatt Gilbride# https://www.apache.org/licenses/LICENSE-2.0 10*55e87721SMatt Gilbride# 11*55e87721SMatt Gilbride# Unless required by applicable law or agreed to in writing, software 12*55e87721SMatt Gilbride# distributed under the License is distributed on an "AS IS" BASIS, 13*55e87721SMatt Gilbride# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14*55e87721SMatt Gilbride# See the License for the specific language governing permissions and 15*55e87721SMatt Gilbride# limitations under the License. 16*55e87721SMatt Gilbride# 17*55e87721SMatt Gilbrideset -eo pipefail 18*55e87721SMatt Gilbride 19*55e87721SMatt Gilbrideif [ -n "${GOOGLE_CLOUD_PROJECT}" ]; then 20*55e87721SMatt Gilbride echo "Using current GOOGLE_CLOUD_PROJECT: $GOOGLE_CLOUD_PROJECT" 21*55e87721SMatt Gilbride return 22*55e87721SMatt Gilbridefi 23*55e87721SMatt Gilbride 24*55e87721SMatt GilbridecurrentProject=$(gcloud config get project) 25*55e87721SMatt Gilbrideif [ -n "${currentProject}" ]; then 26*55e87721SMatt Gilbride echo -n "Do you want to use the current gcloud project ($currentProject)? (Y|n): " 27*55e87721SMatt Gilbride read -r shouldUseCurrent 28*55e87721SMatt Gilbride if [[ "$shouldUseCurrent" != n* ]] && [[ "$shouldUseCurrent" != N* ]]; then 29*55e87721SMatt Gilbride GOOGLE_CLOUD_PROJECT=$currentProject 30*55e87721SMatt Gilbride export GOOGLE_CLOUD_PROJECT 31*55e87721SMatt Gilbride return 32*55e87721SMatt Gilbride fi 33*55e87721SMatt Gilbridefi 34*55e87721SMatt Gilbride 35*55e87721SMatt Gilbrideecho -n "GOOGLE_CLOUD_PROJECT not set. Do you want to create a project? (Y|n): " 36*55e87721SMatt Gilbrideread -r shouldCreate 37*55e87721SMatt Gilbrideif [[ "$shouldCreate" == n* ]] || [[ "$shouldCreate" == N* ]]; then 38*55e87721SMatt Gilbride echo "Project required. Exiting." 39*55e87721SMatt Gilbride exit 1 40*55e87721SMatt Gilbridefi 41*55e87721SMatt Gilbride 42*55e87721SMatt Gilbride# Ensure required environment variables are set. 43*55e87721SMatt Gilbrideif [ -z "${GOOGLE_CLOUD_FOLDER_ID}" ]; then 44*55e87721SMatt Gilbride echo -n "GOOGLE_CLOUD_FOLDER_ID not set. GCP Folder ID: " 45*55e87721SMatt Gilbride read -r folder_id 46*55e87721SMatt Gilbride export GOOGLE_CLOUD_FOLDER_ID="${folder_id}" 47*55e87721SMatt Gilbridefi 48*55e87721SMatt Gilbrideif [ -z "${GOOGLE_CLOUD_BILLING_ACCOUNT}" ]; then 49*55e87721SMatt Gilbride echo -n "GOOGLE_CLOUD_BILLING_ACCOUNT not set. GCP Billing Account ID: " 50*55e87721SMatt Gilbride read -r billing_acct 51*55e87721SMatt Gilbride export GOOGLE_CLOUD_BILLING_ACCOUNT="${billing_acct}" 52*55e87721SMatt Gilbridefi 53*55e87721SMatt Gilbrideif [ -z "${GOOGLE_CLOUD_PROJECT_PREFIX}" ]; then 54*55e87721SMatt Gilbride echo -n "GOOGLE_CLOUD_PROJECT_PREFIX not set. Prefix for New Project: " 55*55e87721SMatt Gilbride read -r prefix 56*55e87721SMatt Gilbride export GOOGLE_CLOUD_PROJECT_PREFIX="${prefix}" 57*55e87721SMatt Gilbridefi 58*55e87721SMatt Gilbride 59*55e87721SMatt Gilbride# Provision GCP Project 60*55e87721SMatt GilbrideprojectId="${GOOGLE_CLOUD_PROJECT_PREFIX}"-"$RANDOM" 61*55e87721SMatt Gilbridegcloud projects create --folder="$GOOGLE_CLOUD_FOLDER_ID" "$projectId" || exit 62*55e87721SMatt Gilbridegcloud config set project "$projectId" 63*55e87721SMatt Gilbridegcloud services enable cloudresourcemanager.googleapis.com 64*55e87721SMatt Gilbridegcloud beta billing projects link "$projectId" --billing-account="$GOOGLE_CLOUD_BILLING_ACCOUNT" 65*55e87721SMatt GilbrideGOOGLE_CLOUD_PROJECT=$projectId 66