Source code for airflow.providers.amazon.aws.hooks.dms
## 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.importjsonfromenumimportEnumfromtypingimportOptionalfromairflow.providers.amazon.aws.hooks.base_awsimportAwsBaseHook
[docs]classDmsHook(AwsBaseHook):"""Interact with AWS Database Migration Service."""def__init__(self,*args,**kwargs,):kwargs['client_type']='dms'super().__init__(*args,**kwargs)
[docs]defdescribe_replication_tasks(self,**kwargs):""" Describe replication tasks :return: Marker and list of replication tasks :rtype: (Optional[str], list) """dms_client=self.get_conn()response=dms_client.describe_replication_tasks(**kwargs)returnresponse.get('Marker'),response.get('ReplicationTasks',[])
[docs]deffind_replication_tasks_by_arn(self,replication_task_arn:str,without_settings:Optional[bool]=False):""" Find and describe replication tasks by task ARN :param replication_task_arn: Replication task arn :type replication_task_arn: str :param without_settings: Indicates whether to return task information with settings. :type without_settings: Optional[bool] :return: list of replication tasks that match the ARN """_,tasks=self.describe_replication_tasks(Filters=[{'Name':'replication-task-arn','Values':[replication_task_arn],}],WithoutSettings=without_settings,)returntasks
[docs]defget_task_status(self,replication_task_arn:str)->Optional[str]:""" Retrieve task status. :param replication_task_arn: Replication task ARN :type replication_task_arn: str :return: Current task status """replication_tasks=self.find_replication_tasks_by_arn(replication_task_arn=replication_task_arn,without_settings=True,)iflen(replication_tasks)==1:status=replication_tasks[0]['Status']self.log.info('Replication task with ARN(%s) has status "%s".',replication_task_arn,status)returnstatuselse:self.log.info('Replication task with ARN(%s) is not found.',replication_task_arn)returnNone
[docs]defdelete_replication_task(self,replication_task_arn):""" Starts replication task deletion and waits for it to be deleted :param replication_task_arn: Replication task ARN :type replication_task_arn: str """dms_client=self.get_conn()dms_client.delete_replication_task(ReplicationTaskArn=replication_task_arn)self.wait_for_task_status(replication_task_arn,DmsTaskWaiterStatus.DELETED)
[docs]defwait_for_task_status(self,replication_task_arn:str,status:DmsTaskWaiterStatus):""" Waits for replication task to reach status. Supported statuses: deleted, ready, running, stopped. :param status: Status to wait for :type status: DmsTaskWaiterStatus :param replication_task_arn: Replication task ARN :type replication_task_arn: str """ifnotisinstance(status,DmsTaskWaiterStatus):raiseTypeError('Status must be an instance of DmsTaskWaiterStatus')dms_client=self.get_conn()waiter=dms_client.get_waiter(f'replication_task_{status}')waiter.wait(Filters=[{'Name':'replication-task-arn','Values':[replication_task_arn,],},],WithoutSettings=True,