Skip to content
This repository was archived by the owner on Jul 20, 2023. It is now read-only.

Commit f4fa61c

Browse files
authored
fix: read projectId from retailClient.getProjectId (#159)
1 parent e907d3b commit f4fa61c

6 files changed

Lines changed: 95 additions & 6 deletions

File tree

samples/interactive-tutorials/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Because you are going to run the code samples in your own Google Cloud project,
5151
To install all the dependencies, run
5252

5353
```
54-
cd cloudshell_open/retail-search-nodejs-samples
54+
cd cloudshell_open/nodejs-retail
5555
npm install
5656
```
5757

@@ -61,4 +61,4 @@ To execute an individual code sample, invoke `node` with a file as a parameter a
6161

6262
```
6363
node search/search-simple-query.js
64-
```
64+
```

samples/interactive-tutorials/product/delete-product.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async function main(generatedProductId) {
4646
// Delete product
4747
console.log('Start deleting the product');
4848
await callDeleteProduct();
49-
console.log(`Product ${product.id} deleted`);
49+
console.log(`Product ${product.name} deleted`);
5050
// [END retail_delete_product]
5151
}
5252

samples/interactive-tutorials/setup/create-gcs-bucket.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
async function main(generatedBucketName) {
1818
const utils = require('./setup-cleanup');
1919

20-
//Get your project ID
21-
const projectId = process.env['PROJECT_ID'];
20+
// Instantiates a client.
21+
const {ProductServiceClient} = require('@google-cloud/retail').v2;
22+
const retailClient = new ProductServiceClient();
23+
24+
const projectId = await retailClient.getProjectId();
2225

2326
// The ID of your GCS bucket
2427
const bucketName = generatedBucketName

samples/interactive-tutorials/test/delete-product.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('Delete product', () => {
5151
});
5252

5353
it('should check that product deleted', async () => {
54-
const regex = new RegExp(`Product ${productId} deleted`, 'g');
54+
const regex = new RegExp(`Product .*${productId} deleted`, 'g');
5555
assert.match(stdout, regex);
5656
});
5757

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
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+
22+
timestamp=$(date +%s)
23+
24+
service_account_id="service-acc-$timestamp"
25+
echo "Service Account: $service_account_id"
26+
27+
# create service account (your service-acc-$timestamp)
28+
gcloud iam service-accounts create "$service_account_id"
29+
30+
# assign necessary roles to your new service account
31+
for role in {retail.admin,editor,bigquery.admin}
32+
do
33+
gcloud projects add-iam-policy-binding "$project_id" --member="serviceAccount:$service_account_id@$project_id.iam.gserviceaccount.com" --role=roles/"${role}"
34+
done
35+
36+
echo "Wait ~60 seconds to be sure the appropriate roles have been assigned to your service account"
37+
sleep 60
38+
39+
# upload your service account key file
40+
service_acc_email="$service_account_id@$project_id.iam.gserviceaccount.com"
41+
gcloud iam service-accounts keys create ~/key.json --iam-account "$service_acc_email"
42+
43+
# activate the service account using the key
44+
gcloud auth activate-service-account --key-file ~/key.json
45+
46+
# install needed Google client libraries
47+
cd ~/cloudshell_open/nodejs-retail/samples || exit
48+
npm install
49+
50+
echo "======================================="
51+
echo "The Google Cloud setup is completed."
52+
echo "Please proceed with the Tutorial steps"
53+
echo "======================================="
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# set the key as GOOGLE_APPLICATION_CREDENTIALS
18+
export GOOGLE_APPLICATION_CREDENTIALS=~/key.json
19+
20+
# Create a GCS bucket and upload the product data to the bucket
21+
output=$(node ~/cloudshell_open/nodejs-retail/samples/interactive-tutorials/setup/create-gcs-bucket.js)
22+
23+
# Get the bucket name and store it in the env variable BUCKET_NAME
24+
temp="${output#*Bucket }"
25+
bucket_name="${temp% created*}"
26+
export BUCKET_NAME=$bucket_name
27+
28+
# Import products to the Retail catalog
29+
node ~/cloudshell_open/nodejs-retail/samples/interactive-tutorials/product/import-products-gcs.js
30+
31+
echo "====================================="
32+
echo "Your Retail catalog is ready to use!"
33+
echo "====================================="

0 commit comments

Comments
 (0)