uranusjr commented on code in PR #25018:
URL: https://github.com/apache/airflow/pull/25018#discussion_r920040428


##########
airflow/providers/microsoft/azure/hooks/cosmos.py:
##########
@@ -320,6 +320,25 @@ def get_documents(
         except CosmosHttpResponseError:
             return None
 
+    def test_connection(self):
+        """Test a configured Azure Cosmos connection."""
+        success = (True, "Successfully connected to Azure Cosmos.")
+        try:
+            # Attempt to list existing databases under the configured 
subscription and retrieve the first in
+            # the returned iterator. The Azure Cosmos API does allow for 
creation of a
+            # CosmosClient with incorrect values but then will fail properly 
once items are
+            # retrieved using the client. We need to _actually_ try to 
retrieve an object to properly test the
+            # connection.
+            next(iter(self.get_conn().list_databases()))
+            return success
+        except StopIteration:
+            # If the iterator returned is empty it should still be considered 
a successful connection since
+            # it's possible to create a Cosmos connection via the 
``AzureCosmosDBHook`` and no database could
+            # legitimately exist yet.
+            return success
+        except Exception as e:
+            return False, str(e)

Review Comment:
   If we’re ignoring both the content and the StopIteration case, it’s much 
easier to do
   
   ```python
   try:
       next(iter(self.get_conn().list_databases()), None)
   except Exception as e:
       return False, str(e)
   return (True, "Successfully connected to Azure Cosmos.")



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