Source code for airflow.providers.google.cloud.links.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 Google Data Fusion links."""
from __future__ import annotations
from typing import TYPE_CHECKING, ClassVar
from airflow.models import BaseOperatorLink, XCom
if TYPE_CHECKING:
    from airflow.models import BaseOperator
    from airflow.models.taskinstancekey import TaskInstanceKey
    from airflow.utils.context import Context
[docs]BASE_LINK = "https://console.cloud.google.com/data-fusion" 
[docs]DATAFUSION_INSTANCE_LINK = BASE_LINK + "/locations/{region}/instances/{instance_name}?project={project_id}" 
[docs]DATAFUSION_PIPELINES_LINK = "{uri}/cdap/ns/{namespace}/pipelines" 
[docs]DATAFUSION_PIPELINE_LINK = "{uri}/pipelines/ns/{namespace}/view/{pipeline_name}" 
[docs]class BaseGoogleLink(BaseOperatorLink):
    """Link for Google operators.
    Prevent adding ``https://console.cloud.google.com`` in front of every link
    where URI is used.
    """
[docs]    def get_link(
        self,
        operator: BaseOperator,
        *,
        ti_key: TaskInstanceKey,
    ) -> str:
        conf = XCom.get_value(key=self.key, ti_key=ti_key)
        if not conf:
            return ""
        # Add a default value for the 'namespace' parameter for backward compatibility.
        conf.setdefault("namespace", "default")
        return self.format_str.format(**conf)  
[docs]class DataFusionInstanceLink(BaseGoogleLink):
    """Helper class for constructing Data Fusion Instance link."""
[docs]    name = "Data Fusion Instance" 
    @staticmethod
[docs]    def persist(
        context: Context,
        task_instance: BaseOperator,
        location: str,
        instance_name: str,
        project_id: str,
    ):
        task_instance.xcom_push(
            context=context,
            key=DataFusionInstanceLink.key,
            value={
                "region": location,
                "instance_name": instance_name,
                "project_id": project_id,
            },
        )  
[docs]class DataFusionPipelineLink(BaseGoogleLink):
    """Helper class for constructing Data Fusion Pipeline link."""
[docs]    name = "Data Fusion Pipeline" 
    @staticmethod
[docs]    def persist(
        context: Context,
        task_instance: BaseOperator,
        uri: str,
        pipeline_name: str,
        namespace: str,
    ):
        task_instance.xcom_push(
            context=context,
            key=DataFusionPipelineLink.key,
            value={
                "uri": uri,
                "pipeline_name": pipeline_name,
                "namespace": namespace,
            },
        )  
[docs]class DataFusionPipelinesLink(BaseGoogleLink):
    """Helper class for constructing list of Data Fusion Pipelines link."""
[docs]    name = "Data Fusion Pipelines List" 
    @staticmethod
[docs]    def persist(
        context: Context,
        task_instance: BaseOperator,
        uri: str,
        namespace: str,
    ):
        task_instance.xcom_push(
            context=context,
            key=DataFusionPipelinesLink.key,
            value={
                "uri": uri,
                "namespace": namespace,
            },
        )