Skip to content

Commit d505d0e

Browse files
authored
chore: Replace permission enums with literals (#113)
2 parents e2f8f88 + 0e62220 commit d505d0e

6 files changed

Lines changed: 49 additions & 104 deletions

File tree

nitric/resources/buckets.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
from nitric.exception import exception_from_grpc_error
2222
from nitric.api.storage import BucketRef, Storage
23-
from typing import List, Union, Callable
24-
from enum import Enum
23+
from typing import List, Callable, Literal
2524
from grpclib import GRPCError
2625

2726
from nitric.application import Nitric
@@ -35,13 +34,7 @@
3534

3635
from nitric.resources.resource import SecureResource
3736

38-
39-
class BucketPermission(Enum):
40-
"""Valid query expression operators."""
41-
42-
reading = "reading"
43-
writing = "writing"
44-
deleting = "deleting"
37+
BucketPermission = Literal["reading", "writing", "deleting"]
4538

4639

4740
class Bucket(SecureResource):
@@ -64,23 +57,19 @@ async def _register(self):
6457
except GRPCError as grpc_err:
6558
raise exception_from_grpc_error(grpc_err)
6659

67-
def _perms_to_actions(self, *args: Union[BucketPermission, str]) -> List[Action]:
68-
permission_actions_map = {
69-
BucketPermission.reading: [Action.BucketFileGet, Action.BucketFileList],
70-
BucketPermission.writing: [Action.BucketFilePut],
71-
BucketPermission.deleting: [Action.BucketFileDelete],
60+
def _perms_to_actions(self, *args: BucketPermission) -> List[int]:
61+
permission_actions_map: dict[BucketPermission, List[int]] = {
62+
"reading": [Action.BucketFileGet, Action.BucketFileList],
63+
"writing": [Action.BucketFilePut],
64+
"deleting": [Action.BucketFileDelete],
7265
}
73-
# convert strings to the enum value where needed
74-
perms = [
75-
permission if isinstance(permission, BucketPermission) else BucketPermission[permission.lower()]
76-
for permission in args
77-
]
78-
return [action for perm in perms for action in permission_actions_map[perm]]
66+
67+
return [action for perm in args for action in permission_actions_map[perm]]
7968

8069
def _to_resource(self) -> Resource:
81-
return Resource(name=self.name, type=ResourceType.Bucket)
70+
return Resource(name=self.name, type=ResourceType.Bucket) # type:ignore
8271

83-
def allow(self, *args: Union[BucketPermission, str]) -> BucketRef:
72+
def allow(self, *args: BucketPermission) -> BucketRef:
8473
"""Request the required permissions for this resource."""
8574
str_args = [str(permission) for permission in args]
8675
self._register_policy(*str_args)

nitric/resources/collections.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,19 @@
2020

2121
from nitric.api.documents import CollectionRef, Documents
2222
from nitric.exception import exception_from_grpc_error
23-
from typing import List, Union
24-
from enum import Enum
23+
from typing import List, Literal
2524
from grpclib import GRPCError
26-
2725
from nitric.application import Nitric
2826
from nitric.proto.nitric.resource.v1 import (
2927
Resource,
3028
ResourceType,
3129
Action,
3230
ResourceDeclareRequest,
3331
)
34-
3532
from nitric.resources.resource import SecureResource
3633

3734

38-
class CollectionPermission(Enum):
39-
"""Valid query expression operators."""
40-
41-
reading = "reading"
42-
writing = "writing"
43-
deleting = "deleting"
35+
CollectionPermission = Literal["reading", "writing", "deleting"]
4436

4537

4638
class Collection(SecureResource):
@@ -60,27 +52,22 @@ async def _register(self):
6052
raise exception_from_grpc_error(grpc_err)
6153

6254
def _to_resource(self) -> Resource:
63-
return Resource(name=self.name, type=ResourceType.Collection)
55+
return Resource(name=self.name, type=ResourceType.Collection) # type:ignore
6456

65-
def _perms_to_actions(self, *args: Union[CollectionPermission, str]) -> List[Action]:
66-
permission_actions_map = {
67-
CollectionPermission.reading: [
57+
def _perms_to_actions(self, *args: CollectionPermission) -> List[int]:
58+
permission_actions_map: dict[CollectionPermission, List[int]] = {
59+
"reading": [
6860
Action.CollectionDocumentRead,
6961
Action.CollectionQuery,
7062
Action.CollectionList,
7163
],
72-
CollectionPermission.writing: [Action.CollectionDocumentWrite, Action.CollectionList],
73-
CollectionPermission.deleting: [Action.CollectionDocumentDelete, Action.CollectionList],
64+
"writing": [Action.CollectionDocumentWrite, Action.CollectionList],
65+
"deleting": [Action.CollectionDocumentDelete, Action.CollectionList],
7466
}
75-
# convert strings to the enum value where needed
76-
perms = [
77-
permission if isinstance(permission, CollectionPermission) else CollectionPermission[permission.lower()]
78-
for permission in args
79-
]
8067

81-
return [action for perm in perms for action in permission_actions_map[perm]]
68+
return [action for perm in args for action in permission_actions_map[perm]]
8269

83-
def allow(self, *args: Union[CollectionPermission, str]) -> CollectionRef:
70+
def allow(self, *args: CollectionPermission) -> CollectionRef:
8471
"""Request the required permissions for this collection."""
8572
# Ensure registration of the resource is complete before requesting permissions.
8673
str_args = [str(permission) for permission in args]

nitric/resources/queues.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
from __future__ import annotations
2020

2121
from nitric.exception import exception_from_grpc_error
22-
from typing import List, Union
23-
from enum import Enum
22+
from typing import List, Union, Literal
2423
from grpclib import GRPCError
2524
from nitric.api.queues import QueueRef, Queues
2625
from nitric.application import Nitric
@@ -33,13 +32,7 @@
3332

3433
from nitric.resources.resource import SecureResource
3534

36-
37-
class QueuePermission(Enum):
38-
"""Valid query expression operators."""
39-
40-
sending = "sending"
41-
receiving = "receiving"
42-
35+
QueuePermission = Literal["sending", "receiving"]
4336

4437
class Queue(SecureResource):
4538
"""A queue resource."""
@@ -53,20 +46,15 @@ def __init__(self, name: str):
5346
self.name = name
5447

5548
def _to_resource(self) -> Resource:
56-
return Resource(name=self.name, type=ResourceType.Queue)
49+
return Resource(name=self.name, type=ResourceType.Queue) # type:ignore
5750

58-
def _perms_to_actions(self, *args: Union[QueuePermission, str]) -> List[Action]:
59-
permission_actions_map = {
60-
QueuePermission.sending: [Action.QueueSend, Action.QueueList, Action.QueueDetail],
61-
QueuePermission.receiving: [Action.QueueReceive, Action.QueueList, Action.QueueDetail],
51+
def _perms_to_actions(self, *args: QueuePermission) -> List[int]:
52+
permission_actions_map: dict[QueuePermission, List[int]] = {
53+
"sending": [Action.QueueSend, Action.QueueList, Action.QueueDetail],
54+
"receiving": [Action.QueueReceive, Action.QueueList, Action.QueueDetail],
6255
}
63-
# convert strings to the enum value where needed
64-
perms = [
65-
permission if isinstance(permission, QueuePermission) else QueuePermission[permission.lower()]
66-
for permission in args
67-
]
6856

69-
return [action for perm in perms for action in permission_actions_map[perm]]
57+
return [action for perm in args for action in permission_actions_map[perm]]
7058

7159
async def _register(self):
7260
try:
@@ -76,7 +64,7 @@ async def _register(self):
7664
except GRPCError as grpc_err:
7765
raise exception_from_grpc_error(grpc_err)
7866

79-
def allow(self, *args: Union[QueuePermission, str]) -> QueueRef:
67+
def allow(self, *args: QueuePermission) -> QueueRef:
8068
"""Request the required permissions for this queue."""
8169
# Ensure registration of the resource is complete before requesting permissions.
8270
str_args = [str(permission) for permission in args]

nitric/resources/resource.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def _to_resource(self) -> WireResource:
7979
pass
8080

8181
@abstractmethod
82-
def _perms_to_actions(self, *args: str) -> List[Action]:
82+
def _perms_to_actions(self, *args: Any) -> List[int]:
8383
pass
8484

8585
async def _register_policy_async(self, *args: str):

nitric/resources/secrets.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
from __future__ import annotations
2020

2121
from nitric.exception import exception_from_grpc_error
22-
from typing import List, Union
23-
from enum import Enum
22+
from typing import List, Literal
2423
from grpclib import GRPCError
2524

2625
from nitric.application import Nitric
@@ -34,12 +33,7 @@
3433

3534
from nitric.resources.resource import SecureResource
3635

37-
38-
class SecretPermission(Enum):
39-
"""Available permissions that can be requested for secret resources."""
40-
41-
accessing = "accessing"
42-
putting = "putting"
36+
SecretPermission = Literal["accessing", "putting"]
4337

4438

4539
class Secret(SecureResource):
@@ -54,7 +48,7 @@ def __init__(self, name: str):
5448
self.name = name
5549

5650
def _to_resource(self) -> Resource:
57-
return Resource(name=self.name, type=ResourceType.Secret)
51+
return Resource(name=self.name, type=ResourceType.Secret) # type:ignore
5852

5953
async def _register(self):
6054
try:
@@ -64,20 +58,15 @@ async def _register(self):
6458
except GRPCError as grpc_err:
6559
raise exception_from_grpc_error(grpc_err)
6660

67-
def _perms_to_actions(self, *args: Union[SecretPermission, str]) -> List[Action]:
68-
permissions_actions_map = {
69-
SecretPermission.accessing: [Action.SecretAccess],
70-
SecretPermission.putting: [Action.SecretPut],
61+
def _perms_to_actions(self, *args: SecretPermission) -> List[int]:
62+
permissions_actions_map: dict[SecretPermission, List[int]] = {
63+
"accessing": [Action.SecretAccess],
64+
"putting": [Action.SecretPut],
7165
}
72-
# convert strings to the enum value where needed
73-
perms = [
74-
permission if isinstance(permission, SecretPermission) else SecretPermission[permission.lower()]
75-
for permission in args
76-
]
7766

78-
return [action for perm in perms for action in permissions_actions_map[perm]]
67+
return [action for perm in args for action in permissions_actions_map[perm]]
7968

80-
def allow(self, *args: Union[SecretPermission, str]) -> SecretContainerRef:
69+
def allow(self, *args: SecretPermission) -> SecretContainerRef:
8170
"""Request the specified permissions to this resource."""
8271
str_args = [str(permission) for permission in args]
8372
self._register_policy(*str_args)

nitric/resources/topics.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
from nitric.api.events import Events, TopicRef
2222
from nitric.exception import exception_from_grpc_error
23-
from typing import List, Union, Callable
24-
from enum import Enum
23+
from typing import List, Union, Callable, Literal
2524
from grpclib import GRPCError
2625
from nitric.application import Nitric
2726
from nitric.faas import FunctionServer, SubscriptionWorkerOptions, EventHandler
@@ -34,11 +33,7 @@
3433

3534
from nitric.resources.resource import SecureResource
3635

37-
38-
class TopicPermission(Enum):
39-
"""Valid query expression operators."""
40-
41-
publishing = "publishing"
36+
TopicPermission = Literal["publishing"]
4237

4338

4439
class Topic(SecureResource):
@@ -61,19 +56,16 @@ async def _register(self):
6156
raise exception_from_grpc_error(grpc_err)
6257

6358
def _to_resource(self) -> Resource:
64-
return Resource(name=self.name, type=ResourceType.Topic)
59+
return Resource(name=self.name, type=ResourceType.Topic) # type:ignore
6560

66-
def _perms_to_actions(self, *args: Union[TopicPermission, str]) -> List[Action]:
67-
_permMap = {TopicPermission.publishing: [Action.TopicEventPublish]}
68-
# convert strings to the enum value where needed
69-
perms = [
70-
permission if isinstance(permission, TopicPermission) else TopicPermission[permission.lower()]
71-
for permission in args
72-
]
61+
def _perms_to_actions(self, *args: TopicPermission) -> List[int]:
62+
_permMap: dict[TopicPermission, List[int]] = {
63+
"publishing": [Action.TopicEventPublish]
64+
}
7365

74-
return [action for perm in perms for action in _permMap[perm]]
66+
return [action for perm in args for action in _permMap[perm]]
7567

76-
def allow(self, *args: Union[TopicPermission, str]) -> TopicRef:
68+
def allow(self, *args: TopicPermission) -> TopicRef:
7769
"""Request the specified permissions to this resource."""
7870
str_args = [str(permission) for permission in args]
7971
self._register_policy(*str_args)

0 commit comments

Comments
 (0)