Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add conn to jinja template context #16686

Merged
merged 5 commits into from Jun 29, 2021
Merged

Add conn to jinja template context #16686

merged 5 commits into from Jun 29, 2021

Conversation

ecerulm
Copy link
Contributor

@ecerulm ecerulm commented Jun 28, 2021

closes: #14597


^ Add meaningful description above

Read the Pull Request Guidelines for more information.
In case of fundamental code change, Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in UPDATING.md.

@ecerulm
Copy link
Contributor Author

ecerulm commented Jun 28, 2021

pinging @uranusjr , @potiuk , and @turbaszek as they were involved in the discussion of #14597

@uranusjr
Copy link
Member

Logic looks good to me, but this won’t pass static checks (formatting issues). Please try to run those locally and fix them first.

@ecerulm
Copy link
Contributor Author

ecerulm commented Jun 28, 2021

Logic looks good to me, but this won’t pass static checks (formatting issues). Please try to run those locally and fix them first.

I did run those locally prior to submitting the PR. pre-commit run did not complain for me:

git fetch origin
pre-commit run --from-ref $(git merge-base origin/main jinjaconn) --to-ref jinjaconn

identity.................................................................................Passed
- hook id: identity
- duration: 0.11s

airflow/models/taskinstance.py
tests/models/test_taskinstance.py

