diff --git a/.kokoro/datacatalog.cfg b/.kokoro/datacatalog.cfg index fca8962802..ddfc7c12e2 100644 --- a/.kokoro/datacatalog.cfg +++ b/.kokoro/datacatalog.cfg @@ -10,4 +10,17 @@ env_vars: { env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/nodejs-docs-samples/.kokoro/build.sh" -} \ No newline at end of file +} + +# This must be different from GCLOUD_PROJECT. +# Since build.sh overrides the value for GCLOUD_PROJECT. +env_vars: { + key: "GCLOUD_ORGANIZATION_PROJECT" + value: "project-a" +} + +# Set organization used by org scoped samples. +env_vars: { + key: "GCLOUD_ORGANIZATION" + value: "1081635000895" +} diff --git a/datacatalog/cloud-client/README.md b/datacatalog/cloud-client/README.md index 66f2f614d1..70e9a56928 100644 --- a/datacatalog/cloud-client/README.md +++ b/datacatalog/cloud-client/README.md @@ -13,7 +13,10 @@ Run the following command to install the library dependencies for Node.js: # Running the sample Commands: + createEntryGroup.js Create a entry group. + createFilesetEntry.js Create a fileset entry. lookupEntry.js Lookup a dataset entry. - + searchCatalogProject.js Search Catalog with project scope. + searchCatalogOrg.js Search Catalog with organization scope. For more information, see https://cloud.google.com/data-catalog/docs/ \ No newline at end of file diff --git a/datacatalog/cloud-client/package.json b/datacatalog/cloud-client/package.json index 881a56ee27..3e37cd4fd9 100644 --- a/datacatalog/cloud-client/package.json +++ b/datacatalog/cloud-client/package.json @@ -14,7 +14,8 @@ }, "devDependencies": { "mocha": "^8.0.0", - "uuid": "^8.0.0" + "uuid": "^8.0.0", + "@google-cloud/bigquery": "^4.0.0" }, "dependencies": { "@google-cloud/datacatalog": "^2.0.0" diff --git a/datacatalog/cloud-client/searchCatalogOrg.js b/datacatalog/cloud-client/searchCatalogOrg.js new file mode 100644 index 0000000000..2e61b7036d --- /dev/null +++ b/datacatalog/cloud-client/searchCatalogOrg.js @@ -0,0 +1,52 @@ +// Copyright 2020 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. + +// [START datacatalog_search_org] +// This application demonstrates how to perform search operations with the +// Cloud Data Catalog API. + +const main = async ( + organizationId = process.env.GCLOUD_ORGANIZATION, + query +) => { + + // ------------------------------- + // Import required modules. + // ------------------------------- + const { DataCatalogClient } = require('@google-cloud/datacatalog').v1; + const datacatalog = new DataCatalogClient(); + + // Create request. + const scope = { + includeOrgIds: [organizationId], + // Alternatively, search using project scopes. + // includeProjectIds: ['my-project'], + }; + + const request = { + scope: scope, + query: query, + }; + + const [response] = await datacatalog.searchCatalog(request); + console.log(response); + return response; +} + +// node searchCatalogOrg.js +// sample values: +// organizationId = 111111000000; +// query = 'type=dataset' +main(...process.argv.slice(2)); +// [END datacatalog_search_org] diff --git a/datacatalog/cloud-client/searchCatalogProject.js b/datacatalog/cloud-client/searchCatalogProject.js new file mode 100644 index 0000000000..2b89886b17 --- /dev/null +++ b/datacatalog/cloud-client/searchCatalogProject.js @@ -0,0 +1,52 @@ +// Copyright 2020 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. + +// [START datacatalog_search_project] +// This application demonstrates how to perform search operations with the +// Cloud Data Catalog API. + +const main = async ( + projectId = process.env.GCLOUD_PROJECT, + query +) => { + + // ------------------------------- + // Import required modules. + // ------------------------------- + const { DataCatalogClient } = require('@google-cloud/datacatalog').v1; + const datacatalog = new DataCatalogClient(); + + // Create request. + const scope = { + includeProjectIds: [projectId], + // Alternatively, search using org scopes. + // includeOrgIds: [organizationId] + }; + + const request = { + scope: scope, + query: query, + }; + + const [response] = await datacatalog.searchCatalog(request); + console.log(response); + return response; +} + +// node searchCatalogProject.js +// sample values: +// projectId = 'my-project'; +// query = 'type=dataset' +main(...process.argv.slice(2)); +// [END datacatalog_search_project] diff --git a/datacatalog/cloud-client/system-test/searchCatalogOrg.test.js b/datacatalog/cloud-client/system-test/searchCatalogOrg.test.js new file mode 100644 index 0000000000..cf40abb9e2 --- /dev/null +++ b/datacatalog/cloud-client/system-test/searchCatalogOrg.test.js @@ -0,0 +1,68 @@ +// Copyright 2020 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 path = require('path'); +const assert = require('assert'); +const cwd = path.join(__dirname, '..'); +const {exec} = require('child_process'); +const uuid = require('uuid'); +const generateUuid = () => `datacatalog-tests-${uuid.v4()}`.replace(/-/gi, '_'); +const datasetId = generateUuid(); +const {BigQuery} = require('@google-cloud/bigquery'); + +const bigquery = new BigQuery(); + +before(async () => { + assert( + process.env.GCLOUD_ORGANIZATION, + `Must set GCLOUD_ORGANIZATION environment variable!` + ); + assert( + process.env.GCLOUD_ORGANIZATION_PROJECT, + `Must set GCLOUD_ORGANIZATION_PROJECT environment variable!` + ); + assert( + process.env.GOOGLE_APPLICATION_CREDENTIALS, + `Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!` + ); + + await bigquery.createDataset(datasetId); +}); +after(async () => { + await bigquery + .dataset(datasetId) + .delete({force: true}) + .catch(console.warn); +}); + +describe('searchCatalog org', () => { + it('should return a dataset entry', (done) => { + const organizationId = process.env.GCLOUD_ORGANIZATION; + const projectId = process.env.GCLOUD_ORGANIZATION_PROJECT; + const query = 'type=dataset'; + const expectedLinkedResource = `//bigquery.googleapis.com/projects/${projectId}/datasets/${datasetId}`; + exec( + `node searchCatalogOrg.js ${organizationId} ${query}`, + {cwd}, + (err, stdout) => { + // ADD line to test return on kokoro build + console.log(`search-org: ${stdout} ${err}`) + assert.ok(stdout.includes(expectedLinkedResource)); + done(); + } + ); + }); +}); diff --git a/datacatalog/cloud-client/system-test/searchCatalogProject.test.js b/datacatalog/cloud-client/system-test/searchCatalogProject.test.js new file mode 100644 index 0000000000..daa232b16f --- /dev/null +++ b/datacatalog/cloud-client/system-test/searchCatalogProject.test.js @@ -0,0 +1,63 @@ +// Copyright 2020 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 path = require('path'); +const assert = require('assert'); +const cwd = path.join(__dirname, '..'); +const {exec} = require('child_process'); +const uuid = require('uuid'); +const generateUuid = () => `datacatalog-tests-${uuid.v4()}`.replace(/-/gi, '_'); +const datasetId = generateUuid(); +const {BigQuery} = require('@google-cloud/bigquery'); + +const bigquery = new BigQuery(); + +before(async () => { + assert( + process.env.GCLOUD_PROJECT, + `Must set GCLOUD_PROJECT environment variable!` + ); + assert( + process.env.GOOGLE_APPLICATION_CREDENTIALS, + `Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!` + ); + + await bigquery.createDataset(datasetId); +}); +after(async () => { + await bigquery + .dataset(datasetId) + .delete({force: true}) + .catch(console.warn); +}); + +describe('searchCatalog project', () => { + it('should return a dataset entry', (done) => { + const projectId = process.env.GCLOUD_PROJECT; + const query = 'type=dataset'; + const expectedLinkedResource = `//bigquery.googleapis.com/projects/${projectId}/datasets/${datasetId}`; + exec( + `node searchCatalogProject.js ${projectId} ${query}`, + {cwd}, + (err, stdout) => { + // ADD line to test return on kokoro build + console.log(`search-project: ${stdout} ${err}`) + assert.ok(stdout.includes(expectedLinkedResource)); + done(); + } + ); + }); +});