Skip to content

Commit 4fb09e7

Browse files
peter-zheng-gJustinBeckwith
authored andcommitted
docs(samples): add quickstart code for ExportAssets (#50)
1 parent 4ac4bef commit 4fb09e7

File tree

3 files changed

+117
-13
lines changed

3 files changed

+117
-13
lines changed

asset/snippets/package.json

+13-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,19 @@
1414
"**/*.test.js"
1515
]
1616
},
17-
"scripts": {},
17+
"scripts": {
18+
"test": "mocha system-test --timeout 20000"
19+
},
1820
"dependencies": {
19-
"@google-cloud/asset": "^0.1.0"
21+
"@google-cloud/asset": "^0.1.0",
22+
"@google-cloud/storage": "^2.3.0",
23+
"express": "^4.16.4",
24+
"uuid": "^3.3.2",
25+
"yargs": "^12.0.0"
2026
},
21-
"devDependencies": {}
27+
"devDependencies": {
28+
"@google-cloud/nodejs-repo-tools": "^3.0.0",
29+
"mocha": "^5.2.0",
30+
"sinon": "^7.0.0"
31+
}
2232
}

asset/snippets/quickstart.js

+53-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2018, Google LLC.
2+
* Copyright 2018, Google, LLC.
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
55
* You may obtain a copy of the License at
@@ -15,14 +15,57 @@
1515

1616
'use strict';
1717

18-
// [START asset_quickstart]
19-
// Imports the Google Cloud client library
20-
const asset = require('@google-cloud/asset');
18+
async function exportAssets(dumpFilePath) {
19+
// [START asset_quickstart_exportassets]
20+
const asset = require('@google-cloud/asset');
21+
const client = new asset.v1beta1.AssetServiceClient({
22+
// optional auth parameters.
23+
});
2124

22-
// eslint-disable-next-line
23-
const client = new asset.AssetServiceClient({
24-
projectId: 'your-project-id',
25-
keyFilename: '/path/to/keyfile.json',
26-
});
25+
// Your Google Cloud Platform project ID
26+
const projectId = process.env.GCLOUD_PROJECT;
27+
const projectResource = client.projectPath(projectId);
2728

28-
// [END asset_quickstart]
29+
// var dumpFilePath = 'Dump file path, e.g.: gs://<my_bucket>/<my_asset_file>'
30+
const outputConfig = {
31+
gcsDestination: {
32+
uri: dumpFilePath,
33+
},
34+
};
35+
const request = {
36+
parent: projectResource,
37+
outputConfig: outputConfig,
38+
};
39+
40+
// Handle the operation using the promise pattern.
41+
const [operation] = await client.exportAssets(request);
42+
// Operation#promise starts polling for the completion of the operation.
43+
const [result] = await operation.promise();
44+
// Do things with with the response.
45+
console.log(result);
46+
// [END asset_quickstart_exportassets]
47+
}
48+
49+
const cli = require('yargs')
50+
.demand(1)
51+
.command(
52+
'export-assets <dumpFilePath>',
53+
'Export asserts to specified dump file path.',
54+
{},
55+
opts => exportAssets(opts.dumpFilePath)
56+
)
57+
.example(
58+
'node $0 export-assets gs://my-bucket/my-assets.txt',
59+
'Export assets to gs://my-bucket/my-assets.txt.'
60+
)
61+
.wrap(10)
62+
.recommendCommands()
63+
.epilogue(
64+
'https://cloud.google.com/resource-manager/docs/cloud-asset-inventory/overview'
65+
)
66+
.help()
67+
.strict();
68+
69+
if (module === require.main) {
70+
cli.parse(process.argv.slice(2));
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2018, Google, LLC.
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+
16+
'use strict';
17+
18+
const assert = require('assert');
19+
const path = require('path');
20+
const test = require('mocha');
21+
const tools = require('@google-cloud/nodejs-repo-tools');
22+
const util = require('util');
23+
const uuid = require('uuid');
24+
const cwd = path.join(__dirname, '..');
25+
const cmd = 'node quickstart.js';
26+
27+
const {Storage} = require('@google-cloud/storage');
28+
29+
const storage = new Storage();
30+
const bucketName = `asset-nodejs-${uuid.v4()}`;
31+
const bucket = storage.bucket(bucketName);
32+
33+
test.describe('quickstart sample tests', () => {
34+
test.before(tools.checkCredentials);
35+
test.before(async () => {
36+
await bucket.create();
37+
});
38+
39+
test.after(async () => {
40+
await bucket.delete();
41+
});
42+
43+
test.it('should export assets to specified path', async () => {
44+
const dumpFilePath = util.format('gs://%s/my-assets.txt', bucketName);
45+
await tools.runAsyncWithIO(`${cmd} export-assets ${dumpFilePath}`, cwd);
46+
const file = await bucket.file('my-assets.txt');
47+
const [exists] = await file.exists();
48+
assert.ok(exists);
49+
await file.delete();
50+
});
51+
});

0 commit comments

Comments
 (0)