From dd5e55b31bf9f804b47fa05ba3cae14224c377a8 Mon Sep 17 00:00:00 2001 From: t-karasova <91195610+t-karasova@users.noreply.github.com> Date: Tue, 15 Mar 2022 14:17:41 +0100 Subject: [PATCH] chore: Scripts were added to simplify the work environment creation (#187) * feat: added scripts to simplify work env setup * fixes for the scripts Co-authored-by: tetiana-karasova Co-authored-by: Anthonios Partheniou --- .../user_environment_setup.sh | 52 +++++++++++++++++++ .../user_import_data_to_catalog.sh | 33 ++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 packages/google-cloud-retail/samples/interactive-tutorials/user_environment_setup.sh create mode 100644 packages/google-cloud-retail/samples/interactive-tutorials/user_import_data_to_catalog.sh diff --git a/packages/google-cloud-retail/samples/interactive-tutorials/user_environment_setup.sh b/packages/google-cloud-retail/samples/interactive-tutorials/user_environment_setup.sh new file mode 100644 index 000000000000..a0353d9e5d66 --- /dev/null +++ b/packages/google-cloud-retail/samples/interactive-tutorials/user_environment_setup.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# Copyright 2022 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# get the project_id from gcloud config +project_id=$(gcloud config get-value project) +echo $project_id +timestamp=$(date +%s) +echo $timestamp +service_account_id="service-acc-"$timestamp + +# create service account (your project_id+timestamp) +gcloud iam service-accounts create $service_account_id + +# assign needed roles to your new service account +for role in {retail.admin,storage.admin,bigquery.admin} + do + gcloud projects add-iam-policy-binding $project_id --member="serviceAccount:"$service_account_id"@"$project_id".iam.gserviceaccount.com" --role="roles/${role}" + done + +# upload your service account key file +service_acc_email=$service_account_id"@"$project_id".iam.gserviceaccount.com" +gcloud iam service-accounts keys create ~/key.json --iam-account $service_acc_email + +# activate the service account using the key +gcloud auth activate-service-account --key-file ~/key.json + +# set the key as GOOGLE_APPLICATION_CREDENTIALS +export GOOGLE_APPLICATION_CREDENTIALS=~/key.json + +# install needed Google client libraries +virtualenv -p python3 myenv +source myenv/bin/activate + +pip install google +pip install google-cloud-retail +pip install google-cloud.storage +pip install google-cloud.bigquery + +echo "Your working environment is set up now!" \ No newline at end of file diff --git a/packages/google-cloud-retail/samples/interactive-tutorials/user_import_data_to_catalog.sh b/packages/google-cloud-retail/samples/interactive-tutorials/user_import_data_to_catalog.sh new file mode 100644 index 000000000000..022d6a684741 --- /dev/null +++ b/packages/google-cloud-retail/samples/interactive-tutorials/user_import_data_to_catalog.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +# Copyright 2022 Google Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Change the working directory +current_path=$(pwd) +temp_path="${current_path%cloudshell_open*}" +full_path=temp_path"cloudshell_open/python-retail/samples/interactive-tutorials/product" + +# Create a GCS bucket and upload the product data to the bucket +output=$(python setup_product/products_create_gcs_bucket.py) + +# Get the bucket name and store it in the env variable BUCKET_NAME +temp="${output#*The gcs bucket }" +bucket_name="${temp% was created*}" +export BUCKET_NAME=$bucket_name + +# Import products to the Retail catalog +python import_products_gcs.py +echo "Products are successfully imported to catalog" +echo "Your Retail catalog is ready to use!" \ No newline at end of file