From 320e2f07e9d8a52f28c47678ccc9febc1497f090 Mon Sep 17 00:00:00 2001 From: Peter Zheng Date: Wed, 14 Nov 2018 17:39:50 -0800 Subject: [PATCH 1/4] [Asset] Add quickstart code for ExportAssets API --- asset/quickstart.js | 82 ++++++++++++++++++++++++++++ asset/system-test/quickstart.test.js | 54 ++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 asset/quickstart.js create mode 100644 asset/system-test/quickstart.test.js diff --git a/asset/quickstart.js b/asset/quickstart.js new file mode 100644 index 0000000000..f7469225bf --- /dev/null +++ b/asset/quickstart.js @@ -0,0 +1,82 @@ +/** + * Copyright 2018, Google, Inc. + * 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'; + +// Imports the Google APIs client library +async function exportAssets (dumpFilePath) { + // [START asset_quickstart_exportassets] + const asset = require('@google-cloud/asset'); + var client = new asset.v1beta1.AssetServiceClient({ + // optional auth parameters. + }); + + // Your Google Cloud Platform project ID + const projectId = process.env.GCLOUD_PROJECT; + var projectResource = client.projectPath(projectId); + + // var dumpFilePath = 'Dump file path, e.g.: gs:///' + var outputConfig = { + gcsDestination: { + uri: dumpFilePath + } + }; + var request = { + parent: projectResource, + outputConfig: outputConfig + }; + + // Handle the operation using the promise pattern. + client.exportAssets(request).then(responses => { + var operation = responses[0]; + // Operation#promise starts polling for the completion of the operation. + return operation.promise(); + }).then(responses => { + // The final result of the operation. + var result = responses[0]; + // The metadata value of the completed operation. + // var metadata = responses[1]; + // The response of the api call returning the complete operation. + // var finalApiResponse = responses[2]; + // Do things with with the response. + console.log(result); + }) + .catch(err => { + console.error(err); + }); + // [END asset_quickstart_exportassets] +} + +const cli = require('yargs') + .demand(1) + .command( + `export-assets `, + `Export asserts to specified dump file path.`, + {}, + opts => exportAssets(opts.dumpFilePath) + ) + .example( + `node $0 export-assets gs://my-bucket/my-assets.txt`, + `Export assets to gs://my-bucket/my-assets.txt.` + ) + .wrap(10) + .recommendCommands() + .epilogue(`https://cloud.google.com/resource-manager/docs/cloud-asset-inventory/overview`) + .help() + .strict(); + +if (module === require.main) { + cli.parse(process.argv.slice(1)); +} diff --git a/asset/system-test/quickstart.test.js b/asset/system-test/quickstart.test.js new file mode 100644 index 0000000000..8b13443706 --- /dev/null +++ b/asset/system-test/quickstart.test.js @@ -0,0 +1,54 @@ +/** + * Copyright 2018, Google, Inc. + * 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'; + +const path = require(`path`); +const test = require(`ava`); +const tools = require(`@google-cloud/nodejs-repo-tools`); +const util = require(`util`); +const uuid = require(`uuid`); +const cwd = path.join(__dirname, `..`); +const cmd = `node quickstart.js`; + +const {Storage} = require(`@google-cloud/storage`, {}); + +const storage = new Storage(); +const bucketName = `asset-nodejs-${uuid.v4()}`; +const bucket = storage.bucket(bucketName); + +test.before(tools.checkCredentials); +test.before(async () => { + await bucket.create(); +}); + +test.after.always(async () => { + try { + await bucket.delete(); + } catch (err) {} // ignore error +}); + +test.beforeEach(tools.stubConsole); +test.afterEach.always(tools.restoreConsole); + +test.serial(`should export assets to specified path`, async t => { + var dumpFilePath = util.format('gs://%s/my-assets.txt', bucketName); + await tools.runAsyncWithIO( + `${cmd} export-assets ${dumpFilePath}`, + cwd + ); + const [exists] = await bucket.file(); + t.true(exists); +}); From e268d2df5433dff95407f0106912ff3ec7671f4a Mon Sep 17 00:00:00 2001 From: Peter Zheng Date: Wed, 14 Nov 2018 18:23:24 -0800 Subject: [PATCH 2/4] [Asset] Add package.json and fix bucket cleanup logic --- asset/package.json | 37 ++++++++++++++++++++++++++++ asset/system-test/quickstart.test.js | 8 +++--- 2 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 asset/package.json diff --git a/asset/package.json b/asset/package.json new file mode 100644 index 0000000000..9bafe2b226 --- /dev/null +++ b/asset/package.json @@ -0,0 +1,37 @@ +{ + "name": "@google-cloud/asset-samples", + "description": "Samples for the Cloud Asset Client Library for Node.js.", + "version": "0.0.1", + "license": "Apache-2.0", + "author": "Google Inc.", + "engines": { + "node": ">=8" + }, + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" + }, + "nyc": { + "exclude": [ + "**/*.test.js" + ] + }, + "scripts": { + "test": "repo-tools test run --cmd ava -- -T 20s --verbose system-test/*.test.js" + }, + "dependencies": { + "@google-cloud/pubsub": "*", + "@google-cloud/storage": "^2.3.0", + "@google-cloud/asset": "^0.1.0", + "uuid": "^3.3.2", + "yargs": "^12.0.0" + }, + "devDependencies": { + "@google-cloud/nodejs-repo-tools": "^3.0.0", + "ava": "^0.25.0", + "mocha": "^5.2.0", + "nyc": "^13.0.0", + "proxyquire": "^2.0.1", + "sinon": "^7.0.0" + } +} diff --git a/asset/system-test/quickstart.test.js b/asset/system-test/quickstart.test.js index 8b13443706..bb7c2d96a8 100644 --- a/asset/system-test/quickstart.test.js +++ b/asset/system-test/quickstart.test.js @@ -35,9 +35,7 @@ test.before(async () => { }); test.after.always(async () => { - try { - await bucket.delete(); - } catch (err) {} // ignore error + await bucket.delete(); }); test.beforeEach(tools.stubConsole); @@ -49,6 +47,8 @@ test.serial(`should export assets to specified path`, async t => { `${cmd} export-assets ${dumpFilePath}`, cwd ); - const [exists] = await bucket.file(); + var file = await bucket.file('my-assets.txt'); + const [exists] = await file.exists(); t.true(exists); + file.delete(); }); From 5e4ebf8b9a30b606daa22f72928cb54c1fe1865c Mon Sep 17 00:00:00 2001 From: Peter Zheng Date: Wed, 14 Nov 2018 19:32:01 -0800 Subject: [PATCH 3/4] [Asset] Fix argument parsing --- asset/quickstart.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asset/quickstart.js b/asset/quickstart.js index f7469225bf..1d15cd7820 100644 --- a/asset/quickstart.js +++ b/asset/quickstart.js @@ -78,5 +78,5 @@ const cli = require('yargs') .strict(); if (module === require.main) { - cli.parse(process.argv.slice(1)); + cli.parse(process.argv.slice(2)); } From 340f8e57e102a93cb136ad54a22fa72ea3f94a65 Mon Sep 17 00:00:00 2001 From: Peter Zheng Date: Wed, 14 Nov 2018 19:43:09 -0800 Subject: [PATCH 4/4] [Asset] Minor code style fix --- asset/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/asset/package.json b/asset/package.json index 9bafe2b226..99d6967b9f 100644 --- a/asset/package.json +++ b/asset/package.json @@ -20,9 +20,9 @@ "test": "repo-tools test run --cmd ava -- -T 20s --verbose system-test/*.test.js" }, "dependencies": { - "@google-cloud/pubsub": "*", - "@google-cloud/storage": "^2.3.0", "@google-cloud/asset": "^0.1.0", + "@google-cloud/storage": "^2.3.0", + "express": "^4.16.4", "uuid": "^3.3.2", "yargs": "^12.0.0" },