Skip to content

Commit

Permalink
docs(samples): improve the setup scripts (#207)
Browse files Browse the repository at this point in the history
* update README

* fix: sleep is added to the setup script

* improve the setup scripts

Co-authored-by: tetiana-karasova <tetiana.karasova@gmail.com>
Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com>
  • Loading branch information
3 people authored and dandhlee committed Dec 2, 2022
1 parent c374abc commit 6455f62
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 32 deletions.
16 changes: 2 additions & 14 deletions generated_samples/interactive-tutorials/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,6 @@ After the project is created, set your PROJECT_ID to a ```project``` variable:

1. Ensure that the Retail API is enabled for your project in the [API & Services page](https://console.cloud.google.com/apis/api/retail.googleapis.com/).

1. Log in with your user credentials to run a code sample from the Cloud Shell:

```bash
gcloud auth login
```

1. Type `Y` and press **Enter**. Click the link in the Terminal. A browser window
should appear asking you to log in using your Gmail account.

1. Provide the Google Auth Library with access to your credentials and paste
the code from the browser to the Terminal.

## Prepare your work environment

To prepare the work environment you should perform the following steps:
Expand Down Expand Up @@ -167,7 +155,7 @@ The bucket name must be unique. For convenience, you can name it `<YOUR_PROJECT_
1. Go to the **product** directory and run the following command in the Terminal:

```bash
python setup_product/products_create_gcs_bucket.py
python product/setup_product/products_create_gcs_bucket.py
```

Now you can see the bucket is created in the [Cloud Storage](https://console.cloud.google.com/storage/browser), and the files are uploaded.
Expand All @@ -189,7 +177,7 @@ The bucket name must be unique. For convenience, you can name it `<YOUR_PROJECT_
To import the prepared products to a catalog, open python-retail/samples/interactive-tutorials/product/import_products_gcs.py file and run the following command in the Terminal:

```bash
python import_products_gcs.py
python product/import_products_gcs.py
```

## Run your code sample
Expand Down
32 changes: 16 additions & 16 deletions generated_samples/interactive-tutorials/user_environment_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,36 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# set the Google Cloud project Id
# set the Google Cloud Project ID
project_id=$1
echo Project ID: $project_id
gcloud config set project project_id
echo "Project ID: $project_id"
gcloud config set project "$project_id"

timestamp=$(date +%s)

service_account_id="service-acc-"$timestamp
echo Service Account: $service_account_id
service_account_id="service-acc-$timestamp"
echo "Service Account: $service_account_id"

# create service account (your project_id+timestamp)
gcloud iam service-accounts create $service_account_id
# create service account
gcloud iam service-accounts create "$service_account_id"

# assign needed roles to your new service account
# assign necessary roles to your new service account
for role in {retail.admin,editor,bigquery.admin}
do
gcloud projects add-iam-policy-binding $project_id --member="serviceAccount:"$service_account_id"@"$project_id".iam.gserviceaccount.com" --role="roles/${role}"
gcloud projects add-iam-policy-binding "$project_id" --member="serviceAccount:$service_account_id@$project_id.iam.gserviceaccount.com" --role=roles/"${role}"
done

echo Wait 70 seconds to be sure the appropriate roles have been assigned to your service account
sleep 70
echo "Wait ~60 seconds to be sure the appropriate roles have been assigned to your service account"
sleep 60

# 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
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

# install needed Google client libraries
# install necessary Google client libraries
virtualenv -p python3 myenv
source myenv/bin/activate
sleep 2
Expand All @@ -53,7 +53,7 @@ pip install google-cloud-retail
pip install google-cloud.storage
pip install google-cloud.bigquery

echo ========================================
echo "======================================="
echo "The Google Cloud setup is completed."
echo "Please proceed with the Tutorial steps"
echo ========================================
echo "======================================="
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export BUCKET_NAME=$bucket_name
# Import products to the Retail catalog
python ~/cloudshell_open/python-retail/samples/interactive-tutorials/product/import_products_gcs.py

echo =====================================
echo "====================================="
echo "Your Retail catalog is ready to use!"
echo =====================================
echo "====================================="

0 comments on commit 6455f62

Please sign in to comment.