Skip to content

Commit

Permalink
core: recognize GCLOUD_PROJECT env var
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenplusplus committed Nov 30, 2015
1 parent 7e75ae1 commit adf3af6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var gcloud = require('gcloud')(config);
A `config` object requires the following properties:

1. `projectId`
If you wish, you can set an environment variable (`GCLOUD_PROJECT`) in place of specifying this inline.

2. One of the following:
1. `config.credentials` object containing `client_email` and `private_key` properties.
Expand Down
9 changes: 8 additions & 1 deletion lib/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var util = module.exports;

var missingProjectIdError = new Error([
'Sorry, we cannot connect to Google Cloud Services without a project ID.',
'You may specify one with an environment variable named "GCLOUD_PROJECT".',
'See https://googlecloudplatform.github.io/gcloud-node/#/authentication for',
'a detailed guide on creating an authenticated connection.'
].join(' '));
Expand Down Expand Up @@ -90,7 +91,13 @@ function extendGlobalConfig(globalConfig, overrides) {
delete options.keyFilename;
}

return extend(true, {}, options, overrides);
var defaults = {};

if (process.env.GCLOUD_PROJECT) {
defaults.projectId = process.env.GCLOUD_PROJECT;
}

return extend(true, defaults, options, overrides);
}

util.extendGlobalConfig = extendGlobalConfig;
Expand Down
22 changes: 22 additions & 0 deletions test/common/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ describe('common/util', function() {
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.',
'You may specify one with an environment variable named',
'"GCLOUD_PROJECT".',
'See https://googlecloudplatform.github.io/gcloud-node/#/authentication',
'for a detailed guide on creating an authenticated connection.'
].join(' '));
Expand Down Expand Up @@ -200,6 +202,26 @@ describe('common/util', function() {
assert.deepEqual(options, { credentials: {} });
});

it('should honor the GCLOUD_PROJECT environment variable', function() {
var newProjectId = 'envvar-project-id';
var cachedProjectId = process.env.GCLOUD_PROJECT;
process.env.GCLOUD_PROJECT = newProjectId;

// No projectId specified:
var globalConfig = { keyFilename: 'key.json' };
var overrides = {};

var options = util.extendGlobalConfig(globalConfig, overrides);

if (cachedProjectId) {
process.env.GCLOUD_PROJECT = cachedProjectId;
} else {
delete process.env.GCLOUD_PROJECT;
}

assert.strictEqual(options.projectId, newProjectId);
});

it('should not modify original object', function() {
var globalConfig = { keyFilename: 'key.json' };
util.extendGlobalConfig(globalConfig, { credentials: {} });
Expand Down

0 comments on commit adf3af6

Please sign in to comment.