ecerulm commented on a change in pull request #16686:
URL: https://github.com/apache/airflow/pull/16686#discussion_r659750668
##########
File path: airflow/models/taskinstance.py
##########
@@ -1670,6 +1696,9 @@ def get(
'json': VariableJsonAccessor(),
'value': VariableAccessor(),
},
+ 'conn': {
+ 'value': ConnectionAccessor(),
+ },
Review comment:
No , there is **no need**, it's just for having a **consistent api for
both var and conns**. I mean not having to remember that when you access a
variable you need `var.value.xxx` but when you access a connection is just
`conn.xxx`. But sure, I'll change it.
##########
File path: docs/apache-airflow/macros-ref.rst
##########
@@ -97,6 +99,13 @@ It is also possible to fetch a variable by string if needed
with
``{{ var.json.get('my.dict.var', {'key1': 'val1'}) }}``. Defaults can be
supplied in case the variable does not exist.
+Similarly, Airflow Connections data can be accessed via the ``conn`` template
variable.
+For example, you could use expressions in your templates like ``{{
conn.my_conn_id.login }}``,
+``{{ conn.my_conn_id.password }}``, etc.
+Just like with ``var`` it's possible to fetch a connection by string or
provide defaults with
+``{{ conn.get('my_conn_id').host }}`` or
Review comment:
This one just show that it's possible to fetch a conn_id via an string
it's not about the defaults. I mean the `.get()` has two advantages one is that
you can lookup a conn_id calculates at runtime like
`conn.get('my_conn_'+index)`. The other advantage is that you can provide
defaults.
It the same examples for `var.value.get` if I recall properly I just copied
them and and changed the wording.
##########
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.<variable_name>.host }}
Review comment:
yes, fixed
##########
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:
the document for variables (variable.rst) uses the `echo {{
conn.<variable_name> }}` so I rather keep it this way to maintain consistency
between those two.
##########
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:
won't be that encouraging one of the security pitfalls ? Users then will
write the URI in the dag source code and the URI most likely contains a secret,
right? Or did I misunderstood you
--
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]