Skip to content

Commit

Permalink
sample: Add quickstart sample code for BatchGetAsstesHistory. (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-zheng-g authored and JustinBeckwith committed Nov 27, 2018
1 parent 9298670 commit 2531f8f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
52 changes: 50 additions & 2 deletions asset/snippets/quickstart.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'use strict';

async function exportAssets(dumpFilePath) {
// [START asset_quickstart_exportassets]
// [START asset_quickstart_export_assets]
const asset = require('@google-cloud/asset');
const client = new asset.v1beta1.AssetServiceClient({
// optional auth parameters.
Expand All @@ -43,7 +43,42 @@ async function exportAssets(dumpFilePath) {
const [result] = await operation.promise();
// Do things with with the response.
console.log(result);
// [END asset_quickstart_exportassets]
// [END asset_quickstart_export_assets]
}

async function batchGetAssetsHistory(assetNames) {
// [START asset_quickstart_batch_get_assets_history]
const util = require('util');
const asset = require('@google-cloud/asset');
const client = new asset.v1beta1.AssetServiceClient({
// optional auth parameters.
});

// Your Google Cloud Platform project ID
const projectId = process.env.GCLOUD_PROJECT;
const projectResource = client.projectPath(projectId);
// Your asset names, such as //storage.googleapis.com/[YOUR_BUCKET_NAME].
// var assetNames = ['ASSET_NAME1', 'ASSET_NAME2', ...];

const contentType = 'RESOURCE';
const readTimeWindow = {
startTime: {
seconds: Math.floor(new Date().getTime() / 1000),
},
};

const request = {
parent: projectResource,
assetNames: assetNames,
contentType: contentType,
readTimeWindow: readTimeWindow,
};

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

const cli = require('yargs')
Expand All @@ -54,10 +89,23 @@ const cli = require('yargs')
{},
opts => exportAssets(opts.dumpFilePath)
)
.command(
'batch-get-history <assetNames>',
'Batch get history of assets.',
{},
opts => {
const assetNameList = opts.assetNames.split(',');
batchGetAssetsHistory(assetNameList);
}
)
.example(
'node $0 export-assets gs://my-bucket/my-assets.txt',
'Export assets to gs://my-bucket/my-assets.txt.'
)
.example(
'node $0 batch-get-history "//storage.googleapis.com/<BUCKET_NAME>,"',
'Batch get history of assets //storage.googleapis.com/<BUCKET_NAME> etc.'
)
.wrap(10)
.recommendCommands()
.epilogue(
Expand Down
24 changes: 18 additions & 6 deletions asset/snippets/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
const assert = require('assert');
const path = require('path');
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';
Expand All @@ -29,20 +28,33 @@ const storage = new Storage();
const bucketName = `asset-nodejs-${uuid.v4()}`;
const bucket = storage.bucket(bucketName);

describe('Quickstart sample tests', () => {
describe('quickstart sample tests', () => {
before(tools.checkCredentials);
before(async () => {
tools.checkCredentials();
await bucket.create();
});

after(async () => await bucket.delete());
after(async () => {
await bucket.delete();
});

it('should export assets to specified path', async () => {
const dumpFilePath = util.format('gs://%s/my-assets.txt', bucketName);
const dumpFilePath = `gs://${bucketName}/my-assets.txt`;
await tools.runAsyncWithIO(`${cmd} export-assets ${dumpFilePath}`, cwd);
const file = await bucket.file('my-assets.txt');
const [exists] = await file.exists();
const exists = await file.exists();
assert.ok(exists);
await file.delete();
});

it('should get assets history successfully', async () => {
const assetName = `//storage.googleapis.com/${bucketName}`;
const output = await tools.runAsyncWithIO(
`${cmd} batch-get-history ${assetName}`,
cwd
);
if (output.stdout) {
assert.ok(output.stdout.includes(assetName));
}
});
});

0 comments on commit 2531f8f

Please sign in to comment.