Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(mongodb): implement roles permissions api #873

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 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions scaleway-async/scaleway_async/mongodb/v1alpha1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .types import SettingPropertyType
from .types import SnapshotStatus
from .content import SNAPSHOT_TRANSIENT_STATUSES
from .types import UserRoleRole
from .types import VolumeType
from .types import EndpointPrivateNetworkDetails
from .types import EndpointPublicDetails
Expand All @@ -19,6 +20,7 @@
from .types import Volume
from .types import NodeTypeVolumeType
from .types import SnapshotVolumeType
from .types import UserRole
from .types import Setting
from .types import EndpointSpec
from .types import CreateInstanceRequestVolumeDetails
Expand Down Expand Up @@ -50,6 +52,7 @@
from .types import ListVersionsRequest
from .types import ListVersionsResponse
from .types import RestoreSnapshotRequest
from .types import SetUserRoleRequest
from .types import UpdateInstanceRequest
from .types import UpdateSnapshotRequest
from .types import UpdateUserRequest
Expand All @@ -66,6 +69,7 @@
"SettingPropertyType",
"SnapshotStatus",
"SNAPSHOT_TRANSIENT_STATUSES",
"UserRoleRole",
"VolumeType",
"EndpointPrivateNetworkDetails",
"EndpointPublicDetails",
Expand All @@ -76,6 +80,7 @@
"Volume",
"NodeTypeVolumeType",
"SnapshotVolumeType",
"UserRole",
"Setting",
"EndpointSpec",
"CreateInstanceRequestVolumeDetails",
Expand Down Expand Up @@ -107,6 +112,7 @@
"ListVersionsRequest",
"ListVersionsResponse",
"RestoreSnapshotRequest",
"SetUserRoleRequest",
"UpdateInstanceRequest",
"UpdateSnapshotRequest",
"UpdateUserRequest",
Expand Down
49 changes: 49 additions & 0 deletions scaleway-async/scaleway_async/mongodb/v1alpha1/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
NodeType,
RestoreSnapshotRequest,
RestoreSnapshotRequestVolumeDetails,
SetUserRoleRequest,
Snapshot,
UpdateInstanceRequest,
UpdateSnapshotRequest,
UpdateUserRequest,
UpgradeInstanceRequest,
User,
UserRole,
Version,
)
from .content import (
Expand All @@ -64,6 +66,7 @@
marshal_CreateSnapshotRequest,
marshal_CreateUserRequest,
marshal_RestoreSnapshotRequest,
marshal_SetUserRoleRequest,
marshal_UpdateInstanceRequest,
marshal_UpdateSnapshotRequest,
marshal_UpdateUserRequest,
Expand Down Expand Up @@ -1211,6 +1214,52 @@ async def delete_user(

self._throw_on_error(res)

async def set_user_role(
self,
*,
instance_id: str,
user_name: str,
region: Optional[ScwRegion] = None,
roles: Optional[List[UserRole]] = None,
) -> User:
"""
:param instance_id: UUID of the Database Instance the user belongs to.
:param user_name: Name of the database user.
:param region: Region to target. If none is passed will use default region from the config.
:param roles: List of roles assigned to the user, along with the corresponding database where each role is granted.
:return: :class:`User <User>`

Usage:
::

result = await api.set_user_role(
instance_id="example",
user_name="example",
)
"""

param_region = validate_path_param(
"region", region or self.client.default_region
)
param_instance_id = validate_path_param("instance_id", instance_id)

res = self._request(
"PUT",
f"/mongodb/v1alpha1/regions/{param_region}/instances/{param_instance_id}/roles",
body=marshal_SetUserRoleRequest(
SetUserRoleRequest(
instance_id=instance_id,
user_name=user_name,
region=region,
roles=roles,
),
self.client,
),
)

self._throw_on_error(res)
return unmarshal_User(res.json())

async def delete_endpoint(
self,
*,
Expand Down
70 changes: 70 additions & 0 deletions scaleway-async/scaleway_async/mongodb/v1alpha1/marshalling.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Instance,
SnapshotVolumeType,
Snapshot,
UserRole,
User,
ListInstancesResponse,
NodeTypeVolumeType,
Expand All @@ -38,6 +39,7 @@
CreateUserRequest,
RestoreSnapshotRequestVolumeDetails,
RestoreSnapshotRequest,
SetUserRoleRequest,
UpdateInstanceRequest,
UpdateSnapshotRequest,
UpdateUserRequest,
Expand Down Expand Up @@ -301,6 +303,33 @@ def unmarshal_Snapshot(data: Any) -> Snapshot:
return Snapshot(**args)


def unmarshal_UserRole(data: Any) -> UserRole:
if not isinstance(data, dict):
raise TypeError(
"Unmarshalling the type 'UserRole' failed as data isn't a dictionary."
)

args: Dict[str, Any] = {}

field = data.get("role", None)
if field is not None:
args["role"] = field

field = data.get("database", None)
if field is not None:
args["database"] = field
else:
args["database"] = None

field = data.get("any_database", None)
if field is not None:
args["any_database"] = field
else:
args["any_database"] = None

return UserRole(**args)


def unmarshal_User(data: Any) -> User:
if not isinstance(data, dict):
raise TypeError(
Expand All @@ -313,6 +342,12 @@ def unmarshal_User(data: Any) -> User:
if field is not None:
args["name"] = field

field = data.get("roles", None)
if field is not None:
args["roles"] = (
[unmarshal_UserRole(v) for v in field] if field is not None else None
)

return User(**args)


Expand Down Expand Up @@ -775,6 +810,41 @@ def marshal_RestoreSnapshotRequest(
return output


def marshal_UserRole(
request: UserRole,
defaults: ProfileDefaults,
) -> Dict[str, Any]:
output: Dict[str, Any] = {}
output.update(
resolve_one_of(
[
OneOfPossibility("database", request.database),
OneOfPossibility("any_database", request.any_database),
]
),
)

if request.role is not None:
output["role"] = str(request.role)

return output


def marshal_SetUserRoleRequest(
request: SetUserRoleRequest,
defaults: ProfileDefaults,
) -> Dict[str, Any]:
output: Dict[str, Any] = {}

if request.user_name is not None:
output["user_name"] = request.user_name

if request.roles is not None:
output["roles"] = [marshal_UserRole(item, defaults) for item in request.roles]

return output


def marshal_UpdateInstanceRequest(
request: UpdateInstanceRequest,
defaults: ProfileDefaults,
Expand Down
47 changes: 47 additions & 0 deletions scaleway-async/scaleway_async/mongodb/v1alpha1/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ def __str__(self) -> str:
return str(self.value)


class UserRoleRole(str, Enum, metaclass=StrEnumMeta):
UNKNOWN_ROLE = "unknown_role"
READ = "read"
READ_WRITE = "read_write"
DB_ADMIN = "db_admin"

def __str__(self) -> str:
return str(self.value)


class VolumeType(str, Enum, metaclass=StrEnumMeta):
UNKNOWN_TYPE = "unknown_type"
SBS_5K = "sbs_5k"
Expand Down Expand Up @@ -220,6 +230,15 @@ class SnapshotVolumeType:
type_: VolumeType


@dataclass
class UserRole:
role: UserRoleRole

database: Optional[str]

any_database: Optional[bool]


@dataclass
class Setting:
name: str
Expand Down Expand Up @@ -484,6 +503,11 @@ class User:
Name of the user (Length must be between 1 and 63 characters. First character must be an alphabet character (a-zA-Z). Only a-zA-Z0-9_$- characters are accepted).
"""

roles: List[UserRole]
"""
List of roles assigned to the user, along with the corresponding database where each role is granted.
"""


@dataclass
class Version:
Expand Down Expand Up @@ -959,6 +983,29 @@ class RestoreSnapshotRequest:
"""


@dataclass
class SetUserRoleRequest:
instance_id: str
"""
UUID of the Database Instance the user belongs to.
"""

user_name: str
"""
Name of the database user.
"""

region: Optional[ScwRegion]
"""
Region to target. If none is passed will use default region from the config.
"""

roles: Optional[List[UserRole]]
"""
List of roles assigned to the user, along with the corresponding database where each role is granted.
"""


@dataclass
class UpdateInstanceRequest:
instance_id: str
Expand Down
6 changes: 6 additions & 0 deletions scaleway/scaleway/mongodb/v1alpha1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .types import SettingPropertyType
from .types import SnapshotStatus
from .content import SNAPSHOT_TRANSIENT_STATUSES
from .types import UserRoleRole
from .types import VolumeType
from .types import EndpointPrivateNetworkDetails
from .types import EndpointPublicDetails
Expand All @@ -19,6 +20,7 @@
from .types import Volume
from .types import NodeTypeVolumeType
from .types import SnapshotVolumeType
from .types import UserRole
from .types import Setting
from .types import EndpointSpec
from .types import CreateInstanceRequestVolumeDetails
Expand Down Expand Up @@ -50,6 +52,7 @@
from .types import ListVersionsRequest
from .types import ListVersionsResponse
from .types import RestoreSnapshotRequest
from .types import SetUserRoleRequest
from .types import UpdateInstanceRequest
from .types import UpdateSnapshotRequest
from .types import UpdateUserRequest
Expand All @@ -66,6 +69,7 @@
"SettingPropertyType",
"SnapshotStatus",
"SNAPSHOT_TRANSIENT_STATUSES",
"UserRoleRole",
"VolumeType",
"EndpointPrivateNetworkDetails",
"EndpointPublicDetails",
Expand All @@ -76,6 +80,7 @@
"Volume",
"NodeTypeVolumeType",
"SnapshotVolumeType",
"UserRole",
"Setting",
"EndpointSpec",
"CreateInstanceRequestVolumeDetails",
Expand Down Expand Up @@ -107,6 +112,7 @@
"ListVersionsRequest",
"ListVersionsResponse",
"RestoreSnapshotRequest",
"SetUserRoleRequest",
"UpdateInstanceRequest",
"UpdateSnapshotRequest",
"UpdateUserRequest",
Expand Down
Loading