Skip to content

Commit

Permalink
Feature: Allow description to be passed in when using variables CLI (#…
Browse files Browse the repository at this point in the history
…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 <uranusjr@gmail.com>

---------

Co-authored-by: Tzu-ping Chung <uranusjr@gmail.com>
Co-authored-by: Elad Kalif <45845474+eladkal@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 29, 2023
1 parent c70f298 commit 77ae1de
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
8 changes: 7 additions & 1 deletion airflow/cli/cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,12 @@ def string_lower_type(val):
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")
Expand Down Expand Up @@ -1445,7 +1451,7 @@ class GroupCommand(NamedTuple):
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",
Expand Down
4 changes: 2 additions & 2 deletions airflow/cli/commands/variable_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand Down
18 changes: 18 additions & 0 deletions tests/cli/commands/test_variable_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
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


Expand All @@ -49,6 +51,22 @@ def test_variables_set(self):
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)

Expand Down

0 comments on commit 77ae1de

Please sign in to comment.