|
14 | 14 | # See the License for the specific language governing permissions and
|
15 | 15 | # limitations under the License.
|
16 | 16 |
|
17 |
| -# set the Google Cloud Project ID |
18 |
| -{ |
19 |
| - project_id=$1 |
20 |
| - echo "Project ID: $project_id" |
21 |
| - gcloud config set project "$project_id" |
22 |
| - |
23 |
| -} && { |
24 |
| - |
25 |
| - timestamp=$(date +%s) |
26 |
| - |
27 |
| - service_account_id="service-acc-$timestamp" |
28 |
| - echo "Service Account: $service_account_id" |
| 17 | +failure() { |
| 18 | + echo "=========================================" |
| 19 | + echo "The Google Cloud setup was not completed." |
| 20 | + echo "Please fix the errors above!" |
| 21 | + echo "=========================================" |
| 22 | + exit 0 |
| 23 | +} |
29 | 24 |
|
30 |
| - # create service account (your service-acc-$timestamp) |
31 |
| - gcloud iam service-accounts create "$service_account_id" |
| 25 | +# catch any error that happened during execution |
| 26 | +trap 'failure' ERR |
32 | 27 |
|
33 |
| -} && { |
| 28 | +# set the Google Cloud Project ID |
34 | 29 |
|
35 |
| - # assign necessary roles to your new service account |
36 |
| - for role in {retail.admin,editor,bigquery.admin} |
| 30 | +project_id=$1 |
| 31 | +echo "Project ID: $project_id" |
| 32 | +gcloud config set project "$project_id" |
| 33 | + |
| 34 | +email=$(gcloud auth list --filter="status:ACTIVE account:$project_id.iam.gserviceaccount.com" --format="value(account)") |
| 35 | +echo $email |
| 36 | + |
| 37 | +# Check if user has service account active |
| 38 | +if [ -z "$email" ] |
| 39 | +then |
| 40 | + # Create a new service account |
| 41 | + timestamp=$(date +%s) |
| 42 | + |
| 43 | + service_account_id="service-acc-$timestamp" |
| 44 | + echo "Service Account: $service_account_id" |
| 45 | + |
| 46 | + # create service account (your service-acc-$timestamp) |
| 47 | + gcloud iam service-accounts create "$service_account_id" |
| 48 | +else |
| 49 | + service_account_id="${email%@*}" |
| 50 | + # Log out of service account |
| 51 | + gcloud auth revoke |
| 52 | +fi |
| 53 | +echo "$service_account_id" |
| 54 | + |
| 55 | +editor=$(gcloud projects get-iam-policy $project_id \ |
| 56 | +--flatten="bindings[].members" \ |
| 57 | +--format='table(bindings.role)' \ |
| 58 | +--filter="bindings.members:$service_account_id ROLE=roles/editor") |
| 59 | + |
| 60 | +retail_admin=$(gcloud projects get-iam-policy $project_id \ |
| 61 | +--flatten="bindings[].members" \ |
| 62 | +--format='table(bindings.role)' \ |
| 63 | +--filter="bindings.members:$service_account_id ROLE=roles/retail.admin") |
| 64 | + |
| 65 | + |
| 66 | +# assign necessary roles to your new service account |
| 67 | +# Check if any of the needed roles is missing |
| 68 | +if [ -z "$editor" ] || [ -z "$retail_admin" ] |
| 69 | +then |
| 70 | + # Assign necessary roles to your new service account. |
| 71 | + for role in {retail.admin,editor} |
37 | 72 | do
|
38 |
| - gcloud projects add-iam-policy-binding "$project_id" --member="serviceAccount:$service_account_id@$project_id.iam.gserviceaccount.com" --role=roles/"${role}" |
39 |
| - done |
| 73 | + gcloud projects add-iam-policy-binding "$project_id" --member="serviceAccount:$service_account_id@$project_id.iam.gserviceaccount.com" --role=roles/"${role}" |
| 74 | + done |
| 75 | + echo "Wait ~60 seconds to be sure the appropriate roles have been assigned to your service account" |
| 76 | + sleep 60 |
| 77 | +fi |
40 | 78 |
|
41 |
| -} && { |
| 79 | +# upload your service account key file |
| 80 | +service_acc_email="$service_account_id@$project_id.iam.gserviceaccount.com" |
| 81 | +gcloud iam service-accounts keys create ~/key.json --iam-account "$service_acc_email" |
42 | 82 |
|
43 |
| - echo "Wait ~60 seconds to be sure the appropriate roles have been assigned to your service account" |
44 |
| - sleep 60 |
| 83 | +# activate the service account using the key |
| 84 | +gcloud auth activate-service-account --key-file ~/key.json |
45 | 85 |
|
46 |
| - # upload your service account key file |
47 |
| - service_acc_email="$service_account_id@$project_id.iam.gserviceaccount.com" |
48 |
| - gcloud iam service-accounts keys create ~/key.json --iam-account "$service_acc_email" |
49 | 86 |
|
50 |
| - # activate the service account using the key |
51 |
| - gcloud auth activate-service-account --key-file ~/key.json |
| 87 | +# install needed Google client libraries |
| 88 | +cd ~/cloudshell_open/java-retail/samples/interactive-tutorials || exit |
| 89 | +mvn clean install -DskipTests |
52 | 90 |
|
53 |
| -} && { |
54 | 91 |
|
55 |
| - # install needed Google client libraries |
56 |
| - cd ~/cloudshell_open/java-retail/samples/interactive-tutorials || exit |
57 |
| - mvn clean install -DskipTests |
58 |
| - |
59 |
| -} && { |
60 |
| - |
61 |
| - # Print success message |
62 |
| - echo "========================================" |
63 |
| - echo "The Google Cloud setup is completed." |
64 |
| - echo "Please proceed with the Tutorial steps" |
65 |
| - echo "========================================" |
66 |
| - |
67 |
| -} || { |
68 |
| - |
69 |
| - # Print error message |
70 |
| - echo "========================================" |
71 |
| - echo "The Google Cloud setup was not completed." |
72 |
| - echo "Please fix the errors above!" |
73 |
| - echo "========================================" |
74 |
| -} |
| 92 | +# Print success message |
| 93 | +echo "========================================" |
| 94 | +echo "The Google Cloud setup is completed." |
| 95 | +echo "Please proceed with the Tutorial steps" |
| 96 | +echo "========================================" |
0 commit comments