Skip to content

Commit 47773ed

Browse files
authored
Merge pull request #2856 from GoogleCloudPlatform/nodejs-asset-migration
migrate code from googleapis/nodejs-asset
2 parents 93e5ef3 + 430d47d commit 47773ed

18 files changed

+1188
-1
lines changed

.github/workflows/workflows.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"appengine/hello-world/flexible",
88
"appengine/hello-world/standard",
99
"appengine/memcached",
10-
"apengine/metadata/flexible",
10+
"appengine/metadata/flexible",
1111
"appengine/metadata/standard",
1212
"appengine/pubsub",
1313
"appengine/static-files",

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

asset/snippets/createFeed.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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: Create Feed
19+
// description: Create Feed.
20+
// usage: node createFeed <FEED_ID> "//storage.googleapis.com/<BUCKET_NAME>", projects/<PROJECT_ID>/topics/<TOPIC_ID>, "RESOURCE"
21+
22+
async function main(feedId, assetNames, topicName, contentType) {
23+
// [START asset_quickstart_create_feed]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const feedId = 'my feed';
28+
// const assetNames = '//storage.googleapis.com/<BUCKET_NAME1>,//storage.googleapis.com/<BUCKET_NAME2>';
29+
// const topicName = 'projects/<PROJECT_ID>/topics/<TOPIC_ID>'
30+
// const contentType = 'RESOURCE';
31+
32+
const util = require('util');
33+
const {AssetServiceClient} = require('@google-cloud/asset');
34+
35+
const client = new AssetServiceClient();
36+
37+
async function createFeed() {
38+
const projectId = await client.getProjectId();
39+
// TODO(developer): Choose asset names, such as //storage.googleapis.com/[YOUR_BUCKET_NAME].
40+
// const assetNames = ['ASSET_NAME1', 'ASSET_NAME2', ...];
41+
42+
const request = {
43+
parent: `projects/${projectId}`,
44+
feedId: feedId,
45+
feed: {
46+
assetNames: assetNames.split(','),
47+
contentType: contentType,
48+
feedOutputConfig: {
49+
pubsubDestination: {
50+
topic: topicName,
51+
},
52+
},
53+
},
54+
};
55+
56+
// Handle the operation using the promise pattern.
57+
const result = await client.createFeed(request);
58+
// Do things with with the response.
59+
console.log(util.inspect(result, {depth: null}));
60+
// [END asset_quickstart_create_feed]
61+
}
62+
createFeed();
63+
}
64+
65+
main(...process.argv.slice(2)).catch(err => {
66+
console.error(err.message);
67+
process.exitCode = 1;
68+
});

asset/snippets/deleteFeed.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2019 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: Delete Feed
19+
// description: Delete Feed.
20+
// usage: node deleteFeed "project/<PROJECT_NUMBER>/feeds/<FEED_ID>"
21+
22+
async function main(feedName) {
23+
// [START asset_quickstart_delete_feed]
24+
const util = require('util');
25+
const {AssetServiceClient} = require('@google-cloud/asset');
26+
27+
const client = new AssetServiceClient();
28+
29+
async function deleteFeed() {
30+
const request = {
31+
name: feedName,
32+
};
33+
34+
// Handle the operation using the promise pattern.
35+
const result = await client.deleteFeed(request);
36+
// Do things with with the response.
37+
console.log(util.inspect(result, {depth: null}));
38+
// [END asset_quickstart_delete_feed]
39+
}
40+
deleteFeed();
41+
}
42+
43+
main(...process.argv.slice(2)).catch(err => {
44+
console.error(err.message);
45+
process.exitCode = 1;
46+
});

asset/snippets/exportAssets.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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: Export Assets
19+
// description: Export asserts to specified dump file path.
20+
// usage: node exportAssets.js <gs://my-bucket/my-assets.txt> <content_type>
21+
22+
async function main(dumpFilePath, contentType) {
23+
// [START asset_quickstart_export_assets]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const dumpFilePath = 'gs://my-bucket/my-assets.txt';
28+
// const contentType = 'RESOURCE';
29+
30+
const {AssetServiceClient} = require('@google-cloud/asset');
31+
const client = new AssetServiceClient();
32+
33+
async function exportAssets() {
34+
const projectId = await client.getProjectId();
35+
const projectResource = `projects/${projectId}`;
36+
37+
// TODO(developer): choose the dump file path
38+
// const dumpFilePath = 'Dump file path, e.g.: gs://<my_bucket>/<my_asset_file>'
39+
const request = {
40+
parent: projectResource,
41+
contentType: contentType,
42+
outputConfig: {
43+
gcsDestination: {
44+
uri: dumpFilePath,
45+
},
46+
},
47+
};
48+
49+
// Handle the operation using the promise pattern.
50+
const [operation] = await client.exportAssets(request);
51+
52+
// Operation#promise starts polling for the completion of the operation.
53+
const [result] = await operation.promise();
54+
55+
// Do things with with the response.
56+
console.log(result);
57+
}
58+
exportAssets().catch(err => {
59+
throw err;
60+
});
61+
// [END asset_quickstart_export_assets]
62+
}
63+
64+
main(...process.argv.slice(2)).catch(err => {
65+
console.error(err.message);
66+
process.exitCode = 1;
67+
});

0 commit comments

Comments
 (0)