Check hooks apply to the repository..................................(no files to check)Skipped
No-tabs checker..........................................................................Passed
Add license for all SQL files........................................(no files to check)Skipped
Add license for all other files......................................(no files to check)Skipped
Add license for all rst files........................................(no files to check)Skipped
Add license for all JS/TS/TSX/CSS/PUML files.........................(no files to check)Skipped
Add license for all JINJA template files.............................(no files to check)Skipped
Add license for all shell files......................................(no files to check)Skipped
Add license for all Python files.........................................................Passed
Add license for all XML files........................................(no files to check)Skipped
Add license for all YAML files.......................................(no files to check)Skipped
Add license for all md files.........................................(no files to check)Skipped
Add license for all mermaid files....................................(no files to check)Skipped
Add TOC for md files.................................................(no files to check)Skipped
black....................................................................................Passed
blacken-docs.............................................................................Passed
Check for merge conflicts................................................................Passed
Debug Statements (Python)................................................................Passed
Check builtin type constructor use.......................................................Passed
Detect Private Key.......................................................................Passed
Fix End of Files.........................................................................Passed
Mixed line ending........................................................................Passed
Check that executables have shebangs.................................(no files to check)Skipped
Check Xml............................................................(no files to check)Skipped
Trim Trailing Whitespace.................................................................Passed
Fix python encoding pragma...............................................................Passed
Pretty format JSON...................................................(no files to check)Skipped
pyupgrade................................................................................Passed
rst ``code`` is two backticks........................................(no files to check)Skipped
use logger.warning(......................................................................Passed
Check YAML files with yamllint.......................................(no files to check)Skipped
Run isort to sort imports in Python files................................................Passed
Run pydocstyle...........................................................................Passed
Lint OpenAPI using spectral..........................................(no files to check)Skipped
Lint OpenAPI using openapi-spec-validator............................(no files to check)Skipped
Lint dockerfile......................................................(no files to check)Skipped
Check order of dependencies in setup.cfg and setup.py................(no files to check)Skipped
Checks setup extra packages..........................................(no files to check)Skipped
Update output of breeze command in BREEZE.rst........................(no files to check)Skipped
Update mounts in the local yml file..................................(no files to check)Skipped
Update setup.cfg file with all licenses..............................(no files to check)Skipped
Build cross-dependencies for providers packages......................(no files to check)Skipped
Update extras in documentation.......................................(no files to check)Skipped
Check for pydevd debug statements accidentally left......................................Passed
Don't use safe in templates..........................................(no files to check)Skipped
No providers imports in core example DAGs............................(no files to check)Skipped
No relative imports......................................................................Passed
Check for language that we do not accept as community....................................Passed
Check for inconsistent pylint disable/enable without space...............................Passed
Check BaseOperator[Link] core imports....................................................Passed
Check BaseOperator[Link] other imports...............................(no files to check)Skipped
Check provide_session and create_session imports.........................................Passed
Make sure LoggingMixin is not used alone.................................................Passed
Make sure days_ago is imported from airflow.utils.dates..................................Passed
'start_date' should not be defined in default_args in example_dags...(no files to check)Skipped
Check if integration list is aligned.................................(no files to check)Skipped
Check if licenses are OK for Apache..................................(no files to check)Skipped
Checks for consistency between config.yml and default_config.cfg.....(no files to check)Skipped
Verify that DB Migrations have been documented.......................(no files to check)Skipped
Checks for Boring Cyborg configuration consistency.......................................Passed
Sort INTHEWILD.md alphabetically.....................................(no files to check)Skipped
Sort alphabetically and uniquify spelling_wordlist.txt...............(no files to check)Skipped
Lint Helm Chart......................................................(no files to check)Skipped
Check Shell scripts syntax correctness...............................(no files to check)Skipped
Run BATS bash tests for changed Breeze bash files....................(no files to check)Skipped
Run BATS bash tests for changed bash files...........................(no files to check)Skipped
stylelint............................................................(no files to check)Skipped
Provider init file.......................................................................Passed
Validate providers.yaml files........................................(no files to check)Skipped
Generate mermaid images..............................................(no files to check)Skipped
Check if pre-commits are described...................................(no files to check)Skipped
Ensure hook ids are not overly long..................................(no files to check)Skipped
Checks providers available when declared by extras in setup.py.......(no files to check)Skipped
Run markdownlint.....................................................(no files to check)Skipped
Lint JSON Schema files with JSON Schema..............................(no files to check)Skipped
Lint NodePort Service with JSON Schema...............................(no files to check)Skipped
Lint Docker compose files with JSON Schema...........................(no files to check)Skipped
Lint chart/values.schema.json file with JSON Schema..................(no files to check)Skipped
Lint chart/values.yaml file with JSON Schema.........................(no files to check)Skipped
Lint airflow/config_templates/config.yml file with JSON Schema.......(no files to check)Skipped
Convert to f-strings with flynt..........................................................Passed
ESLint against airflow/ui............................................(no files to check)Skipped
ESLint against current UI js files...................................(no files to check)Skipped
Run in container bats tests..........................................(no files to check)Skipped
Check if image build is needed...........................................................Passed
Run mypy.................................................................................Passed
Run mypy for helm chart tests........................................(no files to check)Skipped
Run mypy for /docs/ folder...........................................(no files to check)Skipped
Run pylint for main code.................................................................Passed
Run pylint for tests.....................................................................Passed
Run pylint for helm chart tests......................................(no files to check)Skipped
Run flake8...............................................................................Passed

Also it seems to be passing the static checks in CI too https://github.com/apache/airflow/pull/16686/checks?check_run_id=2929988055

@ecerulm
Copy link
Contributor Author

ecerulm commented Jun 28, 2021

@potiuk The CI job is failing due to pylint in a file I didn't touch:

tests/jobs/test_scheduler_job.py:3663:15: E1101: Instance of 'str' has no 'task_id' member (no-member)

I'm rebased to the latest origin/branch commit 2833c2b

@potiuk
Copy link
Member

potiuk commented Jun 28, 2021

Yeah. We are just about to remove pylint. Don't worry about that

@potiuk
Copy link
Member

potiuk commented Jun 28, 2021

See #16682

@ecerulm ecerulm force-pushed the jinjaconn branch 2 times, most recently from f4dca29 to 57283d4 Compare June 28, 2021 12:49
@ecerulm ecerulm requested a review from ashb June 28, 2021 12:50
@github-actions github-actions bot added the full tests needed We need to run full set of tests for this PR to merge label Jun 28, 2021
@github-actions
Copy link

The PR most likely needs to run full matrix of tests because it modifies parts of the core of Airflow. However, committers might decide to merge it quickly and take the risk. If they don't merge it quickly - please rebase it to the latest main at your convenience, or amend the last commit of the PR, and push it with --force-with-lease.

@ecerulm
Copy link
Contributor Author

ecerulm commented Jun 28, 2021

I did change the connection accessor syntax from conn.value.my_connection.host to conn.my_connection.host as per request of @ashb.

Also, I guess I need to do a PR towards airflow-site too? https://github.com/apache/airflow/edit/main/docs/apache-airflow/macros-ref.rst , right? Do I do that already now or we shall wait until this PR is merged.

@potiuk
Copy link
Member

potiuk commented Jun 28, 2021

Also, I guess I need to do a PR towards airflow-site too? https://github.com/apache/airflow/edit/main/docs/apache-airflow/macros-ref.rst , right? Do I do that already now or we shall wait until this PR is merged.

Good point. Not really PR to airflow-site - you need to update the https://github.com/apache/airflow/blob/main/docs/apache-airflow/macros-ref.rst in the current PR. The docs at "airflow-site" are generated from there when we release.

@eladkal
Copy link
Contributor

eladkal commented Jun 28, 2021

Question: how is it going to work for connection custom fields? for example: Asana has workspace in a separate connection field

@kaxil
Copy link
Member

kaxil commented Jun 28, 2021

Question: how is it going to work for connection custom fields? for example: Asana has workspace in a separate connection field

That should be {{ conn.a_connection.extra_dejson['extra__asana__workspace'] }} but good point, we should add test for such case probably

Copy link
Member

@kaxil kaxil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ecerulm
Copy link
Contributor Author

ecerulm commented Jun 28, 2021

Ok, I added testcase for extra_dejson and update documentation in macros-ref.rst and connnections.rst

@ecerulm ecerulm requested a review from kaxil June 28, 2021 14:07
Copy link
Member

@potiuk potiuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really nice now!

@potiuk
Copy link
Member

potiuk commented Jun 28, 2021

Would you please rebase @ecerulm ? We have merged two stability improvements for CI (pylint + helm race condition). It should help a a bit :)

