11
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
+ import logging
15
+ from typing import Union
16
+
17
+ import yaml
14
18
from rest_framework import serializers
15
19
20
+ from aap_eda .api .constants import PG_NOTIFY_TEMPLATE_RULEBOOK_DATA
21
+ from aap_eda .api .exceptions import InvalidEventStreamRulebook
16
22
from aap_eda .api .serializers .credential import CredentialSerializer
17
23
from aap_eda .api .serializers .decision_environment import (
18
24
DecisionEnvironmentRefSerializer ,
19
25
)
26
+ from aap_eda .api .serializers .event_stream import EventStreamOutSerializer
20
27
from aap_eda .api .serializers .project import (
21
28
ExtraVarRefSerializer ,
22
29
ProjectRefSerializer ,
23
30
)
24
31
from aap_eda .api .serializers .rulebook import RulebookRefSerializer
32
+ from aap_eda .api .serializers .utils import (
33
+ substitute_extra_vars ,
34
+ substitute_source_args ,
35
+ swap_sources ,
36
+ )
25
37
from aap_eda .core import models , validators
38
+ from aap_eda .core .enums import ProcessParentType
39
+
40
+ logger = logging .getLogger (__name__ )
41
+
42
+
43
+ def _updated_ruleset (validated_data ):
44
+ try :
45
+ sources_info = []
46
+
47
+ for event_stream_id in validated_data ["event_streams" ]:
48
+ event_stream = models .EventStream .objects .get (id = event_stream_id )
49
+
50
+ if event_stream .rulebook :
51
+ rulesets = yaml .safe_load (event_stream .rulebook .rulesets )
52
+ else :
53
+ rulesets = yaml .safe_load (PG_NOTIFY_TEMPLATE_RULEBOOK_DATA )
54
+
55
+ extra_vars = rulesets [0 ]["sources" ][0 ].get ("extra_vars" , {})
56
+ encrypt_vars = rulesets [0 ]["sources" ][0 ].get ("encrypt_vars" , [])
57
+
58
+ # TODO: encrypt password when engine is ready for vaulted data
59
+ extra_vars = substitute_extra_vars (
60
+ event_stream .__dict__ , extra_vars , encrypt_vars , "password"
61
+ )
62
+
63
+ source = rulesets [0 ]["sources" ][0 ]["complementary_source" ]
64
+ source = substitute_source_args (
65
+ event_stream .__dict__ , source , extra_vars
66
+ )
67
+ sources_info .append (source )
68
+
69
+ return swap_sources (validated_data ["rulebook_rulesets" ], sources_info )
70
+ except Exception as e :
71
+ logger .error (f"Failed to update rulesets: { e } " )
72
+ raise InvalidEventStreamRulebook (e )
26
73
27
74
28
75
class ActivationSerializer (serializers .ModelSerializer ):
@@ -34,6 +81,12 @@ class ActivationSerializer(serializers.ModelSerializer):
34
81
child = CredentialSerializer (),
35
82
)
36
83
84
+ event_streams = serializers .ListField (
85
+ required = False ,
86
+ allow_null = True ,
87
+ child = EventStreamOutSerializer (),
88
+ )
89
+
37
90
class Meta :
38
91
model = models .Activation
39
92
fields = [
@@ -57,6 +110,7 @@ class Meta:
57
110
"status_message" ,
58
111
"awx_token_id" ,
59
112
"credentials" ,
113
+ "event_streams" ,
60
114
]
61
115
read_only_fields = [
62
116
"id" ,
@@ -77,6 +131,12 @@ class ActivationListSerializer(serializers.ModelSerializer):
77
131
child = CredentialSerializer (),
78
132
)
79
133
134
+ event_streams = serializers .ListField (
135
+ required = False ,
136
+ allow_null = True ,
137
+ child = EventStreamOutSerializer (),
138
+ )
139
+
80
140
class Meta :
81
141
model = models .Activation
82
142
fields = [
@@ -100,6 +160,7 @@ class Meta:
100
160
"status_message" ,
101
161
"awx_token_id" ,
102
162
"credentials" ,
163
+ "event_streams" ,
103
164
]
104
165
read_only_fields = ["id" , "created_at" , "modified_at" ]
105
166
@@ -111,6 +172,10 @@ def to_representation(self, activation):
111
172
CredentialSerializer (credential ).data
112
173
for credential in activation .credentials .all ()
113
174
]
175
+ event_streams = [
176
+ EventStreamOutSerializer (event_stream ).data
177
+ for event_stream in activation .event_streams .all ()
178
+ ]
114
179
115
180
return {
116
181
"id" : activation .id ,
@@ -133,6 +198,7 @@ def to_representation(self, activation):
133
198
"status_message" : activation .status_message ,
134
199
"awx_token_id" : activation .awx_token_id ,
135
200
"credentials" : credentials ,
201
+ "event_streams" : event_streams ,
136
202
}
137
203
138
204
@@ -152,6 +218,7 @@ class Meta:
152
218
"restart_policy" ,
153
219
"awx_token_id" ,
154
220
"credentials" ,
221
+ "event_streams" ,
155
222
]
156
223
157
224
rulebook_id = serializers .IntegerField (
@@ -177,6 +244,12 @@ class Meta:
177
244
allow_null = True ,
178
245
child = serializers .IntegerField (),
179
246
)
247
+ event_streams = serializers .ListField (
248
+ required = False ,
249
+ allow_null = True ,
250
+ child = serializers .IntegerField (),
251
+ validators = [validators .check_if_event_streams_exists ],
252
+ )
180
253
181
254
def validate (self , data ):
182
255
user = data ["user" ]
@@ -200,6 +273,10 @@ def create(self, validated_data):
200
273
validated_data ["rulebook_rulesets" ] = rulebook .rulesets
201
274
validated_data ["git_hash" ] = rulebook .project .git_hash
202
275
validated_data ["project_id" ] = rulebook .project .id
276
+ if validated_data .get ("event_streams" ):
277
+ validated_data ["rulebook_rulesets" ] = _updated_ruleset (
278
+ validated_data
279
+ )
203
280
return super ().create (validated_data )
204
281
205
282
@@ -215,6 +292,7 @@ class Meta:
215
292
"git_hash" ,
216
293
"status_message" ,
217
294
"activation_id" ,
295
+ "event_stream_id" ,
218
296
"started_at" ,
219
297
"ended_at" ,
220
298
]
@@ -243,6 +321,11 @@ class ActivationReadSerializer(serializers.ModelSerializer):
243
321
rules_count = serializers .IntegerField ()
244
322
rules_fired_count = serializers .IntegerField ()
245
323
restarted_at = serializers .DateTimeField (required = False , allow_null = True )
324
+ event_streams = serializers .ListField (
325
+ required = False ,
326
+ allow_null = True ,
327
+ child = EventStreamOutSerializer (),
328
+ )
246
329
247
330
class Meta :
248
331
model = models .Activation
@@ -270,6 +353,7 @@ class Meta:
270
353
"status_message" ,
271
354
"awx_token_id" ,
272
355
"credentials" ,
356
+ "event_streams" ,
273
357
]
274
358
read_only_fields = ["id" , "created_at" , "modified_at" , "restarted_at" ]
275
359
@@ -297,7 +381,8 @@ def to_representation(self, activation):
297
381
else None
298
382
)
299
383
activation_instances = models .RulebookProcess .objects .filter (
300
- activation_id = activation .id
384
+ activation_id = activation .id ,
385
+ parent_type = ProcessParentType .ACTIVATION ,
301
386
)
302
387
rules_count , rules_fired_count = get_rules_count (
303
388
activation .ruleset_stats
@@ -315,6 +400,11 @@ def to_representation(self, activation):
315
400
for credential in activation .credentials .all ()
316
401
]
317
402
403
+ event_streams = [
404
+ EventStreamOutSerializer (event_stream ).data
405
+ for event_stream in activation .event_streams .all ()
406
+ ]
407
+
318
408
return {
319
409
"id" : activation .id ,
320
410
"name" : activation .name ,
@@ -341,6 +431,7 @@ def to_representation(self, activation):
341
431
"status_message" : activation .status_message ,
342
432
"awx_token_id" : activation .awx_token_id ,
343
433
"credentials" : credentials ,
434
+ "event_streams" : event_streams ,
344
435
}
345
436
346
437
@@ -356,7 +447,9 @@ class PostActivationSerializer(serializers.ModelSerializer):
356
447
allow_null = True ,
357
448
validators = [validators .check_if_extra_var_exists ],
358
449
)
450
+ # TODO: is_activation_valid needs to tell event stream/activation
359
451
awx_token_id = serializers .IntegerField (
452
+ required = False ,
360
453
allow_null = True ,
361
454
validators = [validators .check_if_awx_token_exists ],
362
455
)
@@ -418,8 +511,11 @@ def parse_validation_errors(errors: dict) -> str:
418
511
return str (messages )
419
512
420
513
421
- def validate_rulebook_token (rulebook_id : int ) -> None :
514
+ def validate_rulebook_token (rulebook_id : Union [ int , None ] ) -> None :
422
515
"""Validate if the rulebook requires an Awx Token."""
516
+ if rulebook_id is None :
517
+ return
518
+
423
519
rulebook = models .Rulebook .objects .get (id = rulebook_id )
424
520
425
521
# TODO: rulesets are stored as a string in the rulebook model
0 commit comments