-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathevent_stream.py
309 lines (279 loc) · 10 KB
/
event_stream.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# Copyright 2024 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""EventStream configuration API Set."""
import logging
from ansible_base.rbac.api.related import check_related_permissions
from ansible_base.rbac.models import RoleDefinition
from django.db import transaction
from django.forms import model_to_dict
from django_filters import rest_framework as defaultfilters
from drf_spectacular.utils import (
OpenApiParameter,
OpenApiResponse,
extend_schema,
)
from rest_framework import mixins, status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from aap_eda.api import exceptions as api_exc, filters, serializers
from aap_eda.core import models
from aap_eda.core.enums import ResourceType
from aap_eda.core.utils import logging_utils
logger = logging.getLogger(__name__)
resource_name = "EventStream"
class EventStreamViewSet(
mixins.CreateModelMixin,
viewsets.ReadOnlyModelViewSet,
mixins.DestroyModelMixin,
):
queryset = models.EventStream.objects.order_by("-created_at")
filter_backends = (defaultfilters.DjangoFilterBackend,)
filterset_class = filters.EventStreamFilter
rbac_resource_type = ResourceType.EVENT_STREAM
def get_serializer_class(self):
if self.action == "list":
return serializers.EventStreamOutSerializer
if self.action == "destroy":
return serializers.EventStreamOutSerializer
if self.action == "create":
return serializers.EventStreamInSerializer
if self.action == "partial_update":
return serializers.EventStreamInSerializer
return serializers.EventStreamOutSerializer
def get_response_serializer_class(self):
return serializers.EventStreamOutSerializer
def filter_queryset(self, queryset):
return super().filter_queryset(
queryset.model.access_qs(self.request.user, queryset=queryset)
)
@extend_schema(
description="Get the EventStream by its id",
responses={
status.HTTP_200_OK: OpenApiResponse(
serializers.EventStreamOutSerializer,
description="Return the event stream by its id.",
),
},
)
def retrieve(self, request, *args, **kwargs):
event_stream = self.get_object()
logger.info(
logging_utils.generate_simple_audit_log(
"Read",
resource_name,
event_stream.name,
event_stream.id,
event_stream.organization,
)
)
return Response(
serializers.EventStreamOutSerializer(event_stream).data
)
@extend_schema(
description="Delete a EventStream by its id",
responses={
status.HTTP_204_NO_CONTENT: OpenApiResponse(
None, description="Delete successful."
)
},
)
def destroy(self, request, *args, **kwargs):
event_stream = self.get_object()
ref_count = event_stream.activations.count()
if ref_count > 0:
raise api_exc.Conflict(
f"Event stream '{event_stream.name}' is being referenced by "
f"{ref_count} activation(s) and cannot be deleted"
)
self.perform_destroy(event_stream)
logger.info(
logging_utils.generate_simple_audit_log(
"Delete",
resource_name,
event_stream.name,
event_stream.id,
event_stream.organization,
)
)
return Response(status=status.HTTP_204_NO_CONTENT)
@extend_schema(
description="List all eventstreams",
responses={
status.HTTP_200_OK: OpenApiResponse(
serializers.EventStreamOutSerializer(many=True),
description="Return a list of eventstreams.",
),
},
)
def list(self, request, *args, **kwargs):
event_streams = models.EventStream.objects.all()
event_streams = self.filter_queryset(event_streams)
serializer = serializers.EventStreamOutSerializer(
event_streams, many=True
)
result = self.paginate_queryset(serializer.data)
logger.info(
logging_utils.generate_simple_audit_log(
"List",
"EventStream",
"*",
"*",
"*",
)
)
return self.get_paginated_response(result)
@extend_schema(
request=serializers.EventStreamInSerializer,
responses={
status.HTTP_201_CREATED: OpenApiResponse(
serializers.EventStreamOutSerializer,
description="Return the new event stream.",
),
status.HTTP_400_BAD_REQUEST: OpenApiResponse(
description="Invalid data to create event stream."
),
},
)
def create(self, request, *args, **kwargs):
context = {"request": request}
serializer = serializers.EventStreamInSerializer(
data=request.data,
context=context,
)
serializer.is_valid(raise_exception=True)
with transaction.atomic():
response = serializer.save()
check_related_permissions(
request.user,
serializer.Meta.model,
{},
model_to_dict(serializer.instance),
)
RoleDefinition.objects.give_creator_permissions(
request.user, serializer.instance
)
logger.info(
logging_utils.generate_simple_audit_log(
"Create",
resource_name,
response.name,
response.id,
response.organization,
)
)
return Response(
serializers.EventStreamOutSerializer(response).data,
status=status.HTTP_201_CREATED,
)
@extend_schema(
request=serializers.EventStreamInSerializer,
responses={
status.HTTP_200_OK: OpenApiResponse(
serializers.EventStreamOutSerializer,
description="Update successful, return the new event stream.",
),
status.HTTP_400_BAD_REQUEST: OpenApiResponse(
description="Unable to update event stream."
),
},
)
def partial_update(self, request, *args, **kwargs):
event_stream = self.get_object()
old_data = model_to_dict(event_stream)
context = {"request": request}
serializer = serializers.EventStreamInSerializer(
event_stream,
data=request.data,
context=context,
partial=True,
)
serializer.is_valid(raise_exception=True)
if event_stream.test_mode != request.data.get(
"test_mode", event_stream.test_mode
):
event_stream.test_content_type = ""
event_stream.test_content = ""
event_stream.test_headers = ""
event_stream.test_error_message = ""
for key, value in serializer.validated_data.items():
setattr(event_stream, key, value)
with transaction.atomic():
event_stream.save()
check_related_permissions(
request.user,
serializer.Meta.model,
old_data,
model_to_dict(event_stream),
)
logger.info(
logging_utils.generate_simple_audit_log(
"Update",
resource_name,
event_stream.name,
event_stream.id,
event_stream.organization,
)
)
return Response(
serializers.EventStreamOutSerializer(event_stream).data,
status=status.HTTP_200_OK,
)
@extend_schema(
description="List all activations for the event stream",
request=None,
responses={
status.HTTP_200_OK: serializers.ActivationListSerializer(
many=True
),
},
parameters=[
OpenApiParameter(
name="id",
type=int,
location=OpenApiParameter.PATH,
description="A unique integer value identifying this event stream.", # noqa: E501
)
],
)
@action(
detail=False,
queryset=models.Activation.objects.order_by("id"),
filterset_class=filters.ActivationFilter,
url_path="(?P<id>[^/.]+)/activations",
)
def activations(self, request, id):
if (
not models.EventStream.access_qs(request.user)
.filter(id=id)
.exists()
):
raise api_exc.NotFound(
code=status.HTTP_404_NOT_FOUND,
detail=f"Event stream with ID={id} does not exist.",
)
event_stream = models.EventStream.objects.get(id=id)
activations = event_stream.activations.all()
filtered_activations = self.filter_queryset(activations)
result = self.paginate_queryset(filtered_activations)
serializer = serializers.ActivationListSerializer(result, many=True)
logger.info(
logging_utils.generate_simple_audit_log(
"ListActivations",
resource_name,
event_stream.name,
event_stream.id,
event_stream.organization,
)
)
return self.get_paginated_response(serializer.data)