Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions airflow-core/newsfragments/72036.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add an ``airflow teams inspect`` CLI command for displaying resources associated with a team, including Dag bundles, pools, connections, and variables.
7 changes: 7 additions & 0 deletions airflow-core/src/airflow/cli/cli_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,13 @@ class GroupCommand(NamedTuple):
func=lazy_load_command("airflow.cli.commands.team_command.team_verify"),
args=(ARG_VERBOSE,),
),
ActionCommand(
name="inspect",
help="Inspect a team",
description="Display resources associated with a team.\n",
func=lazy_load_command("airflow.cli.commands.team_command.team_inspect"),
args=(ARG_TEAM_NAME, ARG_OUTPUT, ARG_VERBOSE),
),
)
STATE_STORE_COMMANDS = (
ActionCommand(
Expand Down
47 changes: 47 additions & 0 deletions airflow-core/src/airflow/cli/commands/team_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,50 @@ def team_verify(args, *, session=NEW_SESSION):
raise SystemExit(1)

print("Verification succeeded.")


@cli_utils.action_cli
@providers_configuration_loaded
@provide_session
def team_inspect(args, *, session=NEW_SESSION):
"""Inspect resources belonging to a team."""
if not conf.getboolean("core", "multi_team"):
print("Multi-team is not enabled.")
return

team_name = _extract_team_name(args)
Comment thread
SameerMesiah97 marked this conversation as resolved.
Resolved

team = session.scalar(select(Team).where(Team.name == team_name))
if team is None:
raise SystemExit(f"Team '{team_name}' does not exist")

bundle_names = session.scalars(
select(dag_bundle_team_association_table.c.dag_bundle_name)
.where(dag_bundle_team_association_table.c.team_name == team_name)
.order_by(dag_bundle_team_association_table.c.dag_bundle_name)
).all()

pool_names = session.scalars(
select(Pool.pool).where(Pool.team_name == team_name).order_by(Pool.pool)
).all()

connection_ids = session.scalars(
select(Connection.conn_id).where(Connection.team_name == team_name).order_by(Connection.conn_id)
).all()

variable_keys = session.scalars(
select(Variable.key).where(Variable.team_name == team_name).order_by(Variable.key)
).all()

AirflowConsole().print_as(
data=[
{
"name": team.name,
"dag_bundles": bundle_names,
"pools": pool_names,
"connections": connection_ids,
"variables": variable_keys,
}
],
output=args.output,
)
123 changes: 123 additions & 0 deletions airflow-core/tests/unit/cli/commands/test_team_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,3 +628,126 @@ def test_team_verify_unknown_bundle_team(self, stdout_capture):
team_command.team_verify(self.parser.parse_args(["teams", "verify"]))

assert "references unknown team 'missing-team'" in stdout.getvalue()

def test_team_inspect(self, stdout_capture):
Comment thread
SameerMesiah97 marked this conversation as resolved.
Resolved
"""Test inspecting a team with associated resources."""
self.session.add_all([Team(name="team1"), Team(name="team2")])
self.session.commit()

self.session.add_all(
[
DagBundleModel(name="bundle1"),
DagBundleModel(name="bundle2"),
]
)
self.session.commit()

self.session.execute(
dag_bundle_team_association_table.insert(),
[
{"dag_bundle_name": "bundle1", "team_name": "team1"},
{"dag_bundle_name": "bundle2", "team_name": "team2"},
],
)

self.session.add_all(
[
Pool(
pool=Pool.get_default_team_pool_name("team1"),
slots=128,
description="Default pool",
include_deferred=False,
team_name="team1",
),
Connection(conn_id="conn1", conn_type="http", team_name="team1"),
Variable(key="var1", val="value", team_name="team1"),
Pool(
pool="pool1",
slots=5,
description="Additional pool",
include_deferred=False,
team_name="team1",
),
Pool(
pool=Pool.get_default_team_pool_name("team2"),
slots=128,
description="Default pool",
include_deferred=False,
team_name="team2",
),
Connection(conn_id="conn2", conn_type="http", team_name="team2"),
Variable(key="var2", val="value", team_name="team2"),
Pool(
pool="pool2",
slots=5,
description="Additional pool",
include_deferred=False,
team_name="team2",
),
]
)
self.session.commit()

with conf_vars({("core", "multi_team"): "True"}):
with stdout_capture as stdout:
team_command.team_inspect(
self.parser.parse_args(["teams", "inspect", "team1", "--output", "json"])
)

assert json.loads(stdout.getvalue()) == [
{
"name": "team1",
"dag_bundles": ["bundle1"],
"pools": [
Pool.get_default_team_pool_name("team1"),
"pool1",
],
"connections": ["conn1"],
"variables": ["var1"],
}
]

def test_team_inspect_empty_team(self, stdout_capture):
"""Test inspecting a team with no associated resources."""
self.session.add(Team(name="team1"))
self.session.commit()

self.session.add(
Pool(
pool=Pool.get_default_team_pool_name("team1"),
slots=128,
description="Default pool",
include_deferred=False,
team_name="team1",
)
)

self.session.commit()
with conf_vars({("core", "multi_team"): "True"}):
with stdout_capture as stdout:
team_command.team_inspect(
self.parser.parse_args(["teams", "inspect", "team1", "--output", "json"])
)

assert json.loads(stdout.getvalue()) == [
{
"name": "team1",
"dag_bundles": [],
"pools": [Pool.get_default_team_pool_name("team1")],
"connections": [],
"variables": [],
}
]

def test_team_inspect_nonexistent_team(self):
"""Test inspecting a team that does not exist.""" #
with conf_vars({("core", "multi_team"): "True"}):
with pytest.raises(SystemExit, match="Team 'team1' does not exist"):
team_command.team_inspect(self.parser.parse_args(["teams", "inspect", "team1"]))

def test_team_inspect_multi_team_disabled(self, stdout_capture):
with conf_vars({("core", "multi_team"): "False"}):
with stdout_capture as stdout:
team_command.team_inspect(self.parser.parse_args(["teams", "inspect", "team1"]))

assert "Multi-team is not enabled." in stdout.getvalue()