Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Asset] Add quickstart code for ExportAssets API #906

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions asset/quickstart.js
Original file line number Diff line number Diff line change
@@ -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://<my_bucket>/<my_asset_file>'
var outputConfig = {
gcsDestination: {
uri: dumpFilePath
}
};
var request = {
parent: projectResource,
outputConfig: outputConfig
};

// Handle the operation using the promise pattern.
client.exportAssets(request).then(responses => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you have an async function, you could use await instead of promises, which is much nicer:

let responses = await client.exportAssets(request);
var operation = responses[0];
responses = await operation.promise();
var result = responses[0];

Feel free to leave out the catch in the sample. If the sample throws, we'll see the error either way.

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 <dumpFilePath>`,
`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));
}
54 changes: 54 additions & 0 deletions asset/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -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);
});