Skip to content

Commit 82a6ae1

Browse files
authored
Merge pull request #60 from svalabs/update_exclusions
to update exclusions do put-request
2 parents bdd1a04 + 848a80f commit 82a6ae1

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

plugins/module_utils/sentinelone/sentinelone_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def api_call(self, module: AnsibleModule, api_endpoint: str, http_method: str =
120120

121121
body = kwargs.get("body", {})
122122

123-
error_msg = kwargs.get("error_msg", "API call failed.")
123+
error_msg = f'{kwargs.get("error_msg", "API call failed.")} API-Endpoint: {api_endpoint}'
124124

125125
retry_count = 0
126126
try:

plugins/modules/sentinelone_path_exclusions.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,10 @@ def get_current_exclusions(self, current_group_ids: list, exclusion_path: str, m
371371
"""
372372

373373
api_url = self.api_endpoint_exclusions + (f"?siteIds={quote_plus(self.site_id)}&"
374-
f"value={quote_plus(exclusion_path)}&type=path")
374+
f"value={quote_plus(exclusion_path)}&"
375+
f"osTypes={quote_plus(self.os_type)}&"
376+
f"type=path"
377+
)
375378
if current_group_ids:
376379
# Scope is group level
377380
api_url += f"&groupIds={quote_plus(','.join(current_group_ids))}"
@@ -404,6 +407,26 @@ def delete_exclusions(self, module: AnsibleModule):
404407

405408
return response
406409

410+
def update_exclusions(self, module: AnsibleModule, exclusion_id):
411+
"""
412+
Update exclusions
413+
414+
:param module: Ansible module for error handling
415+
:type module: AnsibleModule
416+
:return: API response of the create query
417+
:rtype: dict
418+
"""
419+
api_url = self.api_endpoint_exclusions
420+
update_body = self.get_desired_state_exclusion_body()
421+
update_body['data']['id'] = exclusion_id
422+
error_msg = "Failed to update exclusions."
423+
response = self.api_call(module, api_url, "PUT", body=update_body, error_msg=error_msg)
424+
425+
if len(response['data']) == 0:
426+
module.fail_json(msg="Exclusions could not be updated - API result was empty")
427+
428+
return response
429+
407430
def create_exclusions(self, module: AnsibleModule):
408431
"""
409432
Create exclusions
@@ -504,7 +527,7 @@ def run_module():
504527
# Get name for group with group_id
505528
group_name = list(filter(lambda filterobj: filterobj[0] == group_id,
506529
current_group_ids_names))[0][1]
507-
diffs.append({'changes': dict(diff), 'groupId': group_id})
530+
diffs.append({'changes': dict(diff), 'groupId': group_id, "exclusion_id": current_exclusion['id']})
508531
basic_message.append(f"Exclusion exists in group {group_name} but is not up-to-date. "
509532
f"Updating exclusion.")
510533
else:
@@ -524,22 +547,32 @@ def run_module():
524547
current_exclusion = current_exclusions['data'][0]
525548
diff = exclusion_obj.merge_compare(current_exclusion, desired_state_exclusion['data'])[0]
526549
if diff:
527-
diffs.append({'changes': dict(diff), 'siteId': current_exclusion['scope']['siteIds']})
550+
diffs.append({'changes': dict(diff),
551+
'siteId': current_exclusion['scope']['siteIds'],
552+
'exclusion_id': current_exclusion['id']
553+
})
528554
basic_message.append(f"Exclusion exists in site {site_name} but is not up-to-date. "
529555
f"Updating exclusion.")
530556

531557
if diffs:
532-
# Delete Exclusions
533-
exclusion_obj.delete_exclusions(module)
558+
if diffs[0].get('exclusion_id'):
559+
# Update Exclusions
560+
exclusion_obj.update_exclusions(module, exclusion_id=diffs[0]['exclusion_id'])
534561

535-
# Create Exclusions
536-
exclusion_obj.create_exclusions(module)
562+
else:
563+
# Create Exclusions
564+
exclusion_obj.create_exclusions(module)
565+
566+
else:
567+
basic_message.append(f"Nothing to change, all desired changes are already set")
537568

538569
else:
539570
if current_exclusions['pagination']['totalItems'] != 0:
540571
# Exclusions should be deleted
541572
exclusion_obj.delete_exclusions(module)
542573
diffs.append({'changes': 'Deleted all exclusions in Scope'})
574+
else:
575+
basic_message.append(f"Nothing to change, exclusion does not exist")
543576

544577
result = dict(
545578
changed=False,

0 commit comments

Comments
 (0)