|
| 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 | +}); |
0 commit comments