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_r371262602
 
 

 ##########
 File path: airflow/providers/amazon/aws/hooks/cloud_formation.py
 ##########
 @@ -0,0 +1,88 @@
+# -*- 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 AWS CloudFormation Hook
+"""
+from botocore.exceptions import ClientError
+
+from airflow.contrib.hooks.aws_hook import AwsHook
+
+
+class AWSCloudFormationHook(AwsHook):
+    """
+    Interact with AWS CloudFormation.
+    """
+
+    def __init__(self, region_name=None, *args, **kwargs):
+        self.region_name = region_name
+        self.conn = None
+        super().__init__(*args, **kwargs)
+
+    def get_conn(self):
+        if not self.conn:
+            self.conn = self.get_client_type('cloudformation', 
self.region_name)
+        return self.conn
+
+    def get_stack_status(self, stack_name):
+        """
+        Get stack status from CloudFormation.
+        """
+        cloudformation = self.get_conn()
+
+        self.log.info('Poking for stack %s', stack_name)
+
+        try:
+            stacks = 
cloudformation.describe_stacks(StackName=stack_name)['Stacks']
+            return stacks[0]['StackStatus']
+        except ClientError as e:
+            if 'does not exist' in str(e):
+                return None
+            else:
+                raise e
 
 Review comment:
   
![image](https://user-images.githubusercontent.com/5708714/73180571-39a28d00-411e-11ea-960a-69a582d1b7e0.png)
   
   We could check the error code instead of the message string, and assume that 
`ValidationError` returned from `describe_stacks` could only be due to stack 
not existing.
   
   However potentially there be other scenarios in which that can happen, for 
example, illegal characters in `StackName`:
   ```>>> cf.describe_stacks(StackName='||')
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File "/usr/local/lib/python3.7/site-packages/botocore/client.py", line 
357, in _api_call
       return self._make_api_call(operation_name, kwargs)
     File "/usr/local/lib/python3.7/site-packages/botocore/client.py", line 
661, in _make_api_call
       raise error_class(parsed_response, operation_name)
   botocore.exceptions.ClientError: An error occurred (ValidationError) when 
calling the DescribeStacks operation: 1 validation error detected: Value '||' 
at 'stackName' failed to satisfy constraint: Member must satisfy regular 
expression pattern: [a-zA-Z][-a-zA-Z0-9]*|arn:[-a-zA-Z0-9:/._+]*```
   
   

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to