Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add users_to_add and users_to_remove fields to replace user update mode #1705

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
28bf838
revert functioned description and deprecated msg
fregataa Nov 8, 2023
55d74b8
impl user add/delete group mutation
fregataa Nov 8, 2023
b0cd2a4
replace input type of ID from UUID to String
fregataa Nov 8, 2023
e1528e8
keep the modify function alive
fregataa Nov 8, 2023
4f19cbc
change mutation name typo
fregataa Nov 8, 2023
00a9b3e
Merge branch 'main' into fix/refactor-group-graphql
fregataa Nov 14, 2023
175a5fd
Merge branch 'main' into fix/refactor-group-graphql
fregataa Nov 14, 2023
c75db1e
impl user add/remove in ModifyGroup mutation and revert description m…
fregataa Nov 14, 2023
fb68733
Merge branch 'main' into fix/refactor-group-graphql
fregataa Nov 23, 2023
b0c85d3
add news fragment
fregataa Nov 24, 2023
0760819
change added versions of some fields
fregataa Nov 24, 2023
fed0bfe
update news fragment
fregataa Nov 24, 2023
b446de3
Merge branch 'main' into fix/refactor-group-graphql
fregataa Jan 3, 2024
b5c76cc
fix wrong code and change naming
fregataa Jan 3, 2024
84a8f13
Merge branch 'main' into fix/refactor-group-graphql
fregataa Apr 18, 2024
7d8c88f
update version notation
fregataa Apr 18, 2024
20b3787
remove use of deprecated fields
fregataa Apr 18, 2024
c6f9116
little change of error msg
fregataa Apr 18, 2024
999736b
chore: update GraphQL schema dump
fregataa Apr 18, 2024
f62c10e
Merge branch 'main' into fix/refactor-group-graphql
fregataa Apr 23, 2024
a4f5e04
Merge remote-tracking branch 'origin/fix/refactor-group-graphql' into…
fregataa Apr 23, 2024
278d878
update gql fields description
fregataa Apr 23, 2024
0820ce5
chore: update GraphQL schema dump
fregataa Apr 23, 2024
81c8cdb
Merge branch 'main' into fix/refactor-group-graphql
fregataa May 1, 2024
f4ee3bb
Merge remote-tracking branch 'origin/fix/refactor-group-graphql' into…
fregataa May 1, 2024
f39454a
Merge branch 'main' into fix/refactor-group-graphql
fregataa May 8, 2024
4352885
rename fields
fregataa May 8, 2024
a9c05ed
chore: update GraphQL schema dump
fregataa May 8, 2024
4a17f83
Merge branch 'main' into fix/refactor-group-graphql
fregataa May 9, 2024
66a6880
minor update for pythonic code
fregataa May 9, 2024
62c65a7
Merge remote-tracking branch 'origin/fix/refactor-group-graphql' into…
fregataa May 9, 2024
9e068fd
update news fragment
fregataa May 10, 2024
5621850
handle duplicate user associating to group
fregataa May 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1705.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor group mutation by adding `add_users` and `remove_users` fields to replace `user_update_mode`.
agatha197 marked this conversation as resolved.
Show resolved Hide resolved
10 changes: 8 additions & 2 deletions src/ai/backend/manager/api/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1139,8 +1139,14 @@
is_active: Boolean
domain_name: String
total_resource_slots: JSONString
user_update_mode: String
user_uuids: [String]
user_update_mode: String @deprecated(reason: "Deprecated since 24.09.0. Use `add_users` and `remove_users` fields")
user_uuids: [String] @deprecated(reason: "Deprecated since 24.09.0. Use `add_users` and `remove_users` fields")

"""Added in 24.09.0. ID array of the users to be added to the group."""
add_users: [String]

Check warning on line 1146 in src/ai/backend/manager/api/schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Input field 'add_users' was added to input object type 'ModifyGroupInput'

Input field 'add_users' was added to input object type 'ModifyGroupInput'

"""Added in 24.09.0. ID array of the users to be removed from the group."""
remove_users: [String]

Check warning on line 1149 in src/ai/backend/manager/api/schema.graphql

View workflow job for this annotation

GitHub Actions / GraphQL Inspector

Input field 'remove_users' was added to input object type 'ModifyGroupInput'

