Skip to content

Commit

Permalink
more user api
Browse files Browse the repository at this point in the history
  • Loading branch information
sir-sigurd committed Jun 14, 2024
1 parent 11a4fe0 commit 5c4dcae
Show file tree
Hide file tree
Showing 13 changed files with 919 additions and 28 deletions.
64 changes: 58 additions & 6 deletions api/python/quilt3-admin/queries.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ fragment UserSelection on User {
...RoleSelection
}
}
fragment UserMutationSelection on UserResult {
...UserSelection
...InvalidInputSelection
...OperationErrorSelection
}
fragment InvalidInputSelection on InvalidInput {
errors {
path
Expand All @@ -43,6 +48,12 @@ fragment OperationErrorSelection on OperationError {
context
}

query getRoles {
roles {
...RoleSelection
}
}

query getUser($name: String!) {
admin {
user {
Expand All @@ -67,9 +78,7 @@ mutation createUser($input: UserInput!) {
admin {
user {
create(input: $input) {
...UserSelection
...InvalidInputSelection
...OperationErrorSelection
...UserMutationSelection
}
}
}
Expand All @@ -88,9 +97,52 @@ mutation deleteUser($name: String!) {
}
}

query getRoles {
roles {
...RoleSelection
mutation setUserEmail($email: String!, $name: String!) {
admin {
user {
mutate(name: $name) {
setEmail(email: $email) {
...UserMutationSelection
}
}
}
}
}

mutation setUserAdmin($name: String!, $admin: Boolean!) {
admin {
user {
mutate(name: $name) {
setAdmin(admin: $admin) {
...UserMutationSelection
}
}
}
}
}

mutation setUserActive($active: Boolean!, $name: String!) {
admin {
user {
mutate(name: $name) {
setActive(active: $active) {
...UserMutationSelection
}
}
}
}
}

mutation resetUserPassword($name: String!) {
admin {
user {
mutate(name: $name) {
resetPassword {
...InvalidInputSelection
...OperationErrorSelection
}
}
}
}
}

Expand Down
77 changes: 68 additions & 9 deletions api/python/quilt3/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,27 @@ def __init__(self):
super().__init__(None)


def _handle_errors(result: _graphql_client.BaseModel) -> Any:
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.
Expand Down Expand Up @@ -98,11 +109,11 @@ def create_user(name: str, email: str, role: str, extra_roles: Optional[List[str
extra_roles: Additional roles to assign to the user.
"""

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


def delete_user(name: str) -> None:
Expand All @@ -118,11 +129,59 @@ def delete_user(name: str) -> None:
_handle_errors(result.delete)


def get_roles() -> List[Role]:
def set_user_email(name: str, email: str) -> User:
"""
Get a list of all roles in the registry.
Set the email for a user.
Args:
name: Username of user to update.
email: Email to set for the user.
"""
return [role_adapter.validate_python(r.model_dump()) for r in _get_client().get_roles()]
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(
Expand All @@ -144,7 +203,7 @@ def set_role(
result = _get_client().set_role(name=name, role=role, extra_roles=extra_roles, append=append)
if result is None:
raise UserNotFoundError
return User(**_handle_errors(result.set_role).model_dump())
return _handle_user_mutation(result.set_role)


def add_roles(name: str, roles: List[str]) -> User:
Expand All @@ -158,7 +217,7 @@ def add_roles(name: str, roles: List[str]) -> User:
result = _get_client().add_roles(name=name, roles=roles)
if result is None:
raise UserNotFoundError
return User(**_handle_errors(result.add_roles).model_dump())
return _handle_user_mutation(result.add_roles)


def remove_roles(
Expand All @@ -177,4 +236,4 @@ def remove_roles(
result = _get_client().remove_roles(name=name, roles=roles, fallback=fallback)
if result is None:
raise UserNotFoundError
return User(**_handle_errors(result.remove_roles).model_dump())
return _handle_user_mutation(result.remove_roles)
64 changes: 64 additions & 0 deletions api/python/quilt3/admin/_graphql_client/__init__.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5c4dcae

Please sign in to comment.