Skip to content

Commit

Permalink
Merge pull request #639 from stephenplusplus/spp--common-throw-error-…
Browse files Browse the repository at this point in the history
…on-missing-projectid

require project ID
  • Loading branch information
stephenplusplus committed Jun 5, 2015
2 parents 8e98bdf + 08a0b2d commit 143ecf2
Show file tree
Hide file tree
Showing 22 changed files with 122 additions and 33 deletions.
5 changes: 4 additions & 1 deletion lib/bigquery/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ function Dataset(bigQuery, id) {
* schema: 'UNITID,INSTNM,ADDR,CITY,STABBR,ZIP,FIPS,OBEREG,CHFNM,...'
* };
*
* var bigquery = gcloud.bigquery();
* var bigquery = gcloud.bigquery({
* projectId: 'grape-spaceship-123'
* });
* var dataset = bigquery.dataset();
*
* dataset.createTable(tableConfig, function(err, table, apiResponse) {});
*/
Dataset.prototype.createTable = function(options, callback) {
Expand Down
4 changes: 4 additions & 0 deletions lib/bigquery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ function BigQuery(options) {

options = options || {};

if (!options.projectId) {
throw util.missingProjectIdError;
}

this.makeAuthorizedRequest_ = util.makeAuthorizedRequestFactory({
credentials: options.credentials,
keyFile: options.keyFilename,
Expand Down
2 changes: 1 addition & 1 deletion lib/bigquery/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var util = require('../common/util');
* @param {string} id - The ID of the job.
*
* @example
* var bigquery = gcloud.bigquery();
* var bigquery = gcloud.bigquery({ projectId: 'grape-spaceship-123' });
* var Job = require('gcloud/lib/bigquery/job');
* var job = new Job(bigquery, 'job-id');
*/
Expand Down
6 changes: 4 additions & 2 deletions lib/bigquery/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var util = require('../common/util');
* @param {string} id - The ID of the table.
*
* @example
* var bigquery = gcloud.bigquery();
* var bigquery = gcloud.bigquery({ projectId: 'grape-spaceship-123' });
* var Dataset = require('gcloud/lib/bigquery/dataset');
* var dataset = new Dataset(bigquery, 'dataset-id');
* var Table = require('gcloud/lib/bigquery/table');
Expand Down Expand Up @@ -369,7 +369,9 @@ Table.prototype.delete = function(callback) {
* @throws {Error} If destination format isn't recongized.
*
* @example
* var storage = gcloud.storage();
* var storage = gcloud.storage({
* projectId: 'grape-spaceship-123'
* });
* var exportedFile = storage.bucket('institutions').file('2014.csv');
*
* //-
Expand Down
8 changes: 8 additions & 0 deletions lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ var USER_AGENT = 'gcloud-node/' + PKG.version;

var util = module.exports;

var missingProjectIdError = new Error([
'Sorry, we cannot connect to Google Cloud Services without a project ID.',
'See https://googlecloudplatform.github.io/gcloud-node/#/authorization for',
'a detailed guide on creating an authorized connection.'
].join(' '));

util.missingProjectIdError = missingProjectIdError;

/**
* Extend a global configuration object with user options provided at the time
* of sub-module instantiation.
Expand Down
4 changes: 4 additions & 0 deletions lib/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ function Dataset(options) {

options = options || {};

if (!options.projectId) {
throw util.missingProjectIdError;
}

this.makeAuthorizedRequest_ = util.makeAuthorizedRequestFactory({
customEndpoint: typeof options.apiEndpoint !== 'undefined',
credentials: options.credentials,
Expand Down
18 changes: 11 additions & 7 deletions lib/datastore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,14 @@ var Dataset = require('./dataset');
* @example
* var datastore = gcloud.datastore;
*
* var dataset = datastore.dataset();
* // equal to:
* // datastore.dataset({
* // keyFilename: '/path/to/keyfile.json'
* // });
* // datastore.dataset();
* //
* // is equal to...
* //
* // datastore.dataset({
* // projectId: 'grape-spaceship-123',
* // keyFilename: '/path/to/keyfile.json'
* // });
*/
/**
* The example below will demonstrate the different usage patterns your app may
Expand All @@ -64,8 +67,8 @@ var Dataset = require('./dataset');
*
* @example
* var gcloud = require('gcloud')({
* keyFilename: '/path/to/keyfile.json',
* projectId: 'my-project'
* projectId: 'grape-spaceship-123',
* keyFilename: '/path/to/keyfile.json'
* });
*
* var datastore = gcloud.datastore;
Expand All @@ -84,6 +87,7 @@ function Datastore(options) {
*
* // Create a Dataset object.
* var dataset = datastore.dataset({
* projectId: 'grape-spaceship-123',
* keyFilename: '/path/to/keyfile.json'
* });
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/datastore/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ var util = require('../common/util.js');
* @param {string} kind - Kind to query.
*
* @example
* var dataset = gcloud.datastore.dataset();
* var dataset = gcloud.datastore.dataset({
* projectId: 'grape-spaceship-123'
* });
*
* // If your dataset was scoped to a namespace at initialization, your query
* // will likewise be scoped to that namespace.
Expand Down
4 changes: 2 additions & 2 deletions lib/datastore/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var MODE_TRANSACTIONAL = 'TRANSACTIONAL';
* // This is how to create a transaction object directly using this Transaction
* // class. The following transaction object is created for use in the examples
* // in this file below.
* var dataset = gcloud.datastore.dataset();
* var dataset = gcloud.datastore.dataset({ projectId: 'project-id' });
* var Transaction = require('gcloud/lib/datastore/transaction');
* var transaction = new Transaction(dataset, 'my-project-id');
* transaction.id = '1234'; // Give the transaction an ID.
Expand Down Expand Up @@ -655,7 +655,7 @@ DatastoreRequest.prototype.upsert = function(entities, callback) {
* }
* };
*
* var dataset = gcloud.datastore.dataset();
* var dataset = gcloud.datastore.dataset({ projectId: 'project-id' });
* var callback = function(err, result, apiResponse) {};
* var Transaction = require('gcloud/lib/datastore/transaction');
* var transaction = new Transaction(dataset, 'my-project-id');
Expand Down
2 changes: 1 addition & 1 deletion lib/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var extend = require('extend');
* // This is how to create a transaction object directly using this Transaction
* // class. The following transaction object is created for use in the examples
* // in this file below.
* var dataset = gcloud.datastore.dataset();
* var dataset = gcloud.datastore.dataset({ projectId: 'project-id' });
* var Transaction = require('gcloud/lib/datastore/transaction');
* var transaction = new Transaction(dataset, 'my-project-id');
* transaction.id = '1234'; // Give the transaction an ID.
Expand Down
4 changes: 4 additions & 0 deletions lib/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ var SCOPES = [
function PubSub(options) {
options = options || {};

if (!options.projectId) {
throw util.missingProjectIdError;
}

this.makeAuthorizedRequest_ = util.makeAuthorizedRequestFactory({
credentials: options.credentials,
keyFile: options.keyFilename,
Expand Down
4 changes: 3 additions & 1 deletion lib/pubsub/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ var util = require('../common/util.js');
* @constructor
*
* @example
* var pubsub = gcloud.pubsub();
* var pubsub = gcloud.pubsub({
* projectId: 'grape-spaceship-123'
* });
*
* //-
* // From {@linkcode module:pubsub#getSubscriptions}:
Expand Down
4 changes: 3 additions & 1 deletion lib/pubsub/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ var Subscription = require('./subscription.js');
* @alias module:pubsub/topic
*
* @example
* var pubsub = gcloud.pubsub();
* var pubsub = gcloud.pubsub({
* projectId: 'grape-spaceship-123'
* });
*
* // From pubsub.topic:
* var topic = pubsub.topic('my-existing-topic');
Expand Down
8 changes: 6 additions & 2 deletions lib/storage/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ function Acl(options) {
* @return {object}
*
* @example
* var storage = gcloud.storage();
* var storage = gcloud.storage({
* projectId: 'grape-spaceship-123'
* });
*
* //-
* // Add a user as an owner of a file.
Expand Down Expand Up @@ -381,7 +383,9 @@ Acl.prototype.get = function(options, callback) {
* @alias acl.update
*
* @example
* var storage = gcloud.storage();
* var storage = gcloud.storage({
* projectId: 'grape-spaceship-123'
* });
*
* myBucket.acl.update({
* entity: 'user-useremail@example.com',
Expand Down
12 changes: 4 additions & 8 deletions lib/storage/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,12 @@ var RESUMABLE_THRESHOLD = 5000000;
*
* @example
* var gcloud = require('gcloud');
* var storage = gcloud.storage();
*
* // From Google Compute Engine
* var albums = storage.bucket('albums');
*
* // From elsewhere
* var photos = storage.bucket({
* keyFilename: '/path/to/keyfile.json', // If you have not yet provided it.
* name: 'bucket'
* var storage = gcloud.storage({
* projectId: 'grape-spaceship-123'
* });
*
* var albums = storage.bucket('albums');
*/
function Bucket(storage, name) {
this.metadata = {};
Expand Down
8 changes: 6 additions & 2 deletions lib/storage/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,13 @@ function File(bucket, name, options) {
* //-
* // Make a file publicly readable.
* //-
* var storage = gcloud.storage();
* var storage = gcloud.storage({
* projectId: 'grape-spaceship-123'
* });
*
* var myFile = storage.bucket('my-bucket').file('my-file');
* myFile.acl.add({
*
* myFile.acl.add({
* scope: 'allUsers',
* role: storage.acl.READER_ROLE
* }, function(err, aclObject) {});
Expand Down
10 changes: 9 additions & 1 deletion lib/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ function Storage(options) {

options = options || {};

if (!options.projectId) {
throw util.missingProjectIdError;
}

this.makeAuthorizedRequest_ = util.makeAuthorizedRequestFactory({
credentials: options.credentials,
keyFile: options.keyFilename,
Expand Down Expand Up @@ -111,7 +115,10 @@ function Storage(options) {
* @type {object}
*
* @example
* var storage = gcloud.storage();
* var storage = gcloud.storage({
* projectId: 'grape-spaceship-123'
* });
*
* var albums = storage.bucket('albums');
*
* //-
Expand Down Expand Up @@ -155,6 +162,7 @@ Storage.prototype.acl = Storage.acl;
*
* @example
* var gcloud = require('gcloud')({
* projectId: 'grape-spaceship-123',
* keyFilename: '/path/to/keyfile.json'
* });
*
Expand Down
8 changes: 8 additions & 0 deletions test/bigquery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ describe('BigQuery', function() {
bq = new BigQuery({ projectId: PROJECT_ID });
});

describe('instantiation', function() {
it('should throw if a projectId is not specified', function() {
assert.throws(function() {
new BigQuery();
}, /Sorry, we cannot connect/);
});
});

describe('createDataset', function() {
var DATASET_ID = 'kittens';

Expand Down
14 changes: 14 additions & 0 deletions test/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ describe('common/util', function() {
// Override all util methods, allowing them to be mocked. Overrides are
// removed before each test.
Object.keys(util).forEach(function(utilMethod) {
if (typeof util[utilMethod] !== 'function') {
return;
}

util[utilMethod] = function() {
return (utilOverrides[utilMethod] || util_Cached[utilMethod])
.apply(this, arguments);
Expand All @@ -83,6 +87,16 @@ describe('common/util', function() {
assert.deepEqual(REQUEST_DEFAULT_CONF, { pool: { maxSockets: Infinity } });
});

it('should export an error for module instantiation errors', function() {
var missingProjectIdError = new Error([
'Sorry, we cannot connect to Google Cloud Services without a project ID.',
'See https://googlecloudplatform.github.io/gcloud-node/#/authorization',
'for a detailed guide on creating an authorized connection.'
].join(' '));

assert.deepEqual(util.missingProjectIdError, missingProjectIdError);
});

describe('arrayize', function() {
it('should arrayize if the input is not an array', function() {
assert.deepEqual(util.arrayize('text'), ['text']);
Expand Down
8 changes: 7 additions & 1 deletion test/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ describe('Dataset', function() {
});

describe('instantiation', function() {
it('should throw if a projectId is not specified', function() {
assert.throws(function() {
new Dataset();
}, /Sorry, we cannot connect/);
});

it('should set default API connection details', function() {
var options = { a: 'b', c: 'd' };
var options = { a: 'b', c: 'd', projectId: 'project-id' };
var mockApiEndpoint = 'http://localhost:8080';

Dataset.determineApiEndpoint_ = function (opts) {
Expand Down
10 changes: 9 additions & 1 deletion test/pubsub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ describe('PubSub', function() {
};
});

describe('instantiation', function() {
it('should throw if a projectId is not specified', function() {
assert.throws(function() {
new PubSub();
}, /Sorry, we cannot connect/);
});
});

describe('getTopics', function() {
beforeEach(function() {
pubsub.makeReq_ = function(method, path, q, body, callback) {
Expand Down Expand Up @@ -239,7 +247,7 @@ describe('PubSub', function() {

describe('makeReq_', function() {
it('should pass network requests to the connection object', function(done) {
var pubsub = new PubSub();
var pubsub = new PubSub({ projectId: PROJECT_ID });

pubsub.makeAuthorizedRequest_ = function() {
done();
Expand Down
8 changes: 7 additions & 1 deletion test/storage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ describe('Storage', function() {
storage = new Storage({ projectId: 'project-id' });
});

describe('initialization', function() {
describe('instantiation', function() {
it('should throw if a projectId is not specified', function() {
assert.throws(function() {
new Storage();
}, /Sorry, we cannot connect/);
});

it('should set the project id', function() {
assert.equal(storage.projectId, 'project-id');
});
Expand Down

0 comments on commit 143ecf2

Please sign in to comment.