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

fix: Fixed getting project ID when running on Vertex AI; Fixes #852 #943

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 25 additions & 0 deletions google/cloud/aiplatform/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,31 @@ def project(self) -> str:
if self._project:
return self._project

# Project is not set. Trying to get it from the environment.
# See https://github.com/googleapis/python-aiplatform/issues/852
# See https://github.com/googleapis/google-auth-library-python/issues/924
# TODO: Remove when google.auth.default() learns the
# CLOUD_ML_PROJECT_ID env variable or Vertex AI starts setting GOOGLE_CLOUD_PROJECT env variable.
project_number = os.environ.get("CLOUD_ML_PROJECT_ID")
if project_number:
self._project = project_number
# Try to convert project number to project ID which is more readable.
try:
from googleapiclient import discovery
Ark-kun marked this conversation as resolved.
Show resolved Hide resolved

cloud_resource_manager_service = discovery.build(
"cloudresourcemanager", "v3"
)
project_id = (
cloud_resource_manager_service.projects()
.get(name=f"projects/{project_number}")
.execute()["projectId"]
)
self._project = project_id
Ark-kun marked this conversation as resolved.
Show resolved Hide resolved
except Exception as e:
logging.warning(f"Error converting project number to project ID: {e}")
return self._project

project_not_found_exception_str = (
"Unable to find your project. Please provide a project ID by:"
"\n- Passing a constructor argument"
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
"packaging >= 14.3",
"google-cloud-storage >= 1.32.0, < 2.0.0dev",
"google-cloud-bigquery >= 1.15.0, < 3.0.0dev",
"google-api-python-client >= 2.29", # for API discovery
Ark-kun marked this conversation as resolved.
Show resolved Hide resolved
),
extras_require={
"full": full_extra_require,
Expand Down