airflow.providers.google.common.hooks.base_google

This module contains a Google Cloud API base hook.

Module Contents

airflow.providers.google.common.hooks.base_google.log[source]
airflow.providers.google.common.hooks.base_google.INVALID_KEYS = ['DefaultRequestsPerMinutePerProject', 'DefaultRequestsPerMinutePerUser', 'RequestsPerMinutePerProject', 'Resource has been exhausted (e.g. check quota).'][source]
airflow.providers.google.common.hooks.base_google.INVALID_REASONS = ['userRateLimitExceeded'][source]
airflow.providers.google.common.hooks.base_google.is_soft_quota_exception(exception: Exception)[source]
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: Exception)bool[source]
Some of the calls return 429 (too many requests!) or 409 errors (Conflict)
in case of operation in progress.
  • Google Cloud SQL

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 for exceeding the temporary quote limit.

airflow.providers.google.common.hooks.base_google.T[source]
airflow.providers.google.common.hooks.base_google.RT[source]
class airflow.providers.google.common.hooks.base_google.GoogleBaseHook(gcp_conn_id: str = 'google_cloud_default', delegate_to: Optional[str] = None, impersonation_chain: Optional[Union[str, Sequence[str]]] = None)[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.

  • delegate_to (str) -- The account to impersonate using domain-wide delegation of authority, if any. For this to work, the service account making the request must have domain-wide delegation enabled.

  • impersonation_chain (Union[str, Sequence[str]]) -- 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.

conn_name_attr = gcp_conn_id[source]
default_conn_name = google_cloud_default[source]
conn_type = google_cloud_platform[source]
hook_name = Google Cloud[source]
project_id[source]

Returns project id.

Returns

id of the project

Return type

str

num_retries[source]

Returns num_retries from Connection.

Returns

the number of times each API request should be retried

Return type

int

client_info[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.

scopes[source]

Return OAuth 2.0 scopes.

Returns

Returns the scope defined in the connection configuration, or the default scope

Return type

Sequence[str]

static get_connection_form_widgets()[source]

Returns connection widgets to add to connection form

static get_ui_field_behaviour()[source]

Returns custom field behaviour

_get_credentials_and_project_id(self)[source]

Returns the Credentials object for Google API and the associated project_id

_get_credentials(self)[source]

Returns the Credentials object for Google API

_get_access_token(self)[source]

Returns a valid access token from Google API Credentials

_get_credentials_email(self)[source]

Returns the email address associated with the currently logged in account

If a service account is used, it returns the service account. If user authentication (e.g. gcloud auth) is used, it returns the e-mail account of that user.

_authorize(self)[source]

Returns an authorized HTTP object to be used to build a Google cloud service hook connection.

_get_field(self, f: str, default: Any = None)[source]

Fetches a field from extras, and returns it. This is some Airflow magic. The google_cloud_platform hook type adds custom UI elements to the hook page, which allow admins to specify service_account, key_path, etc. They get formatted as shown below.

static quota_retry(*args, **kwargs)[source]

A decorator that provides a mechanism to repeat requests in response to exceeding a temporary quote limit.

static operation_in_progress_retry(*args, **kwargs)[source]

A decorator that provides a mechanism to repeat requests in response to operation in progress (HTTP 409) limit.

static fallback_to_default_project_id(func: Callable[..., RT])[source]

Decorator that provides fallback for Google Cloud project id. 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 -- function to wrap

Returns

result of the function call

static provide_gcp_credential_file(func: T)[source]

Function decorator that provides a Google Cloud credentials for application supporting Application Default Credentials (ADC) strategy.

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(self)[source]

Context manager that provides a Google Cloud credentials for application supporting Application Default Credentials (ADC) strategy.

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(self)[source]

Provides 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 Credentials gcloud 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.

static download_content_from_request(file_handle, request: dict, chunk_size: int)[source]

Download media resources. Note that the Python file object is compatible with io.Base and can be used with this class also.

Parameters
  • file_handle (io.Base or file object) -- io.Base or file object. The stream in which to write the downloaded bytes.

  • request (Dict) -- googleapiclient.http.HttpRequest, the media request to perform in chunks.

  • chunk_size (int) -- int, File will be downloaded in chunks of this many bytes.

Was this entry helpful?