docs/apache-airflow/concepts/connections.rst Outdated Show resolved Hide resolved
docs/apache-airflow/macros-ref.rst Outdated Show resolved Hide resolved
@ashb
Copy link
Member

ashb commented Jun 29, 2021

{{ conn.a_connection.extra_dejson['extra__asana__workspace'] }}

Because jinja is flexible this can also be done as

{{ conn.a_connection.extra_dejson.extra__asana__workspace }}

"""
Wrapper around Connection. This way you can get connections in
templates by using ``{{ conn.conn_id }}`` or
``{{ conn.get('conn_id', 'fallback') }}``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

docs/apache-airflow/concepts/connections.rst Show resolved Hide resolved
airflow/models/taskinstance.py Show resolved Hide resolved
@ashb
Copy link
Member

ashb commented Jun 29, 2021

(None of my comments are blocking -- if you think they are useful go for it)

@ecerulm
Copy link
Contributor Author

ecerulm commented Jun 29, 2021

@potiuk the CI still fails for reason I believe unrelated to the content of this PR. I can rebase again but judging from the last PR merged https://github.com/apache/airflow/pull/15397/checks which had the same checks failing as this one, I don't think that will help.

@potiuk
Copy link
Member

potiuk commented Jun 29, 2021

Yeah. It looks like a problem with the GitHub (public) runners this time. There seem to be no relation between Python/DB or any other consistent pattern with those failures. Maybe you could apply some of the comments from @ashb and when you do - we merge it finally.

@potiuk
Copy link
Member

potiuk commented Jun 29, 2021

Something fishy going on with helm installation on public runners randomly:

  Error: failed post-install: timed out waiting for the condition

@potiuk
Copy link
Member

potiuk commented Jun 29, 2021

But unrelated. Merging.

@potiuk potiuk merged commit d3ba80a into apache:main Jun 29, 2021
@ashb ashb added this to the Airflow 2.2 milestone Jun 29, 2021
@ecerulm ecerulm deleted the jinjaconn branch June 29, 2021 13:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
full tests needed We need to run full set of tests for this PR to merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide jinja template syntax to access connections
7 participants