Source code for airflow.providers.amazon.aws.sensors.rds
# 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.fromtypingimportTYPE_CHECKING,List,Optional,Sequencefrombotocore.exceptionsimportClientErrorfromairflowimportAirflowExceptionfromairflow.providers.amazon.aws.hooks.rdsimportRdsHookfromairflow.providers.amazon.aws.utils.rdsimportRdsDbTypefromairflow.sensors.baseimportBaseSensorOperatorifTYPE_CHECKING:fromairflow.utils.contextimportContextclassRdsBaseSensor(BaseSensorOperator):"""Base operator that implements common functions for all sensors"""ui_color="#ddbb77"ui_fgcolor="#ffffff"def__init__(self,*args,aws_conn_id:str="aws_conn_id",hook_params:Optional[dict]=None,**kwargs):hook_params=hook_paramsor{}self.hook=RdsHook(aws_conn_id=aws_conn_id,**hook_params)self.target_statuses:List[str]=[]super().__init__(*args,**kwargs)def_describe_item(self,item_type:str,item_name:str)->list:ifitem_type=='instance_snapshot':db_snaps=self.hook.conn.describe_db_snapshots(DBSnapshotIdentifier=item_name)returndb_snaps['DBSnapshots']elifitem_type=='cluster_snapshot':cl_snaps=self.hook.conn.describe_db_cluster_snapshots(DBClusterSnapshotIdentifier=item_name)returncl_snaps['DBClusterSnapshots']elifitem_type=='export_task':exports=self.hook.conn.describe_export_tasks(ExportTaskIdentifier=item_name)returnexports['ExportTasks']else:raiseAirflowException(f"Method for {item_type} is not implemented")def_check_item(self,item_type:str,item_name:str)->bool:"""Get certain item from `_describe_item()` and check its status"""try:items=self._describe_item(item_type,item_name)exceptClientError:returnFalseelse:returnbool(items)andany(map(lambdas:items[0]['Status'].lower()==s,self.target_statuses))
[docs]classRdsSnapshotExistenceSensor(RdsBaseSensor):""" Waits for RDS snapshot with a specific status. .. seealso:: For more information on how to use this sensor, take a look at the guide: :ref:`howto/sensor:RdsSnapshotExistenceSensor` :param db_type: Type of the DB - either "instance" or "cluster" :param db_snapshot_identifier: The identifier for the DB snapshot :param target_statuses: Target status of snapshot """
[docs]defpoke(self,context:'Context'):self.log.info('Poking for statuses : %s\nfor snapshot %s',self.target_statuses,self.db_snapshot_identifier)ifself.db_type.value=="instance":returnself._check_item(item_type='instance_snapshot',item_name=self.db_snapshot_identifier)else:returnself._check_item(item_type='cluster_snapshot',item_name=self.db_snapshot_identifier)
[docs]classRdsExportTaskExistenceSensor(RdsBaseSensor):""" Waits for RDS export task with a specific status. .. seealso:: For more information on how to use this sensor, take a look at the guide: :ref:`howto/sensor:RdsExportTaskExistenceSensor` :param export_task_identifier: A unique identifier for the snapshot export task. :param target_statuses: Target status of export task """