Skip to content

Commit

Permalink
split API into namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
sir-sigurd committed Jun 17, 2024
1 parent 97b57fb commit a03aa71
Show file tree
Hide file tree
Showing 35 changed files with 1,034 additions and 1,003 deletions.
24 changes: 12 additions & 12 deletions api/python/quilt3-admin/queries.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ fragment OperationErrorSelection on OperationError {
context
}

query getRoles {
query rolesList {
roles {
...RoleSelection
}
}

query getUser($name: String!) {
query usersGet($name: String!) {
admin {
user {
get(name: $name) {
Expand All @@ -64,7 +64,7 @@ query getUser($name: String!) {
}
}

query getUsers {
query usersList {
admin {
user {
list {
Expand All @@ -74,7 +74,7 @@ query getUsers {
}
}

mutation createUser($input: UserInput!) {
mutation usersCreate($input: UserInput!) {
admin {
user {
create(input: $input) {
Expand All @@ -84,7 +84,7 @@ mutation createUser($input: UserInput!) {
}
}

mutation deleteUser($name: String!) {
mutation usersDelete($name: String!) {
admin {
user {
mutate(name: $name) {
Expand All @@ -97,7 +97,7 @@ mutation deleteUser($name: String!) {
}
}

mutation setUserEmail($email: String!, $name: String!) {
mutation usersSetEmail($email: String!, $name: String!) {
admin {
user {
mutate(name: $name) {
Expand All @@ -109,7 +109,7 @@ mutation setUserEmail($email: String!, $name: String!) {
}
}

mutation setUserAdmin($name: String!, $admin: Boolean!) {
mutation usersSetAdmin($name: String!, $admin: Boolean!) {
admin {
user {
mutate(name: $name) {
Expand All @@ -121,7 +121,7 @@ mutation setUserAdmin($name: String!, $admin: Boolean!) {
}
}

mutation setUserActive($active: Boolean!, $name: String!) {
mutation usersSetActive($active: Boolean!, $name: String!) {
admin {
user {
mutate(name: $name) {
Expand All @@ -133,7 +133,7 @@ mutation setUserActive($active: Boolean!, $name: String!) {
}
}

mutation resetUserPassword($name: String!) {
mutation usersResetPassword($name: String!) {
admin {
user {
mutate(name: $name) {
Expand All @@ -146,7 +146,7 @@ mutation resetUserPassword($name: String!) {
}
}

mutation setRole($name: String!, $role: String!, $extraRoles: [String!], $append: Boolean!) {
mutation usersSetRole($name: String!, $role: String!, $extraRoles: [String!], $append: Boolean!) {
admin {
user {
mutate(name: $name) {
Expand All @@ -160,7 +160,7 @@ mutation setRole($name: String!, $role: String!, $extraRoles: [String!], $append
}
}

mutation addRoles($name: String!, $roles: [String!]!) {
mutation usersAddRoles($name: String!, $roles: [String!]!) {
admin {
user {
mutate(name: $name) {
Expand All @@ -174,7 +174,7 @@ mutation addRoles($name: String!, $roles: [String!]!) {
}
}

mutation removeRoles($name: String!, $roles: [String!]!, $fallback: String) {
mutation usersRemoveRoles($name: String!, $roles: [String!]!, $fallback: String) {
admin {
user {
mutate(name: $name) {
Expand Down
236 changes: 3 additions & 233 deletions api/python/quilt3/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,237 +3,7 @@
"""

# This wraps code generated by aridne-codegen to provide a more user-friendly API.
from datetime import datetime
from typing import Annotated, Any, List, Literal, Optional, Union

import pydantic

from . import _graphql_client


@pydantic.dataclasses.dataclass
class ManagedRole:
id: str
name: str
arn: str
typename__: Literal["ManagedRole"]


@pydantic.dataclasses.dataclass
class UnmanagedRole:
id: str
name: str
arn: str
typename__: Literal["UnmanagedRole"]


Role = Union[ManagedRole, UnmanagedRole]
AnnotatedRole = Annotated[Role, pydantic.Field(discriminator="typename__")]
role_adapter = pydantic.TypeAdapter(AnnotatedRole)


@pydantic.dataclasses.dataclass
class User:
name: str
email: str
date_joined: datetime
last_login: datetime
is_active: bool
is_admin: bool
is_sso_only: bool
is_service: bool
role: Optional[AnnotatedRole]
extra_roles: List[AnnotatedRole]


class Quilt3AdminError(Exception):
def __init__(self, details):
super().__init__(details)
self.details = details


class UserNotFoundError(Quilt3AdminError):
def __init__(self):
super().__init__(None)


def _handle_errors(result: _graphql_client.BaseModel) -> _graphql_client.BaseModel:
if isinstance(result, (_graphql_client.InvalidInputSelection, _graphql_client.OperationErrorSelection)):
raise Quilt3AdminError(result)
return result


def _handle_user_mutation(result: _graphql_client.BaseModel) -> User:
return User(**_handle_errors(result).model_dump())


def _get_client():
return _graphql_client.Client()


def get_roles() -> List[Role]:
"""
Get a list of all roles in the registry.
"""
return [role_adapter.validate_python(r.model_dump()) for r in _get_client().get_roles()]


def get_user(name: str) -> Optional[User]:
"""
Get a specific user from the registry. Return `None` if the user does not exist.
Args:
name: Username of user to get.
"""
result = _get_client().get_user(name=name)
if result is None:
return None
return User(**result.model_dump())


def get_users() -> List[User]:
"""
Get a list of all users in the registry.
"""
return [User(**u.model_dump()) for u in _get_client().get_users()]


def create_user(name: str, email: str, role: str, extra_roles: Optional[List[str]] = None) -> User:
"""
Create a new user in the registry.
Args:
name: Username of user to create.
email: Email of user to create.
role: Active role of the user.
extra_roles: Additional roles to assign to the user.
"""

return _handle_user_mutation(
_get_client().create_user(
input=_graphql_client.UserInput(name=name, email=email, role=role, extraRoles=extra_roles)
)
)


def delete_user(name: str) -> None:
"""
Delete user from the registry.
Args:
name: Username of user to delete.
"""
result = _get_client().delete_user(name=name)
if result is None:
raise UserNotFoundError
_handle_errors(result.delete)


def set_user_email(name: str, email: str) -> User:
"""
Set the email for a user.
Args:
name: Username of user to update.
email: Email to set for the user.
"""
result = _get_client().set_user_email(name=name, email=email)
if result is None:
raise UserNotFoundError
return _handle_user_mutation(result.set_email)


def set_user_admin(name: str, admin: bool) -> User:
"""
Set the admin status for a user.
Args:
name: Username of user to update.
admin: Admin status to set for the user.
"""
result = _get_client().set_user_admin(name=name, admin=admin)
if result is None:
raise UserNotFoundError
return _handle_user_mutation(result.set_admin)


def set_user_active(name: str, active: bool) -> User:
"""
Set the active status for a user.
Args:
name: Username of user to update.
active: Active status to set for the user.
"""
result = _get_client().set_user_active(name=name, active=active)
if result is None:
raise UserNotFoundError
return _handle_user_mutation(result.set_active)


def reset_user_password(name: str) -> None:
"""
Reset the password for a user.
Args:
name: Username of user to update.
"""
result = _get_client().reset_user_password(name=name)
if result is None:
raise UserNotFoundError
_handle_errors(result.reset_password)


def set_role(
name: str,
role: str,
extra_roles: Optional[List[str]] = None,
*,
append: bool = False,
) -> User:
"""
Set the active and extra roles for a user.
Args:
name: Username of user to update.
role: Role to be set as the active role.
extra_roles: Additional roles to assign to the user.
append: If True, append the extra roles to the existing roles. If False, replace the existing roles.
"""
result = _get_client().set_role(name=name, role=role, extra_roles=extra_roles, append=append)
if result is None:
raise UserNotFoundError
return _handle_user_mutation(result.set_role)


def add_roles(name: str, roles: List[str]) -> User:
"""
Add roles to a user.
Args:
name: Username of user to update.
roles: Roles to add to the user.
"""
result = _get_client().add_roles(name=name, roles=roles)
if result is None:
raise UserNotFoundError
return _handle_user_mutation(result.add_roles)


def remove_roles(
name: str,
roles: List[str],
fallback: Optional[str] = None,
) -> User:
"""
Remove roles from a user.
Args:
name: Username of user to update.
roles: Roles to remove from the user.
fallback: If set, the role to assign to the user if the active role is removed.
"""
result = _get_client().remove_roles(name=name, roles=roles, fallback=fallback)
if result is None:
raise UserNotFoundError
return _handle_user_mutation(result.remove_roles)
from . import roles, users
from .exceptions import Quilt3AdminError, UserNotFoundError
from .types import ManagedRole, UnmanagedRole, User
Loading

0 comments on commit a03aa71

Please sign in to comment.