1- from _collections import OrderedDict
2-
31import click
42import py42 .sdk .queries .alerts .filters as f
53from c42eventextractor .extractors import AlertExtractor
1210import code42cli .cmds .search .options as searchopt
1311import code42cli .errors as errors
1412import code42cli .options as opt
13+ from code42cli .cmds .search import SendToCommand
1514from code42cli .cmds .search .cursor_store import AlertCursorStore
1615from code42cli .cmds .search .extraction import handle_no_events
16+ from code42cli .cmds .search .options import server_options
1717from code42cli .date_helper import convert_datetime_to_timestamp
1818from code42cli .date_helper import limit_date_range
19- from code42cli .logger import get_logger_for_server
2019from code42cli .options import format_option
21- from code42cli .options import server_options
2220from code42cli .output_formats import JsonOutputFormat
2321from code42cli .output_formats import OutputFormatter
2422
25- ALERTS_KEYWORD = "alerts"
26- SEARCH_DEFAULT_HEADER = OrderedDict ()
27- SEARCH_DEFAULT_HEADER ["name" ] = "RuleName"
28- SEARCH_DEFAULT_HEADER ["actor" ] = "Username"
29- SEARCH_DEFAULT_HEADER ["createdAt" ] = "ObservedDate"
30- SEARCH_DEFAULT_HEADER ["state" ] = "Status"
31- SEARCH_DEFAULT_HEADER ["severity" ] = "Severity"
32- SEARCH_DEFAULT_HEADER ["description" ] = "Description"
33-
3423
24+ ALERTS_KEYWORD = "alerts"
3525begin = opt .begin_option (
3626 ALERTS_KEYWORD ,
3727 callback = lambda ctx , param , arg : convert_datetime_to_timestamp (
4131end = opt .end_option (ALERTS_KEYWORD )
4232checkpoint = opt .checkpoint_option (ALERTS_KEYWORD )
4333advanced_query = searchopt .advanced_query_option (ALERTS_KEYWORD )
44-
45-
46- def search_options (f ):
47- f = checkpoint (f )
48- f = advanced_query (f )
49- f = end (f )
50- f = begin (f )
51- return f
52-
53-
5434severity_option = click .option (
5535 "--severity" ,
5636 multiple = True ,
@@ -147,16 +127,34 @@ def search_options(f):
147127 callback = searchopt .contains_filter (f .Description ),
148128 help = "Filter alerts by description. Does fuzzy search by default." ,
149129)
150-
151130send_to_format_options = click .option (
152131 "-f" ,
153132 "--format" ,
154133 type = click .Choice (JsonOutputFormat (), case_sensitive = False ),
155134 help = "The output format of the result. Defaults to json format." ,
156- default = JsonOutputFormat .JSON ,
135+ default = JsonOutputFormat .RAW ,
157136)
158137
159138
139+ def _get_search_default_header ():
140+ return {
141+ "name" : "RuleName" ,
142+ "actor" : "Username" ,
143+ "createdAt" : "ObservedDate" ,
144+ "state" : "Status" ,
145+ "severity" : "Severity" ,
146+ "description" : "Description" ,
147+ }
148+
149+
150+ def search_options (f ):
151+ f = checkpoint (f )
152+ f = advanced_query (f )
153+ f = end (f )
154+ f = begin (f )
155+ return f
156+
157+
160158def alert_options (f ):
161159 f = actor_option (f )
162160 f = actor_contains_option (f )
@@ -231,7 +229,7 @@ def search(
231229):
232230 """Search for alerts."""
233231 output_header = ext .try_get_default_header (
234- include_all , SEARCH_DEFAULT_HEADER , format
232+ include_all , _get_search_default_header () , format
235233 )
236234 formatter = OutputFormatter (format , output_header )
237235 cursor = _get_alert_cursor_store (cli_state .profile .name ) if use_checkpoint else None
@@ -247,7 +245,7 @@ def search(
247245 handle_no_events (not handlers .TOTAL_EVENTS and not errors .ERRORED )
248246
249247
250- @alerts .command ()
248+ @alerts .command (cls = SendToCommand )
251249@alert_options
252250@search_options
253251@click .option (
@@ -262,31 +260,23 @@ def search(
262260 help = "Display simple properties of the primary level of the nested response." ,
263261)
264262@send_to_format_options
265- def send_to (
266- cli_state ,
267- format ,
268- hostname ,
269- protocol ,
270- begin ,
271- end ,
272- advanced_query ,
273- use_checkpoint ,
274- or_query ,
275- ** kwargs
276- ):
263+ def send_to (cli_state , begin , end , advanced_query , use_checkpoint , or_query , ** kwargs ):
277264 """Send alerts to the given server address.
278265
279266 HOSTNAME format: address:port where port is optional and defaults to 514.
280267 """
281- logger = get_logger_for_server (hostname , protocol , format )
282- cursor = _get_alert_cursor_store (cli_state .profile .name ) if use_checkpoint else None
268+ cursor = _get_cursor (cli_state , use_checkpoint )
283269 handlers = ext .create_send_to_handlers (
284- cli_state .sdk , AlertExtractor , cursor , use_checkpoint , logger ,
270+ cli_state .sdk , AlertExtractor , cursor , use_checkpoint , cli_state . logger ,
285271 )
286272 _call_extractor (cli_state , handlers , begin , end , or_query , advanced_query , ** kwargs )
287273 handle_no_events (not handlers .TOTAL_EVENTS and not errors .ERRORED )
288274
289275
276+ def _get_cursor (state , use_checkpoint ):
277+ return _get_alert_cursor_store (state .profile .name ) if use_checkpoint else None
278+
279+
290280def _get_alert_extractor (sdk , handlers ):
291281 return AlertExtractor (sdk , handlers )
292282
0 commit comments