Skip to content

Commit f8f9640

Browse files
committed
Adding and removing systems from groups
1 parent 3502d9d commit f8f9640

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

src/mcp_server_uyuni/server.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,88 @@ async def list_group_systems(group_name: str, ctx: Context) -> List[Dict[str, An
14321432
print(f"Warning: Unexpected item format in group systems list: {system}")
14331433
return filtered_systems
14341434

1435+
@write_tool()
1436+
async def add_systems_to_group(group_name: str, system_identifiers: List[Union[str, int]], ctx: Context, confirm: Union[bool, str] = False) -> str:
1437+
"""
1438+
Adds systems to a system group.
1439+
1440+
Args:
1441+
group_name: The name of the system group.
1442+
system_identifiers: A list of system names or IDs to add to the group.
1443+
confirm: User confirmation is required to execute this action. This parameter
1444+
is `False` by default. To obtain the confirmation message that must
1445+
be presented to the user, the model must first call the tool with
1446+
`confirm=False`. If the user agrees, the model should call the tool
1447+
a second time with `confirm=True`.
1448+
1449+
Returns:
1450+
str: A success message if the systems were added.
1451+
"""
1452+
return await _manage_group_systems(group_name, system_identifiers, True, ctx, confirm)
1453+
1454+
@write_tool()
1455+
async def remove_systems_from_group(group_name: str, system_identifiers: List[Union[str, int]], ctx: Context, confirm: Union[bool, str] = False) -> str:
1456+
"""
1457+
Removes systems from a system group.
1458+
1459+
Args:
1460+
group_name: The name of the system group.
1461+
system_identifiers: A list of system names or IDs to remove from the group.
1462+
confirm: User confirmation is required to execute this action. This parameter
1463+
is `False` by default. To obtain the confirmation message that must
1464+
be presented to the user, the model must first call the tool with
1465+
`confirm=False`. If the user agrees, the model should call the tool
1466+
a second time with `confirm=True`.
1467+
1468+
Returns:
1469+
str: A success message if the systems were removed.
1470+
"""
1471+
return await _manage_group_systems(group_name, system_identifiers, False, ctx, confirm)
1472+
1473+
async def _manage_group_systems(group_name: str, system_identifiers: List[Union[str, int]], add: bool, ctx: Context, confirm: Union[bool, str] = False) -> str:
1474+
"""
1475+
Internal helper to add or remove systems from a group.
1476+
"""
1477+
action_str = "add" if add else "remove"
1478+
log_string = f"Attempting to {action_str} systems {system_identifiers} to/from group '{group_name}'"
1479+
logger.info(log_string)
1480+
await ctx.info(log_string)
1481+
1482+
is_confirmed = _to_bool(confirm)
1483+
1484+
if not is_confirmed:
1485+
return f"CONFIRMATION REQUIRED: This will {action_str} {len(system_identifiers)} systems to/from group '{group_name}'. Do you confirm?"
1486+
1487+
# Resolve all system IDs
1488+
resolved_ids = []
1489+
for identifier in system_identifiers:
1490+
sid = await _resolve_system_id(identifier)
1491+
if sid:
1492+
resolved_ids.append(int(sid))
1493+
else:
1494+
print(f"Warning: Could not resolve system identifier '{identifier}'. Skipping.")
1495+
1496+
if not resolved_ids:
1497+
return "No valid system identifiers found. Aborting."
1498+
1499+
add_remove_path = '/rhn/manager/api/systemgroup/addOrRemoveSystems'
1500+
1501+
async with httpx.AsyncClient(verify=UYUNI_MCP_SSL_VERIFY) as client:
1502+
api_result = await _call_uyuni_api(
1503+
client=client,
1504+
method="POST",
1505+
api_path=add_remove_path,
1506+
json_body={"systemGroupName": group_name, "serverIds": resolved_ids, "add": add},
1507+
error_context=f"{action_str}ing systems to/from group '{group_name}'",
1508+
default_on_error=None
1509+
)
1510+
1511+
if api_result == 1:
1512+
past_tense_action = "added" if add else "removed"
1513+
return f"Successfully {past_tense_action} {len(resolved_ids)} systems to/from group '{group_name}'."
1514+
else:
1515+
return f"Failed to {action_str} systems. Check server logs. (API Result: {api_result})"
1516+
14351517
def main_cli():
14361518

14371519
logger.info("Running Uyuni MCP server.")

0 commit comments

Comments
 (0)