From 4b23559c8ccb77d8063b1785d75cf7011b582035 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Wed, 23 Dec 2015 11:50:32 -0800 Subject: [PATCH] Updating default project to also use App Engine and Compute Engine. It seems this was intended to be done awhile ago and fell through the cracks. --- gcloud/_helpers.py | 15 +++++++++++---- gcloud/client.py | 5 ++--- gcloud/test__helpers.py | 27 +++++++++++++++++++++++++-- gcloud/test_client.py | 19 ++++++++++--------- 4 files changed, 48 insertions(+), 18 deletions(-) diff --git a/gcloud/_helpers.py b/gcloud/_helpers.py index 07fcc850e649..3a9cbe2704e4 100644 --- a/gcloud/_helpers.py +++ b/gcloud/_helpers.py @@ -188,11 +188,12 @@ def _get_production_project(): def _determine_default_project(project=None): """Determine default project ID explicitly or implicitly as fall-back. - In implicit case, currently only supports enviroment variable but will - support App Engine, Compute Engine and other environments in the future. + In implicit case, supports three environments. In order of precedence, the + implicit environments are: - Local environment variable used is: - - GCLOUD_PROJECT + * GCLOUD_PROJECT environment variable + * Google App Engine application ID + * Google Compute Engine project ID (from metadata server) :type project: string :param project: Optional. The project name to use as default. @@ -203,6 +204,12 @@ def _determine_default_project(project=None): if project is None: project = _get_production_project() + if project is None: + project = _app_engine_id() + + if project is None: + project = _compute_engine_id() + return project diff --git a/gcloud/client.py b/gcloud/client.py index 81a2486039fa..31aa3c48b690 100644 --- a/gcloud/client.py +++ b/gcloud/client.py @@ -16,7 +16,7 @@ import six -from gcloud._helpers import _get_production_project +from gcloud._helpers import _determine_default_project from gcloud.connection import Connection from gcloud.credentials import get_credentials from gcloud.credentials import get_for_service_account_json @@ -137,8 +137,7 @@ class _ClientProjectMixin(object): """ def __init__(self, project=None): - if project is None: - project = _get_production_project() + project = _determine_default_project(project) if project is None: raise ValueError('Project was not passed and could not be ' 'determined from the environment.') diff --git a/gcloud/test__helpers.py b/gcloud/test__helpers.py index 03e1aede76f3..a046450b7fe0 100644 --- a/gcloud/test__helpers.py +++ b/gcloud/test__helpers.py @@ -214,7 +214,8 @@ def _callFUT(self, project=None): from gcloud._helpers import _determine_default_project return _determine_default_project(project=project) - def _determine_default_helper(self, prod=None, project=None): + def _determine_default_helper(self, prod=None, gae=None, gce=None, + project=None): from gcloud._testing import _Monkey from gcloud import _helpers @@ -224,8 +225,18 @@ def prod_mock(): _callers.append('prod_mock') return prod + def gae_mock(): + _callers.append('gae_mock') + return gae + + def gce_mock(): + _callers.append('gce_mock') + return gce + patched_methods = { '_get_production_project': prod_mock, + '_app_engine_id': gae_mock, + '_compute_engine_id': gce_mock, } with _Monkey(_helpers, **patched_methods): @@ -236,7 +247,7 @@ def prod_mock(): def test_no_value(self): project, callers = self._determine_default_helper() self.assertEqual(project, None) - self.assertEqual(callers, ['prod_mock']) + self.assertEqual(callers, ['prod_mock', 'gae_mock', 'gce_mock']) def test_explicit(self): PROJECT = object() @@ -250,6 +261,18 @@ def test_prod(self): self.assertEqual(project, PROJECT) self.assertEqual(callers, ['prod_mock']) + def test_gae(self): + PROJECT = object() + project, callers = self._determine_default_helper(gae=PROJECT) + self.assertEqual(project, PROJECT) + self.assertEqual(callers, ['prod_mock', 'gae_mock']) + + def test_gce(self): + PROJECT = object() + project, callers = self._determine_default_helper(gce=PROJECT) + self.assertEqual(project, PROJECT) + self.assertEqual(callers, ['prod_mock', 'gae_mock', 'gce_mock']) + class Test__millis(unittest2.TestCase): diff --git a/gcloud/test_client.py b/gcloud/test_client.py index 3caa9d1fd251..582728363f96 100644 --- a/gcloud/test_client.py +++ b/gcloud/test_client.py @@ -149,8 +149,8 @@ def test_ctor_defaults(self): CREDENTIALS = object() FUNC_CALLS = [] - def mock_get_proj(): - FUNC_CALLS.append('_get_production_project') + def mock_determine_proj(project): + FUNC_CALLS.append((project, '_determine_default_project')) return PROJECT def mock_get_credentials(): @@ -158,14 +158,15 @@ def mock_get_credentials(): return CREDENTIALS with _Monkey(client, get_credentials=mock_get_credentials, - _get_production_project=mock_get_proj): + _determine_default_project=mock_determine_proj): client_obj = self._makeOne() self.assertTrue(client_obj.project is PROJECT) self.assertTrue(isinstance(client_obj.connection, _MockConnection)) self.assertTrue(client_obj.connection.credentials is CREDENTIALS) - self.assertEqual(FUNC_CALLS, - ['_get_production_project', 'get_credentials']) + self.assertEqual( + FUNC_CALLS, + [(None, '_determine_default_project'), 'get_credentials']) def test_ctor_missing_project(self): from gcloud._testing import _Monkey @@ -173,14 +174,14 @@ def test_ctor_missing_project(self): FUNC_CALLS = [] - def mock_get_proj(): - FUNC_CALLS.append('_get_production_project') + def mock_determine_proj(project): + FUNC_CALLS.append((project, '_determine_default_project')) return None - with _Monkey(client, _get_production_project=mock_get_proj): + with _Monkey(client, _determine_default_project=mock_determine_proj): self.assertRaises(ValueError, self._makeOne) - self.assertEqual(FUNC_CALLS, ['_get_production_project']) + self.assertEqual(FUNC_CALLS, [(None, '_determine_default_project')]) def test_ctor_w_invalid_project(self): CREDENTIALS = object()