airflow.contrib.hooks.gcp_api_base_hook

This module contains a Google Cloud API base hook.

Module Contents

airflow.contrib.hooks.gcp_api_base_hook._DEFAULT_SCOPES :Sequence[str] = ['https://www.googleapis.com/auth/cloud-platform'][source]
airflow.contrib.hooks.gcp_api_base_hook.RT[source]
class airflow.contrib.hooks.gcp_api_base_hook.GoogleCloudBaseHook(gcp_conn_id:str='google_cloud_default', delegate_to:str=None)[source]

Bases: airflow.hooks.base_hook.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 Platform’ 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, if any. For this to work, the service account making the request must have domain-wide delegation enabled.

project_id[source]

Returns project id.

Returns

id of the project

Return type

str

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 Platform. 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]

_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

_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 catch_http_exception(func:Callable[..., RT])[source]

Function decorator that intercepts HTTP Errors and raises AirflowException with more informative message.

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

Decorator that provides fallback for Google Cloud Platform 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:Callable[..., RT])[source]

Function decorator that provides a GOOGLE_APPLICATION_CREDENTIALS environment variable, pointing to file path of a JSON file of service account key.