Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: recognize GCLOUD_PROJECT env var #978

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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