This is an automated email from the ASF dual-hosted git repository.
eladkal pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 77ae1defd9 Feature: Allow description to be passed in when using
variables CLI (#34791)
77ae1defd9 is described below
commit 77ae1defd9282f7dd71a9a61cf7627162a25feb6
Author: Zhenye (Nathan) Na <[email protected]>
AuthorDate: Sun Oct 29 11:42:34 2023 -0700
Feature: Allow description to be passed in when using variables CLI (#34791)
* Feature: Allow description to be passed in when uring variables CLI
* Feature: Allow description to be passed in when uring variables CLI
* fix CLI unit tests
* Remove short option for description argument and update unit tests
* Feature: Allow description to be passed in when uring variables CLI
* Feature: Allow description to be passed in when uring variables CLI
* fix CLI unit tests
* Remove short option for description argument and update unit tests
* Feature: Allow description to be passed in when uring variables CLI
* Feature: Allow description to be passed in when uring variables CLI
* fix CLI unit tests
* Remove short option for description argument and update unit tests
* Add assertion to check variables' description as well
* Update tests/cli/commands/test_variable_command.py
Co-authored-by: Tzu-ping Chung <[email protected]>
---------
Co-authored-by: Tzu-ping Chung <[email protected]>
Co-authored-by: Elad Kalif <[email protected]>
---
airflow/cli/cli_config.py | 8 +++++++-
airflow/cli/commands/variable_command.py | 4 ++--
tests/cli/commands/test_variable_command.py | 18 ++++++++++++++++++
3 files changed, 27 insertions(+), 3 deletions(-)
diff --git a/airflow/cli/cli_config.py b/airflow/cli/cli_config.py
index f828a63ee1..43b2590324 100644
--- a/airflow/cli/cli_config.py
+++ b/airflow/cli/cli_config.py
@@ -543,6 +543,12 @@ ARG_VAR_VALUE = Arg(("value",), metavar="VALUE",
help="Variable value")
ARG_DEFAULT = Arg(
("-d", "--default"), metavar="VAL", default=None, help="Default value
returned if variable does not exist"
)
+ARG_VAR_DESCRIPTION = Arg(
+ ("--description",),
+ default=None,
+ required=False,
+ help="Variable description, optional when setting a variable",
+)
ARG_DESERIALIZE_JSON = Arg(("-j", "--json"), help="Deserialize JSON variable",
action="store_true")
ARG_SERIALIZE_JSON = Arg(("-j", "--json"), help="Serialize JSON variable",
action="store_true")
ARG_VAR_IMPORT = Arg(("file",), help="Import variables from JSON file")
@@ -1445,7 +1451,7 @@ VARIABLES_COMMANDS = (
name="set",
help="Set variable",
func=lazy_load_command("airflow.cli.commands.variable_command.variables_set"),
- args=(ARG_VAR, ARG_VAR_VALUE, ARG_SERIALIZE_JSON, ARG_VERBOSE),
+ args=(ARG_VAR, ARG_VAR_VALUE, ARG_VAR_DESCRIPTION, ARG_SERIALIZE_JSON,
ARG_VERBOSE),
),
ActionCommand(
name="delete",
diff --git a/airflow/cli/commands/variable_command.py
b/airflow/cli/commands/variable_command.py
index 2a624bd738..3dd920eb0c 100644
--- a/airflow/cli/commands/variable_command.py
+++ b/airflow/cli/commands/variable_command.py
@@ -60,8 +60,8 @@ def variables_get(args):
@cli_utils.action_cli
@providers_configuration_loaded
def variables_set(args):
- """Create new variable with a given name and value."""
- Variable.set(args.key, args.value, serialize_json=args.json)
+ """Create new variable with a given name, value and description."""
+ Variable.set(args.key, args.value, args.description,
serialize_json=args.json)
print(f"Variable {args.key} created")
diff --git a/tests/cli/commands/test_variable_command.py
b/tests/cli/commands/test_variable_command.py
index c5d77be8e6..7e0086ad95 100644
--- a/tests/cli/commands/test_variable_command.py
+++ b/tests/cli/commands/test_variable_command.py
@@ -22,11 +22,13 @@ from contextlib import redirect_stdout
from io import StringIO
import pytest
+from sqlalchemy import select
from airflow import models
from airflow.cli import cli_parser
from airflow.cli.commands import variable_command
from airflow.models import Variable
+from airflow.utils.session import create_session
from tests.test_utils.db import clear_db_variables
@@ -49,6 +51,22 @@ class TestCliVariables:
with pytest.raises(KeyError):
Variable.get("foo1")
+ def test_variables_set_with_description(self):
+ """Test variable_set command with optional description argument"""
+ expected_var_desc = "foo_bar_description"
+ var_key = "foo"
+ variable_command.variables_set(
+ self.parser.parse_args(["variables", "set", var_key, "bar",
"--description", expected_var_desc])
+ )
+
+ assert Variable.get(var_key) == "bar"
+ with create_session() as session:
+ actual_var_desc =
session.scalar(select(Variable.description).where(Variable.key == var_key))
+ assert actual_var_desc == expected_var_desc
+
+ with pytest.raises(KeyError):
+ Variable.get("foo1")
+
def test_variables_get(self):
Variable.set("foo", {"foo": "bar"}, serialize_json=True)