Skip to content

Commit

Permalink
samples: add relationship support samples (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvvvvvf authored Sep 28, 2021
1 parent 4ab0cc5 commit 6577ce1
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/google-cloud-asset/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
26 changes: 23 additions & 3 deletions packages/google-cloud-asset/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -110,7 +111,7 @@ View the [source code](https://github.com/googleapis/nodejs-asset/blob/main/samp
__Usage:__


`node createFeed <FEED_ID> "storage.googleapis.com/<BUCKET_NAME>", projects/<PROJECT_ID>/topics/<TOPIC_ID>`
`node createFeed <FEED_ID> "storage.googleapis.com/<BUCKET_NAME>", projects/<PROJECT_ID>/topics/<TOPIC_ID>, "RESOURCE"`


-----
Expand Down Expand Up @@ -148,7 +149,26 @@ View the [source code](https://github.com/googleapis/nodejs-asset/blob/main/samp
__Usage:__


`node exportAssets.js <gs:my-bucket/my-assets.txt>`
`node exportAssets.js <gs:my-bucket/my-assets.txt> <content_type>`


-----




### 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 <projects/project_id/datasets/dataset_id> <table_name>`


-----
Expand Down Expand Up @@ -205,7 +225,7 @@ View the [source code](https://github.com/googleapis/nodejs-asset/blob/main/samp
__Usage:__


`node listAssets <ASSET_TYPES>`
`node listAssets <ASSET_TYPES> <content_type>`


-----
Expand Down
15 changes: 12 additions & 3 deletions packages/google-cloud-asset/samples/createFeed.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,10 +17,18 @@
// sample-metadata:
// title: Create Feed
// description: Create Feed.
// usage: node createFeed <FEED_ID> "//storage.googleapis.com/<BUCKET_NAME>", projects/<PROJECT_ID>/topics/<TOPIC_ID>
// usage: node createFeed <FEED_ID> "//storage.googleapis.com/<BUCKET_NAME>", projects/<PROJECT_ID>/topics/<TOPIC_ID>, "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/<BUCKET_NAME1>,//storage.googleapis.com/<BUCKET_NAME2>';
// const topicName = 'projects/<PROJECT_ID>/topics/<TOPIC_ID>'
// const contentType = 'RESOURCE';

const util = require('util');
const {AssetServiceClient} = require('@google-cloud/asset');

Expand All @@ -36,6 +44,7 @@ async function main(feedId, assetNames, topicName) {
feedId: feedId,
feed: {
assetNames: assetNames.split(','),
contentType: contentType,
feedOutputConfig: {
pubsubDestination: {
topic: topicName,
Expand Down
14 changes: 10 additions & 4 deletions packages/google-cloud-asset/samples/exportAssets.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,10 +17,16 @@
// sample-metadata:
// title: Export Assets
// description: Export asserts to specified dump file path.
// usage: node exportAssets.js <gs://my-bucket/my-assets.txt>
// usage: node exportAssets.js <gs://my-bucket/my-assets.txt> <content_type>

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();

Expand All @@ -30,9 +36,9 @@ async function main(dumpFilePath) {

// TODO(developer): choose the dump file path
// const dumpFilePath = 'Dump file path, e.g.: gs://<my_bucket>/<my_asset_file>'

const request = {
parent: projectResource,
contentType: contentType,
outputConfig: {
gcsDestination: {
uri: dumpFilePath,
Expand Down
66 changes: 66 additions & 0 deletions packages/google-cloud-asset/samples/exportAssetsBigquery.js
Original file line number Diff line number Diff line change
@@ -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 <projects/project_id/datasets/dataset_id> <table_name>

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;
});
12 changes: 9 additions & 3 deletions packages/google-cloud-asset/samples/getBatchAssetHistory.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -19,8 +19,14 @@
// description: Batch get history of assets.
// usage: node getBatchAssetHistory "//storage.googleapis.com/<BUCKET_NAME>"

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/<BUCKET_NAME1>,//storage.googleapis.com/<BUCKET_NAME2>';
// const contentType = 'RESOURCE';

const util = require('util');
const {AssetServiceClient} = require('@google-cloud/asset');

Expand All @@ -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),
Expand Down
16 changes: 11 additions & 5 deletions packages/google-cloud-asset/samples/listAssets.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -17,11 +17,17 @@
// sample-metadata:
// title: List Assets
// description: List assets under the current project.
// usage: node listAssets <ASSET_TYPES>
// example: node listAssets "storage.googleapis.com/Bucket,bigquery.googleapis.com/Table"
// usage: node listAssets <ASSET_TYPES> <content_type>
// 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();
Expand All @@ -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 },
};
Expand Down
34 changes: 29 additions & 5 deletions packages/google-cloud-asset/samples/test/sample.test.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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');
Expand Down

0 comments on commit 6577ce1

Please sign in to comment.