Source code for airflow.providers.google.cloud.sensors.datafusion
## 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 a Google Cloud Data Fusion sensors."""from__future__importannotationsfromcollections.abcimportIterable,SequencefromtypingimportTYPE_CHECKINGfromairflow.exceptionsimportAirflowException,AirflowNotFoundExceptionfromairflow.providers.google.cloud.hooks.datafusionimportDataFusionHookfromairflow.providers.google.common.hooks.base_googleimportPROVIDE_PROJECT_IDfromairflow.sensors.baseimportBaseSensorOperatorifTYPE_CHECKING:fromairflow.utils.contextimportContext
[docs]classCloudDataFusionPipelineStateSensor(BaseSensorOperator):""" Check the status of the pipeline in the Google Cloud Data Fusion. :param pipeline_name: Your pipeline name. :param pipeline_id: Your pipeline ID. :param expected_statuses: State that is expected :param failure_statuses: State that will terminate the sensor with an exception :param instance_name: The name of the instance. :param location: The Cloud Data Fusion location in which to handle the request. :param project_id: The ID of the Google Cloud project that the instance belongs to. :param namespace: If your pipeline belongs to a Basic edition instance, the namespace ID is always default. If your pipeline belongs to an Enterprise edition instance, you can create a namespace. :param gcp_conn_id: The connection ID to use when fetching connection info. :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). """
[docs]defpoke(self,context:Context)->bool:self.log.info("Waiting for pipeline %s to be in one of the states: %s.",self.pipeline_id,", ".join(self.expected_statuses),)hook=DataFusionHook(gcp_conn_id=self.gcp_conn_id,impersonation_chain=self.impersonation_chain,)instance=hook.get_instance(instance_name=self.instance_name,location=self.location,project_id=self.project_id,)api_url=instance["apiEndpoint"]pipeline_status=Nonetry:pipeline_workflow=hook.get_pipeline_workflow(pipeline_name=self.pipeline_name,instance_url=api_url,pipeline_id=self.pipeline_id,namespace=self.namespace,)pipeline_status=pipeline_workflow["status"]exceptAirflowNotFoundException:message="Specified Pipeline ID was not found."raiseAirflowException(message)exceptAirflowException:pass# Because the pipeline may not be visible in system yetifpipeline_statusisnotNone:ifself.failure_statusesandpipeline_statusinself.failure_statuses:message=(f"Pipeline with id '{self.pipeline_id}' state is: {pipeline_status}. "f"Terminating sensor...")raiseAirflowException(message)self.log.debug("Current status of the pipeline workflow for %s: %s.",self.pipeline_id,pipeline_status)returnpipeline_statusinself.expected_statuses