Source code for airflow.providers.google.cloud.operators.cloud_sql
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License."""This module contains Google Cloud SQL operators."""fromtypingimportDict,Iterable,List,Optional,Sequence,Unionfromgoogleapiclient.errorsimportHttpErrorfromairflow.exceptionsimportAirflowExceptionfromairflow.hooks.baseimportBaseHookfromairflow.modelsimportBaseOperatorfromairflow.providers.google.cloud.hooks.cloud_sqlimportCloudSQLDatabaseHook,CloudSQLHookfromairflow.providers.google.cloud.utils.field_validatorimportGcpBodyFieldValidatorfromairflow.providers.mysql.hooks.mysqlimportMySqlHookfromairflow.providers.postgres.hooks.postgresimportPostgresHook
[docs]classCloudSQLBaseOperator(BaseOperator):""" Abstract base operator for Google Cloud SQL operators to inherit from. :param instance: Cloud SQL instance ID. This does not include the project ID. :type instance: str :param project_id: Optional, Google Cloud Project ID. f set to None or missing, the default project_id from the Google Cloud connection is used. :type project_id: str :param gcp_conn_id: The connection ID used to connect to Google Cloud. :type gcp_conn_id: str :param api_version: API version used (e.g. v1beta4). :type api_version: str :param impersonation_chain: 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 (templated). :type impersonation_chain: Union[str, Sequence[str]] """def__init__(self,*,instance:str,project_id:Optional[str]=None,gcp_conn_id:str='google_cloud_default',api_version:str='v1beta4',impersonation_chain:Optional[Union[str,Sequence[str]]]=None,**kwargs,)->None:self.project_id=project_idself.instance=instanceself.gcp_conn_id=gcp_conn_idself.api_version=api_versionself.impersonation_chain=impersonation_chainself._validate_inputs()super().__init__(**kwargs)def_validate_inputs(self)->None:ifself.project_id=='':raiseAirflowException("The required parameter 'project_id' is empty")ifnotself.instance:raiseAirflowException("The required parameter 'instance' is empty or None")def_check_if_instance_exists(self,instance,hook:CloudSQLHook)->Union[dict,bool]:try:returnhook.get_instance(project_id=self.project_id,instance=instance)exceptHttpErrorase:status=e.resp.statusifstatus==404:returnFalseraiseedef_check_if_db_exists(self,db_name,hook:CloudSQLHook)->Union[dict,bool]:try:returnhook.get_database(project_id=self.project_id,instance=self.instance,database=db_name)exceptHttpErrorase:status=e.resp.statusifstatus==404:returnFalseraisee
[docs]classCloudSQLCreateInstanceOperator(CloudSQLBaseOperator):""" Creates a new Cloud SQL instance. If an instance with the same name exists, no action will be taken and the operator will succeed. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudSQLCreateInstanceOperator` :param body: Body required by the Cloud SQL insert API, as described in https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances/insert #request-body :type body: dict :param instance: Cloud SQL instance ID. This does not include the project ID. :type instance: str :param project_id: Optional, Google Cloud Project ID. If set to None or missing, the default project_id from the Google Cloud connection is used. :type project_id: str :param gcp_conn_id: The connection ID used to connect to Google Cloud. :type gcp_conn_id: str :param api_version: API version used (e.g. v1beta4). :type api_version: str :param validate_body: True if body should be validated, False otherwise. :type validate_body: bool :param impersonation_chain: 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 (templated). :type impersonation_chain: Union[str, Sequence[str]] """# [START gcp_sql_create_template_fields]
)# [END gcp_sql_create_template_fields]def__init__(self,*,body:dict,instance:str,project_id:Optional[str]=None,gcp_conn_id:str='google_cloud_default',api_version:str='v1beta4',validate_body:bool=True,impersonation_chain:Optional[Union[str,Sequence[str]]]=None,**kwargs,)->None:self.body=bodyself.validate_body=validate_bodysuper().__init__(project_id=project_id,instance=instance,gcp_conn_id=gcp_conn_id,api_version=api_version,impersonation_chain=impersonation_chain,**kwargs,)def_validate_inputs(self)->None:super()._validate_inputs()ifnotself.body:raiseAirflowException("The required parameter 'body' is empty")def_validate_body_fields(self)->None:ifself.validate_body:GcpBodyFieldValidator(CLOUD_SQL_CREATE_VALIDATION,api_version=self.api_version).validate(self.body)
[docs]defexecute(self,context)->None:hook=CloudSQLHook(gcp_conn_id=self.gcp_conn_id,api_version=self.api_version,impersonation_chain=self.impersonation_chain,)self._validate_body_fields()ifnotself._check_if_instance_exists(self.instance,hook):hook.create_instance(project_id=self.project_id,body=self.body)else:self.log.info("Cloud SQL instance with ID %s already exists. Aborting create.",self.instance)instance_resource=hook.get_instance(project_id=self.project_id,instance=self.instance)service_account_email=instance_resource["serviceAccountEmailAddress"]task_instance=context['task_instance']task_instance.xcom_push(key="service_account_email",value=service_account_email)
[docs]classCloudSQLInstancePatchOperator(CloudSQLBaseOperator):""" Updates settings of a Cloud SQL instance. Caution: This is a partial update, so only included values for the settings will be updated. In the request body, supply the relevant portions of an instance resource, according to the rules of patch semantics. https://cloud.google.com/sql/docs/mysql/admin-api/how-tos/performance#patch .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudSQLInstancePatchOperator` :param body: Body required by the Cloud SQL patch API, as described in https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances/patch#request-body :type body: dict :param instance: Cloud SQL instance ID. This does not include the project ID. :type instance: str :param project_id: Optional, Google Cloud Project ID. If set to None or missing, the default project_id from the Google Cloud connection is used. :type project_id: str :param gcp_conn_id: The connection ID used to connect to Google Cloud. :type gcp_conn_id: str :param api_version: API version used (e.g. v1beta4). :type api_version: str :param impersonation_chain: 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 (templated). :type impersonation_chain: Union[str, Sequence[str]] """# [START gcp_sql_patch_template_fields]
)# [END gcp_sql_patch_template_fields]def__init__(self,*,body:dict,instance:str,project_id:Optional[str]=None,gcp_conn_id:str='google_cloud_default',api_version:str='v1beta4',impersonation_chain:Optional[Union[str,Sequence[str]]]=None,**kwargs,)->None:self.body=bodysuper().__init__(project_id=project_id,instance=instance,gcp_conn_id=gcp_conn_id,api_version=api_version,impersonation_chain=impersonation_chain,**kwargs,)def_validate_inputs(self)->None:super()._validate_inputs()ifnotself.body:raiseAirflowException("The required parameter 'body' is empty")
[docs]defexecute(self,context):hook=CloudSQLHook(gcp_conn_id=self.gcp_conn_id,api_version=self.api_version,impersonation_chain=self.impersonation_chain,)ifnotself._check_if_instance_exists(self.instance,hook):raiseAirflowException('Cloud SQL instance with ID {} does not exist. ''Please specify another instance to patch.'.format(self.instance))else:returnhook.patch_instance(project_id=self.project_id,body=self.body,instance=self.instance)
[docs]classCloudSQLDeleteInstanceOperator(CloudSQLBaseOperator):""" Deletes a Cloud SQL instance. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudSQLDeleteInstanceOperator` :param instance: Cloud SQL instance ID. This does not include the project ID. :type instance: str :param project_id: Optional, Google Cloud Project ID. If set to None or missing, the default project_id from the Google Cloud connection is used. :type project_id: str :param gcp_conn_id: The connection ID used to connect to Google Cloud. :type gcp_conn_id: str :param api_version: API version used (e.g. v1beta4). :type api_version: str :param impersonation_chain: 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 (templated). :type impersonation_chain: Union[str, Sequence[str]] """# [START gcp_sql_delete_template_fields]
[docs]defexecute(self,context)->Optional[bool]:hook=CloudSQLHook(gcp_conn_id=self.gcp_conn_id,api_version=self.api_version,impersonation_chain=self.impersonation_chain,)ifnotself._check_if_instance_exists(self.instance,hook):print(f"Cloud SQL instance with ID {self.instance} does not exist. Aborting delete.")returnTrueelse:returnhook.delete_instance(project_id=self.project_id,instance=self.instance)
[docs]classCloudSQLCreateInstanceDatabaseOperator(CloudSQLBaseOperator):""" Creates a new database inside a Cloud SQL instance. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudSQLCreateInstanceDatabaseOperator` :param instance: Database instance ID. This does not include the project ID. :type instance: str :param body: The request body, as described in https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/databases/insert#request-body :type body: dict :param project_id: Optional, Google Cloud Project ID. If set to None or missing, the default project_id from the Google Cloud connection is used. :type project_id: str :param gcp_conn_id: The connection ID used to connect to Google Cloud. :type gcp_conn_id: str :param api_version: API version used (e.g. v1beta4). :type api_version: str :param validate_body: Whether the body should be validated. Defaults to True. :type validate_body: bool :param impersonation_chain: 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 (templated). :type impersonation_chain: Union[str, Sequence[str]] """# [START gcp_sql_db_create_template_fields]
)# [END gcp_sql_db_create_template_fields]def__init__(self,*,instance:str,body:dict,project_id:Optional[str]=None,gcp_conn_id:str='google_cloud_default',api_version:str='v1beta4',validate_body:bool=True,impersonation_chain:Optional[Union[str,Sequence[str]]]=None,**kwargs,)->None:self.body=bodyself.validate_body=validate_bodysuper().__init__(project_id=project_id,instance=instance,gcp_conn_id=gcp_conn_id,api_version=api_version,impersonation_chain=impersonation_chain,**kwargs,)def_validate_inputs(self)->None:super()._validate_inputs()ifnotself.body:raiseAirflowException("The required parameter 'body' is empty")def_validate_body_fields(self)->None:ifself.validate_body:GcpBodyFieldValidator(CLOUD_SQL_DATABASE_CREATE_VALIDATION,api_version=self.api_version).validate(self.body)
[docs]defexecute(self,context)->Optional[bool]:self._validate_body_fields()database=self.body.get("name")ifnotdatabase:self.log.error("Body doesn't contain 'name'. Cannot check if the"" database already exists in the instance %s.",self.instance,)returnFalsehook=CloudSQLHook(gcp_conn_id=self.gcp_conn_id,api_version=self.api_version,impersonation_chain=self.impersonation_chain,)ifself._check_if_db_exists(database,hook):self.log.info("Cloud SQL instance with ID %s already contains database '%s'. Aborting database insert.",self.instance,database,)returnTrueelse:returnhook.create_database(project_id=self.project_id,instance=self.instance,body=self.body)
[docs]classCloudSQLPatchInstanceDatabaseOperator(CloudSQLBaseOperator):""" Updates a resource containing information about a database inside a Cloud SQL instance using patch semantics. See: https://cloud.google.com/sql/docs/mysql/admin-api/how-tos/performance#patch .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudSQLPatchInstanceDatabaseOperator` :param instance: Database instance ID. This does not include the project ID. :type instance: str :param database: Name of the database to be updated in the instance. :type database: str :param body: The request body, as described in https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/databases/patch#request-body :type body: dict :param project_id: Optional, Google Cloud Project ID. :type project_id: str :param gcp_conn_id: The connection ID used to connect to Google Cloud. :type gcp_conn_id: str :param api_version: API version used (e.g. v1beta4). :type api_version: str :param validate_body: Whether the body should be validated. Defaults to True. :type validate_body: bool :param impersonation_chain: 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 (templated). :type impersonation_chain: Union[str, Sequence[str]] """# [START gcp_sql_db_patch_template_fields]
)# [END gcp_sql_db_patch_template_fields]def__init__(self,*,instance:str,database:str,body:dict,project_id:Optional[str]=None,gcp_conn_id:str='google_cloud_default',api_version:str='v1beta4',validate_body:bool=True,impersonation_chain:Optional[Union[str,Sequence[str]]]=None,**kwargs,)->None:self.database=databaseself.body=bodyself.validate_body=validate_bodysuper().__init__(project_id=project_id,instance=instance,gcp_conn_id=gcp_conn_id,api_version=api_version,impersonation_chain=impersonation_chain,**kwargs,)def_validate_inputs(self)->None:super()._validate_inputs()ifnotself.body:raiseAirflowException("The required parameter 'body' is empty")ifnotself.database:raiseAirflowException("The required parameter 'database' is empty")def_validate_body_fields(self)->None:ifself.validate_body:GcpBodyFieldValidator(CLOUD_SQL_DATABASE_PATCH_VALIDATION,api_version=self.api_version).validate(self.body)
[docs]defexecute(self,context)->None:self._validate_body_fields()hook=CloudSQLHook(gcp_conn_id=self.gcp_conn_id,api_version=self.api_version,impersonation_chain=self.impersonation_chain,)ifnotself._check_if_db_exists(self.database,hook):raiseAirflowException("Cloud SQL instance with ID {instance} does not contain ""database '{database}'. ""Please specify another database to patch.".format(instance=self.instance,database=self.database))else:returnhook.patch_database(project_id=self.project_id,instance=self.instance,database=self.database,body=self.body
)
[docs]classCloudSQLDeleteInstanceDatabaseOperator(CloudSQLBaseOperator):""" Deletes a database from a Cloud SQL instance. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudSQLDeleteInstanceDatabaseOperator` :param instance: Database instance ID. This does not include the project ID. :type instance: str :param database: Name of the database to be deleted in the instance. :type database: str :param project_id: Optional, Google Cloud Project ID. If set to None or missing, the default project_id from the Google Cloud connection is used. :type project_id: str :param gcp_conn_id: The connection ID used to connect to Google Cloud. :type gcp_conn_id: str :param api_version: API version used (e.g. v1beta4). :type api_version: str :param impersonation_chain: 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 (templated). :type impersonation_chain: Union[str, Sequence[str]] """# [START gcp_sql_db_delete_template_fields]
)# [END gcp_sql_db_delete_template_fields]def__init__(self,*,instance:str,database:str,project_id:Optional[str]=None,gcp_conn_id:str='google_cloud_default',api_version:str='v1beta4',impersonation_chain:Optional[Union[str,Sequence[str]]]=None,**kwargs,)->None:self.database=databasesuper().__init__(project_id=project_id,instance=instance,gcp_conn_id=gcp_conn_id,api_version=api_version,impersonation_chain=impersonation_chain,**kwargs,)def_validate_inputs(self)->None:super()._validate_inputs()ifnotself.database:raiseAirflowException("The required parameter 'database' is empty")
[docs]defexecute(self,context)->Optional[bool]:hook=CloudSQLHook(gcp_conn_id=self.gcp_conn_id,api_version=self.api_version,impersonation_chain=self.impersonation_chain,)ifnotself._check_if_db_exists(self.database,hook):print("Cloud SQL instance with ID {} does not contain database '{}'. ""Aborting database delete.".format(self.instance,self.database))returnTrueelse:returnhook.delete_database(project_id=self.project_id,instance=self.instance,database=self.database
)
[docs]classCloudSQLExportInstanceOperator(CloudSQLBaseOperator):""" Exports data from a Cloud SQL instance to a Cloud Storage bucket as a SQL dump or CSV file. Note: This operator is idempotent. If executed multiple times with the same export file URI, the export file in GCS will simply be overridden. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudSQLExportInstanceOperator` :param instance: Cloud SQL instance ID. This does not include the project ID. :type instance: str :param body: The request body, as described in https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances/export#request-body :type body: dict :param project_id: Optional, Google Cloud Project ID. If set to None or missing, the default project_id from the Google Cloud connection is used. :type project_id: str :param gcp_conn_id: The connection ID used to connect to Google Cloud. :type gcp_conn_id: str :param api_version: API version used (e.g. v1beta4). :type api_version: str :param validate_body: Whether the body should be validated. Defaults to True. :type validate_body: bool :param impersonation_chain: 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 (templated). :type impersonation_chain: Union[str, Sequence[str]] """# [START gcp_sql_export_template_fields]
[docs]classCloudSQLImportInstanceOperator(CloudSQLBaseOperator):""" Imports data into a Cloud SQL instance from a SQL dump or CSV file in Cloud Storage. CSV IMPORT: This operator is NOT idempotent for a CSV import. If the same file is imported multiple times, the imported data will be duplicated in the database. Moreover, if there are any unique constraints the duplicate import may result in an error. SQL IMPORT: This operator is idempotent for a SQL import if it was also exported by Cloud SQL. The exported SQL contains 'DROP TABLE IF EXISTS' statements for all tables to be imported. If the import file was generated in a different way, idempotence is not guaranteed. It has to be ensured on the SQL file level. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudSQLImportInstanceOperator` :param instance: Cloud SQL instance ID. This does not include the project ID. :type instance: str :param body: The request body, as described in https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances/export#request-body :type body: dict :param project_id: Optional, Google Cloud Project ID. If set to None or missing, the default project_id from the Google Cloud connection is used. :type project_id: str :param gcp_conn_id: The connection ID used to connect to Google Cloud. :type gcp_conn_id: str :param api_version: API version used (e.g. v1beta4). :type api_version: str :param validate_body: Whether the body should be validated. Defaults to True. :type validate_body: bool :param impersonation_chain: 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 (templated). :type impersonation_chain: Union[str, Sequence[str]] """# [START gcp_sql_import_template_fields]
[docs]classCloudSQLExecuteQueryOperator(BaseOperator):""" Performs DML or DDL query on an existing Cloud Sql instance. It optionally uses cloud-sql-proxy to establish secure connection with the database. .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:CloudSQLExecuteQueryOperator` :param sql: SQL query or list of queries to run (should be DML or DDL query - this operator does not return any data from the database, so it is useless to pass it DQL queries. Note that it is responsibility of the author of the queries to make sure that the queries are idempotent. For example you can use CREATE TABLE IF NOT EXISTS to create a table. :type sql: str or list[str] :param parameters: (optional) the parameters to render the SQL query with. :type parameters: dict or iterable :param autocommit: if True, each command is automatically committed. (default value: False) :type autocommit: bool :param gcp_conn_id: The connection ID used to connect to Google Cloud for cloud-sql-proxy authentication. :type gcp_conn_id: str :param gcp_cloudsql_conn_id: The connection ID used to connect to Google Cloud SQL its schema should be gcpcloudsql://. See :class:`~airflow.providers.google.cloud.hooks.cloud_sql.CloudSQLDatabaseHook` for details on how to define ``gcpcloudsql://`` connection. :type gcp_cloudsql_conn_id: str """# [START gcp_sql_query_template_fields]
# [END gcp_sql_query_template_fields]def__init__(self,*,sql:Union[List[str],str],autocommit:bool=False,parameters:Optional[Union[Dict,Iterable]]=None,gcp_conn_id:str='google_cloud_default',gcp_cloudsql_conn_id:str='google_cloud_sql_default',**kwargs,)->None:super().__init__(**kwargs)self.sql=sqlself.gcp_conn_id=gcp_conn_idself.gcp_cloudsql_conn_id=gcp_cloudsql_conn_idself.autocommit=autocommitself.parameters=parametersself.gcp_connection=Nonedef_execute_query(self,hook:CloudSQLDatabaseHook,database_hook:Union[PostgresHook,MySqlHook])->None:cloud_sql_proxy_runner=Nonetry:ifhook.use_proxy:cloud_sql_proxy_runner=hook.get_sqlproxy_runner()hook.free_reserved_port()# There is very, very slim chance that the socket will# be taken over here by another bind(0).# It's quite unlikely to happen though!cloud_sql_proxy_runner.start_proxy()self.log.info('Executing: "%s"',self.sql)database_hook.run(self.sql,self.autocommit,parameters=self.parameters)finally:ifcloud_sql_proxy_runner:cloud_sql_proxy_runner.stop_proxy()