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 0829ce4 commit 67950ff
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 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
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 67950ff

Please sign in to comment.