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 2019/01/10 22:49:13 UTC

[GitHub] stale[bot] closed pull request #2069: [AIRFLOW-865] - Configure FTP connection mode

stale[bot] closed pull request #2069: [AIRFLOW-865] - Configure FTP connection mode
URL: https://github.com/apache/airflow/pull/2069
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/airflow/contrib/hooks/ftp_hook.py b/airflow/contrib/hooks/ftp_hook.py
index 2f2e4c287f..ac10bc9ebb 100644
--- a/airflow/contrib/hooks/ftp_hook.py
+++ b/airflow/contrib/hooks/ftp_hook.py
@@ -58,8 +58,9 @@ class FTPHook(BaseHook):
     """
     Interact with FTP.
 
-    Errors that may occur throughout but should be handled
-    downstream.
+    Errors that may occur throughout but should be handled downstream.
+    You can specify mode for data transfers in the extra field of your
+    connection as ``{"passive": "true"}``.
     """
 
     def __init__(self, ftp_conn_id='ftp_default'):
@@ -79,7 +80,10 @@ def get_conn(self):
         """
         if self.conn is None:
             params = self.get_connection(self.ftp_conn_id)
+            pasv = params.extra_dejson.get("passive", True)
+
             self.conn = ftplib.FTP(params.host, params.login, params.password)
+            self.conn.set_pasv(pasv)
 
         return self.conn
 
@@ -237,7 +241,11 @@ def get_conn(self):
         """
         if self.conn is None:
             params = self.get_connection(self.ftp_conn_id)
+            pasv = params.extra_dejson.get("passive", True)
+
             self.conn = ftplib.FTP_TLS(
                 params.host, params.login, params.password
             )
+            self.conn.set_pasv(pasv)
+
         return self.conn
diff --git a/tests/contrib/hooks/test_ftp_hook.py b/tests/contrib/hooks/test_ftp_hook.py
index ab6f459aa5..f053406af6 100644
--- a/tests/contrib/hooks/test_ftp_hook.py
+++ b/tests/contrib/hooks/test_ftp_hook.py
@@ -79,5 +79,52 @@ def test_rename(self):
         self.conn_mock.quit.assert_called_once_with()
 
 
+class TestIntegrationFTPHook(unittest.TestCase):
+
+    def setUp(self):
+        super(TestIntegrationFTPHook, self).setUp()
+
+        from airflow import configuration
+        from airflow.utils import db
+        from airflow import models
+
+        configuration.load_test_config()
+        db.merge_conn(
+                models.Connection(
+                        conn_id='ftp_passive', conn_type='ftp',
+                        host='localhost',
+                        extra='{"passive": true}'))
+
+        db.merge_conn(
+                models.Connection(
+                        conn_id='ftp_active', conn_type='ftp',
+                        host='localhost',
+                        extra='{"passive": false}'))
+
+    def _test_mode(self, hook_type, connection_id, expected_mode):
+        hook = hook_type(connection_id)
+        conn = hook.get_conn()
+        conn.set_pasv.assert_called_with(expected_mode)
+
+    @mock.patch("ftplib.FTP")
+    def test_ftp_passive_mode(self, ftp_mock):
+        from airflow.contrib.hooks.ftp_hook import FTPHook
+        self._test_mode(FTPHook, "ftp_passive", True)
+
+    @mock.patch("ftplib.FTP")
+    def test_ftp_active_mode(self, ftp_mock):
+        from airflow.contrib.hooks.ftp_hook import FTPHook
+        self._test_mode(FTPHook, "ftp_active", False)
+
+    @mock.patch("ftplib.FTP_TLS")
+    def test_ftps_passive_mode(self, ftps_mock):
+        from airflow.contrib.hooks.ftp_hook import FTPSHook
+        self._test_mode(FTPSHook, "ftp_passive", True)
+
+    @mock.patch("ftplib.FTP_TLS")
+    def test_ftps_active_mode(self, ftps_mock):
+        from airflow.contrib.hooks.ftp_hook import FTPSHook
+        self._test_mode(FTPSHook, "ftp_active", False)
+
 if __name__ == '__main__':
     unittest.main()


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services