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 8b737ab
Show file tree
Hide file tree
Showing 5 changed files with 536 additions and 23 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
67 changes: 63 additions & 4 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 @@ -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 Down
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 8b737ab

Please sign in to comment.