Skip to content

Commit ab89691

Browse files
authored
feat(samples): add sample code for ListAssets v1p5beta1 (#355)
1 parent 5f1bdb7 commit ab89691

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
// sample-metadata:
18+
// title: List Assets
19+
// description: List assets under the current project.
20+
// usage: node listAssets <ASSET_TYPES>
21+
// example: node listAssets "storage.googleapis.com/Bucket,bigquery.googleapis.com/Table"
22+
23+
async function main(assetTypes) {
24+
// [START asset_quickstart_list_assets]
25+
const util = require('util');
26+
const {v1p5beta1} = require('@google-cloud/asset');
27+
const client = new v1p5beta1.AssetServiceClient();
28+
29+
const projectId = await client.getProjectId();
30+
const projectResource = `projects/${projectId}`;
31+
// TODO(developer): Choose types of assets to list, such as 'storage.googleapis.com/Bucket':
32+
// const assetTypes = 'storage.googleapis.com/Bucket,bigquery.googleapis.com/Table';
33+
// Or simply use empty string to list all types of assets:
34+
// const assetTypes = '';
35+
const assetTypesList = assetTypes ? assetTypes.split(',') : [];
36+
37+
async function listAssets() {
38+
const request = {
39+
parent: projectResource,
40+
assetTypes: assetTypesList,
41+
contentType: 'RESOURCE',
42+
// (Optional) Add readTime parameter to list assets at the given time instead of current time:
43+
// readTime: { seconds: 1593988758 },
44+
};
45+
46+
// Call cloud.assets.v1p5beta1.ListAssets API.
47+
const result = await client.listAssets(request);
48+
// Handle the response.
49+
console.log(util.inspect(result, {depth: null}));
50+
}
51+
listAssets();
52+
// [END asset_quickstart_list_assets]
53+
}
54+
55+
main(...process.argv.slice(2)).catch(err => {
56+
console.error(err.message);
57+
process.exitCode = 1;
58+
});

packages/google-cloud-asset/samples/test/sample.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,10 @@ describe('quickstart sample tests', () => {
9191
const stdout = execSync(`node searchAllIamPolicies '' ${query}`);
9292
assert.include(stdout, 'roles/owner');
9393
});
94+
95+
it('should list assets successfully', async () => {
96+
const assetType = 'storage.googleapis.com/Bucket';
97+
const stdout = execSync(`node listAssets ${assetType}`);
98+
assert.include(stdout, assetType);
99+
});
94100
});

0 commit comments

Comments
 (0)