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 2022/07/13 13:55:44 UTC

[GitHub] [airflow] phanikumv commented on a diff in pull request #25018: Add `test_connection` method to AzureCosmosDBHook

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


##########
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:
   `StopIteration` will be raised if there is no database created . Am I understanding it correctly that the `None` will cover for that scenario?



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org