11#!/usr/bin/env python3
22
3- # Copyright 2025 Google LLC
3+ # Copyright 2024 Google LLC
44#
55# Licensed under the Apache License, Version 2.0 (the "License");
66# you may not use this file except in compliance with the License.
1414# See the License for the specific language governing permissions and
1515# limitations under the License.
1616#
17- # pylint: disable=line-too-long
18- r"""Executable and reusable v1alpha API sample for bulk updating alerts.
17+ r"""Executable and reusable sample for bulk updating alerts.
1918
19+ The file provided to the --alert_ids_file parameter should have one alert
20+ ID per line like so:
21+ ```
22+ de_ad9d2771-a567-49ee-6452-1b2db13c1d33
23+ de_3c2e2556-aba1-a253-7518-b4ddb666cc32
24+ ```
2025Usage:
21- python -m detect .v1alpha.bulk_update_alerts \
22- --project_id=<PROJECT_ID> \
26+ python -m alerts .v1alpha.bulk_update_alerts \
27+ --project_id=<PROJECT_ID> \
2328 --project_instance=<PROJECT_INSTANCE> \
24- --region=<REGION> \
2529 --alert_ids_file=<PATH_TO_FILE> \
26- --status=CLOSED \
27- --reason=REASON_MAINTENANCE
28-
29- # The alert_ids_file should contain one alert ID per line:
30- # de_ad9d2771-a567-49ee-6452-1b2db13c1d33
31- # de_3c2e2556-aba1-a253-7518-b4ddb666cc32
30+ --confidence_score=<CONFIDENCE_SCORE> \
31+ --priority=<PRIORITY> \
32+ --reason=<REASON> \
33+ --reputation=<REPUTATION> \
34+ --priority=<PRIORITY> \
35+ --status=<STATUS> \
36+ --verdict=<VERDICT> \
37+ --risk_score=<RISK_SCORE> \
38+ --disregarded=<DISREGARDED> \
39+ --severity=<SEVERITY> \
40+ --comment=<COMMENT> \
41+ --root_cause=<ROOT_CAUSE> \
42+ --severity_display=<SEVERITY_DISPLAY>
3243
44+ # pylint: disable=line-too-long
3345API reference:
34- https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.legacy/legacyUpdateAlert
35- https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Priority
36- https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Reason
37- https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Reputation
38- https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Status
39- https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Verdict
46+ https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.legacy/legacyUpdateAlert
47+ https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Priority
48+ https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Reason
49+ https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Reputation
50+ https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Priority
51+ https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Status
52+ https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/Noun#Verdict
4053"""
4154# pylint: enable=line-too-long
4255
43- import argparse
4456import json
4557
4658from common import chronicle_auth
47- from common import project_id
48- from common import project_instance
49- from common import regions
50- from google .auth .transport import requests
5159
5260from . import update_alert
5361
62+
5463CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com"
5564SCOPES = [
5665 "https://www.googleapis.com/auth/cloud-platform" ,
5766]
58-
5967DEFAULT_FEEDBACK = {
6068 "comment" : "automated cleanup" ,
6169 "reason" : "REASON_MAINTENANCE" ,
6573 "verdict" : "VERDICT_UNSPECIFIED" ,
6674}
6775
76+
6877if __name__ == "__main__" :
6978 parser = update_alert .get_update_parser ()
70- # local
71- parser .add_argument ("--alert_ids_file" ,
72- type = str ,
73- required = True ,
74- help = "Path to file containing one alert ID per line" )
75-
76- # Set default values from DEFAULT_FEEDBACK
79+ parser .add_argument (
80+ "--alert_ids_file" , type = str , required = True ,
81+ help = "File with one alert ID per line."
82+ )
7783 parser .set_defaults (
7884 comment = DEFAULT_FEEDBACK ["comment" ],
7985 reason = DEFAULT_FEEDBACK ["reason" ],
8288 status = DEFAULT_FEEDBACK ["status" ],
8389 verdict = DEFAULT_FEEDBACK ["verdict" ],
8490 )
85-
8691 args = parser .parse_args ()
8792
88- # Validate required arguments
93+ # raise error if required args are not present
8994 update_alert .check_args (parser , args )
9095
91- auth_session = chronicle_auth .initialize_http_session (args .credentials_file ,
92- SCOPES )
93-
94- with open (args .alert_ids_file ) as alert_file :
95- for alert_id in alert_file :
96- result = update_alert .update_alert (
96+ auth_session = chronicle_auth .initialize_http_session (
97+ args .credentials_file ,
98+ SCOPES ,
99+ )
100+ with open (args .alert_ids_file ) as fh :
101+ for alert_id in fh :
102+ a_list = update_alert .update_alert (
97103 auth_session ,
98104 args .project_id ,
99105 args .project_instance ,
111117 args .comment ,
112118 args .root_cause ,
113119 )
114- print (json .dumps (result , indent = 2 ))
120+ print (json .dumps (a_list , indent = 2 ))
0 commit comments