Skip to content

Commit 5858cc1

Browse files
Ace Nassrijmdobry
authored andcommitted
Add list_datasets_and_projects sample (GoogleCloudPlatform#161)
list_datasets_and_projects sample
1 parent e69606a commit 5858cc1

File tree

8 files changed

+310
-11
lines changed

8 files changed

+310
-11
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
/**
15+
* Command-line application to list all projects and datasets in BigQuery.
16+
*
17+
* This sample is used on this page:
18+
*
19+
* https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects
20+
*/
21+
22+
'use strict';
23+
24+
// [START all]
25+
// [START auth]
26+
// By default, gcloud will authenticate using the service account file specified
27+
// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the
28+
// project specified by the GCLOUD_PROJECT environment variable. See
29+
// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication
30+
var gcloud = require('gcloud');
31+
// [END auth]
32+
33+
// [START list_tables]
34+
/**
35+
* Retrieve all datasets for the specified project.
36+
*
37+
* @param {string} projectId The project to get datasets from.
38+
* @param {Function} callback Callback function.
39+
*/
40+
function listDatasets (projectId, callback) {
41+
if (!projectId) {
42+
return callback(new Error('projectId is required!'));
43+
}
44+
var bigquery = gcloud.bigquery({
45+
projectId: projectId
46+
});
47+
48+
bigquery.getDatasets(function (err, datasets) {
49+
if (err) {
50+
return callback(err);
51+
}
52+
53+
console.log('Found %d datasets!', datasets.length);
54+
return callback(null, datasets);
55+
});
56+
}
57+
// [END list_tables]
58+
59+
// [START list_projects]
60+
/**
61+
* Retrieve all projects a user has access to.
62+
*
63+
* @param {Function} callback Callback function.
64+
*/
65+
function listProjects (callback) {
66+
var resource = gcloud.resource();
67+
68+
resource.getProjects(function (err, projects) {
69+
if (err) {
70+
return callback(err);
71+
}
72+
73+
console.log('Found %d projects!', projects.length);
74+
return callback(null, projects);
75+
});
76+
}
77+
// [END list_projects]
78+
79+
// [START usage]
80+
function printUsage () {
81+
console.log('Usage: node list_datasets_and_projects [COMMAND] [ARGS...]');
82+
console.log('\nCommands:\n');
83+
console.log('\tlist-datasets PROJECT_ID');
84+
console.log('\tlist-projects');
85+
}
86+
// [END usage]
87+
88+
// The command-line program
89+
var program = {
90+
// Print usage instructions
91+
printUsage: printUsage,
92+
93+
// Exports
94+
listDatasets: listDatasets,
95+
listProjects: listProjects,
96+
97+
// Run the examples
98+
main: function (args, cb) {
99+
var command = args.shift();
100+
if (command === 'list-datasets') {
101+
this.listDatasets(args[0], cb);
102+
} else if (command === 'list-projects') {
103+
this.listProjects(cb);
104+
} else {
105+
this.printUsage();
106+
}
107+
}
108+
};
109+
110+
if (module === require.main) {
111+
program.main(process.argv.slice(2), console.log);
112+
}
113+
// [END all]
114+
115+
module.exports = program;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var example = require('../list_datasets_and_projects');
17+
var projectId = process.env.GCLOUD_PROJECT || 'nodejs-docs-samples';
18+
19+
describe('bigquery:list_datasets_and_projects', function () {
20+
describe('listDatasets', function () {
21+
it('should list datasets', function (done) {
22+
example.listDatasets(projectId, function (err, datasets) {
23+
assert.ifError(err);
24+
assert(Array.isArray(datasets));
25+
assert(datasets.length > 0);
26+
assert(datasets[0].id);
27+
assert(console.log.calledWith('Found %d datasets!', datasets.length));
28+
done();
29+
});
30+
});
31+
});
32+
describe('listProjects', function () {
33+
it('should list projects', function (done) {
34+
example.listProjects(function (err, projects) {
35+
assert.ifError(err);
36+
assert(Array.isArray(projects));
37+
assert(projects.length > 0);
38+
assert(projects[0].id);
39+
assert(console.log.calledWith('Found %d projects!', projects.length));
40+
done();
41+
});
42+
});
43+
});
44+
});
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var proxyquire = require('proxyquire').noCallThru();
17+
18+
function getSample () {
19+
var datasetsMock = [
20+
{
21+
id: 'foo'
22+
}
23+
];
24+
var projectsMock = [
25+
{
26+
id: 'bar'
27+
}
28+
];
29+
var bigqueryMock = {
30+
getDatasets: sinon.stub().callsArgWith(0, null, datasetsMock)
31+
};
32+
var resourceMock = {
33+
getProjects: sinon.stub().callsArgWith(0, null, projectsMock)
34+
};
35+
var gcloudMock = {
36+
bigquery: sinon.stub().returns(bigqueryMock),
37+
resource: sinon.stub().returns(resourceMock)
38+
};
39+
return {
40+
program: proxyquire('../list_datasets_and_projects', {
41+
gcloud: gcloudMock
42+
}),
43+
mocks: {
44+
gcloud: gcloudMock,
45+
bigquery: bigqueryMock,
46+
resource: resourceMock,
47+
datasets: datasetsMock,
48+
projects: projectsMock
49+
}
50+
};
51+
}
52+
53+
describe('bigquery:list_datasets_and_projects', function () {
54+
describe('main', function () {
55+
it('should show usage if no arguments exist', function () {
56+
var program = getSample().program;
57+
58+
sinon.stub(program, 'printUsage');
59+
program.main([]);
60+
assert(program.printUsage.calledOnce);
61+
});
62+
it('should show usage if first argument is -h', function () {
63+
var program = getSample().program;
64+
65+
sinon.stub(program, 'printUsage');
66+
program.main(['-h']);
67+
assert(program.printUsage.calledOnce);
68+
69+
program.main(['--help']);
70+
assert(program.printUsage.calledTwice);
71+
});
72+
it('should call correct commands', function () {
73+
var program = getSample().program;
74+
75+
sinon.stub(program, 'listDatasets');
76+
program.main(['list-datasets']);
77+
assert(program.listDatasets.calledOnce);
78+
79+
sinon.stub(program, 'listProjects');
80+
program.main(['list-projects']);
81+
assert(program.listProjects.calledOnce);
82+
});
83+
});
84+
85+
describe('printUsage', function () {
86+
it('should print usage', function () {
87+
var example = getSample();
88+
example.program.printUsage();
89+
assert(console.log.calledWith('Usage: node list_datasets_and_projects [COMMAND] [ARGS...]'));
90+
assert(console.log.calledWith('\nCommands:\n'));
91+
assert(console.log.calledWith('\tlist-datasets PROJECT_ID'));
92+
assert(console.log.calledWith('\tlist-projects'));
93+
});
94+
});
95+
96+
describe('listProjects', function () {
97+
it('should list projects', function () {
98+
var example = getSample();
99+
example.program.listProjects(function (err, projects) {
100+
assert.ifError(err);
101+
assert.strictEqual(projects, example.mocks.projects);
102+
assert(console.log.calledWith('Found %d projects!', projects.length));
103+
});
104+
});
105+
it('should handle error', function () {
106+
var error = 'listProjectsError';
107+
var example = getSample();
108+
example.mocks.resource.getProjects = sinon.stub().callsArgWith(0, error);
109+
example.program.listProjects(function (err, projects) {
110+
assert.equal(err, error);
111+
assert.equal(projects, undefined);
112+
});
113+
});
114+
});
115+
116+
describe('listDatasets', function () {
117+
it('should list datasets', function () {
118+
var example = getSample();
119+
example.program.listDatasets('googledata', function (err, datasets) {
120+
assert.ifError(err);
121+
assert.strictEqual(datasets, example.mocks.datasets);
122+
assert(console.log.calledWith('Found %d datasets!', datasets.length));
123+
});
124+
});
125+
it('should require a Project ID', function () {
126+
var example = getSample();
127+
example.program.listDatasets(undefined, function (err) {
128+
assert(err);
129+
assert.equal(err.message, 'projectId is required!');
130+
});
131+
});
132+
it('should handle error', function () {
133+
var error = 'listDatasetsError';
134+
var example = getSample();
135+
example.mocks.bigquery.getDatasets = sinon.stub().callsArgWith(0, error);
136+
example.program.listDatasets('googledata', function (err, datasets) {
137+
assert.equal(err, error);
138+
assert.equal(datasets, undefined);
139+
});
140+
});
141+
});
142+
});

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@
5050
"scripts": {
5151
"lint": "semistandard",
5252
"pretest": "npm run lint && ./scripts/geddy && ./scripts/clean",
53-
"mocha": "mocha -R spec -S -t 120000 --require intelli-espower-loader ./test/_setup.js '{*,appengine/*,functions/*}/test/*.test.js'",
53+
"mocha": "mocha -R spec -t 120000 --require intelli-espower-loader ./test/_setup.js '{*,appengine/*,functions/*}/test/*.test.js'",
5454
"test": "npm run mocha",
5555
"cover": "nyc --cache npm test && nyc report --reporter=html && nyc report --reporter=lcov",
56-
"system-test": "mocha -R spec -S -t 120000 --require intelli-espower-loader ./system-test/_setup.js '{*,appengine/*}/system-test/*.test.js'",
56+
"system-test": "mocha -R spec -t 120000 --require intelli-espower-loader ./system-test/_setup.js '{*,appengine/*}/system-test/*.test.js'",
5757
"system-cover": "npm run pretest && nyc --cache npm run system-test && nyc report --reporter=html && nyc report --reporter=lcov",
58-
"all-test": "mocha -R spec -S -t 120000 --require intelli-espower-loader ./system-test/_setup.js '{*,appengine/*,functions/*}/test/*.test.js' '{*,appengine/*}/system-test/*.test.js'",
58+
"all-test": "mocha -R spec -t 120000 --require intelli-espower-loader ./system-test/_setup.js '{*,appengine/*,functions/*}/test/*.test.js' '{*,appengine/*}/system-test/*.test.js'",
5959
"all-cover": "npm run pretest && nyc --cache npm run all-test && nyc report --reporter=html && nyc report --reporter=lcov"
6060
},
6161
"devDependencies": {

pubsub/system-test/iam.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var proxyquire = require('proxyquire');
1818
describe('pubsub:iam', function () {
1919
it('should run the sample', function (done) {
2020
proxyquire('../iam', {}).main(function (err, results) {
21-
assert(!err);
21+
assert.ifError(err);
2222
assert(results.length === 8);
2323
// Got topic and apiResponse
2424
assert(results[0].length === 2);

pubsub/system-test/subscription.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var proxyquire = require('proxyquire');
1818
describe('pubsub:subscription', function () {
1919
it('should run the sample', function (done) {
2020
proxyquire('../subscription', {}).main(function (err, results) {
21-
assert(!err);
21+
assert.ifError(err);
2222
assert(results.length === 8);
2323
// Got topic and apiResponse
2424
assert(results[0].length === 2);

system-test/_setup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ global.sinon = sinon;
2222
var log = console.log;
2323

2424
beforeEach(function () {
25-
assert(process.env.GCLOUD_PROJECT, 'Must set GCLOUD_PROJECT environment variable!');
26-
assert(process.env.GOOGLE_APPLICATION_CREDENTIALS, 'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!');
2725
if (process.env.DEBUG) {
2826
sinon.spy(console, 'error');
2927
sinon.spy(console, 'log');
@@ -35,6 +33,8 @@ beforeEach(function () {
3533
}
3634
});
3735
}
36+
assert(process.env.GCLOUD_PROJECT, 'Must set GCLOUD_PROJECT environment variable!');
37+
assert(process.env.GOOGLE_APPLICATION_CREDENTIALS, 'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!');
3838
});
3939

4040
afterEach(function () {

test/_setup.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@
1515

1616
var assert = require('power-assert');
1717
var sinon = require('sinon');
18-
var uuid = require('node-uuid');
1918

2019
global.assert = assert;
2120
global.sinon = sinon;
22-
global.uuid = uuid;
2321

2422
var log = console.log;
2523

2624
beforeEach(function () {
27-
assert(process.env.GCLOUD_PROJECT, 'Must set GCLOUD_PROJECT environment variable!');
28-
assert(process.env.GOOGLE_APPLICATION_CREDENTIALS, 'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!');
2925
if (process.env.DEBUG) {
3026
sinon.spy(console, 'error');
3127
sinon.spy(console, 'log');
@@ -37,6 +33,8 @@ beforeEach(function () {
3733
}
3834
});
3935
}
36+
assert(process.env.GCLOUD_PROJECT, 'Must set GCLOUD_PROJECT environment variable!');
37+
assert(process.env.GOOGLE_APPLICATION_CREDENTIALS, 'Must set GOOGLE_APPLICATION_CREDENTIALS environment variable!');
4038
});
4139

4240
afterEach(function () {

0 commit comments

Comments
 (0)