-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Closed
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
320e2f0
[Asset] Add quickstart code for ExportAssets API
peter-zheng-g 68ddcee
Merge branch 'master' into master
peter-zheng-g e268d2d
[Asset] Add package.json and fix bucket cleanup logic
peter-zheng-g 5a9ec4c
Merge remote-tracking branch 'refs/remotes/origin/master'
peter-zheng-g 5e4ebf8
[Asset] Fix argument parsing
peter-zheng-g 340f8e5
[Asset] Minor code style fix
peter-zheng-g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 => { | ||
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 useawait
instead of promises, which is much nicer:Feel free to leave out the
catch
in the sample. If the sample throws, we'll see the error either way.