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 2020/01/22 14:39:30 UTC

[GitHub] [airflow] feluelle commented on a change in pull request #6824: [AIRFLOW-6258] add CloudFormation operators to AWS providers

feluelle commented on a change in pull request #6824: [AIRFLOW-6258] add CloudFormation operators to AWS providers
URL: https://github.com/apache/airflow/pull/6824#discussion_r369589043
 
 

 ##########
 File path: airflow/providers/amazon/aws/sensors/cloud_formation.py
 ##########
 @@ -0,0 +1,98 @@
+# -*- coding: utf-8 -*-
+#
+# 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.
+"""
+This module contains sensors for AWS CloudFormation.
+"""
+from airflow.providers.amazon.aws.hooks.cloud_formation import AWSCloudFormationHook
+from airflow.sensors.base_sensor_operator import BaseSensorOperator
+from airflow.utils.decorators import apply_defaults
+
+
+class CloudFormationCreateStackSensor(BaseSensorOperator):
+    """
+    Waits for a stack to be created successfully on AWS CloudFormation.
+
+    :param stack_name: The name of the stack to wait for (templated)
+    :type stack_name: str
+    :param aws_conn_id: ID of the Airflow connection where credentials and extra configuration are
+        stored
+    :type aws_conn_id: str
+    :param poke_interval: Time in seconds that the job should wait between each try
+    :type poke_interval: int
+    """
+
+    template_fields = ['stack_name']
+    ui_color = '#C5CAE9'
+
+    @apply_defaults
+    def __init__(self,
+                 stack_name,
+                 aws_conn_id='aws_default',
+                 region_name=None,
+                 *args,
+                 **kwargs):
+        super().__init__(*args, **kwargs)
+        self.stack_name = stack_name
+        self.hook = AWSCloudFormationHook(aws_conn_id=aws_conn_id, region_name=region_name)
+
+    def poke(self, context):
+        stack_status = self.hook.get_stack_status(self.stack_name)
+        if stack_status == 'CREATE_COMPLETE':
+            return True
+        elif stack_status in ('CREATE_IN_PROGRESS', None):
+            return False
+        else:
+            raise ValueError(f'Stack {self.stack_name} in bad state: {stack_status}')
+
+
+class CloudFormationDeleteStackSensor(BaseSensorOperator):
+    """
+    Waits for a stack to be deleted successfully on AWS CloudFormation.
+
+    :param stack_name: The name of the stack to wait for (templated)
+    :type stack_name: str
+    :param aws_conn_id: ID of the Airflow connection where credentials and extra configuration are
+        stored
+    :type aws_conn_id: str
+    :param poke_interval: Time in seconds that the job should wait between each try
+    :type poke_interval: int
+    """
+
+    template_fields = ['stack_name']
+    ui_color = '#C5CAE9'
+
+    @apply_defaults
+    def __init__(self,
+                 stack_name,
+                 aws_conn_id='aws_default',
+                 region_name=None,
+                 *args,
+                 **kwargs):
+        super().__init__(*args, **kwargs)
+        self.stack_name = stack_name
+        self.hook = AWSCloudFormationHook(aws_conn_id=aws_conn_id, region_name=region_name)
+
+    def poke(self, context):
+        stack_status = self.hook.get_stack_status(self.stack_name)
+        if stack_status in ('DELETE_COMPLETE', None):
+            return True
+        elif stack_status == 'DELETE_IN_PROGRESS':
+            return False
+        else:
+            raise ValueError(f'Stack {self.stack_name} in bad state: {stack_status}')
 
 Review comment:
   Same could be done here.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services