tirkarthi commented on issue #26861: URL: https://github.com/apache/airflow/issues/26861#issuecomment-1271161788
Looking further into this it seems the variables set through environment variables are not returned in the list of all variables endpoints. There is also a note on this in docs that environment variables are not listed in web UI. I have a patch that restores the previous behavior along with keeping the changes from commit https://github.com/apache/airflow/commit/98f16aa7f3b577022791494e13b6aa7057afde9d . But this creates a discrepancy where the variable is present in get variables endpoint and not in the list variables endpoint. Shall I proceed with the patch or this can be closed as not a bug? https://airflow.apache.org/docs/apache-airflow/stable/howto/variable.html#storing-variables-in-environment-variables > Variables set using Environment Variables would not appear in the Airflow UI but you will be able to use them in your DAG file. Variables set using Environment Variables will also take precedence over variables defined in the Airflow UI. Sample patch with tests : ```diff diff --git a/airflow/api_connexion/endpoints/variable_endpoint.py b/airflow/api_connexion/endpoints/variable_endpoint.py index 25f8969d46..a80c31756d 100644 --- a/airflow/api_connexion/endpoints/variable_endpoint.py +++ b/airflow/api_connexion/endpoints/variable_endpoint.py @@ -46,10 +46,17 @@ def delete_variable(*, variable_key: str) -> Response: @provide_session def get_variable(*, variable_key: str, session: Session = NEW_SESSION) -> Response: """Get a variable by key""" - var = session.query(Variable).filter(Variable.key == variable_key) - if not var.count(): - raise NotFound("Variable not found") - return variable_schema.dump(var.first()) + var = session.query(Variable).filter(Variable.key == variable_key).first() + if var: + return variable_schema.dump(var) + else: + try: + val = Variable.get(variable_key) + except KeyError: + raise NotFound("Variable not found") + + variable = Variable(key=variable_key, val=val) + return variable_schema.dump(variable) @security.requires_access([(permissions.ACTION_CAN_READ, permissions.RESOURCE_VARIABLE)]) diff --git a/tests/api_connexion/endpoints/test_variable_endpoint.py b/tests/api_connexion/endpoints/test_variable_endpoint.py index 1f490fb9eb..4cc4f49d83 100644 --- a/tests/api_connexion/endpoints/test_variable_endpoint.py +++ b/tests/api_connexion/endpoints/test_variable_endpoint.py @@ -138,6 +138,14 @@ class TestGetVariable(TestVariableEndpoint): assert response.status_code == 200 assert response.json == {"key": "foo/bar", "value": expected_value, "description": None} + def test_environment_variable(self, monkeypatch): + monkeypatch.setenv("AIRFLOW_VAR_HELLO", "bar") + response = self.client.get( + "/api/v1/variables/HELLO", environ_overrides={'REMOTE_USER': "test"} + ) + assert response.status_code == 200 + assert response.json == {"key": "HELLO", "value": "bar", "description": None} + class TestGetVariables(TestVariableEndpoint): @parameterized.expand( ``` -- 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]
