marshall7m commented on a change in pull request #13072:
URL: https://github.com/apache/airflow/pull/13072#discussion_r549739066



##########
File path: airflow/providers/amazon/aws/hooks/glue_crawler.py
##########
@@ -0,0 +1,181 @@
+#
+# 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 time
+
+from cached_property import cached_property
+
+from airflow.exceptions import AirflowException
+from airflow.providers.amazon.aws.hooks.base_aws import AwsBaseHook
+
+
+class AwsGlueCrawlerHook(AwsBaseHook):
+    """
+    Interacts with AWS Glue Crawler
+    :param poll_interval: Time (in seconds) to wait between two consecutive 
calls to check crawler status
+    :type poll_interval: int
+    :param config = Configurations for the AWS Glue crawler
+    :type config = Optional[dict]
+    """
+
+    def __init__(self, poll_interval, *args, **kwargs):
+        self.poll_interval = poll_interval
+        kwargs['client_type'] = 'glue'
+        super().__init__(*args, **kwargs)
+
+    @cached_property
+    def glue_client(self):
+        """:return: AWS Glue client"""
+        return self.get_conn()
+
+    def get_iam_execution_role(self, role_name) -> str:
+        """:return: iam role for crawler execution"""
+        iam_client = self.get_client_type('iam', self.region_name)
+
+        try:
+            glue_execution_role = iam_client.get_role(RoleName=role_name)
+            self.log.info("Iam Role Name: %s", role_name)
+            return glue_execution_role
+        except Exception as general_error:
+            self.log.error("Failed to create aws glue crawler, error: %s", 
general_error)
+            raise
+
+    def get_or_create_crawler(self, config) -> str:
+        """
+        Creates the crawler if the crawler doesn't exists and returns the 
crawler name
+
+        :param config = Configurations for the AWS Glue crawler
+        :type config = Optional[dict]
+        :return: Name of the crawler
+        """
+        self.get_iam_execution_role(config["Role"])

Review comment:
       @dstandish I tested the glue API's create_crawler() method with an 
invalid role and the exception was misleading because it outputted: 
```botocore.errorfactory.InvalidInputException: An error occurred 
(InvalidInputException) when calling the CreateCrawler operation: Service is 
unable to assume role arn:aws:iam::111111111111:role/test-foo-role. Please 
verify role's TrustPolicy```.  The `self.get_iam_execution_role()` outputted a 
more direct exception: ```botocore.errorfactory.NoSuchEntityException: An error 
occurred (NoSuchEntity) when calling the GetRole operation: The role with name 
test-foo-role cannot be found.```




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


Reply via email to