Skip to content

Commit e74ccfb

Browse files
authored
feat(mongodb): implement roles permissions api (scaleway#873)
1 parent 6e6d3cd commit e74ccfb

File tree

8 files changed

+344
-0
lines changed

8 files changed

+344
-0
lines changed

scaleway-async/scaleway_async/mongodb/v1alpha1/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .types import SettingPropertyType
1010
from .types import SnapshotStatus
1111
from .content import SNAPSHOT_TRANSIENT_STATUSES
12+
from .types import UserRoleRole
1213
from .types import VolumeType
1314
from .types import EndpointPrivateNetworkDetails
1415
from .types import EndpointPublicDetails
@@ -19,6 +20,7 @@
1920
from .types import Volume
2021
from .types import NodeTypeVolumeType
2122
from .types import SnapshotVolumeType
23+
from .types import UserRole
2224
from .types import Setting
2325
from .types import EndpointSpec
2426
from .types import CreateInstanceRequestVolumeDetails
@@ -50,6 +52,7 @@
5052
from .types import ListVersionsRequest
5153
from .types import ListVersionsResponse
5254
from .types import RestoreSnapshotRequest
55+
from .types import SetUserRoleRequest
5356
from .types import UpdateInstanceRequest
5457
from .types import UpdateSnapshotRequest
5558
from .types import UpdateUserRequest
@@ -66,6 +69,7 @@
6669
"SettingPropertyType",
6770
"SnapshotStatus",
6871
"SNAPSHOT_TRANSIENT_STATUSES",
72+
"UserRoleRole",
6973
"VolumeType",
7074
"EndpointPrivateNetworkDetails",
7175
"EndpointPublicDetails",
@@ -76,6 +80,7 @@
7680
"Volume",
7781
"NodeTypeVolumeType",
7882
"SnapshotVolumeType",
83+
"UserRole",
7984
"Setting",
8085
"EndpointSpec",
8186
"CreateInstanceRequestVolumeDetails",
@@ -107,6 +112,7 @@
107112
"ListVersionsRequest",
108113
"ListVersionsResponse",
109114
"RestoreSnapshotRequest",
115+
"SetUserRoleRequest",
110116
"UpdateInstanceRequest",
111117
"UpdateSnapshotRequest",
112118
"UpdateUserRequest",

scaleway-async/scaleway_async/mongodb/v1alpha1/api.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
NodeType,
3838
RestoreSnapshotRequest,
3939
RestoreSnapshotRequestVolumeDetails,
40+
SetUserRoleRequest,
4041
Snapshot,
4142
UpdateInstanceRequest,
4243
UpdateSnapshotRequest,
4344
UpdateUserRequest,
4445
UpgradeInstanceRequest,
4546
User,
47+
UserRole,
4648
Version,
4749
)
4850
from .content import (
@@ -64,6 +66,7 @@
6466
marshal_CreateSnapshotRequest,
6567
marshal_CreateUserRequest,
6668
marshal_RestoreSnapshotRequest,
69+
marshal_SetUserRoleRequest,
6770
marshal_UpdateInstanceRequest,
6871
marshal_UpdateSnapshotRequest,
6972
marshal_UpdateUserRequest,
@@ -1211,6 +1214,52 @@ async def delete_user(
12111214

12121215
self._throw_on_error(res)
12131216

1217+
async def set_user_role(
1218+
self,
1219+
*,
1220+
instance_id: str,
1221+
user_name: str,
1222+
region: Optional[ScwRegion] = None,
1223+
roles: Optional[List[UserRole]] = None,
1224+
) -> User:
1225+
"""
1226+
:param instance_id: UUID of the Database Instance the user belongs to.
1227+
:param user_name: Name of the database user.
1228+
:param region: Region to target. If none is passed will use default region from the config.
1229+
:param roles: List of roles assigned to the user, along with the corresponding database where each role is granted.
1230+
:return: :class:`User <User>`
1231+
1232+
Usage:
1233+
::
1234+
1235+
result = await api.set_user_role(
1236+
instance_id="example",
1237+
user_name="example",
1238+
)
1239+
"""
1240+
1241+
param_region = validate_path_param(
1242+
"region", region or self.client.default_region
1243+
)
1244+
param_instance_id = validate_path_param("instance_id", instance_id)
1245+
1246+
res = self._request(
1247+
"PUT",
1248+
f"/mongodb/v1alpha1/regions/{param_region}/instances/{param_instance_id}/roles",
1249+
body=marshal_SetUserRoleRequest(
1250+
SetUserRoleRequest(
1251+
instance_id=instance_id,
1252+
user_name=user_name,
1253+
region=region,
1254+
roles=roles,
1255+
),
1256+
self.client,
1257+
),
1258+
)
1259+
1260+
self._throw_on_error(res)
1261+
return unmarshal_User(res.json())
1262+
12141263
async def delete_endpoint(
12151264
self,
12161265
*,

scaleway-async/scaleway_async/mongodb/v1alpha1/marshalling.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Instance,
1919
SnapshotVolumeType,
2020
Snapshot,
21+
UserRole,
2122
User,
2223
ListInstancesResponse,
2324
NodeTypeVolumeType,
@@ -38,6 +39,7 @@
3839
CreateUserRequest,
3940
RestoreSnapshotRequestVolumeDetails,
4041
RestoreSnapshotRequest,
42+
SetUserRoleRequest,
4143
UpdateInstanceRequest,
4244
UpdateSnapshotRequest,
4345
UpdateUserRequest,
@@ -301,6 +303,33 @@ def unmarshal_Snapshot(data: Any) -> Snapshot:
301303
return Snapshot(**args)
302304

303305

306+
def unmarshal_UserRole(data: Any) -> UserRole:
307+
if not isinstance(data, dict):
308+
raise TypeError(
309+
"Unmarshalling the type 'UserRole' failed as data isn't a dictionary."
310+
)
311+
312+
args: Dict[str, Any] = {}
313+
314+
field = data.get("role", None)
315+
if field is not None:
316+
args["role"] = field
317+
318+
field = data.get("database", None)
319+
if field is not None:
320+
args["database"] = field
321+
else:
322+
args["database"] = None
323+
324+
field = data.get("any_database", None)
325+
if field is not None:
326+
args["any_database"] = field
327+
else:
328+
args["any_database"] = None
329+
330+
return UserRole(**args)
331+
332+
304333
def unmarshal_User(data: Any) -> User:
305334
if not isinstance(data, dict):
306335
raise TypeError(
@@ -313,6 +342,12 @@ def unmarshal_User(data: Any) -> User:
313342
if field is not None:
314343
args["name"] = field
315344

345+
field = data.get("roles", None)
346+
if field is not None:
347+
args["roles"] = (
348+
[unmarshal_UserRole(v) for v in field] if field is not None else None
349+
)
350+
316351
return User(**args)
317352

318353

@@ -775,6 +810,41 @@ def marshal_RestoreSnapshotRequest(
775810
return output
776811

777812

813+
def marshal_UserRole(
814+
request: UserRole,
815+
defaults: ProfileDefaults,
816+
) -> Dict[str, Any]:
817+
output: Dict[str, Any] = {}
818+
output.update(
819+
resolve_one_of(
820+
[
821+
OneOfPossibility("database", request.database),
822+
OneOfPossibility("any_database", request.any_database),
823+
]
824+
),
825+
)
826+
827+
if request.role is not None:
828+
output["role"] = str(request.role)
829+
830+
return output
831+
832+
833+
def marshal_SetUserRoleRequest(
834+
request: SetUserRoleRequest,
835+
defaults: ProfileDefaults,
836+
) -> Dict[str, Any]:
837+
output: Dict[str, Any] = {}
838+
839+
if request.user_name is not None:
840+
output["user_name"] = request.user_name
841+
842+
if request.roles is not None:
843+
output["roles"] = [marshal_UserRole(item, defaults) for item in request.roles]
844+
845+
return output
846+
847+
778848
def marshal_UpdateInstanceRequest(
779849
request: UpdateInstanceRequest,
780850
defaults: ProfileDefaults,

scaleway-async/scaleway_async/mongodb/v1alpha1/types.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ def __str__(self) -> str:
9595
return str(self.value)
9696

9797

98+
class UserRoleRole(str, Enum, metaclass=StrEnumMeta):
99+
UNKNOWN_ROLE = "unknown_role"
100+
READ = "read"
101+
READ_WRITE = "read_write"
102+
DB_ADMIN = "db_admin"
103+
104+
def __str__(self) -> str:
105+
return str(self.value)
106+
107+
98108
class VolumeType(str, Enum, metaclass=StrEnumMeta):
99109
UNKNOWN_TYPE = "unknown_type"
100110
SBS_5K = "sbs_5k"
@@ -220,6 +230,15 @@ class SnapshotVolumeType:
220230
type_: VolumeType
221231

222232

233+
@dataclass
234+
class UserRole:
235+
role: UserRoleRole
236+
237+
database: Optional[str]
238+
239+
any_database: Optional[bool]
240+
241+
223242
@dataclass
224243
class Setting:
225244
name: str
@@ -484,6 +503,11 @@ class User:
484503
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).
485504
"""
486505

506+
roles: List[UserRole]
507+
"""
508+
List of roles assigned to the user, along with the corresponding database where each role is granted.
509+
"""
510+
487511

488512
@dataclass
489513
class Version:
@@ -959,6 +983,29 @@ class RestoreSnapshotRequest:
959983
"""
960984

961985

986+
@dataclass
987+
class SetUserRoleRequest:
988+
instance_id: str
989+
"""
990+
UUID of the Database Instance the user belongs to.
991+
"""
992+
993+
user_name: str
994+
"""
995+
Name of the database user.
996+
"""
997+
998+
region: Optional[ScwRegion]
999+
"""
1000+
Region to target. If none is passed will use default region from the config.
1001+
"""
1002+
1003+
roles: Optional[List[UserRole]]
1004+
"""
1005+
List of roles assigned to the user, along with the corresponding database where each role is granted.
1006+
"""
1007+
1008+
9621009
@dataclass
9631010
class UpdateInstanceRequest:
9641011
instance_id: str

scaleway/scaleway/mongodb/v1alpha1/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .types import SettingPropertyType
1010
from .types import SnapshotStatus
1111
from .content import SNAPSHOT_TRANSIENT_STATUSES
12+
from .types import UserRoleRole
1213
from .types import VolumeType
1314
from .types import EndpointPrivateNetworkDetails
1415
from .types import EndpointPublicDetails
@@ -19,6 +20,7 @@
1920
from .types import Volume
2021
from .types import NodeTypeVolumeType
2122
from .types import SnapshotVolumeType
23+
from .types import UserRole
2224
from .types import Setting
2325
from .types import EndpointSpec
2426
from .types import CreateInstanceRequestVolumeDetails
@@ -50,6 +52,7 @@
5052
from .types import ListVersionsRequest
5153
from .types import ListVersionsResponse
5254
from .types import RestoreSnapshotRequest
55+
from .types import SetUserRoleRequest
5356
from .types import UpdateInstanceRequest
5457
from .types import UpdateSnapshotRequest
5558
from .types import UpdateUserRequest
@@ -66,6 +69,7 @@
6669
"SettingPropertyType",
6770
"SnapshotStatus",
6871
"SNAPSHOT_TRANSIENT_STATUSES",
72+
"UserRoleRole",
6973
"VolumeType",
7074
"EndpointPrivateNetworkDetails",
7175
"EndpointPublicDetails",
@@ -76,6 +80,7 @@
7680
"Volume",
7781
"NodeTypeVolumeType",
7882
"SnapshotVolumeType",
83+
"UserRole",
7984
"Setting",
8085
"EndpointSpec",
8186
"CreateInstanceRequestVolumeDetails",
@@ -107,6 +112,7 @@
107112
"ListVersionsRequest",
108113
"ListVersionsResponse",
109114
"RestoreSnapshotRequest",
115+
"SetUserRoleRequest",
110116
"UpdateInstanceRequest",
111117
"UpdateSnapshotRequest",
112118
"UpdateUserRequest",

0 commit comments

Comments
 (0)