Input field 'remove_users' was added to input object type 'ModifyGroupInput'
allowed_vfolder_hosts: JSONString
integration_id: String
resource_policy: String
Expand Down
68 changes: 45 additions & 23 deletions src/ai/backend/manager/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import sqlalchemy as sa
import trafaret as t
from graphene.types.datetime import DateTime as GQLDateTime
from graphql import Undefined
from sqlalchemy.engine.row import Row
from sqlalchemy.ext.asyncio import AsyncConnection as SAConnection
from sqlalchemy.orm import relationship
Expand Down Expand Up @@ -61,7 +60,7 @@
from .minilang.ordering import QueryOrderParser
from .minilang.queryfilter import QueryFilterParser
from .storage import StorageSessionManager
from .user import ModifyUserInput, UserConnection, UserNode, UserRole
from .user import UserConnection, UserNode, UserRole
from .utils import ExtendedAsyncSAEngine, execute_with_retry

if TYPE_CHECKING:
Expand Down Expand Up @@ -451,8 +450,23 @@ class ModifyGroupInput(graphene.InputObjectType):
is_active = graphene.Boolean(required=False)
domain_name = graphene.String(required=False)
total_resource_slots = graphene.JSONString(required=False)
user_update_mode = graphene.String(required=False)
user_uuids = graphene.List(lambda: graphene.String, required=False)
user_update_mode = graphene.String(
deprecation_reason=("Deprecated since 24.09.0. Use `add_users` and `remove_users` fields")
)
user_uuids = graphene.List(
lambda: graphene.String,
deprecation_reason=("Deprecated since 24.09.0. Use `add_users` and `remove_users` fields"),
)
add_users = graphene.List(
lambda: graphene.String,
required=False,
description="Added in 24.09.0. ID array of the users to be added to the group.",
)
remove_users = graphene.List(
lambda: graphene.String,
required=False,
description="Added in 24.09.0. ID array of the users to be removed from the group.",
)
fregataa marked this conversation as resolved.
Show resolved Hide resolved
allowed_vfolder_hosts = graphene.JSONString(required=False)
integration_id = graphene.String(required=False)
resource_policy = graphene.String(required=False)
Expand Down Expand Up @@ -530,10 +544,11 @@ async def mutate(
root,
info: graphene.ResolveInfo,
gid: uuid.UUID,
props: ModifyUserInput,
props: ModifyGroupInput,
) -> ModifyGroup:
graph_ctx: GraphQueryContext = info.context
data: Dict[str, Any] = {}
user_data: dict[str, set] = {}
set_if_set(props, data, "name")
set_if_set(props, data, "description")
set_if_set(props, data, "is_active")
Expand All @@ -549,33 +564,40 @@ async def mutate(
set_if_set(props, data, "resource_policy")
set_if_set(props, data, "container_registry")

set_if_set(props, user_data, "add_users", clean_func=set)
set_if_set(props, user_data, "remove_users", clean_func=set)

if "name" in data and _rx_slug.search(data["name"]) is None:
raise ValueError("invalid name format. slug format required.")
if props.user_update_mode not in (None, Undefined, "add", "remove"):
raise ValueError("invalid user_update_mode")
if not props.user_uuids:
props.user_update_mode = None
if not data and props.user_update_mode is None:
if not data and not user_data:
return cls(ok=False, msg="nothing to update", group=None)

async def _do_mutate() -> ModifyGroup:
async with graph_ctx.db.begin() as conn:
# TODO: refactor user addition/removal in groups as separate mutations
# (to apply since 21.09)
if props.user_update_mode == "add":
values = [{"user_id": uuid, "group_id": gid} for uuid in props.user_uuids]
await conn.execute(
async with graph_ctx.db.begin_session() as db_session:
add_users = user_data.get("add_users") or set()
remove_users = user_data.get("remove_users") or set()
fregataa marked this conversation as resolved.
Show resolved Hide resolved
if union := (add_users & remove_users):
raise ValueError(
"Should be no user IDs included in both `add_users` and `remove_users`."
f" (IDs: {list(union)})"
)
if add_users:
values = [{"user_id": uuid, "group_id": gid} for uuid in add_users]
await db_session.execute(
sa.insert(association_groups_users).values(values),
)
elif props.user_update_mode == "remove":
await conn.execute(
sa.delete(association_groups_users).where(
(association_groups_users.c.user_id.in_(props.user_uuids))
& (association_groups_users.c.group_id == gid),
),
if remove_users:
await db_session.execute(
(
sa.delete(association_groups_users).where(
(association_groups_users.c.group_id == gid)
& (association_groups_users.c.user_id.in_(remove_users))
)
)
)

if data:
result = await conn.execute(
result = await db_session.execute(
sa.update(groups).values(data).where(groups.c.id == gid).returning(groups),
)
if result.rowcount > 0:
Expand Down