Improvement: Populate 'Configuration JSON' form with DAG default params json in the Trigger-DAG UI (#10839)
This commit is contained in:
Родитель
73f63671ef
Коммит
a221ccb956
|
@ -35,7 +35,8 @@ dag = DAG(
|
|||
schedule_interval='0 0 * * *',
|
||||
start_date=days_ago(2),
|
||||
dagrun_timeout=timedelta(minutes=60),
|
||||
tags=['example']
|
||||
tags=['example'],
|
||||
params={"example_key": "example_value"}
|
||||
)
|
||||
|
||||
run_this_last = DummyOperator(
|
||||
|
|
|
@ -1244,13 +1244,24 @@ class Airflow(AirflowBaseView): # noqa: D101 pylint: disable=too-many-public-m
|
|||
"""Triggers DAG Run."""
|
||||
dag_id = request.values.get('dag_id')
|
||||
origin = get_safe_url(request.values.get('origin'))
|
||||
request_conf = request.values.get('conf')
|
||||
|
||||
if request.method == 'GET':
|
||||
# Populate conf textarea with conf requests parameter, or dag.params
|
||||
default_conf = ''
|
||||
if request_conf:
|
||||
default_conf = request_conf
|
||||
else:
|
||||
try:
|
||||
dag = current_app.dag_bag.get_dag(dag_id)
|
||||
default_conf = json.dumps(dag.params, indent=4)
|
||||
except TypeError:
|
||||
flash("Could not pre-populate conf field due to non-JSON-serializable data-types")
|
||||
return self.render_template(
|
||||
'airflow/trigger.html',
|
||||
dag_id=dag_id,
|
||||
origin=origin,
|
||||
conf=''
|
||||
conf=default_conf
|
||||
)
|
||||
|
||||
dag_orm = session.query(models.DagModel).filter(models.DagModel.dag_id == dag_id).first()
|
||||
|
@ -1266,7 +1277,6 @@ class Airflow(AirflowBaseView): # noqa: D101 pylint: disable=too-many-public-m
|
|||
return redirect(origin)
|
||||
|
||||
run_conf = {}
|
||||
request_conf = request.values.get('conf')
|
||||
if request_conf:
|
||||
try:
|
||||
run_conf = json.loads(request_conf)
|
||||
|
|
|
@ -2519,6 +2519,34 @@ class TestTriggerDag(TestBase):
|
|||
expected_origin),
|
||||
resp)
|
||||
|
||||
@parameterized.expand([
|
||||
(None, {"example_key": "example_value"}),
|
||||
({"other": "test_data", "key": 12}, {"other": "test_data", "key": 12}),
|
||||
])
|
||||
def test_trigger_dag_params_conf(self, request_conf, expected_conf):
|
||||
"""
|
||||
Test that textarea in Trigger DAG UI is pre-populated
|
||||
with json config when the conf URL parameter is passed,
|
||||
or if a params dict is passed in the DAG
|
||||
|
||||
1. Conf is not included in URL parameters -> DAG.conf is in textarea
|
||||
2. Conf is passed as a URL parameter -> passed conf json is in textarea
|
||||
"""
|
||||
test_dag_id = "example_bash_operator"
|
||||
|
||||
if not request_conf:
|
||||
resp = self.client.get('trigger?dag_id={}'.format(test_dag_id))
|
||||
else:
|
||||
test_request_conf = json.dumps(request_conf, indent=4)
|
||||
resp = self.client.get('trigger?dag_id={}&conf={}'.format(test_dag_id, test_request_conf))
|
||||
|
||||
expected_dag_conf = json.dumps(expected_conf, indent=4) \
|
||||
.replace("\"", """)
|
||||
|
||||
self.check_content_in_response(
|
||||
'<textarea class="form-control" name="conf">{}</textarea>'.format(expected_dag_conf),
|
||||
resp)
|
||||
|
||||
def test_trigger_endpoint_uses_existing_dagbag(self):
|
||||
"""
|
||||
Test that Trigger Endpoint uses the DagBag already created in views.py
|
||||
|
|
Загрузка…
Ссылка в новой задаче