ashb commented on a change in pull request #16686:
URL: https://github.com/apache/airflow/pull/16686#discussion_r660375106



##########
File path: docs/apache-airflow/concepts/connections.rst
##########
@@ -24,9 +24,11 @@ A Connection is essentially set of parameters - such as 
username, password and h
 
 They can be managed via the UI or via the CLI; see :doc:`/howto/connection` 
for more information on creating, editing and managing connections. There are 
customizable connection storage and backend options.
 
-You can use Connections directly from your own code, or you can use them via 
Hooks.
+You can use Connections directly from your own code, you can use them via 
Hooks or use them from :ref:`templates <concepts:jinja-templating>`::
 
 
+    echo {{ conn.<conn_id>.host }}

Review comment:
       ```suggestion
       echo {{ conn.my_conn_id.host }}
   ```

##########
File path: airflow/models/taskinstance.py
##########
@@ -1630,6 +1632,30 @@ def get(
                 """Get Airflow Variable after deserializing JSON value"""
                 return Variable.get(item, default_var=default_var, 
deserialize_json=True)
 
+        class ConnectionAccessor:
+            """
+            Wrapper around Connection. This way you can get connections in
+            templates by using ``{{ conn.conn_id }}`` or
+            ``{{ conn.get('conn_id', 'fallback') }}``.

Review comment:
       Ah, you've got a useful example in the macros doc. Maybe just put a "see 
macros" here to avoid duplication.

##########
File path: airflow/models/taskinstance.py
##########
@@ -1630,6 +1632,30 @@ def get(
                 """Get Airflow Variable after deserializing JSON value"""
                 return Variable.get(item, default_var=default_var, 
deserialize_json=True)
 
+        class ConnectionAccessor:
+            """
+            Wrapper around Connection. This way you can get connections in
+            templates by using ``{{ conn.conn_id }}`` or
+            ``{{ conn.get('conn_id', 'fallback') }}``.

Review comment:
       Does a fallback make sense here? For variables is was easier as it is a 
"simple" object, but connection is much more complex, so I can't think _what_ 
we would put in the fallback value to make the template be useful?
   
   (Not saying we should remove it, just that maybe the `'fallback'` isn't the 
best example here.

##########
File path: airflow/models/taskinstance.py
##########
@@ -1630,6 +1632,30 @@ def get(
                 """Get Airflow Variable after deserializing JSON value"""
                 return Variable.get(item, default_var=default_var, 
deserialize_json=True)
 
+        class ConnectionAccessor:
+            """
+            Wrapper around Connection. This way you can get connections in
+            templates by using ``{{ conn.conn_id }}`` or
+            ``{{ conn.get('conn_id', 'fallback') }}``.
+            """
+
+            def __getattr__(
+                self,
+                item: str,
+            ):
+                return Connection.get_connection_from_secrets(item)
+
+            @staticmethod
+            def get(
+                item: str,
+                default_conn: Any = None,
+            ):
+                """Get Airflow Connection value"""
+                try:
+                    return Connection.get_connection_from_secrets(item)
+                except AirflowNotFoundException:
+                    return default_conn

Review comment:
       Is it worth doing something like:
   
   
   ```suggestion
                       if isinstance(default_conn, str):
                           return Connection(uri=default_conn)
                       else:
                           return default_conn
   ```
   
   to support passing a URI as the fallback and have it parsed to a Connection 
object?

##########
File path: airflow/models/taskinstance.py
##########
@@ -1630,6 +1632,30 @@ def get(
                 """Get Airflow Variable after deserializing JSON value"""
                 return Variable.get(item, default_var=default_var, 
deserialize_json=True)
 
+        class ConnectionAccessor:
+            """
+            Wrapper around Connection. This way you can get connections in
+            templates by using ``{{ conn.conn_id }}`` or
+            ``{{ conn.get('conn_id', 'fallback') }}``.
+            """
+
+            def __getattr__(
+                self,
+                item: str,
+            ):
+                return Connection.get_connection_from_secrets(item)
+
+            @staticmethod
+            def get(
+                item: str,
+                default_conn: Any = None,
+            ):
+                """Get Airflow Connection value"""
+                try:
+                    return Connection.get_connection_from_secrets(item)
+                except AirflowNotFoundException:
+                    return default_conn

Review comment:
       Yeah, you're probably right.




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