-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add list_datasets_and_projects sample (#161)
list_datasets_and_projects sample
- Loading branch information
Showing
8 changed files
with
310 additions
and
11 deletions.
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,115 @@ | ||
// Copyright 2016, 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. | ||
|
||
/** | ||
* Command-line application to list all projects and datasets in BigQuery. | ||
* | ||
* This sample is used on this page: | ||
* | ||
* https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects | ||
*/ | ||
|
||
'use strict'; | ||
|
||
// [START all] | ||
// [START auth] | ||
// By default, gcloud will authenticate using the service account file specified | ||
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the | ||
// project specified by the GCLOUD_PROJECT environment variable. See | ||
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication | ||
var gcloud = require('gcloud'); | ||
// [END auth] | ||
|
||
// [START list_tables] | ||
/** | ||
* Retrieve all datasets for the specified project. | ||
* | ||
* @param {string} projectId The project to get datasets from. | ||
* @param {Function} callback Callback function. | ||
*/ | ||
function listDatasets (projectId, callback) { | ||
if (!projectId) { | ||
return callback(new Error('projectId is required!')); | ||
} | ||
var bigquery = gcloud.bigquery({ | ||
projectId: projectId | ||
}); | ||
|
||
bigquery.getDatasets(function (err, datasets) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
console.log('Found %d datasets!', datasets.length); | ||
return callback(null, datasets); | ||
}); | ||
} | ||
// [END list_tables] | ||
|
||
// [START list_projects] | ||
/** | ||
* Retrieve all projects a user has access to. | ||
* | ||
* @param {Function} callback Callback function. | ||
*/ | ||
function listProjects (callback) { | ||
var resource = gcloud.resource(); | ||
|
||
resource.getProjects(function (err, projects) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
|
||
console.log('Found %d projects!', projects.length); | ||
return callback(null, projects); | ||
}); | ||
} | ||
// [END list_projects] | ||
|
||
// [START usage] | ||
function printUsage () { | ||
console.log('Usage: node list_datasets_and_projects [COMMAND] [ARGS...]'); | ||
console.log('\nCommands:\n'); | ||
console.log('\tlist-datasets PROJECT_ID'); | ||
console.log('\tlist-projects'); | ||
} | ||
// [END usage] | ||
|
||
// The command-line program | ||
var program = { | ||
// Print usage instructions | ||
printUsage: printUsage, | ||
|
||
// Exports | ||
listDatasets: listDatasets, | ||
listProjects: listProjects, | ||
|
||
// Run the examples | ||
main: function (args, cb) { | ||
var command = args.shift(); | ||
if (command === 'list-datasets') { | ||
this.listDatasets(args[0], cb); | ||
} else if (command === 'list-projects') { | ||
this.listProjects(cb); | ||
} else { | ||
this.printUsage(); | ||
} | ||
} | ||
}; | ||
|
||
if (module === require.main) { | ||
program.main(process.argv.slice(2), console.log); | ||
} | ||
// [END all] | ||
|
||
module.exports = program; |
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,44 @@ | ||
// Copyright 2016, 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'; | ||
|
||
var example = require('../list_datasets_and_projects'); | ||
var projectId = process.env.GCLOUD_PROJECT || 'nodejs-docs-samples'; | ||
|
||
describe('bigquery:list_datasets_and_projects', function () { | ||
describe('listDatasets', function () { | ||
it('should list datasets', function (done) { | ||
example.listDatasets(projectId, function (err, datasets) { | ||
assert.ifError(err); | ||
assert(Array.isArray(datasets)); | ||
assert(datasets.length > 0); | ||
assert(datasets[0].id); | ||
assert(console.log.calledWith('Found %d datasets!', datasets.length)); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('listProjects', function () { | ||
it('should list projects', function (done) { | ||
example.listProjects(function (err, projects) { | ||
assert.ifError(err); | ||
assert(Array.isArray(projects)); | ||
assert(projects.length > 0); | ||
assert(projects[0].id); | ||
assert(console.log.calledWith('Found %d projects!', projects.length)); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
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,142 @@ | ||
// Copyright 2016, 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'; | ||
|
||
var proxyquire = require('proxyquire').noCallThru(); | ||
|
||
function getSample () { | ||
var datasetsMock = [ | ||
{ | ||
id: 'foo' | ||
} | ||
]; | ||
var projectsMock = [ | ||
{ | ||
id: 'bar' | ||
} | ||
]; | ||
var bigqueryMock = { | ||
getDatasets: sinon.stub().callsArgWith(0, null, datasetsMock) | ||
}; | ||
var resourceMock = { | ||
getProjects: sinon.stub().callsArgWith(0, null, projectsMock) | ||
}; | ||
var gcloudMock = { | ||
bigquery: sinon.stub().returns(bigqueryMock), | ||
resource: sinon.stub().returns(resourceMock) | ||
}; | ||
return { | ||
program: proxyquire('../list_datasets_and_projects', { | ||
gcloud: gcloudMock | ||
}), | ||
mocks: { | ||
gcloud: gcloudMock, | ||
bigquery: bigqueryMock, | ||
resource: resourceMock, | ||
datasets: datasetsMock, | ||
projects: projectsMock | ||
} | ||
}; | ||
} | ||
|
||
describe('bigquery:list_datasets_and_projects', function () { | ||
describe('main', function () { | ||
it('should show usage if no arguments exist', function () { | ||
var program = getSample().program; | ||
|
||
sinon.stub(program, 'printUsage'); | ||
program.main([]); | ||
assert(program.printUsage.calledOnce); | ||
}); | ||
it('should show usage if first argument is -h', function () { | ||
var program = getSample().program; | ||
|
||
sinon.stub(program, 'printUsage'); | ||
program.main(['-h']); | ||
assert(program.printUsage.calledOnce); | ||
|
||
program.main(['--help']); | ||
assert(program.printUsage.calledTwice); | ||
}); | ||
it('should call correct commands', function () { | ||
var program = getSample().program; | ||
|
||
sinon.stub(program, 'listDatasets'); | ||
program.main(['list-datasets']); | ||
assert(program.listDatasets.calledOnce); | ||
|
||
sinon.stub(program, 'listProjects'); | ||
program.main(['list-projects']); | ||
assert(program.listProjects.calledOnce); | ||
}); | ||
}); | ||
|
||
describe('printUsage', function () { | ||
it('should print usage', function () { | ||
var example = getSample(); | ||
example.program.printUsage(); | ||
assert(console.log.calledWith('Usage: node list_datasets_and_projects [COMMAND] [ARGS...]')); | ||
assert(console.log.calledWith('\nCommands:\n')); | ||
assert(console.log.calledWith('\tlist-datasets PROJECT_ID')); | ||
assert(console.log.calledWith('\tlist-projects')); | ||
}); | ||
}); | ||
|
||
describe('listProjects', function () { | ||
it('should list projects', function () { | ||
var example = getSample(); | ||
example.program.listProjects(function (err, projects) { | ||
assert.ifError(err); | ||
assert.strictEqual(projects, example.mocks.projects); | ||
assert(console.log.calledWith('Found %d projects!', projects.length)); | ||
}); | ||
}); | ||
it('should handle error', function () { | ||
var error = 'listProjectsError'; | ||
var example = getSample(); | ||
example.mocks.resource.getProjects = sinon.stub().callsArgWith(0, error); | ||
example.program.listProjects(function (err, projects) { | ||
assert.equal(err, error); | ||
assert.equal(projects, undefined); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('listDatasets', function () { | ||
it('should list datasets', function () { | ||
var example = getSample(); | ||
example.program.listDatasets('googledata', function (err, datasets) { | ||
assert.ifError(err); | ||
assert.strictEqual(datasets, example.mocks.datasets); | ||
assert(console.log.calledWith('Found %d datasets!', datasets.length)); | ||
}); | ||
}); | ||
it('should require a Project ID', function () { | ||
var example = getSample(); | ||
example.program.listDatasets(undefined, function (err) { | ||
assert(err); | ||
assert.equal(err.message, 'projectId is required!'); | ||
}); | ||
}); | ||
it('should handle error', function () { | ||
var error = 'listDatasetsError'; | ||
var example = getSample(); | ||
example.mocks.bigquery.getDatasets = sinon.stub().callsArgWith(0, error); | ||
example.program.listDatasets('googledata', function (err, datasets) { | ||
assert.equal(err, error); | ||
assert.equal(datasets, undefined); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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
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
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
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