Skip to content

Commit

Permalink
Merge pull request #2856 from GoogleCloudPlatform/nodejs-asset-migration
Browse files Browse the repository at this point in the history
migrate code from googleapis/nodejs-asset
  • Loading branch information
ahrarmonsur committed Nov 18, 2022
2 parents 93e5ef3 + 430d47d commit 47773ed
Show file tree
Hide file tree
Showing 18 changed files with 1,188 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/workflows.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"appengine/hello-world/flexible",
"appengine/hello-world/standard",
"appengine/memcached",
"apengine/metadata/flexible",
"appengine/metadata/flexible",
"appengine/metadata/standard",
"appengine/pubsub",
"appengine/static-files",
Expand Down
57 changes: 57 additions & 0 deletions asset/snippets/analyzeIamPolicy.js
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 asset/snippets/analyzeIamPolicyLongrunningBigquery.js
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));
69 changes: 69 additions & 0 deletions asset/snippets/analyzeIamPolicyLongrunningGcs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// 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));
68 changes: 68 additions & 0 deletions asset/snippets/createFeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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: Create Feed
// description: Create Feed.
// usage: node createFeed <FEED_ID> "//storage.googleapis.com/<BUCKET_NAME>", projects/<PROJECT_ID>/topics/<TOPIC_ID>, "RESOURCE"

async function main(feedId, assetNames, topicName, contentType) {
// [START asset_quickstart_create_feed]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const feedId = 'my feed';
// const assetNames = '//storage.googleapis.com/<BUCKET_NAME1>,//storage.googleapis.com/<BUCKET_NAME2>';
// const topicName = 'projects/<PROJECT_ID>/topics/<TOPIC_ID>'
// const contentType = 'RESOURCE';

const util = require('util');
const {AssetServiceClient} = require('@google-cloud/asset');

const client = new AssetServiceClient();

async function createFeed() {
const projectId = await client.getProjectId();
// TODO(developer): Choose asset names, such as //storage.googleapis.com/[YOUR_BUCKET_NAME].
// const assetNames = ['ASSET_NAME1', 'ASSET_NAME2', ...];

const request = {
parent: `projects/${projectId}`,
feedId: feedId,
feed: {
assetNames: assetNames.split(','),
contentType: contentType,
feedOutputConfig: {
pubsubDestination: {
topic: topicName,
},
},
},
};

// Handle the operation using the promise pattern.
const result = await client.createFeed(request);
// Do things with with the response.
console.log(util.inspect(result, {depth: null}));
// [END asset_quickstart_create_feed]
}
createFeed();
}

main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
46 changes: 46 additions & 0 deletions asset/snippets/deleteFeed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2019 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: Delete Feed
// description: Delete Feed.
// usage: node deleteFeed "project/<PROJECT_NUMBER>/feeds/<FEED_ID>"

async function main(feedName) {
// [START asset_quickstart_delete_feed]
const util = require('util');
const {AssetServiceClient} = require('@google-cloud/asset');

const client = new AssetServiceClient();

async function deleteFeed() {
const request = {
name: feedName,
};

// Handle the operation using the promise pattern.
const result = await client.deleteFeed(request);
// Do things with with the response.
console.log(util.inspect(result, {depth: null}));
// [END asset_quickstart_delete_feed]
}
deleteFeed();
}

main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
67 changes: 67 additions & 0 deletions asset/snippets/exportAssets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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: Export Assets
// description: Export asserts to specified dump file path.
// usage: node exportAssets.js <gs://my-bucket/my-assets.txt> <content_type>

async function main(dumpFilePath, contentType) {
// [START asset_quickstart_export_assets]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const dumpFilePath = 'gs://my-bucket/my-assets.txt';
// const contentType = 'RESOURCE';

const {AssetServiceClient} = require('@google-cloud/asset');
const client = new AssetServiceClient();

async function exportAssets() {
const projectId = await client.getProjectId();
const projectResource = `projects/${projectId}`;

// TODO(developer): choose the dump file path
// const dumpFilePath = 'Dump file path, e.g.: gs://<my_bucket>/<my_asset_file>'
const request = {
parent: projectResource,
contentType: contentType,
outputConfig: {
gcsDestination: {
uri: dumpFilePath,
},
},
};

// Handle the operation using the promise pattern.
const [operation] = await client.exportAssets(request);

// Operation#promise starts polling for the completion of the operation.
const [result] = await operation.promise();

// Do things with with the response.
console.log(result);
}
exportAssets().catch(err => {
throw err;
});
// [END asset_quickstart_export_assets]
}

main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
Loading

0 comments on commit 47773ed

Please sign in to comment.