Miretpl commented on code in PR #71067:
URL: https://github.com/apache/airflow/pull/71067#discussion_r3855285433


##########
providers/redis/src/airflow/providers/redis/hooks/redis.py:
##########
@@ -111,18 +130,54 @@ def get_conn(self):
                     "lib_name": 
f"redis-py(apache-airflow-providers-redis_v{provider_version})",
                 }
 
-            self.redis = Redis(
-                host=self.host,
-                port=self.port,
-                username=self.username,
-                password=self.password,
-                db=self.db,
-                **ssl_args,
-                **driver_info_options,
-            )
+            if self.cluster:
+                self.redis = RedisCluster(
+                    host=self.host,
+                    port=self.port,
+                    startup_nodes=self._build_startup_nodes(),
+                    username=self.username,
+                    password=self.password,
+                    **ssl_args,
+                    **driver_info_options,
+                )
+            else:
+                self.redis = Redis(
+                    host=self.host,
+                    port=self.port,
+                    username=self.username,
+                    password=self.password,
+                    db=self.db,
+                    **ssl_args,
+                    **driver_info_options,
+                )
 
         return self.redis
 
+    def _build_startup_nodes(self) -> list[ClusterNode]:
+        """Build redis-py cluster nodes from the ``startup_nodes`` extra, 
given as ``host`` or ``host:port``."""
+        if not self.startup_nodes:
+            return []
+
+        if not isinstance(self.startup_nodes, str):
+            raise ValueError(
+                "`startup_nodes` must be a comma-separated string of 
`host:port` entries, got "

Review Comment:
   ```suggestion
                   "The `startup_nodes` parameter value must be a 
comma-separated string of `host:port` entries, got "
   ```



##########
providers/redis/src/airflow/providers/redis/hooks/redis.py:
##########
@@ -111,18 +130,54 @@ def get_conn(self):
                     "lib_name": 
f"redis-py(apache-airflow-providers-redis_v{provider_version})",
                 }
 
-            self.redis = Redis(
-                host=self.host,
-                port=self.port,
-                username=self.username,
-                password=self.password,
-                db=self.db,
-                **ssl_args,
-                **driver_info_options,
-            )
+            if self.cluster:
+                self.redis = RedisCluster(
+                    host=self.host,
+                    port=self.port,
+                    startup_nodes=self._build_startup_nodes(),
+                    username=self.username,
+                    password=self.password,
+                    **ssl_args,
+                    **driver_info_options,
+                )
+            else:
+                self.redis = Redis(
+                    host=self.host,
+                    port=self.port,
+                    username=self.username,
+                    password=self.password,
+                    db=self.db,
+                    **ssl_args,
+                    **driver_info_options,
+                )
 
         return self.redis
 
+    def _build_startup_nodes(self) -> list[ClusterNode]:
+        """Build redis-py cluster nodes from the ``startup_nodes`` extra, 
given as ``host`` or ``host:port``."""
+        if not self.startup_nodes:
+            return []
+
+        if not isinstance(self.startup_nodes, str):
+            raise ValueError(
+                "`startup_nodes` must be a comma-separated string of 
`host:port` entries, got "
+                f"{self.startup_nodes!r}."
+            )
+
+        nodes = []
+        for entry in self.startup_nodes.split(","):
+            host, _, port = entry.strip().partition(":")
+            if not host:
+                raise ValueError(f"Missing host in `startup_nodes` entry 
{entry!r}; expected `host:port`.")

Review Comment:
   ```suggestion
                   raise ValueError(f"Missing host in `startup_nodes` parameter 
value for entry {entry!r}; expected `host:port`.")
   ```



##########
providers/redis/src/airflow/providers/redis/hooks/redis.py:
##########
@@ -111,18 +130,54 @@ def get_conn(self):
                     "lib_name": 
f"redis-py(apache-airflow-providers-redis_v{provider_version})",
                 }
 
-            self.redis = Redis(
-                host=self.host,
-                port=self.port,
-                username=self.username,
-                password=self.password,
-                db=self.db,
-                **ssl_args,
-                **driver_info_options,
-            )
+            if self.cluster:
+                self.redis = RedisCluster(
+                    host=self.host,
+                    port=self.port,
+                    startup_nodes=self._build_startup_nodes(),
+                    username=self.username,
+                    password=self.password,
+                    **ssl_args,
+                    **driver_info_options,
+                )
+            else:
+                self.redis = Redis(
+                    host=self.host,
+                    port=self.port,
+                    username=self.username,
+                    password=self.password,
+                    db=self.db,
+                    **ssl_args,
+                    **driver_info_options,
+                )
 
         return self.redis
 
+    def _build_startup_nodes(self) -> list[ClusterNode]:
+        """Build redis-py cluster nodes from the ``startup_nodes`` extra, 
given as ``host`` or ``host:port``."""
+        if not self.startup_nodes:
+            return []
+
+        if not isinstance(self.startup_nodes, str):
+            raise ValueError(
+                "`startup_nodes` must be a comma-separated string of 
`host:port` entries, got "
+                f"{self.startup_nodes!r}."
+            )
+
+        nodes = []
+        for entry in self.startup_nodes.split(","):
+            host, _, port = entry.strip().partition(":")
+            if not host:
+                raise ValueError(f"Missing host in `startup_nodes` entry 
{entry!r}; expected `host:port`.")
+            try:
+                parsed_port = int(port) if port else DEFAULT_REDIS_PORT
+            except ValueError:
+                raise ValueError(
+                    f"Invalid port in `startup_nodes` entry {entry!r}; 
expected `host:port`."

Review Comment:
   ```suggestion
                       f"Invalid port in `startup_nodes` parameter value for 
entry {entry!r}; expected `host:port`."
   ```



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