@@ -59,21 +59,32 @@ def add_service(**_):
59
59
60
60
new_heuristics = []
61
61
if heuristics :
62
+ plan = STORAGE .heuristic .get_bulk_plan ()
62
63
for index , heuristic in enumerate (heuristics ):
63
64
try :
64
65
# Append service name to heuristic ID
65
66
heuristic ['heur_id' ] = f"{ service .name .upper ()} .{ str (heuristic ['heur_id' ])} "
66
67
68
+ # Attack_id field is now a list, make it a list if we receive otherwise
69
+ attack_id = heuristic .get ('attack_id' , None )
70
+ if isinstance (attack_id , str ):
71
+ heuristic ['attack_id' ] = [attack_id ]
72
+
67
73
heuristic = Heuristic (heuristic )
68
- if not STORAGE .heuristic .get_if_exists (heuristic .heur_id ):
69
- STORAGE .heuristic .save (heuristic .heur_id , heuristic )
70
- STORAGE .heuristic .commit ()
71
- new_heuristics .append (heuristic .heur_id )
74
+ heuristic_id = heuristic .heur_id
75
+ plan .add_upsert_operation (heuristic_id , heuristic )
72
76
except Exception as e :
73
- raise ValueError (f"Error parsing heuristics: { str (e )} " )
77
+ raise ValueError ("Error parsing heuristics" )
78
+
79
+ for item in STORAGE .heuristic .bulk (plan )['items' ]:
80
+ if item ['update' ]['result' ] != "noop" :
81
+ new_heuristics .append (item ['update' ]['_id' ])
82
+
83
+ STORAGE .heuristic .commit ()
74
84
75
85
return make_api_response (dict (
76
- service_name = service .name
86
+ service_name = service .name ,
87
+ new_heuristics = new_heuristics
77
88
))
78
89
except ValueError as e : # Catch errors when building Service or Heuristic model(s)
79
90
return make_api_response ("" , err = str (e ), status_code = 400 )
0 commit comments