-
Notifications
You must be signed in to change notification settings - Fork 162
fix: updates to make scripts region agnostic #1385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -33,7 +33,12 @@ if [[ -z "$PROJECT_ID" ]]; then | |||||||||
| fi | ||||||||||
|
|
||||||||||
| # Determine Region | ||||||||||
| REGION="${REGION:-us-central1}" | ||||||||||
| if [ -z "${REGION:-}" ]; then | ||||||||||
| echo -e "\033[1;33m⚠️ REGION environment variable is not set.\033[0m" | ||||||||||
| while [ -z "${REGION:-}" ]; do | ||||||||||
| read -p "Please explicitly enter your assigned Google Cloud region (e.g., us-central1, europe-west1): " REGION | ||||||||||
| done | ||||||||||
| fi | ||||||||||
|
|
||||||||||
| # Save to .env file | ||||||||||
| echo "Writing environment variables to $ENV_FILE..." | ||||||||||
|
|
@@ -103,7 +108,7 @@ echo "" | |||||||||
| # [2/8] Create BigQuery dataset | ||||||||||
| # --------------------------------------------------------------- | ||||||||||
| echo "[2/8] Creating BigQuery dataset 'lost_cargo_dataset'..." | ||||||||||
| bq --location="$REGION" mk --dataset "$PROJECT_ID:lost_cargo_dataset" 2>/dev/null || true | ||||||||||
| bq --location=$REGION mk --dataset "$PROJECT_ID:lost_cargo_dataset" 2>/dev/null || true | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the double quotes around
Suggested change
|
||||||||||
| echo " Done." | ||||||||||
|
|
||||||||||
| # --------------------------------------------------------------- | ||||||||||
|
|
@@ -112,7 +117,7 @@ echo " Done." | |||||||||
| echo "[3/8] Creating Cloud Resource connection and granting permissions..." | ||||||||||
| bq mk --connection --location=$REGION --connection_type=CLOUD_RESOURCE lost_cargo_conn 2>/dev/null || true | ||||||||||
|
|
||||||||||
| SA_EMAIL=$(bq show --format=prettyjson --connection $REGION.lost_cargo_conn \ | ||||||||||
| SA_EMAIL=$(bq show --format=prettyjson --connection ${REGION}.lost_cargo_conn \ | ||||||||||
| | grep "serviceAccountId" | cut -d '"' -f 4) | ||||||||||
|
Comment on lines
+120
to
121
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The connection string parameter should be double-quoted to prevent word splitting and globbing, ensuring robust execution even if the region name contains unexpected characters or is empty. Additionally, parsing the JSON output using standard tools like
Suggested change
References
|
||||||||||
| echo " Connection service account: $SA_EMAIL" | ||||||||||
|
|
||||||||||
|
|
@@ -147,7 +152,7 @@ curl -s -X POST \ | |||||||||
| }' > /dev/null || true | ||||||||||
|
|
||||||||||
| # Grant the connection's service account access to AlloyDB | ||||||||||
| SA_EMAIL_ALLOYDB=$(bq show --format=prettyjson --connection "$REGION.lost_cargo_alloydb_conn" | grep "serviceAccountId" | cut -d '"' -f 4) | ||||||||||
| SA_EMAIL_ALLOYDB=$(bq show --format=prettyjson --connection ${REGION}.lost_cargo_alloydb_conn | grep "serviceAccountId" | cut -d '"' -f 4) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the double quotes around
Suggested change
References
|
||||||||||
| if [[ -n "$SA_EMAIL_ALLOYDB" ]]; then | ||||||||||
| grant_iam_role_with_retry "$PROJECT_ID" "serviceAccount:$SA_EMAIL_ALLOYDB" "roles/alloydb.client" | ||||||||||
| fi | ||||||||||
|
|
@@ -161,7 +166,7 @@ if gcloud storage buckets describe "$BUCKET" &>/dev/null; then | |||||||||
| echo " Bucket already exists: $BUCKET" | ||||||||||
| else | ||||||||||
| echo " Creating bucket $BUCKET..." | ||||||||||
| gcloud storage buckets create "$BUCKET" --location="$REGION" | ||||||||||
| gcloud storage buckets create "$BUCKET" --location=$REGION | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||
| fi | ||||||||||
|
|
||||||||||
| echo " Copying images from central bucket..." | ||||||||||
|
|
@@ -223,8 +228,8 @@ echo "============================================" | |||||||||
| echo "" | ||||||||||
| echo " Created resources:" | ||||||||||
| echo " - BigQuery dataset: lost_cargo_dataset" | ||||||||||
| echo " - BQ connection: $REGION.lost_cargo_conn (Cloud Resource)" | ||||||||||
| echo " - BQ connection: $REGION.lost_cargo_alloydb_conn (AlloyDB)" | ||||||||||
| echo " - BQ connection: ${REGION}.lost_cargo_conn (Cloud Resource)" | ||||||||||
| echo " - BQ connection: ${REGION}.lost_cargo_alloydb_conn (AlloyDB)" | ||||||||||
| echo " - GCS bucket: $BUCKET" | ||||||||||
| echo " - images/: Port security images" | ||||||||||
| echo " - data/: Telemetry data" | ||||||||||
|
|
@@ -236,3 +241,4 @@ echo " Next: Return to the codelab and continue with" | |||||||||
| echo " setting up the Data Agent Kit." | ||||||||||
| echo "" | ||||||||||
|
|
||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
return 1 2>/dev/null || exit 1in a script that can be executed directly (rather than sourced) will cause Bash to print an error message to standard error:return: can only return from a function or sourced script. To prevent this and handle both execution and sourcing cleanly, you can check if the script is being sourced usingBASH_SOURCE.