From d0873bf8a91786e657c4a6b9cb12b7a0316cb1f0 Mon Sep 17 00:00:00 2001 From: Nick Cook Date: Tue, 27 Aug 2024 17:31:39 -0700 Subject: [PATCH 1/3] docs: add a REST sample and test for AML AI --- .github/workflows/aml-ai.yaml | 52 ++++++++++++++++++++++++++ .github/workflows/utils/workflows.json | 1 + CODEOWNERS | 1 + aml-ai/listLocations.js | 41 ++++++++++++++++++++ aml-ai/package.json | 19 ++++++++++ aml-ai/system-test/aml-ai.test.js | 30 +++++++++++++++ 6 files changed, 144 insertions(+) create mode 100644 .github/workflows/aml-ai.yaml create mode 100644 aml-ai/listLocations.js create mode 100644 aml-ai/package.json create mode 100644 aml-ai/system-test/aml-ai.test.js diff --git a/.github/workflows/aml-ai.yaml b/.github/workflows/aml-ai.yaml new file mode 100644 index 0000000000..863c2ffa7a --- /dev/null +++ b/.github/workflows/aml-ai.yaml @@ -0,0 +1,52 @@ +# Copyright 2024 Google LLC +# +# 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. + +name: aml-ai +on: + push: + branches: + - main + paths: + - 'aml-ai/**' + - '.github/workflows/aml-ai.yaml' + pull_request: + types: + - opened + - reopened + - synchronize + - labeled + paths: + - 'aml-ai/**' + - '.github/workflows/aml-ai.yaml' + schedule: + - cron: '0 0 * * 0' +jobs: + test: + # Ref: https://github.com/google-github-actions/auth#usage + permissions: + contents: 'read' + id-token: 'write' + if: github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' + uses: ./.github/workflows/test.yaml + with: + name: 'aml-ai' + path: 'aml-ai' + flakybot: + # Ref: https://github.com/google-github-actions/auth#usage + permissions: + contents: 'read' + id-token: 'write' + if: github.event_name == 'schedule' && always() # always() submits logs even if tests fail + uses: ./.github/workflows/flakybot.yaml + needs: [test] diff --git a/.github/workflows/utils/workflows.json b/.github/workflows/utils/workflows.json index ff954978f8..0ce121783d 100644 --- a/.github/workflows/utils/workflows.json +++ b/.github/workflows/utils/workflows.json @@ -1,4 +1,5 @@ [ + "aml-ai", "appengine/analytics", "appengine/building-an-app/build", "appengine/building-an-app/update", diff --git a/CODEOWNERS b/CODEOWNERS index d1ec34b8bc..15b2945928 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -63,6 +63,7 @@ vision @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-revi # Self-service ai-platform @GoogleCloudPlatform/dee-data-ai @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/text-embedding @GoogleCloudPlatform/cloud-samples-reviewers +aml-ai @GoogleCloudPlatform/aml-ai @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers asset @GoogleCloudPlatform/cloud-asset-analysis-team @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers dlp @GoogleCloudPlatform/googleapis-dlp @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers security-center @GoogleCloudPlatform/gcp-security-command-center @GoogleCloudPlatform/nodejs-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers diff --git a/aml-ai/listLocations.js b/aml-ai/listLocations.js new file mode 100644 index 0000000000..759b76a420 --- /dev/null +++ b/aml-ai/listLocations.js @@ -0,0 +1,41 @@ +/** + * Copyright 2024 Google LLC + * 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. + */ + +'use strict'; + +const main = (projectId = process.env.GOOGLE_CLOUD_PROJECT) => { + // [START antimoneylaunderingai_list_locations] + // Import google-auth-library for authentication. + const {GoogleAuth} = require('google-auth-library'); + + const listLocations = async () => { + const auth = new GoogleAuth({ + scopes: 'https://www.googleapis.com/auth/cloud-platform', + headers: {'Content-Type': 'application/json; charset=utf-8'}, + }); + // TODO(developer): uncomment these lines before running the sample + // const projectId = 'my-project-id'; + const url = `https://financialservices.googleapis.com/v1/projects/${projectId}/locations`; + const client = await auth.getClient(); + const response = await client.request({url, method: 'GET'}); + console.log(JSON.stringify(response.data)); + }; + + listLocations(); + // [END antimoneylaunderingai_list_locations] +}; + +// node listLocations.js +main(...process.argv.slice(2)); diff --git a/aml-ai/package.json b/aml-ai/package.json new file mode 100644 index 0000000000..48d693098b --- /dev/null +++ b/aml-ai/package.json @@ -0,0 +1,19 @@ +{ + "name": "nodejs-docs-samples-aml-ai", + "version": "0.0.1", + "private": true, + "license": "Apache-2.0", + "author": "Google LLC", + "repository": "GoogleCloudPlatform/nodejs-docs-samples", + "engines": { + "node": ">=12.0.0" + }, + "scripts": { + "test": "c8 mocha -p -j 2 system-test/*.test.js --timeout=60000" + }, + "devDependencies": { + "c8": "^10.0.0", + "google-auth-library": "^9.0.0", + "mocha": "^10.0.0" + } +} diff --git a/aml-ai/system-test/aml-ai.test.js b/aml-ai/system-test/aml-ai.test.js new file mode 100644 index 0000000000..7315d9ab3b --- /dev/null +++ b/aml-ai/system-test/aml-ai.test.js @@ -0,0 +1,30 @@ +/** + * Copyright 2024 Google LLC + * 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. + */ + +'use strict'; + +const assert = require('assert'); +const path = require('path'); +const cwd = path.join(__dirname, '..'); +const {execSync} = require('child_process'); +const {it} = require('mocha'); + +const projectId = process.env.GCLOUD_PROJECT; +const cloudRegion = 'us-central1'; + +it('should get the AML AI API locations', () => { + const output = execSync(`node listLocations.js ${projectId}`, {cwd}); + assert.ok(output.includes(cloudRegion)); +}); From 0cc6c9e17d60d2322465fdb98f38c72dca7984f1 Mon Sep 17 00:00:00 2001 From: Nick Cook Date: Wed, 11 Sep 2024 13:06:39 -0700 Subject: [PATCH 2/3] address feedback --- aml-ai/listLocations.js | 23 ++++++++++++++++------- aml-ai/system-test/aml-ai.test.js | 3 +-- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/aml-ai/listLocations.js b/aml-ai/listLocations.js index 759b76a420..740f1dc209 100644 --- a/aml-ai/listLocations.js +++ b/aml-ai/listLocations.js @@ -15,7 +15,7 @@ 'use strict'; -const main = (projectId = process.env.GOOGLE_CLOUD_PROJECT) => { +const main = async () => { // [START antimoneylaunderingai_list_locations] // Import google-auth-library for authentication. const {GoogleAuth} = require('google-auth-library'); @@ -25,17 +25,26 @@ const main = (projectId = process.env.GOOGLE_CLOUD_PROJECT) => { scopes: 'https://www.googleapis.com/auth/cloud-platform', headers: {'Content-Type': 'application/json; charset=utf-8'}, }); - // TODO(developer): uncomment these lines before running the sample - // const projectId = 'my-project-id'; + // TODO(developer): update the project ID as needed + const projectId = process.env.GOOGLE_CLOUD_PROJECT; const url = `https://financialservices.googleapis.com/v1/projects/${projectId}/locations`; const client = await auth.getClient(); - const response = await client.request({url, method: 'GET'}); + let response; + try { + response = await client.request({url, method: 'GET'}); + } catch (err) { + throw new Error(`API request failed: ${err}`); + } + console.log(JSON.stringify(response.data)); }; - listLocations(); + return await listLocations(); // [END antimoneylaunderingai_list_locations] }; -// node listLocations.js -main(...process.argv.slice(2)); +// node listLocations.js +main().catch(err => { + console.log(err); + process.exitCode = 1; +}); diff --git a/aml-ai/system-test/aml-ai.test.js b/aml-ai/system-test/aml-ai.test.js index 7315d9ab3b..25c80f79df 100644 --- a/aml-ai/system-test/aml-ai.test.js +++ b/aml-ai/system-test/aml-ai.test.js @@ -21,10 +21,9 @@ const cwd = path.join(__dirname, '..'); const {execSync} = require('child_process'); const {it} = require('mocha'); -const projectId = process.env.GCLOUD_PROJECT; const cloudRegion = 'us-central1'; it('should get the AML AI API locations', () => { - const output = execSync(`node listLocations.js ${projectId}`, {cwd}); + const output = execSync('node listLocations.js', {cwd}); assert.ok(output.includes(cloudRegion)); }); From 100207c70ccaf83ab74b01125f3beec4c465d01d Mon Sep 17 00:00:00 2001 From: Tony Pujals Date: Sun, 15 Sep 2024 14:43:50 -0700 Subject: [PATCH 3/3] refactor: use error instead of log --- aml-ai/listLocations.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aml-ai/listLocations.js b/aml-ai/listLocations.js index 740f1dc209..3cc99f58bc 100644 --- a/aml-ai/listLocations.js +++ b/aml-ai/listLocations.js @@ -45,6 +45,6 @@ const main = async () => { // node listLocations.js main().catch(err => { - console.log(err); + console.error(err); process.exitCode = 1; });