|
| 1 | +// Copyright 2021 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: Analyze Iam Policy Longrunning and write results to Bigquery |
| 19 | +// description: Analyzes accessible IAM policies that match a request. |
| 20 | +// usage: node analyzeIamPolicyLongrunningBigquery <dataset_id> <table_prefix> |
| 21 | + |
| 22 | +async function main(datasetId, tablePrefix) { |
| 23 | + // [START asset_quickstart_analyze_iam_policy_longrunning_bigquery] |
| 24 | + const util = require('util'); |
| 25 | + const {AssetServiceClient} = require('@google-cloud/asset'); |
| 26 | + |
| 27 | + const client = new AssetServiceClient(); |
| 28 | + const projectId = await client.getProjectId(); |
| 29 | + |
| 30 | + async function analyzeIamPolicyLongrunningBigquery() { |
| 31 | + // TODO(developer): choose the dataset and table prefix |
| 32 | + // const datasetId = '' |
| 33 | + // const tablePrefix = '' |
| 34 | + |
| 35 | + const request = { |
| 36 | + analysisQuery: { |
| 37 | + scope: `projects/${projectId}`, |
| 38 | + resourceSelector: { |
| 39 | + fullResourceName: `//cloudresourcemanager.googleapis.com/projects/${projectId}`, |
| 40 | + }, |
| 41 | + options: { |
| 42 | + expandGroups: true, |
| 43 | + outputGroupEdges: true, |
| 44 | + }, |
| 45 | + }, |
| 46 | + outputConfig: { |
| 47 | + bigqueryDestination: { |
| 48 | + dataset: `projects/${projectId}/datasets/${datasetId}`, |
| 49 | + tablePrefix: tablePrefix, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + // Handle the operation using the promise pattern. |
| 55 | + const [operation] = await client.analyzeIamPolicyLongrunning(request); |
| 56 | + |
| 57 | + // Operation#promise starts polling for the completion of the operation. |
| 58 | + const [result] = await operation.promise(); |
| 59 | + |
| 60 | + // Do things with with the response. |
| 61 | + console.log(util.inspect(result, {depth: null})); |
| 62 | + } |
| 63 | + // [END asset_quickstart_analyze_iam_policy_longrunning_bigquery] |
| 64 | + analyzeIamPolicyLongrunningBigquery(); |
| 65 | +} |
| 66 | + |
| 67 | +process.on('unhandledRejection', err => { |
| 68 | + console.error(err.message); |
| 69 | + process.exitCode = 1; |
| 70 | +}); |
| 71 | +main(...process.argv.slice(2)); |
0 commit comments