xref: /aosp_15_r20/external/google-cloud-java/.cloud/helpers/gcloud-sync-env.sh (revision 55e87721aa1bc457b326496a7ca40f3ea1a63287)
1*55e87721SMatt Gilbride#
2*55e87721SMatt Gilbride# Copyright 2022 Google LLC
3*55e87721SMatt Gilbride#
4*55e87721SMatt Gilbride# Licensed under the Apache License, Version 2.0 (the "License");
5*55e87721SMatt Gilbride# you may not use this file except in compliance with the License.
6*55e87721SMatt Gilbride# You may obtain a copy of the License at
7*55e87721SMatt Gilbride#
8*55e87721SMatt Gilbride#      https://www.apache.org/licenses/LICENSE-2.0
9*55e87721SMatt Gilbride#
10*55e87721SMatt Gilbride# Unless required by applicable law or agreed to in writing, software
11*55e87721SMatt Gilbride# distributed under the License is distributed on an "AS IS" BASIS,
12*55e87721SMatt Gilbride# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*55e87721SMatt Gilbride# See the License for the specific language governing permissions and
14*55e87721SMatt Gilbride# limitations under the License.
15*55e87721SMatt Gilbride#
16*55e87721SMatt Gilbrideset -eo pipefail
17*55e87721SMatt Gilbride
18*55e87721SMatt Gilbrideif ! terraform output -json project_id &>/dev/null; then
19*55e87721SMatt Gilbride  if ! terraform refresh &>/dev/null; then
20*55e87721SMatt Gilbride    echo "No terraform state found."
21*55e87721SMatt Gilbride    exit 1
22*55e87721SMatt Gilbride  fi
23*55e87721SMatt Gilbride  if ! terraform output -json project_id &>/dev/null; then
24*55e87721SMatt Gilbride    echo "No project found in terraform state."
25*55e87721SMatt Gilbride    exit 1
26*55e87721SMatt Gilbride  fi
27*55e87721SMatt Gilbridefi
28*55e87721SMatt Gilbride
29*55e87721SMatt Gilbride# Ensure the gcloud project matches Terraform's current state.
30*55e87721SMatt Gilbrideterraform_project_id=$(terraform output -raw project_id)
31*55e87721SMatt Gilbridegcloud_project_id=$(gcloud config get project) || ""
32*55e87721SMatt Gilbrideif [[ "$terraform_project_id" != "$gcloud_project_id" ]]; then
33*55e87721SMatt Gilbride  echo -n "Do you want to make $terraform_project_id your current gcloud project? (Y/n): "
34*55e87721SMatt Gilbride  read -r shouldSwitch
35*55e87721SMatt Gilbride  if [[ "$shouldSwitch" == n* ]] || [[ "$shouldSwitch" == N* ]]; then
36*55e87721SMatt Gilbride    exit
37*55e87721SMatt Gilbride  fi
38*55e87721SMatt Gilbride  gcloud config set project "$terraform_project_id"
39*55e87721SMatt Gilbridefi
40*55e87721SMatt Gilbride
41*55e87721SMatt GilbrideGOOGLE_CLOUD_PROJECT="$terraform_project_id"
42*55e87721SMatt Gilbrideexport GOOGLE_CLOUD_PROJECT
43*55e87721SMatt Gilbride
44*55e87721SMatt Gilbridesource ./helpers/common.sh
45*55e87721SMatt GilbrideGOOGLE_IMPERSONATE_SERVICE_ACCOUNT=$(getTerraformServiceAccountEmail)
46*55e87721SMatt Gilbrideexport GOOGLE_IMPERSONATE_SERVICE_ACCOUNT
47