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 b255d0d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ 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.
2. `config.keyFilename` path to a .json, .pem, or .p12 key file.
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 b255d0d

Please sign in to comment.