You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/02/04 21:00:47 UTC

[GitHub] [airflow] kazanzhy commented on a change in pull request #20907: Added AWS RDS operators

kazanzhy commented on a change in pull request #20907:
URL: https://github.com/apache/airflow/pull/20907#discussion_r799807600



##########
File path: airflow/providers/amazon/aws/operators/rds.py
##########
@@ -0,0 +1,605 @@
+#
+# 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.
+
+import json
+import time
+from typing import TYPE_CHECKING, List, Optional, Sequence
+
+from mypy_boto3_rds.type_defs import TagTypeDef
+
+from airflow.exceptions import AirflowException
+from airflow.models import BaseOperator
+from airflow.providers.amazon.aws.hooks.rds import RdsHook
+from airflow.providers.amazon.aws.utils.rds import RdsDbType
+
+if TYPE_CHECKING:
+    from airflow.utils.context import Context
+
+
+class BaseRdsOperator(BaseOperator):
+    """Base operator that implements common functions for all operators"""
+
+    ui_color = "#eeaa88"
+    ui_fgcolor = "#ffffff"
+
+    def __init__(self, *args, aws_conn_id: str = "aws_conn_id", hook_params: Optional[dict] = None, **kwargs):
+        hook_params = hook_params or {}
+        self.hook = RdsHook(aws_conn_id=aws_conn_id, **hook_params)
+        super().__init__(*args, **kwargs)
+
+        self._wait_interval = 60  # seconds
+
+    def _describe_item(self, **kwargs) -> list:
+        """Returns information about target item: snapshot, task or event"""
+        raise NotImplementedError
+
+    def _await_termination(
+        self,
+        wait_statuses: Optional[List[str]] = None,
+        ok_statuses: Optional[List[str]] = None,
+        error_statuses: Optional[List[str]] = None,
+        **kwargs,
+    ) -> list:
+        """
+        Continuously gets item description from `_describe_item()` and waits until:
+        - status is in `wait_statuses`
+        - status not in `ok_statuses` and `error_statuses`
+        - `_describe_item()` returns non-empty list
+        """
+        while True:
+            items = self._describe_item(**kwargs)
+
+            if len(items) == 0:
+                break
+            elif len(items) > 1:
+                raise AirflowException(f"There is more than one item with the same identifier: {items}")
+
+            if wait_statuses and items[0]['Status'] in wait_statuses:
+                continue
+            elif ok_statuses and items[0]['Status'] in ok_statuses:
+                break
+            elif error_statuses and items[0]['Status'] in error_statuses:
+                raise AirflowException(f"All items have error statuses: {items}")
+
+            time.sleep(self._wait_interval)
+
+        return items
+
+    def execute(self, context: 'Context') -> str:
+        """Different implementations for snapshots, tasks and events"""
+        raise NotImplementedError
+
+    def on_kill(self) -> None:
+        """Different implementations for snapshots, tasks and events"""
+        raise NotImplementedError
+
+
+class RdsCreateDbSnapshotOperator(BaseRdsOperator):
+    """
+    Creates a snapshot of a DB instance or DB cluster.
+    The source DB instance or cluster must be in the available or storage-optimization state.
+
+    .. seealso::
+        For more information on how to use this operator, take a look at the guide:
+        :ref:`howto/operator:RdsCreateDbSnapshotOperator`
+
+    :param db_type: Type of the DB - either "instance" or "cluster"
+    :type db_type: RDSDbType
+    :param db_identifier: The identifier of the instance or cluster that you want to create the snapshot of
+    :type db_identifier: str
+    :param db_snapshot_identifier: The identifier for the DB snapshot
+    :type db_snapshot_identifier: str
+    :param tags: A list of tags in format `[{"Key": "something", "Value": "something"},]
+        `USER Tagging <https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_Tagging.html>`__
+    :type tags: Sequence[TagTypeDef] or None
+    """
+
+    template_fields = ("db_snapshot_identifier", "db_instance_identifier", "tags")
+
+    def __init__(
+        self,
+        *,
+        db_type: str,

Review comment:
       I created this `RdsDbType` just for the easy check. Internal `self.db_type` has type as `RdsDbType`.
   
   For example, a similar check of the `mode` parameter in `BaseSensorOperator` is implemented like:
   ```
           if self.mode not in self.valid_modes:
               raise AirflowException(
                   f"The mode must be one of {self.valid_modes},'{self.dag.dag_id if self.has_dag() else ''} "
                   f".{self.task_id}'; received '{self.mode}'."
               )
    ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org