Source code for

# 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
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
"""This module contains Google Cloud Bigtable sensor."""
from __future__ import annotations

from typing import TYPE_CHECKING, Sequence

import google.api_core.exceptions
from import enums
from import ClusterState

from import BigtableHook
from import BigtableTablesLink
from import BigtableValidationMixin
from airflow.sensors.base import BaseSensorOperator

    from airflow.utils.context import Context

[docs]class BigtableTableReplicationCompletedSensor(BaseSensorOperator, BigtableValidationMixin): """ Sensor that waits for Cloud Bigtable table to be fully replicated to its clusters. No exception will be raised if the instance or the table does not exist. For more details about cluster states for a table, have a look at the reference: .. seealso:: For more information on how to use this operator, take a look at the guide: :ref:`howto/operator:BigtableTableReplicationCompletedSensor` :param instance_id: The ID of the Cloud Bigtable instance. :param table_id: The ID of the table to check replication status. :param project_id: Optional, the ID of the Google Cloud project. :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] REQUIRED_ATTRIBUTES = ("instance_id", "table_id")
[docs] template_fields: Sequence[str] = ( "project_id", "instance_id", "table_id", "impersonation_chain", )
def __init__( self, *, instance_id: str, table_id: str, project_id: str | None = None, gcp_conn_id: str = "google_cloud_default", impersonation_chain: str | Sequence[str] | None = None, **kwargs, ) -> None: self.project_id = project_id self.instance_id = instance_id self.table_id = table_id self.gcp_conn_id = gcp_conn_id self._validate_inputs() self.impersonation_chain = impersonation_chain super().__init__(**kwargs)
[docs] def poke(self, context: Context) -> bool: hook = BigtableHook( gcp_conn_id=self.gcp_conn_id, impersonation_chain=self.impersonation_chain, ) instance = hook.get_instance(project_id=self.project_id, instance_id=self.instance_id) if not instance:"Dependency: instance '%s' does not exist.", self.instance_id) return False try: cluster_states = hook.get_cluster_states_for_table(instance=instance, table_id=self.table_id) except google.api_core.exceptions.NotFound: "Dependency: table '%s' does not exist in instance '%s'.", self.table_id, self.instance_id ) return False ready_state = ClusterState(enums.Table.ReplicationState.READY) is_table_replicated = True for cluster_id in cluster_states.keys(): if cluster_states[cluster_id] != ready_state:"Table '%s' is not yet replicated on cluster '%s'.", self.table_id, cluster_id) is_table_replicated = False if not is_table_replicated: return False"Table '%s' is replicated.", self.table_id) BigtableTablesLink.persist(context=context, task_instance=self) return True

Was this entry helpful?