diff --git a/packages/google-cloud-asset/README.md b/packages/google-cloud-asset/README.md index d30519cd46b0..30f841e8d653 100644 --- a/packages/google-cloud-asset/README.md +++ b/packages/google-cloud-asset/README.md @@ -101,6 +101,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-asset/tree/ | Create Feed | [source code](https://github.com/googleapis/nodejs-asset/blob/main/samples/createFeed.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-asset&page=editor&open_in_editor=samples/createFeed.js,samples/README.md) | | Delete Feed | [source code](https://github.com/googleapis/nodejs-asset/blob/main/samples/deleteFeed.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-asset&page=editor&open_in_editor=samples/deleteFeed.js,samples/README.md) | | Export Assets | [source code](https://github.com/googleapis/nodejs-asset/blob/main/samples/exportAssets.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-asset&page=editor&open_in_editor=samples/exportAssets.js,samples/README.md) | +| Export Assets To BigQuery | [source code](https://github.com/googleapis/nodejs-asset/blob/main/samples/exportAssetsBigquery.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-asset&page=editor&open_in_editor=samples/exportAssetsBigquery.js,samples/README.md) | | Get Batch Asset History | [source code](https://github.com/googleapis/nodejs-asset/blob/main/samples/getBatchAssetHistory.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-asset&page=editor&open_in_editor=samples/getBatchAssetHistory.js,samples/README.md) | | Get Feed | [source code](https://github.com/googleapis/nodejs-asset/blob/main/samples/getFeed.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-asset&page=editor&open_in_editor=samples/getFeed.js,samples/README.md) | | List Assets | [source code](https://github.com/googleapis/nodejs-asset/blob/main/samples/listAssets.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-asset&page=editor&open_in_editor=samples/listAssets.js,samples/README.md) | diff --git a/packages/google-cloud-asset/samples/README.md b/packages/google-cloud-asset/samples/README.md index f1b21c7214de..7d4134da4205 100644 --- a/packages/google-cloud-asset/samples/README.md +++ b/packages/google-cloud-asset/samples/README.md @@ -18,6 +18,7 @@ * [Create Feed](#create-feed) * [Delete Feed](#delete-feed) * [Export Assets](#export-assets) + * [Export Assets To BigQuery](#export-assets-to-bigquery) * [Get Batch Asset History](#get-batch-asset-history) * [Get Feed](#get-feed) * [List Assets](#list-assets) @@ -110,7 +111,7 @@ View the [source code](https://github.com/googleapis/nodejs-asset/blob/main/samp __Usage:__ -`node createFeed "storage.googleapis.com/", projects//topics/` +`node createFeed "storage.googleapis.com/", projects//topics/, "RESOURCE"` ----- @@ -148,7 +149,26 @@ View the [source code](https://github.com/googleapis/nodejs-asset/blob/main/samp __Usage:__ -`node exportAssets.js ` +`node exportAssets.js ` + + +----- + + + + +### Export Assets To BigQuery + +Export asserts to specified BigQuery table. + +View the [source code](https://github.com/googleapis/nodejs-asset/blob/main/samples/exportAssetsBigquery.js). + +[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-asset&page=editor&open_in_editor=samples/exportAssetsBigquery.js,samples/README.md) + +__Usage:__ + + +`node exportAssetsBigquery.js ` ----- @@ -205,7 +225,7 @@ View the [source code](https://github.com/googleapis/nodejs-asset/blob/main/samp __Usage:__ -`node listAssets ` +`node listAssets ` ----- diff --git a/packages/google-cloud-asset/samples/createFeed.js b/packages/google-cloud-asset/samples/createFeed.js index 117c837db45c..e139d747cfad 100644 --- a/packages/google-cloud-asset/samples/createFeed.js +++ b/packages/google-cloud-asset/samples/createFeed.js @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -17,10 +17,18 @@ // sample-metadata: // title: Create Feed // description: Create Feed. -// usage: node createFeed "//storage.googleapis.com/", projects//topics/ +// usage: node createFeed "//storage.googleapis.com/", projects//topics/, "RESOURCE" -async function main(feedId, assetNames, topicName) { +async function main(feedId, assetNames, topicName, contentType) { // [START asset_quickstart_create_feed] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const feedId = 'my feed'; + // const assetNames = '//storage.googleapis.com/,//storage.googleapis.com/'; + // const topicName = 'projects//topics/' + // const contentType = 'RESOURCE'; + const util = require('util'); const {AssetServiceClient} = require('@google-cloud/asset'); @@ -36,6 +44,7 @@ async function main(feedId, assetNames, topicName) { feedId: feedId, feed: { assetNames: assetNames.split(','), + contentType: contentType, feedOutputConfig: { pubsubDestination: { topic: topicName, diff --git a/packages/google-cloud-asset/samples/exportAssets.js b/packages/google-cloud-asset/samples/exportAssets.js index bd30bb212c51..176176f68d5b 100644 --- a/packages/google-cloud-asset/samples/exportAssets.js +++ b/packages/google-cloud-asset/samples/exportAssets.js @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -17,10 +17,16 @@ // sample-metadata: // title: Export Assets // description: Export asserts to specified dump file path. -// usage: node exportAssets.js +// usage: node exportAssets.js -async function main(dumpFilePath) { +async function main(dumpFilePath, contentType) { // [START asset_quickstart_export_assets] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const dumpFilePath = 'gs://my-bucket/my-assets.txt'; + // const contentType = 'RESOURCE'; + const {AssetServiceClient} = require('@google-cloud/asset'); const client = new AssetServiceClient(); @@ -30,9 +36,9 @@ async function main(dumpFilePath) { // TODO(developer): choose the dump file path // const dumpFilePath = 'Dump file path, e.g.: gs:///' - const request = { parent: projectResource, + contentType: contentType, outputConfig: { gcsDestination: { uri: dumpFilePath, diff --git a/packages/google-cloud-asset/samples/exportAssetsBigquery.js b/packages/google-cloud-asset/samples/exportAssetsBigquery.js new file mode 100644 index 000000000000..5105fbe0db15 --- /dev/null +++ b/packages/google-cloud-asset/samples/exportAssetsBigquery.js @@ -0,0 +1,66 @@ +// Copyright 2021 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'; + +// sample-metadata: +// title: Export Assets To BigQuery +// description: Export asserts to specified BigQuery table. +// usage: node exportAssetsBigquery.js + +async function main(dataSet, table) { + // [START asset_quickstart_export_assets_bigquery] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const dataSet = 'projects/project_id/datasets/dataset_id'; + // const table = 'mytable'; + + const {AssetServiceClient} = require('@google-cloud/asset'); + const client = new AssetServiceClient(); + + async function exportAssetsBigquery() { + const projectId = await client.getProjectId(); + const projectResource = client.projectPath(projectId); + const dataset = dataSet; + + const request = { + parent: projectResource, + outputConfig: { + bigqueryDestination: { + dataset: `projects/${projectId}/${dataset}`, + table: table, + force: true, + }, + }, + }; + + // Handle the operation using the promise pattern. + const [operation] = await client.exportAssets(request); + + // Operation#promise starts polling for the completion of the operation. + const [result] = await operation.promise(); + + // Do things with with the response. + console.log(result); + } + + exportAssetsBigquery(); + // [END asset_quickstart_export_assets_bigquery] +} + +main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; +}); diff --git a/packages/google-cloud-asset/samples/getBatchAssetHistory.js b/packages/google-cloud-asset/samples/getBatchAssetHistory.js index 38afb6a96f5f..1e664ff96190 100644 --- a/packages/google-cloud-asset/samples/getBatchAssetHistory.js +++ b/packages/google-cloud-asset/samples/getBatchAssetHistory.js @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -19,8 +19,14 @@ // description: Batch get history of assets. // usage: node getBatchAssetHistory "//storage.googleapis.com/" -async function main(assetNames) { +async function main(assetNames, contentType) { // [START asset_quickstart_batch_get_assets_history] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const assetNames = '//storage.googleapis.com/,//storage.googleapis.com/'; + // const contentType = 'RESOURCE'; + const util = require('util'); const {AssetServiceClient} = require('@google-cloud/asset'); @@ -35,7 +41,7 @@ async function main(assetNames) { const request = { parent: projectResource, assetNames: assetNames.split(','), - contentType: 'RESOURCE', + contentType: contentType, readTimeWindow: { startTime: { seconds: Math.floor(new Date().getTime() / 1000), diff --git a/packages/google-cloud-asset/samples/listAssets.js b/packages/google-cloud-asset/samples/listAssets.js index b47636e4e581..36293caf8177 100644 --- a/packages/google-cloud-asset/samples/listAssets.js +++ b/packages/google-cloud-asset/samples/listAssets.js @@ -1,4 +1,4 @@ -// Copyright 2020 Google LLC +// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -17,11 +17,17 @@ // sample-metadata: // title: List Assets // description: List assets under the current project. -// usage: node listAssets -// example: node listAssets "storage.googleapis.com/Bucket,bigquery.googleapis.com/Table" +// usage: node listAssets +// example: node listAssets "storage.googleapis.com/Bucket,bigquery.googleapis.com/Table" 'RESOURCE' -async function main(assetTypes) { +async function main(assetTypes, contentType) { // [START asset_quickstart_list_assets] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const assetTypes = 'storage.googleapis.com/Bucket,bigquery.googleapis.com/Table'; + // const contentType = 'RESOURCE'; + const util = require('util'); const {v1} = require('@google-cloud/asset'); const client = new v1.AssetServiceClient(); @@ -38,7 +44,7 @@ async function main(assetTypes) { const request = { parent: projectResource, assetTypes: assetTypesList, - contentType: 'RESOURCE', + contentType: contentType, // (Optional) Add readTime parameter to list assets at the given time instead of current time: // readTime: { seconds: 1593988758 }, }; diff --git a/packages/google-cloud-asset/samples/test/sample.test.js b/packages/google-cloud-asset/samples/test/sample.test.js index fa474f58dea8..343b8b17123a 100644 --- a/packages/google-cloud-asset/samples/test/sample.test.js +++ b/packages/google-cloud-asset/samples/test/sample.test.js @@ -1,4 +1,4 @@ -// Copyright 2018 Google LLC +// Copyright 2021 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); const storage = new Storage(); const bucketName = `asset-nodejs-${uuid.v4()}`; const bucket = storage.bucket(bucketName); +const fileSuffix = `${uuid.v4()}`; const {BigQuery} = require('@google-cloud/bigquery'); const bigquery = new BigQuery(); @@ -61,14 +62,30 @@ describe('quickstart sample tests', () => { }); it('should export assets to specified path', async () => { - const dumpFilePath = `gs://${bucketName}/my-assets.txt`; + const dumpFilePath = `gs://${bucketName}/my-assets-${fileSuffix}.txt`; execSync(`node exportAssets ${dumpFilePath}`); let waitMs = 1000; let exists = false; let file; for (let retry = 0; retry < 3 && !exists; ++retry) { await sleep((waitMs *= 2)); - file = await bucket.file('my-assets.txt'); + file = await bucket.file(`my-assets-${fileSuffix}.txt`); + exists = await file.exists(); + } + assert.ok(exists); + await file.delete(); + }); + + it('should export asset relationships to specified path', async () => { + const dumpFilePath = `gs://${bucketName}/my-relationships-${fileSuffix}.txt`; + const contentType = 'RELATIONSHIP'; + execSync(`node exportAssets ${dumpFilePath} ${contentType}`); + let waitMs = 1000; + let exists = false; + let file; + for (let retry = 0; retry < 3 && !exists; ++retry) { + await sleep((waitMs *= 2)); + file = await bucket.file(`my-relationships-${fileSuffix}.txt`); exists = await file.exists(); } assert.ok(exists); @@ -83,7 +100,9 @@ describe('quickstart sample tests', () => { let included = false; for (let retry = 0; retry < 3 && !included; ++retry) { await sleep((waitMs *= 2)); - const stdout = execSync(`node getBatchAssetHistory ${assetName}`); + const stdout = execSync( + `node getBatchAssetHistory ${assetName} 'RESOURCE'` + ); included = stdout.includes(assetName); } assert.ok(included); @@ -109,10 +128,15 @@ describe('quickstart sample tests', () => { it('should list assets successfully', async () => { const assetType = 'storage.googleapis.com/Bucket'; - const stdout = execSync(`node listAssets ${assetType}`); + const stdout = execSync(`node listAssets ${assetType} 'RESOURCE'`); assert.include(stdout, assetType); }); + it('should list asset relationship successfully', async () => { + const stdout = execSync("node listAssets '' 'RELATIONSHIP'"); + assert.include(stdout, 'relatedAsset'); + }); + it('should analyze iam policy successfully', async () => { const stdout = execSync('node analyzeIamPolicy'); assert.include(stdout, '//cloudresourcemanager.googleapis.com/projects');