Skip to content

Commit 13821b5

Browse files
t-karasovatetiana-karasovaparthea
authored andcommitted
chore: update README and setup scripts (#193)
* update README * fix: sleep is added to the setup script * fix: sleep is added to the setup script Co-authored-by: tetiana-karasova <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent f90b86b commit 13821b5

File tree

3 files changed

+101
-50
lines changed

3 files changed

+101
-50
lines changed

generated_samples/interactive-tutorials/README.md

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -43,44 +43,69 @@ If, for some reason, you have decided to proceed with these code samples without
4343
### Select your project and enable the Retail API
4444

4545
Google Cloud organizes resources into projects. This lets you
46-
collect all the related resources for a single application in one place.
46+
collect all related resources for a single application in one place.
4747

4848
If you don't have a Google Cloud project yet or you're not the owner of an existing one, you can
4949
[create a new project](https://console.cloud.google.com/projectcreate).
5050

51-
After the project is created, set your PROJECT_ID to a ```project``` variable.
52-
1. Run the following command in Terminal:
51+
After the project is created, set your PROJECT_ID to a ```project``` variable:
52+
53+
1. Run the following command in the Terminal:
54+
5355
```bash
5456
gcloud config set project <YOUR_PROJECT_ID>
5557
```
5658

57-
1. To check that the Retail API is enabled for your Project, go to the [Admin Console](https://console.cloud.google.com/ai/retail/).
59+
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/).
60+
61+
1. Log in with your user credentials to run a code sample from the Cloud Shell:
62+
63+
```bash
64+
gcloud auth login
65+
```
66+
67+
1. Type `Y` and press **Enter**. Click the link in the Terminal. A browser window
68+
should appear asking you to log in using your Gmail account.
69+
70+
1. Provide the Google Auth Library with access to your credentials and paste
71+
the code from the browser to the Terminal.
72+
73+
## Prepare your work environment
74+
75+
To prepare the work environment you should perform the following steps:
76+
- Create a service account.
77+
- Create service account key and set it to authorize your calls to the Retail API.
78+
- Install Google Cloud Retail library.
5879

5980
### Create service account
6081

61-
To access the Retail API, you must create a service account.
82+
To access the Retail API, you must create a service account. Check that you are an owner of your Google Cloud project on the [IAM page](https://console.cloud.google.com/iam-admin/iam).
6283

63-
1. To create a service account, follow this [instruction](https://cloud.google.com/retail/docs/setting-up#service-account)
84+
1. To create a service account, perform the following command:
6485

65-
1. Find your service account on the [IAM page](https://console.cloud.google.com/iam-admin/iam),
66-
click `Edit` icon, add the 'Storage Admin' and 'BigQuery Admin' roles. It may take some time for changes to apply.
86+
```bash
87+
gcloud iam service-accounts create <YOUR_SERVICE_ACCOUNT_ID>
88+
```
6789

68-
1. Copy the service account email in the Principal field.
90+
1. Assign the needed roles to your service account:
6991

70-
### Set up authentication
92+
```bash
93+
for role in {retail.admin,storage.admin,bigquery.admin}
94+
do gcloud projects add-iam-policy-binding <YOUR_PROJECT_ID> --member="serviceAccount:<YOUR_SERVICE_ACCOUNT_ID>@<YOUR_PROJECT_ID>.iam.gserviceaccount.com" --role="roles/${role}"
95+
done
96+
```
7197

72-
To run a code sample from the Cloud Shell, you need to be authenticated using the service account credentials.
98+
1. Use the following command to print out the service account email:
7399

74-
1. Login with your user credentials.
75100
```bash
76-
gcloud auth login
101+
gcloud iam service-accounts list|grep <YOUR_SERVICE_ACCOUNT_ID>
77102
```
78103

79-
1. Type `Y` and press **Enter**. Click the link in a Terminal. A browser window should appear asking you to log in using your Gmail account.
104+
Copy the service account email.
80105

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

83-
1. Upload your service account key JSON file and use it to activate the service account:
107+
1. Upload your service account key JSON file and use it to activate the service
108+
account:
84109

85110
```bash
86111
gcloud iam service-accounts keys create ~/key.json --iam-account <YOUR_SERVICE_ACCOUNT_EMAIL>
@@ -90,7 +115,9 @@ To run a code sample from the Cloud Shell, you need to be authenticated using th
90115
gcloud auth activate-service-account --key-file ~/key.json
91116
```
92117

93-
1. To request the Retail API, set your service account key JSON file as the GOOGLE_APPLICATION_CREDENTIALS environment variable :
118+
1. Set the key as the GOOGLE_APPLICATION_CREDENTIALS environment variable to
119+
use it for sending requests to the Retail API.
120+
94121
```bash
95122
export GOOGLE_APPLICATION_CREDENTIALS=~/key.json
96123
```
@@ -100,12 +127,14 @@ To run a code sample from the Cloud Shell, you need to be authenticated using th
100127
To run Python code samples for the Retail API tutorial, you need to set up your virtual environment.
101128

102129
1. Run the following commands in a Terminal to create an isolated Python environment:
130+
103131
```bash
104-
pip install virtualenv
105-
virtualenv myenv
132+
virtualenv -p python3 myenv
106133
source myenv/bin/activate
107134
```
135+
108136
1. Next, install Google packages:
137+
109138
```bash
110139
pip install google
111140
pip install google-cloud-retail
@@ -114,41 +143,53 @@ To run Python code samples for the Retail API tutorial, you need to set up your
114143
115144
```
116145

117-
## Import Catalog Data
146+
## Import catalog data
118147

119-
This step is required if this is the first Retail API Tutorial you run.
120-
Otherwise, you can skip it.
148+
There is a python-retail/samples/interactive-tutorials/resources/products.json file with valid products prepared in the `resources` directory.
149+
150+
The other file, python-retail/samples/interactive-tutorials/resources/products_some_invalid.json, contains both valid and invalid products. You will use it to check the error handling.
121151

122152
### Upload catalog data to Cloud Storage
123153

124-
There is a JSON file with valid products prepared in the `product` directory:
125-
`product/resources/products.json`.
154+
In your own project you need to create a Cloud Storage bucket and put the JSON file there.
155+
The bucket name must be unique. For convenience, you can name it `<YOUR_PROJECT_ID>_<TIMESTAMP>`.
126156

127-
Another file, `product/resources/products_some_invalid.json`, contains both valid and invalid products, and you will use it to check the error handling.
157+
1. The code samples for each of the Retail services are stored in different directories.
128158

129-
In your own project, create a Cloud Storage bucket and put the JSON file there.
130-
The bucket name must be unique. For convenience, you can name it `<YOUR_PROJECT_ID>_<TIMESTAMP>`.
159+
Go to the code samples directory, your starting point to run more commands.
131160

132-
1. To create the bucket and upload the JSON file, run the following command in the Terminal:
161+
```bash
162+
cd python-retail/samples/interactive-tutorials
163+
```
164+
165+
1. To create the bucket and upload the JSON file, open python-retail/samples/interactive-tutorials/product/setup_product/products_create_gcs_bucket.py file
166+
167+
1. Go to the **product** directory and run the following command in the Terminal:
133168

134169
```bash
135-
python product/setup_product/create_gcs_bucket.py
170+
python setup_product/products_create_gcs_bucket.py
136171
```
137172

138173
Now you can see the bucket is created in the [Cloud Storage](https://console.cloud.google.com/storage/browser), and the files are uploaded.
139174

140-
1. The name of the created Retail Search bucket is printed in the Terminal. Copy the name and set it as the environment variable `BUCKET_NAME`:
175+
1. The name of the created Cloud Storage bucket is printed in the Terminal.
176+
177+
```
178+
The gcs bucket <YOUR_PROJECT_ID>_<TIMESTAMP> was created
179+
```
180+
181+
Copy the name and set it as the environment variable `BUCKET_NAME`:
141182

142183
```bash
143184
export BUCKET_NAME=<YOUR_BUCKET_NAME>
144185
```
145186

146187
### Import products to the Retail Catalog
147188

148-
To import the prepared products to a catalog, run the following command in the Terminal:
189+
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:
149190

150191
```bash
151-
python product/import_products_gcs.py
192+
python import_products_gcs.py
152193
```
153194

154195
## Run your code sample

generated_samples/interactive-tutorials/user_environment_setup.sh

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,27 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
# get the project_id from gcloud config
18-
project_id=$(gcloud config get-value project)
19-
echo $project_id
17+
# set the Google Cloud project Id
18+
project_id=$1
19+
echo Project ID: $project_id
20+
gcloud config set project project_id
21+
2022
timestamp=$(date +%s)
21-
echo $timestamp
23+
2224
service_account_id="service-acc-"$timestamp
25+
echo Service Account: $service_account_id
2326

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

2730
# assign needed roles to your new service account
28-
for role in {retail.admin,storage.admin,bigquery.admin}
31+
for role in {retail.admin,editor,bigquery.admin}
2932
do
3033
gcloud projects add-iam-policy-binding $project_id --member="serviceAccount:"$service_account_id"@"$project_id".iam.gserviceaccount.com" --role="roles/${role}"
31-
done
34+
done
35+
36+
echo Wait 70 seconds to be sure the appropriate roles have been assigned to your service account
37+
sleep 70
3238

3339
# upload your service account key file
3440
service_acc_email=$service_account_id"@"$project_id".iam.gserviceaccount.com"
@@ -37,16 +43,17 @@ gcloud iam service-accounts keys create ~/key.json --iam-account $service_acc_em
3743
# activate the service account using the key
3844
gcloud auth activate-service-account --key-file ~/key.json
3945

40-
# set the key as GOOGLE_APPLICATION_CREDENTIALS
41-
export GOOGLE_APPLICATION_CREDENTIALS=~/key.json
42-
4346
# install needed Google client libraries
4447
virtualenv -p python3 myenv
4548
source myenv/bin/activate
49+
sleep 2
4650

4751
pip install google
4852
pip install google-cloud-retail
4953
pip install google-cloud.storage
5054
pip install google-cloud.bigquery
5155

52-
echo "Your working environment is set up now!"
56+
echo ========================================
57+
echo "The Google Cloud setup is completed."
58+
echo "Please proceed with the Tutorial steps"
59+
echo ========================================

generated_samples/interactive-tutorials/user_import_data_to_catalog.sh

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,23 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17-
# Change the working directory
18-
current_path=$(pwd)
19-
temp_path="${current_path%cloudshell_open*}"
20-
full_path=$temp_path"cloudshell_open/python-retail/samples/interactive-tutorials/product"
21-
cd $full_path
17+
# set the service account key as a GOOGLE_APPLICATION_CREDENTIALS
18+
export GOOGLE_APPLICATION_CREDENTIALS=~/key.json
19+
20+
# activate the python virtual env
21+
source ~/cloudshell_open/myenv/bin/activate
22+
2223
# Create a GCS bucket and upload the product data to the bucket
23-
output=$(python setup_product/products_create_gcs_bucket.py)
24+
output=$(python ~/cloudshell_open/python-retail/samples/interactive-tutorials/product/setup_product/products_create_gcs_bucket.py)
2425

2526
# Get the bucket name and store it in the env variable BUCKET_NAME
2627
temp="${output#*The gcs bucket }"
2728
bucket_name="${temp% was created*}"
2829
export BUCKET_NAME=$bucket_name
2930

3031
# Import products to the Retail catalog
31-
python import_products_gcs.py
32-
echo "Products are successfully imported to catalog"
32+
python ~/cloudshell_open/python-retail/samples/interactive-tutorials/product/import_products_gcs.py
33+
34+
echo =====================================
3335
echo "Your Retail catalog is ready to use!"
36+
echo =====================================

0 commit comments

Comments
 (0)