airflow.providers.google.common.hooks.base_google
¶
This module contains a Google Cloud API base hook.
Module Contents¶
Classes¶
Retries if there was an exception for exceeding the temporary quote limit. |
|
Retries if there was an exception in case of operation in progress. |
|
Retries if there was an exception for refreshing credentials. |
|
A base hook for Google cloud-related hooks. |
|
GoogleBaseAsyncHook inherits from BaseHook class, run on the trigger worker. |
Functions¶
|
Check for quota violation errors. |
|
Handle operation in-progress exceptions. |
|
Handle refresh credentials exceptions. |
|
Get field from extra, first checking short name, then for backcompat we check for prefixed name. |
Attributes¶
- airflow.providers.google.common.hooks.base_google.INVALID_KEYS = ['DefaultRequestsPerMinutePerProject', 'DefaultRequestsPerMinutePerUser',...[source]¶
- airflow.providers.google.common.hooks.base_google.INVALID_REASONS = ['userRateLimitExceeded'][source]¶
- airflow.providers.google.common.hooks.base_google.is_soft_quota_exception(exception)[source]¶
Check for quota violation errors.
API for Google services does not have a standardized way to report quota violation errors.
The function has been adapted by trial and error to the following services: * Google Translate * Google Vision * Google Text-to-Speech * Google Speech-to-Text * Google Natural Language * Google Video Intelligence
- airflow.providers.google.common.hooks.base_google.is_operation_in_progress_exception(exception)[source]¶
Handle operation in-progress exceptions.
Some calls return 429 (too many requests!) or 409 errors (Conflict) in case of operation in progress.
Google Cloud SQL
- airflow.providers.google.common.hooks.base_google.is_refresh_credentials_exception(exception)[source]¶
Handle refresh credentials exceptions.
Some calls return 502 (server error) in case a new token cannot be obtained.
Google BigQuery
- class airflow.providers.google.common.hooks.base_google.retry_if_temporary_quota[source]¶
Bases:
tenacity.retry_if_exception
Retries if there was an exception for exceeding the temporary quote limit.
- class airflow.providers.google.common.hooks.base_google.retry_if_operation_in_progress[source]¶
Bases:
tenacity.retry_if_exception
Retries if there was an exception in case of operation in progress.
- class airflow.providers.google.common.hooks.base_google.retry_if_temporary_refresh_credentials[source]¶
Bases:
tenacity.retry_if_exception
Retries if there was an exception for refreshing credentials.
- airflow.providers.google.common.hooks.base_google.get_field(extras, field_name)[source]¶
Get field from extra, first checking short name, then for backcompat we check for prefixed name.
- class airflow.providers.google.common.hooks.base_google.GoogleBaseHook(gcp_conn_id='google_cloud_default', impersonation_chain=None, **kwargs)[source]¶
Bases:
airflow.hooks.base.BaseHook
A base hook for Google cloud-related hooks.
Google cloud has a shared REST API client that is built in the same way no matter which service you use. This class helps construct and authorize the credentials needed to then call googleapiclient.discovery.build() to actually discover and build a client for a Google cloud service.
The class also contains some miscellaneous helper functions.
All hook derived from this base hook use the ‘Google Cloud’ connection type. Three ways of authentication are supported:
Default credentials: Only the ‘Project Id’ is required. You’ll need to have set up default credentials, such as by the
GOOGLE_APPLICATION_DEFAULT
environment variable or from the metadata server on Google Compute Engine.JSON key file: Specify ‘Project Id’, ‘Keyfile Path’ and ‘Scope’.
Legacy P12 key files are not supported.
JSON data provided in the UI: Specify ‘Keyfile JSON’.
- Parameters
gcp_conn_id (str) – The connection ID to use when fetching connection info.
impersonation_chain (str | collections.abc.Sequence[str] | None) – Optional service account to impersonate using short-term credentials, or chained list of accounts required to get the access_token of the last account in the list, which will be impersonated in the request. If set as a string, the account must grant the originating account the Service Account Token Creator IAM role. If set as a sequence, the identities from the list must grant Service Account Token Creator IAM role to the directly preceding identity, with first account from the list granting this role to the originating account.
- property num_retries: int[source]¶
Returns num_retries from Connection.
- Returns
the number of times each API request should be retried
- Return type
- property client_info: google.api_core.gapic_v1.client_info.ClientInfo[source]¶
Return client information used to generate a user-agent for API calls.
It allows for better errors tracking.
This object is only used by the google-cloud-* libraries that are built specifically for the Google Cloud. It is not supported by The Google APIs Python Client that use Discovery based APIs.
- property scopes: collections.abc.Sequence[str][source]¶
Return OAuth 2.0 scopes.
- Returns
Returns the scope defined in the connection configuration, or the default scope
- Return type
- classmethod get_connection_form_widgets()[source]¶
Return connection widgets to add to connection form.
- get_credentials_and_project_id()[source]¶
Return the Credentials object for Google API and the associated project_id.
- static quota_retry(*args, **kwargs)[source]¶
Provide a mechanism to repeat requests in response to exceeding a temporary quota limit.
- static operation_in_progress_retry(*args, **kwargs)[source]¶
Provide a mechanism to repeat requests in response to operation in progress (HTTP 409) limit.
- static refresh_credentials_retry(*args, **kwargs)[source]¶
Provide a mechanism to repeat requests in response to a temporary refresh credential issue.
- static fallback_to_default_project_id(func)[source]¶
Provide fallback for Google Cloud project id. To be used as a decorator.
If the project is None it will be replaced with the project_id from the service account the Hook is authenticated with. Project id can be specified either via project_id kwarg or via first parameter in positional args.
- Parameters
func (Callable[Ellipsis, RT]) – function to wrap
- Returns
result of the function call
- Return type
Callable[Ellipsis, RT]
- static provide_gcp_credential_file(func)[source]¶
Provide a Google Cloud credentials for Application Default Credentials (ADC) strategy support.
It is recommended to use
provide_gcp_credential_file_as_context
context manager to limit the scope when authorization data is available. Using context manager also makes it easier to use multiple connection in one function.
- provide_gcp_credential_file_as_context()[source]¶
Provide a Google Cloud credentials for Application Default Credentials (ADC) strategy support.
It can be used to provide credentials for external programs (e.g. gcloud) that expect authorization file in
GOOGLE_APPLICATION_CREDENTIALS
environment variable.
- provide_authorized_gcloud()[source]¶
Provide a separate gcloud configuration with current credentials.
The gcloud tool allows you to login to Google Cloud only -
gcloud auth login
and for the needs of Application Default Credentialsgcloud auth application-default login
. In our case, we want all commands to use only the credentials from ADCm so we need to configure the credentials in gcloud manually.
- class airflow.providers.google.common.hooks.base_google.GoogleBaseAsyncHook(**kwargs)[source]¶
Bases:
airflow.hooks.base.BaseHook
GoogleBaseAsyncHook inherits from BaseHook class, run on the trigger worker.
- async get_sync_hook()[source]¶
Sync version of the Google Cloud Hook makes blocking calls in
__init__
; don’t inherit it.
- async get_token(*, session=None)[source]¶
Return a Token instance for use in [gcloud-aio](https://talkiq.github.io/gcloud-aio/) clients.