Skip to content

Commit

Permalink
more changes:
Browse files Browse the repository at this point in the history
* move admin to ql
* remove get_role()
* add delete_user()
* some docstrings
  • Loading branch information
sir-sigurd committed Jun 7, 2024
1 parent 88872ff commit edd2c19
Show file tree
Hide file tree
Showing 22 changed files with 235 additions and 59 deletions.
2 changes: 1 addition & 1 deletion api/python/quilt3-admin/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.ariadne-codegen]
schema_path = "../../../shared/graphql/schema.graphql"
queries_path = "queries.graphql"
target_package_path = "../quilt3/adminql/"
target_package_path = "../quilt3/admin/"
target_package_name = "_graphql_client"
files_to_include = [
"exceptions.py",
Expand Down
18 changes: 13 additions & 5 deletions api/python/quilt3-admin/queries.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,16 @@ mutation createUser($input: UserInput!) {
}
}

query getRole($roleId: ID!) {
role(id: $roleId) {
...RoleSelection
mutation deleteUser($name: String!) {
admin {
user {
mutate(name: $name) {
delete {
...InvalidInputSelection
...OperationErrorSelection
}
}
}
}
}

Expand All @@ -99,7 +106,7 @@ mutation setRole($name: String!, $role: String!, $extraRoles: [String!], $append
}
}

mutation addRoles($roles: [String!]!, $name: String!) {
mutation addRoles($name: String!, $roles: [String!]!) {
admin {
user {
mutate(name: $name) {
Expand All @@ -111,7 +118,8 @@ mutation addRoles($roles: [String!]!, $name: String!) {
}
}
}
mutation removeRoles($roles: [String!]!, $name: String!, $fallback: String) {

mutation removeRoles($name: String!, $roles: [String!]!, $fallback: String) {
admin {
user {
mutate(name: $name) {
Expand Down
2 changes: 1 addition & 1 deletion api/python/quilt3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

__version__ = Path(Path(__file__).parent, "VERSION").read_text().strip()

from . import admin, adminql
from . import admin
from .api import (
config,
copy,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""APIs for Quilt administrators. 'Registry' refers to Quilt stack backend services, including identity management."""

from typing import Any, Union, Optional, List

from ._graphql_client import *
Expand All @@ -20,6 +22,12 @@ def _get_client():


def get_user(name: str) -> GetUserAdminUserGet:
"""
Get a specific user from the registry.
Args:
name: Username of user to get.
"""
result = _get_client().get_user(name=name)
# XXX: should we really throw an exception here?
if result is None:
Expand All @@ -28,22 +36,47 @@ def get_user(name: str) -> GetUserAdminUserGet:


def get_users() -> List[GetUsersAdminUserList]:
"""
Get a list of all users in the registry.
"""
return _get_client().get_users()


def create_user(name: str, email: str, role: str, extra_roles: Optional[List[str]] = None) -> None:
"""
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.
"""

_handle_errors(
_get_client().create_user(input=UserInput(name=name, email=email, role=role, extra_roles=extra_roles))
)
return None


def get_role(role_id: str) -> Optional[Union[GetRoleRoleUnmanagedRole, GetRoleRoleManagedRole]]:
# XXX: should we throw an exception here if the role is not found?
return _get_client().get_role(role_id=role_id)
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)
return None


def get_roles() -> List[Union[GetRolesRolesUnmanagedRole, GetRolesRolesManagedRole]]:
"""
Get a list of all roles in the registry.
"""
return _get_client().get_roles()


Expand All @@ -54,27 +87,51 @@ def set_role(
*,
append: bool = False,
) -> None:
"""
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
_handle_errors(result.set_role)
return None


def add_roles(roles: List[str], name: str) -> None:
result = _get_client().add_roles(roles=roles, name=name)
def add_roles(name: str, roles: List[str]) -> None:
"""
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
_handle_errors(result.add_roles)
return None


def remove_roles(
roles: List[str],
name: str,
roles: List[str],
fallback: Union[Optional[str], UnsetType] = UNSET,
) -> None:
result = _get_client().remove_roles(roles=roles, name=name, fallback=fallback)
"""
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
_handle_errors(result.remove_roles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@
CreateUserAdminUserCreateOperationError,
CreateUserAdminUserCreateUser,
)
from .delete_user import (
DeleteUser,
DeleteUserAdmin,
DeleteUserAdminUser,
DeleteUserAdminUserMutate,
DeleteUserAdminUserMutateDeleteInvalidInput,
DeleteUserAdminUserMutateDeleteOk,
DeleteUserAdminUserMutateDeleteOperationError,
)
from .fragments import (
InvalidInputSelection,
InvalidInputSelectionErrors,
Expand All @@ -32,7 +41,6 @@
UserSelectionRoleManagedRole,
UserSelectionRoleUnmanagedRole,
)
from .get_role import GetRole, GetRoleRoleManagedRole, GetRoleRoleUnmanagedRole
from .get_roles import GetRoles, GetRolesRolesManagedRole, GetRolesRolesUnmanagedRole
from .get_user import GetUser, GetUserAdmin, GetUserAdminUser, GetUserAdminUserGet
from .get_users import GetUsers, GetUsersAdmin, GetUsersAdminUser, GetUsersAdminUserList
Expand Down Expand Up @@ -73,9 +81,13 @@
"CreateUserAdminUserCreateInvalidInput",
"CreateUserAdminUserCreateOperationError",
"CreateUserAdminUserCreateUser",
"GetRole",
"GetRoleRoleManagedRole",
"GetRoleRoleUnmanagedRole",
"DeleteUser",
"DeleteUserAdmin",
"DeleteUserAdminUser",
"DeleteUserAdminUserMutate",
"DeleteUserAdminUserMutateDeleteInvalidInput",
"DeleteUserAdminUserMutateDeleteOk",
"DeleteUserAdminUserMutateDeleteOperationError",
"GetRoles",
"GetRolesRolesManagedRole",
"GetRolesRolesUnmanagedRole",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
CreateUserAdminUserCreateOperationError,
CreateUserAdminUserCreateUser,
)
from .get_role import GetRole, GetRoleRoleManagedRole, GetRoleRoleUnmanagedRole
from .delete_user import DeleteUser, DeleteUserAdminUserMutate
from .get_roles import GetRoles, GetRolesRolesManagedRole, GetRolesRolesUnmanagedRole
from .get_user import GetUser, GetUserAdminUserGet
from .get_users import GetUsers, GetUsersAdminUserList
Expand Down Expand Up @@ -180,42 +180,47 @@ def create_user(self, input: UserInput, **kwargs: Any) -> Union[
data = self.get_data(response)
return CreateUser.model_validate(data).admin.user.create

def get_role(
self, role_id: str, **kwargs: Any
) -> Optional[Union[GetRoleRoleUnmanagedRole, GetRoleRoleManagedRole]]:
def delete_user(
self, name: str, **kwargs: Any
) -> Optional[DeleteUserAdminUserMutate]:
query = gql(
"""
query getRole($roleId: ID!) {
role(id: $roleId) {
...RoleSelection
mutation deleteUser($name: String!) {
admin {
user {
mutate(name: $name) {
delete {
__typename
...InvalidInputSelection
...OperationErrorSelection
}
}
}
}
}
fragment ManagedRoleSelection on ManagedRole {
id
name
arn
}
fragment RoleSelection on Role {
__typename
...UnmanagedRoleSelection
...ManagedRoleSelection
fragment InvalidInputSelection on InvalidInput {
errors {
path
message
name
context
}
}
fragment UnmanagedRoleSelection on UnmanagedRole {
id
fragment OperationErrorSelection on OperationError {
message
name
arn
context
}
"""
)
variables: Dict[str, object] = {"roleId": role_id}
variables: Dict[str, object] = {"name": name}
response = self.execute(
query=query, operation_name="getRole", variables=variables, **kwargs
query=query, operation_name="deleteUser", variables=variables, **kwargs
)
data = self.get_data(response)
return GetRole.model_validate(data).role
return DeleteUser.model_validate(data).admin.user.mutate

def get_roles(
self, **kwargs: Any
Expand Down Expand Up @@ -307,11 +312,11 @@ def set_role(
return SetRole.model_validate(data).admin.user.mutate

def add_roles(
self, roles: List[str], name: str, **kwargs: Any
self, name: str, roles: List[str], **kwargs: Any
) -> Optional[AddRolesAdminUserMutate]:
query = gql(
"""
mutation addRoles($roles: [String!]!, $name: String!) {
mutation addRoles($name: String!, $roles: [String!]!) {
admin {
user {
mutate(name: $name) {
Expand Down Expand Up @@ -341,7 +346,7 @@ def add_roles(
}
"""
)
variables: Dict[str, object] = {"roles": roles, "name": name}
variables: Dict[str, object] = {"name": name, "roles": roles}
response = self.execute(
query=query, operation_name="addRoles", variables=variables, **kwargs
)
Expand All @@ -350,14 +355,14 @@ def add_roles(

def remove_roles(
self,
roles: List[str],
name: str,
roles: List[str],
fallback: Union[Optional[str], UnsetType] = UNSET,
**kwargs: Any
) -> Optional[RemoveRolesAdminUserMutate]:
query = gql(
"""
mutation removeRoles($roles: [String!]!, $name: String!, $fallback: String) {
mutation removeRoles($name: String!, $roles: [String!]!, $fallback: String) {
admin {
user {
mutate(name: $name) {
Expand Down Expand Up @@ -388,8 +393,8 @@ def remove_roles(
"""
)
variables: Dict[str, object] = {
"roles": roles,
"name": name,
"roles": roles,
"fallback": fallback,
}
response = self.execute(
Expand Down
Loading

0 comments on commit edd2c19

Please sign in to comment.