Skip to content

Commit caf8006

Browse files
committed
Add Delete ETD Custom Module sample
1 parent c3cd421 commit caf8006

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

securitycenter/snippets_management_api/event_threat_detection_custom_modules.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
import time
1919
from typing import Dict
2020

21+
from google.api_core.exceptions import NotFound
2122
from google.cloud import securitycentermanagement_v1
22-
from google.protobuf.struct_pb2 import Struct
2323
from google.protobuf.field_mask_pb2 import FieldMask
24+
from google.protobuf.struct_pb2 import Struct
2425

2526

2627
# [START securitycenter_create_event_threat_detection_custom_module]
@@ -89,6 +90,7 @@ def create_event_threat_detection_custom_module(parent: str) -> Dict:
8990

9091
# [END securitycenter_create_event_threat_detection_custom_module]
9192

93+
9294
# [START securitycenter_get_event_threat_detection_custom_module]
9395
def get_event_threat_detection_custom_module(parent: str, module_id: str):
9496
"""
@@ -118,6 +120,7 @@ def get_event_threat_detection_custom_module(parent: str, module_id: str):
118120
raise e
119121
# [END securitycenter_get_event_threat_detection_custom_module]
120122

123+
121124
# [START securitycenter_list_event_threat_detection_custom_module]
122125
def list_event_threat_detection_custom_module(parent: str):
123126
"""
@@ -155,6 +158,7 @@ def list_event_threat_detection_custom_module(parent: str):
155158
raise e
156159
# [END securitycenter_list_event_threat_detection_custom_module]
157160

161+
158162
# [START securitycenter_update_event_threat_detection_custom_module]
159163
def update_event_threat_detection_custom_module(parent: str, module_id: str):
160164
"""
@@ -194,3 +198,32 @@ def update_event_threat_detection_custom_module(parent: str, module_id: str):
194198
raise
195199

196200
# [END securitycenter_update_event_threat_detection_custom_module]
201+
202+
203+
# [START securitycenter_delete_event_threat_detection_custom_module]
204+
def delete_event_threat_detection_custom_module(parent: str, module_id: str):
205+
"""
206+
Deletes an Event Threat Detection custom module.
207+
Args:
208+
parent: Use any one of the following options:
209+
- organizations/{organization_id}/locations/{location_id}
210+
- folders/{folder_id}/locations/{location_id}
211+
- projects/{project_id}/locations/{location_id}
212+
Returns:
213+
Message that Event Threat Detection custom module is deleted.
214+
Raises:
215+
NotFound: If the specified custom module does not exist.
216+
"""
217+
client = securitycentermanagement_v1.SecurityCenterManagementClient()
218+
219+
try:
220+
request = securitycentermanagement_v1.DeleteEventThreatDetectionCustomModuleRequest(
221+
name=f"{parent}/eventThreatDetectionCustomModules/{module_id}",
222+
)
223+
224+
client.delete_event_threat_detection_custom_module(request=request)
225+
print(f"Deleted Event Threat Detection Custom Module Successfully: {module_id}")
226+
except NotFound as e:
227+
print(f"Custom Module not found: {module_id}")
228+
raise e
229+
# [END securitycenter_delete_event_threat_detection_custom_module]

securitycenter/snippets_management_api/event_threat_detection_custom_modules_test.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ def test_create_event_threat_detection_custom_module():
131131
assert response.display_name.startswith(PREFIX)
132132
assert response.enablement_state == securitycentermanagement_v1.EventThreatDetectionCustomModule.EnablementState.ENABLED
133133

134+
134135
@backoff.on_exception(
135136
backoff.expo, (InternalServerError, ServiceUnavailable, NotFound), max_tries=3
136137
)
@@ -148,6 +149,7 @@ def test_get_event_threat_detection_custom_module():
148149
assert response.enablement_state == securitycentermanagement_v1.EventThreatDetectionCustomModule.EnablementState.ENABLED
149150
print(f"Retrieved Custom Module: {response.name}")
150151

152+
151153
@backoff.on_exception(
152154
backoff.expo, (InternalServerError, ServiceUnavailable, NotFound), max_tries=3
153155
)
@@ -172,6 +174,7 @@ def test_list_event_threat_detection_custom_module():
172174
== securitycentermanagement_v1.EventThreatDetectionCustomModule.EnablementState.ENABLED
173175
)
174176

177+
175178
@backoff.on_exception(
176179
backoff.expo, (InternalServerError, ServiceUnavailable, NotFound), max_tries=3
177180
)
@@ -187,3 +190,20 @@ def test_update_event_threat_detection_custom_module():
187190
# Verify that the custom module was created
188191
assert response.display_name.startswith(PREFIX)
189192
assert response.enablement_state == securitycentermanagement_v1.EventThreatDetectionCustomModule.EnablementState.DISABLED
193+
194+
195+
@backoff.on_exception(
196+
backoff.expo, (InternalServerError, ServiceUnavailable, NotFound), max_tries=3
197+
)
198+
def test_delete_event_threat_detection_custom_module():
199+
200+
module_name, module_id = add_custom_module(ORGANIZATION_ID)
201+
parent = f"organizations/{ORGANIZATION_ID}/locations/{LOCATION}"
202+
try:
203+
response = event_threat_detection_custom_modules.delete_event_threat_detection_custom_module(parent, module_id)
204+
except Exception as e:
205+
pytest.fail(f"delete_event_threat_detection_custom_module() failed: {e}")
206+
return
207+
assert response is None
208+
209+
print(f"Custom module was deleted successfully: {module_id}")

0 commit comments

Comments
 (0)