-
Notifications
You must be signed in to change notification settings - Fork 175
feat(BA-5769): add my-role SDK, CLI, and v2 component tests #11179
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
63c4f73
feat(BA-5769): add my-role SDK, CLI, and v2 component tests
fregataa 48ab2fa
changelog: add news fragment for PR #11179
fregataa fa434ae
feat(BA-5769): add my-role SDK, CLI, and v2 component tests
fregataa 97a7030
fix(BA-5769): apply PR review feedback — UUIDFilter, click.UUID, adap…
fregataa c15d2cf
chore: update api schema dump
fregataa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add my-role REST v2 SDK endpoint and CLI command for users to query their own role assignments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """CLI commands for self-service role operations.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from uuid import UUID | ||
|
|
||
| import click | ||
|
|
||
| from ai.backend.client.cli.v2.helpers import ( | ||
| create_v2_registry, | ||
| load_v2_config, | ||
| parse_order_options, | ||
| print_result, | ||
| ) | ||
|
|
||
|
|
||
| @click.group() | ||
| def role() -> None: | ||
| """My role commands.""" | ||
|
|
||
|
|
||
| @role.command() | ||
| @click.option("--limit", default=None, type=int, help="Maximum number of results to return.") | ||
| @click.option("--offset", default=None, type=int, help="Number of results to skip.") | ||
| @click.option("--first", default=None, type=int, help="Cursor-based: return first N items.") | ||
| @click.option("--after", default=None, type=str, help="Cursor-based: return items after cursor.") | ||
| @click.option("--last", default=None, type=int, help="Cursor-based: return last N items.") | ||
| @click.option("--before", default=None, type=str, help="Cursor-based: return items before cursor.") | ||
| @click.option("--role-id", type=click.UUID, default=None, help="Filter by role UUID.") | ||
| @click.option( | ||
| "--order-by", | ||
| multiple=True, | ||
| help="Order by field:direction (e.g., granted_at:desc).", | ||
| ) | ||
| def search( | ||
| limit: int | None, | ||
| offset: int | None, | ||
| first: int | None, | ||
| after: str | None, | ||
| last: int | None, | ||
| before: str | None, | ||
| role_id: UUID | None, | ||
| order_by: tuple[str, ...], | ||
| ) -> None: | ||
| """Search my role assignments.""" | ||
| from ai.backend.common.dto.manager.query import UUIDFilter | ||
| from ai.backend.common.dto.manager.v2.rbac.request import ( | ||
| RoleAssignmentFilter, | ||
| RoleAssignmentOrderBy, | ||
| SearchRoleAssignmentsInput, | ||
| ) | ||
| from ai.backend.common.dto.manager.v2.rbac.types import RoleAssignmentOrderField | ||
|
|
||
| filter_dto: RoleAssignmentFilter | None = None | ||
| if role_id is not None: | ||
| filter_dto = RoleAssignmentFilter( | ||
| role_id=UUIDFilter(equals=role_id), | ||
| ) | ||
|
|
||
| orders = ( | ||
| parse_order_options(order_by, RoleAssignmentOrderField, RoleAssignmentOrderBy) | ||
| if order_by | ||
| else None | ||
| ) | ||
|
|
||
| async def _run() -> None: | ||
| registry = await create_v2_registry(load_v2_config()) | ||
| try: | ||
| request = SearchRoleAssignmentsInput( | ||
| filter=filter_dto, | ||
| order=orders, | ||
| first=first, | ||
| after=after, | ||
| last=last, | ||
| before=before, | ||
| limit=limit, | ||
| offset=offset, | ||
| ) | ||
| result = await registry.rbac.my_search_assignments(request) | ||
| print_result(result) | ||
| finally: | ||
| await registry.close() | ||
|
|
||
| asyncio.run(_run()) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| from uuid import UUID | ||
|
|
||
| from ai.backend.common.api_handlers import SENTINEL | ||
| from ai.backend.common.contexts.user import current_user | ||
| from ai.backend.common.data.filter_specs import StringMatchSpec | ||
| from ai.backend.common.data.permission.types import OperationType as InternalOperationType | ||
| from ai.backend.common.data.permission.types import RBACElementType | ||
|
|
@@ -51,7 +52,7 @@ | |
| from ai.backend.common.dto.manager.v2.rbac.request import ( | ||
| AdminSearchEntitiesGQLInput, | ||
| AdminSearchPermissionsGQLInput, | ||
| AdminSearchRoleAssignmentsGQLInput, | ||
| SearchRoleAssignmentsInput, | ||
| SearchRolesInput, | ||
| ) | ||
| from ai.backend.common.dto.manager.v2.rbac.request import ( | ||
|
|
@@ -111,6 +112,7 @@ | |
| from ai.backend.common.dto.manager.v2.rbac.types import ( | ||
| OrderDirection as OrderDirectionV2, | ||
| ) | ||
| from ai.backend.common.exception import UnreachableError | ||
| from ai.backend.manager.actions.action import build_operation_description | ||
| from ai.backend.manager.api.adapters.pagination import PaginationSpec | ||
| from ai.backend.manager.data.common.types import SearchResult | ||
|
|
@@ -680,12 +682,33 @@ async def search_roles_in_scope( | |
| has_previous_page=raw.has_previous_page, | ||
| ) | ||
|
|
||
| async def admin_search_role_assignments_gql( | ||
| async def my_search_role_assignments( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I think |
||
| self, | ||
| input: AdminSearchRoleAssignmentsGQLInput, | ||
| input: SearchRoleAssignmentsInput, | ||
| ) -> SearchResult[RoleAssignmentNode]: | ||
| """Search role assignments for the current authenticated user.""" | ||
| me = current_user() | ||
| if me is None: | ||
| raise UnreachableError("User context is not available") | ||
| return await self._search_role_assignments( | ||
| input, | ||
| base_conditions=[AssignedUserConditions.by_user_id(me.user_id)], | ||
| ) | ||
|
|
||
| async def admin_search_role_assignments( | ||
|
fregataa marked this conversation as resolved.
|
||
| self, | ||
| input: SearchRoleAssignmentsInput, | ||
| base_conditions: Sequence[QueryCondition] | None = None, | ||
| ) -> SearchResult[RoleAssignmentNode]: | ||
| """Search role assignments with cursor/offset pagination.""" | ||
| """Search role assignments with cursor/offset pagination (admin).""" | ||
| return await self._search_role_assignments(input, base_conditions=base_conditions) | ||
|
|
||
| async def _search_role_assignments( | ||
| self, | ||
| input: SearchRoleAssignmentsInput, | ||
| base_conditions: Sequence[QueryCondition] | None = None, | ||
| ) -> SearchResult[RoleAssignmentNode]: | ||
| """Internal implementation for searching role assignments.""" | ||
| conditions = self._convert_assignment_filter(input.filter) if input.filter else [] | ||
| orders = self._convert_assignment_orders(input.order) if input.order else [] | ||
| querier = self._build_querier( | ||
|
|
@@ -1242,7 +1265,13 @@ def _convert_permission_nested_filter( | |
| def _convert_assignment_filter(self, f: RoleAssignmentFilterDTO) -> list[QueryCondition]: | ||
| conditions: list[QueryCondition] = [] | ||
| if f.role_id is not None: | ||
| conditions.append(AssignedUserConditions.by_role_id(f.role_id)) | ||
| condition = self.convert_uuid_filter( | ||
| f.role_id, | ||
| equals_factory=AssignedUserConditions.by_role_id_equals, | ||
| in_factory=AssignedUserConditions.by_role_id_in, | ||
| ) | ||
| if condition is not None: | ||
| conditions.append(condition) | ||
| if f.role is not None: | ||
| conditions.extend(self._convert_role_nested_filter(f.role)) | ||
| if f.permission is not None: | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.