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 09:18:08 UTC

[GitHub] [airflow] phanikumv opened a new pull request, #25018: Add `test_connection` method to AzureCosmosDBHook

phanikumv opened a new pull request, #25018:
URL: https://github.com/apache/airflow/pull/25018

   This PR enables the Test Connection button on the airflow UI for the connection type Azure CosmosDB.
   
   <img width="1020" alt="Screenshot 2022-07-13 at 11 27 15 AM" src="https://user-images.githubusercontent.com/94376113/178697991-2a381574-d1b3-4b20-9be3-b812047b66d6.png">
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   ---
   **^ Add meaningful description above**
   
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvements+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


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


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

Posted by GitBox <gi...@apache.org>.
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


[GitHub] [airflow] potiuk merged pull request #25018: Add `test_connection` method to AzureCosmosDBHook

Posted by GitBox <gi...@apache.org>.
potiuk merged PR #25018:
URL: https://github.com/apache/airflow/pull/25018


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


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

Posted by GitBox <gi...@apache.org>.
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 @uranusjr ?
   
   ```
   >>> next(iter([]), None)
   >>> next(iter([]))
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
   StopIteration
   ```
   



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


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

Posted by GitBox <gi...@apache.org>.
phanikumv commented on code in PR #25018:
URL: https://github.com/apache/airflow/pull/25018#discussion_r920173648


##########
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:
   Thanks ! Changed the code as per suggestion.



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


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

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

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


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

Posted by GitBox <gi...@apache.org>.
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 @uranusjr ?



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


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

Posted by GitBox <gi...@apache.org>.
uranusjr commented on code in PR #25018:
URL: https://github.com/apache/airflow/pull/25018#discussion_r920130411


##########
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:
   Yes
   
   https://docs.python.org/3/library/functions.html#next



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