Skip to content

Commit d88946c

Browse files
feat(samples): add samples for analyzeIamPolicy and analyzeIamPolicyLongrunning (#433)
1 parent 06e30b0 commit d88946c

4 files changed

Lines changed: 199 additions & 0 deletions

File tree

asset/snippets/analyzeIamPolicy.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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
19+
// description: Analyzes accessible IAM policies that match a request.
20+
// usage: node analyzeIamPolicy
21+
22+
async function main() {
23+
// [START asset_quickstart_analyze_iam_policy]
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 analyzeIamPolicy() {
31+
const request = {
32+
analysisQuery: {
33+
scope: `projects/${projectId}`,
34+
resourceSelector: {
35+
fullResourceName: `//cloudresourcemanager.googleapis.com/projects/${projectId}`,
36+
},
37+
options: {
38+
expandGroups: true,
39+
outputGroupEdges: true,
40+
},
41+
},
42+
};
43+
44+
// Handle the operation using the promise pattern.
45+
const result = await client.analyzeIamPolicy(request);
46+
// Do things with with the response.
47+
console.log(util.inspect(result, {depth: null}));
48+
}
49+
// [END asset_quickstart_analyze_iam_policy]
50+
analyzeIamPolicy();
51+
}
52+
53+
process.on('unhandledRejection', err => {
54+
console.error(err.message);
55+
process.exitCode = 1;
56+
});
57+
main(...process.argv.slice(2));
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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));
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 GCS
19+
// description: Analyzes accessible IAM policies that match a request.
20+
// usage: node analyzeIamPolicyLongrunningGcs
21+
// <gs://my-bucket/my-analysis.json>
22+
23+
async function main(gcsUri) {
24+
// [START asset_quickstart_analyze_iam_policy_longrunning_gcs]
25+
const util = require('util');
26+
const {AssetServiceClient} = require('@google-cloud/asset');
27+
28+
const client = new AssetServiceClient();
29+
const projectId = await client.getProjectId();
30+
31+
async function analyzeIamPolicyLongrunningGcs() {
32+
// TODO(developer): choose the gcs path uri
33+
// const gcsUri = 'Gcs path uri, e.g.: gs://<my_bucket>/<my_analysis_file>'
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+
gcsDestination: {
48+
uri: gcsUri,
49+
},
50+
},
51+
};
52+
53+
// Handle the operation using the promise pattern.
54+
const [operation] = await client.analyzeIamPolicyLongrunning(request);
55+
56+
// Operation#promise starts polling for the completion of the operation.
57+
const [result] = await operation.promise();
58+
59+
// Do things with with the response.
60+
console.log(util.inspect(result, {depth: null}));
61+
}
62+
// [END asset_quickstart_analyze_iam_policy_longrunning_gcs]
63+
analyzeIamPolicyLongrunningGcs();
64+
}
65+
66+
process.on('unhandledRejection', err => {
67+
console.error(err.message);
68+
process.exitCode = 1;
69+
});
70+
main(...process.argv.slice(2));

asset/snippets/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
},
1717
"dependencies": {
1818
"@google-cloud/asset": "^3.11.0",
19+
"@google-cloud/bigquery": "^5.5.0",
1920
"@google-cloud/compute": "^2.0.0",
2021
"@google-cloud/storage": "^5.0.0",
2122
"uuid": "^8.0.0",

0 commit comments

Comments
 (0)