-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathorchestration_system.py
More file actions
196 lines (167 loc) · 6.44 KB
/
orchestration_system.py
File metadata and controls
196 lines (167 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import uuid
from typing import Optional, Literal, get_args
from ninja.errors import HttpError
from django.http import HttpResponse
from django.contrib.auth import get_user_model
from django.db.models import QuerySet
from domains.iam.models import APIKey
from domains.etl.models import OrchestrationSystem
from interfaces.api.schemas import (
OrchestrationSystemSummaryResponse,
OrchestrationSystemDetailResponse,
OrchestrationSystemPostBody,
OrchestrationSystemPatchBody,
)
from interfaces.api.schemas.orchestration_system import (
OrchestrationSystemFields,
OrchestrationSystemOrderByFields,
)
from interfaces.api.service import ServiceUtils
User = get_user_model()
class OrchestrationSystemService(ServiceUtils):
def get_orchestration_system_for_action(
self,
principal: User | APIKey,
uid: uuid.UUID,
action: Literal["view", "edit", "delete"],
expand_related: Optional[bool] = None,
raise_400: bool = False,
):
try:
orchestration_system = OrchestrationSystem.objects
if expand_related:
orchestration_system = self.select_expanded_fields(orchestration_system)
orchestration_system = orchestration_system.get(pk=uid)
except OrchestrationSystem.DoesNotExist:
raise HttpError(
404 if not raise_400 else 400, "Orchestration system does not exist"
)
orchestration_system_permissions = (
orchestration_system.get_principal_permissions(principal=principal)
)
if "view" not in orchestration_system_permissions:
raise HttpError(
404 if not raise_400 else 400, "Orchestration system does not exist"
)
if action not in orchestration_system_permissions:
raise HttpError(
403 if not raise_400 else 400,
f"You do not have permission to {action} this orchestration system",
)
return orchestration_system
@staticmethod
def select_expanded_fields(queryset: QuerySet) -> QuerySet:
return queryset.select_related("workspace")
def list(
self,
principal: Optional[User | APIKey],
response: HttpResponse,
page: Optional[int] = None,
page_size: Optional[int] = None,
order_by: Optional[list[str]] = None,
filtering: Optional[dict] = None,
expand_related: Optional[bool] = None,
):
queryset = OrchestrationSystem.objects
for field in [
"workspace_id",
"orchestration_system_type",
]:
if field in filtering:
queryset = self.apply_filters(queryset, field, filtering[field])
if order_by:
queryset = self.apply_ordering(
queryset,
order_by,
list(get_args(OrchestrationSystemOrderByFields)),
{"type": "orchestration_system_type"},
)
if expand_related:
queryset = self.select_expanded_fields(queryset)
queryset = queryset.visible(principal=principal).distinct()
queryset, count = self.apply_pagination(queryset, response, page, page_size)
return [
(
OrchestrationSystemDetailResponse.model_validate(orchestration_system)
if expand_related
else OrchestrationSystemSummaryResponse.model_validate(
orchestration_system
)
)
for orchestration_system in queryset.all()
]
def get(
self,
principal: Optional[User | APIKey],
uid: uuid.UUID,
expand_related: Optional[bool] = None,
):
orchestration_system = self.get_orchestration_system_for_action(
principal=principal, uid=uid, action="view", expand_related=expand_related
)
return (
OrchestrationSystemDetailResponse.model_validate(orchestration_system)
if expand_related
else OrchestrationSystemSummaryResponse.model_validate(orchestration_system)
)
def create(
self,
principal: User | APIKey,
data: OrchestrationSystemPostBody,
expand_related: Optional[bool] = None,
):
workspace, _ = (
self.get_workspace(principal=principal, workspace_id=data.workspace_id)
if data.workspace_id
else (
None,
None,
)
)
if not OrchestrationSystem.can_principal_create(
principal=principal, workspace=workspace
) or data.orchestration_system_type.upper() == "INTERNAL":
raise HttpError(
403, "You do not have permission to create this orchestration system"
)
orchestration_system = OrchestrationSystem.objects.create(
workspace=workspace,
**data.dict(include=set(OrchestrationSystemFields.model_fields.keys())),
)
return self.get(
principal=principal,
uid=orchestration_system.id,
expand_related=expand_related,
)
def update(
self,
principal: User | APIKey,
uid: uuid.UUID,
data: OrchestrationSystemPatchBody,
expand_related: Optional[bool] = None,
):
orchestration_system = self.get_orchestration_system_for_action(
principal=principal, uid=uid, action="edit", expand_related=expand_related
)
orchestration_system_data = data.dict(
include=set(OrchestrationSystemFields.model_fields.keys()),
exclude_unset=True,
)
for field, value in orchestration_system_data.items():
if field == "orchestration_system_type" and value.upper() == "INTERNAL":
raise HttpError(
403, "You do not have permission to set this orchestration system type to INTERNAL"
)
setattr(orchestration_system, field, value)
orchestration_system.save()
return self.get(
principal=principal,
uid=orchestration_system.id,
expand_related=expand_related,
)
def delete(self, principal: User | APIKey, uid: uuid.UUID):
orchestration_system = self.get_orchestration_system_for_action(
principal=principal, uid=uid, action="delete", expand_related=True
)
orchestration_system.delete()
return "Orchestration system deleted"