From b255d0d73d77892432a204febfa2868a9ea7004d Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Mon, 30 Nov 2015 14:09:05 -0500 Subject: [PATCH] core: recognize GCLOUD_PROJECT env var --- docs/authentication.md | 2 ++ lib/common/util.js | 9 ++++++++- test/common/util.js | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/authentication.md b/docs/authentication.md index 468b5aaa598..373390365a1 100644 --- a/docs/authentication.md +++ b/docs/authentication.md @@ -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. diff --git a/lib/common/util.js b/lib/common/util.js index ca86014e65a..1cb9bc08d9e 100644 --- a/lib/common/util.js +++ b/lib/common/util.js @@ -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(' ')); @@ -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; diff --git a/test/common/util.js b/test/common/util.js index 2a90f7f4058..2bef7673f8c 100644 --- a/test/common/util.js +++ b/test/common/util.js @@ -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(' ')); @@ -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: {} });