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/07 08:50:17 UTC

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

aviemzur 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_r363644240
 
 

 ##########
 File path: airflow/providers/amazon/aws/sensors/cloud_formation.py
 ##########
 @@ -0,0 +1,162 @@
+# -*- 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 botocore.exceptions import ClientError
+
+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 BaseCloudFormationSensor(BaseSensorOperator):
+    """
+    Waits for a stack operation to complete 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
+    """
+
+    @apply_defaults
+    def __init__(self,
+                 stack_name,
+                 complete_status,
+                 in_progress_status,
+                 aws_conn_id='aws_default',
+                 poke_interval=60 * 3,
+                 *args,
+                 **kwargs):
+        super().__init__(poke_interval=poke_interval, *args, **kwargs)
+        self.aws_conn_id = aws_conn_id
+        self.stack_name = stack_name
+        self.complete_status = complete_status
+        self.in_progress_status = in_progress_status
+        self.hook = None
+
+    def poke(self, context):
+        """
+        Checks for existence of the stack in AWS CloudFormation.
+        """
+        cloudformation = self.get_hook().get_conn()
+
+        self.log.info('Poking for stack %s', self.stack_name)
+
+        try:
+            stacks = cloudformation.describe_stacks(StackName=self.stack_name)['Stacks']
+            stack_status = stacks[0]['StackStatus']
+            if stack_status == self.complete_status:
+                return True
+            elif stack_status == self.in_progress_status:
+                return False
+            else:
+                raise ValueError(f'Stack {self.stack_name} in bad state: {stack_status}')
+        except ClientError as e:
+            if 'does not exist' in str(e):
+                if not self.allow_non_existing_stack_status():
+                    raise ValueError(f'Stack {self.stack_name} does not exist')
+                else:
+                    return True
+            else:
+                raise e
+
+    def get_hook(self):
+        """
+        Gets the AwsGlueCatalogHook
+        """
+        if not self.hook:
+            self.hook = AWSCloudFormationHook(aws_conn_id=self.aws_conn_id)
+
+        return self.hook
+
+    def allow_non_existing_stack_status(self):
+        """
+        Boolean value whether or not sensor should allow non existing stack responses.
+        """
+        return False
 
 Review comment:
   When you query CloudFormation about a stack and it doesn't exist it throws an exception.
   For checking if a cluster was created and you get this exception, that means something went wrong.
   For checking if a cluster was deleted, this sadly could be an appropriate response from CloudFormation API, the cluster has indeed been deleted.

----------------------------------------------------------------
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