Skip to content

Commit

Permalink
fix: Allow regular users to assign agent manually when creating sessi…
Browse files Browse the repository at this point in the history
…ons (#2614)

Co-authored-by: Sanghun Lee <sanghun@lablup.com>
  • Loading branch information
lizable and fregataa authored Nov 22, 2024
1 parent fc6e44b commit ea9ea02
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
1 change: 1 addition & 0 deletions changes/2614.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow regular users to assign agent manually if `hide-agent` configuration is disabled
1 change: 0 additions & 1 deletion src/ai/backend/client/cli/session/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,6 @@ def prepare_mount_arg(
type=list_expr,
help=(
"Show mapping list of tuple which mapped containers with agent. "
"When user role is Super Admin. "
"(e.g., --assign-agent agent_id_1,agent_id_2,...)"
),
)
Expand Down
1 change: 0 additions & 1 deletion src/ai/backend/client/cli/session/lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def _create_cmd(docs: Optional[str] = None):
type=list_expr,
help=(
"Assign the session to specific agents. "
"This option is only applicable when the user role is Super Admin. "
"(e.g., --assign-agent agent_id_1,agent_id_2,...)"
),
)
Expand Down
49 changes: 26 additions & 23 deletions src/ai/backend/manager/api/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
List,
Mapping,
MutableMapping,
Optional,
Set,
Tuple,
Union,
Expand Down Expand Up @@ -654,30 +655,32 @@ async def create_from_params(request: web.Request, params: dict[str, Any]) -> we
else:
raise InvalidAPIParameters("API version not supported")
params["config"] = creation_config
if params["config"]["agent_list"] is not None and request["user"]["role"] != (
UserRole.SUPERADMIN
):
raise InsufficientPrivilege(
"You are not allowed to manually assign agents for your session."
)
if request["user"]["role"] == (UserRole.SUPERADMIN):
if not params["config"]["agent_list"]:
pass

root_ctx: RootContext = request.app["_root.context"]

agent_list = cast(Optional[list[str]], params["config"]["agent_list"])
if agent_list is not None:
if (
request["user"]["role"] != UserRole.SUPERADMIN
and root_ctx.local_config["manager"]["hide-agents"]
):
raise InsufficientPrivilege(
"You are not allowed to manually assign agents for your session."
)
agent_count = len(agent_list)
if params["cluster_mode"] == "multi-node":
if agent_count != params["cluster_size"]:
raise InvalidAPIParameters(
"For multi-node cluster sessions, the number of manually assigned"
" agents must be same to the cluster size. Note that you may specify"
" duplicate agents in the list.",
)
else:
agent_count = len(params["config"]["agent_list"])
if params["cluster_mode"] == "multi-node":
if agent_count != params["cluster_size"]:
raise InvalidAPIParameters(
"For multi-node cluster sessions, the number of manually assigned"
" agents must be same to the cluster size. Note that you may specify"
" duplicate agents in the list.",
)
else:
if agent_count != 1:
raise InvalidAPIParameters(
"For non-cluster sessions and single-node cluster sessions, "
"you may specify only one manually assigned agent.",
)
if agent_count != 1:
raise InvalidAPIParameters(
"For non-cluster sessions and single-node cluster sessions, "
"you may specify only one manually assigned agent.",
)
return await _create(request, params)


Expand Down

0 comments on commit ea9ea02

Please sign in to comment.