Skip to content

Commit

Permalink
[AIRFLOW-7085] Cache credentials, project_id in GCP Base Hook (#7759)
Browse files Browse the repository at this point in the history
  • Loading branch information
mik-laj committed Mar 19, 2020
1 parent d2ee4be commit 7e1e954
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions airflow/providers/google/cloud/hooks/base.py
Expand Up @@ -141,11 +141,16 @@ def __init__(self, gcp_conn_id: str = 'google_cloud_default', delegate_to: Optio
self.gcp_conn_id = gcp_conn_id
self.delegate_to = delegate_to
self.extras = self.get_connection(self.gcp_conn_id).extra_dejson # type: Dict
self._cached_credentials: Optional[google.auth.credentials.Credentials] = None
self._cached_project_id: Optional[str] = None

def _get_credentials_and_project_id(self) -> Tuple[google.auth.credentials.Credentials, Optional[str]]:
"""
Returns the Credentials object for Google API and the associated project_id
"""
if self._cached_credentials is not None:
return self._cached_credentials, self._cached_project_id

key_path = self._get_field('key_path', None) # type: Optional[str]
keyfile_dict = self._get_field('keyfile_dict', None) # type: Optional[str]
if key_path and keyfile_dict:
Expand Down Expand Up @@ -200,6 +205,9 @@ def _get_credentials_and_project_id(self) -> Tuple[google.auth.credentials.Crede
if overridden_project_id:
project_id = overridden_project_id

self._cached_credentials = credentials
self._cached_project_id = project_id

return credentials, project_id

def _get_credentials(self) -> google.auth.credentials.Credentials:
Expand Down

0 comments on commit 7e1e954

Please sign in to comment.