ferruzzi commented on a change in pull request #19665:
URL: https://github.com/apache/airflow/pull/19665#discussion_r754503713



##########
File path: airflow/providers/amazon/aws/operators/redshift_pause_cluster.py
##########
@@ -0,0 +1,58 @@
+#
+# 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.
+
+from airflow.models import BaseOperator
+from airflow.providers.amazon.aws.hooks.redshift import ClusterStates, 
RedshiftHook
+
+
+class RedshiftPauseClusterOperator(BaseOperator):
+    """
+    Pause an AWS Redshift Cluster using boto3.
+
+    :param cluster_identifier: id of the AWS Redshift Cluster
+    :type cluster_identifier: str
+    :param aws_conn_id: aws connection to use
+    :type aws_conn_id: str
+    :param check_interval: time in seconds that the job should wait in
+        between each instance state checks until operation is completed
+    :type check_interval: float
+    """
+
+    template_fields = ("cluster_identifier",)
+    ui_color = "#eeaa11"
+    ui_fgcolor = "#ffffff"
+
+    def __init__(
+        self,
+        *,
+        cluster_identifier: str,
+        aws_conn_id: str = "aws_default",
+        check_interval: float = 15,
+        **kwargs,
+    ):
+        super().__init__(**kwargs)
+        self.cluster_identifier = cluster_identifier
+        self.aws_conn_id = aws_conn_id
+        self.check_interval = check_interval
+
+    def execute(self, context):
+        redshift_hook = RedshiftHook(aws_conn_id=self.aws_conn_id)
+        self.log.info("Pausing Redshift cluster %s", self.cluster_identifier)
+        cluster_state = 
ClusterStates(redshift_hook.cluster_status(cluster_identifier=self.cluster_identifier))
+        if cluster_state == ClusterStates.AVAILABLE:
+            
redshift_hook.get_conn().pause_cluster(ClusterIdentifier=self.cluster_identifier)

Review comment:
       First, thanks for the contribution!  As a new contributor myself, I'll 
make a comment but leave it up to a Committer to decide if it's a required 
change.
   
   I'd suggest adding these as methods in the hook as well.  That would make 
your code a bit cleaner and make it easier for others to write custom 
Operators.  For example, check out how the 
[hooks/redshift.py](https://github.com/ferruzzi/airflow/blob/main/airflow/providers/amazon/aws/hooks/redshift.py#L72)
 file handles `delete_cluster`.  It basically just offloads the `get_conn()` 
part and passes the variables through, but then in the Operator it's a cleaner 
`redshift_hook.delete_cluster(ClusterIdentifier=self.cluster_identifier)` 
instead of having to stub in the  `get_conn()` everywhere in your Operator.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to