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

Updating default project to also use App Engine and Compute Engine. #1331

Merged
merged 1 commit into from
Dec 24, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 11 additions & 4 deletions gcloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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


Expand Down
5 changes: 2 additions & 3 deletions gcloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.')
Expand Down
27 changes: 25 additions & 2 deletions gcloud/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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):
Expand All @@ -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()
Expand All @@ -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):

Expand Down
19 changes: 10 additions & 9 deletions gcloud/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,38 +149,39 @@ 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():
FUNC_CALLS.append('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
from gcloud import client

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()
Expand Down