Check for the project ID env var before warning about missing project ID (#227)
* Check for project_id envvar before warning
Resolves GoogleCloudPlatform/google-auth-library-python#221
* Add missed env check and fix line length
* Remove redundant missing-project log statements
* Add _default.default() test without project_id
* Fix line length
diff --git a/google/auth/_default.py b/google/auth/_default.py
index 5410a4c..d63dcee 100644
--- a/google/auth/_default.py
+++ b/google/auth/_default.py
@@ -129,12 +129,6 @@
if not project_id:
project_id = _cloud_sdk.get_project_id()
- if not project_id:
- _LOGGER.warning(
- 'No project ID could be determined from the Cloud SDK '
- 'configuration. Consider running `gcloud config set project` or '
- 'setting the %s environment variable', environment_vars.PROJECT)
-
return credentials, project_id
@@ -147,12 +141,6 @@
credentials, project_id = _load_credentials_from_file(
os.environ[environment_vars.CREDENTIALS])
- if not project_id:
- _LOGGER.warning(
- 'No project ID could be determined from the credentials at %s '
- 'Consider setting the %s environment variable',
- environment_vars.CREDENTIALS, environment_vars.PROJECT)
-
return credentials, project_id
else:
@@ -188,10 +176,6 @@
try:
project_id = _metadata.get_project_id(request=request)
except exceptions.TransportError:
- _LOGGER.warning(
- 'No project ID could be determined from the Compute Engine '
- 'metadata service. Consider setting the %s environment '
- 'variable.', environment_vars.PROJECT)
project_id = None
return compute_engine.Credentials(), project_id
@@ -287,6 +271,13 @@
credentials, project_id = checker()
if credentials is not None:
credentials = with_scopes_if_required(credentials, scopes)
- return credentials, explicit_project_id or project_id
+ effective_project_id = explicit_project_id or project_id
+ if not effective_project_id:
+ _LOGGER.warning(
+ 'No project ID could be determined. Consider running '
+ '`gcloud config set project` or setting the %s '
+ 'environment variable',
+ environment_vars.PROJECT)
+ return credentials, effective_project_id
raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)