-
Notifications
You must be signed in to change notification settings - Fork 598
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(samples): add samples for analyzeIamPolicy and analyzeIamPolicyL…
…ongrunning (#433)
- Loading branch information
1 parent
c4e39f9
commit 8cfb843
Showing
5 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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: Analyze Iam Policy | ||
// description: Analyzes accessible IAM policies that match a request. | ||
// usage: node analyzeIamPolicy | ||
|
||
async function main() { | ||
// [START asset_quickstart_analyze_iam_policy] | ||
const util = require('util'); | ||
const {AssetServiceClient} = require('@google-cloud/asset'); | ||
|
||
const client = new AssetServiceClient(); | ||
const projectId = await client.getProjectId(); | ||
|
||
async function analyzeIamPolicy() { | ||
const request = { | ||
analysisQuery: { | ||
scope: `projects/${projectId}`, | ||
resourceSelector: { | ||
fullResourceName: `//cloudresourcemanager.googleapis.com/projects/${projectId}`, | ||
}, | ||
options: { | ||
expandGroups: true, | ||
outputGroupEdges: true, | ||
}, | ||
}, | ||
}; | ||
|
||
// Handle the operation using the promise pattern. | ||
const result = await client.analyzeIamPolicy(request); | ||
// Do things with with the response. | ||
console.log(util.inspect(result, {depth: null})); | ||
} | ||
// [END asset_quickstart_analyze_iam_policy] | ||
analyzeIamPolicy(); | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
main(...process.argv.slice(2)); |
71 changes: 71 additions & 0 deletions
71
packages/google-cloud-asset/samples/analyzeIamPolicyLongrunningBigquery.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// 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: Analyze Iam Policy Longrunning and write results to Bigquery | ||
// description: Analyzes accessible IAM policies that match a request. | ||
// usage: node analyzeIamPolicyLongrunningBigquery <dataset_id> <table_prefix> | ||
|
||
async function main(datasetId, tablePrefix) { | ||
// [START asset_quickstart_analyze_iam_policy_longrunning_bigquery] | ||
const util = require('util'); | ||
const {AssetServiceClient} = require('@google-cloud/asset'); | ||
|
||
const client = new AssetServiceClient(); | ||
const projectId = await client.getProjectId(); | ||
|
||
async function analyzeIamPolicyLongrunningBigquery() { | ||
// TODO(developer): choose the dataset and table prefix | ||
// const datasetId = '' | ||
// const tablePrefix = '' | ||
|
||
const request = { | ||
analysisQuery: { | ||
scope: `projects/${projectId}`, | ||
resourceSelector: { | ||
fullResourceName: `//cloudresourcemanager.googleapis.com/projects/${projectId}`, | ||
}, | ||
options: { | ||
expandGroups: true, | ||
outputGroupEdges: true, | ||
}, | ||
}, | ||
outputConfig: { | ||
bigqueryDestination: { | ||
dataset: `projects/${projectId}/datasets/${datasetId}`, | ||
tablePrefix: tablePrefix, | ||
}, | ||
}, | ||
}; | ||
|
||
// Handle the operation using the promise pattern. | ||
const [operation] = await client.analyzeIamPolicyLongrunning(request); | ||
|
||
// Operation#promise starts polling for the completion of the operation. | ||
const [result] = await operation.promise(); | ||
|
||
// Do things with with the response. | ||
console.log(util.inspect(result, {depth: null})); | ||
} | ||
// [END asset_quickstart_analyze_iam_policy_longrunning_bigquery] | ||
analyzeIamPolicyLongrunningBigquery(); | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
main(...process.argv.slice(2)); |
70 changes: 70 additions & 0 deletions
70
packages/google-cloud-asset/samples/analyzeIamPolicyLongrunningGcs.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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: Analyze Iam Policy Longrunning and write results to GCS | ||
// description: Analyzes accessible IAM policies that match a request. | ||
// usage: node analyzeIamPolicyLongrunningGcs | ||
// <gs://my-bucket/my-analysis.json> | ||
|
||
async function main(gcsUri) { | ||
// [START asset_quickstart_analyze_iam_policy_longrunning_gcs] | ||
const util = require('util'); | ||
const {AssetServiceClient} = require('@google-cloud/asset'); | ||
|
||
const client = new AssetServiceClient(); | ||
const projectId = await client.getProjectId(); | ||
|
||
async function analyzeIamPolicyLongrunningGcs() { | ||
// TODO(developer): choose the gcs path uri | ||
// const gcsUri = 'Gcs path uri, e.g.: gs://<my_bucket>/<my_analysis_file>' | ||
|
||
const request = { | ||
analysisQuery: { | ||
scope: `projects/${projectId}`, | ||
resourceSelector: { | ||
fullResourceName: `//cloudresourcemanager.googleapis.com/projects/${projectId}`, | ||
}, | ||
options: { | ||
expandGroups: true, | ||
outputGroupEdges: true, | ||
}, | ||
}, | ||
outputConfig: { | ||
gcsDestination: { | ||
uri: gcsUri, | ||
}, | ||
}, | ||
}; | ||
|
||
// Handle the operation using the promise pattern. | ||
const [operation] = await client.analyzeIamPolicyLongrunning(request); | ||
|
||
// Operation#promise starts polling for the completion of the operation. | ||
const [result] = await operation.promise(); | ||
|
||
// Do things with with the response. | ||
console.log(util.inspect(result, {depth: null})); | ||
} | ||
// [END asset_quickstart_analyze_iam_policy_longrunning_gcs] | ||
analyzeIamPolicyLongrunningGcs(); | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
main(...process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters