Skip to content

Commit

Permalink
Increased request timeout and implemented error handling for requests…
Browse files Browse the repository at this point in the history
… that timed out. (#12)
  • Loading branch information
mwester117 authored Mar 13, 2023
1 parent 121b90c commit c8874ce
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 15 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ Sva.Sentinelone Release Notes
.. contents:: Topics


v1.0.3
======

Release Summary
---------------

Increased request timeout and implemented error handling for requests that timed out.

v1.0.2
======

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ See [Ansible Using collections](https://docs.ansible.com/ansible/devel/user_guid
The module documentation can be found [here](https://svalabs.github.io/ansible-collection-sva.sentinelone/branch/main/collections/index_module.html).

## Changelog
**v1.0.3**: Increased request timeout and implemented error handling for requests that timed out.

**v1.0.2**: Added detailed error message to module output if an API call fails

**v1.0.1**: Bugfix release
Expand Down
2 changes: 1 addition & 1 deletion changelogs/.plugin-cache.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ plugins:
shell: {}
strategy: {}
vars: {}
version: 1.0.2
version: 1.0.3
7 changes: 7 additions & 0 deletions changelogs/changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,10 @@ releases:
fragments:
- v1.0.2.yml
release_date: '2023-03-08'
1.0.3:
changes:
release_summary: Increased request timeout and implemented error handling for
requests that timed out.
fragments:
- v1.0.3.yml
release_date: '2023-03-13'
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace: "sva"
name: "sentinelone"

# The version of the collection. Must be compatible with semantic versioning
version: "1.0.2"
version: "1.0.3"

# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
readme: "README.md"
Expand Down
13 changes: 9 additions & 4 deletions plugins/module_utils/sentinelone/sentinelone_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def api_call(self, module: AnsibleModule, api_endpoint: str, http_method: str =
:rtype: dict
"""

request_timeout = 120
retries = 3
retry_pause = 3

Expand All @@ -131,12 +132,16 @@ def api_call(self, module: AnsibleModule, api_endpoint: str, http_method: str =
if body:
body_json = json.dumps(body)
response_raw, response_info = fetch_url(module, api_endpoint, headers=headers, data=body_json,
method=http_method)
method=http_method, timeout=request_timeout)
else:
response_raw, response_info = fetch_url(module, api_endpoint, headers=headers,
method=http_method)
method=http_method, timeout=request_timeout)

status_code = response_info['status']
if status_code >= 400:
if status_code == -1:
# If the request runtime exceeds the timout
module.exit_json(msg=f"{error_msg} Error: {response_info['msg']} after {request_timeout}s.")
elif status_code >= 400:
response_unparsed = response_info['body'].decode('utf-8')
response = json.loads(response_unparsed)
raise response_raw
Expand All @@ -152,7 +157,7 @@ def api_call(self, module: AnsibleModule, api_endpoint: str, http_method: str =
module.fail_json(msg=f"API response is no valid JSON. Error: {str(err)}")
except urllib_error.HTTPError as err:
module.fail_json(
msg=f"{error_msg} Status code: {err.code} {err.reason}. Response body: {response_unparsed}")
msg=f"{error_msg} Status code: {err.code} {err.reason}. Error: {response_unparsed}")

return response

Expand Down
24 changes: 15 additions & 9 deletions plugins/modules/sentinelone_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,21 @@ def get_desired_state_group_body(self, group_name: str):

return desired_state_groups

def create_group(self, create_body: dict, module: AnsibleModule):
def create_group(self, create_body: dict, error_msg: str, module: AnsibleModule):
"""
API call to create a group
:param create_body: Body for the create query
:type create_body: dict
:param error_msg: Message used if an API request failes
:type error_msg: str
:param module: Ansible module for error handling
:type module: AnsibleModule
:return: create query response object
:rtype: dict
"""

api_url = self.api_endpoint_groups
error_msg = "Failed to create group."
response = self.api_call(module, api_url, "POST", body=create_body, error_msg=error_msg)

if response['data']['name'] != create_body['data']['name']:
Expand All @@ -237,22 +238,23 @@ def create_group(self, create_body: dict, module: AnsibleModule):

return response

def update_group(self, update_body: dict, groupid: str, module: AnsibleModule):
def update_group(self, update_body: dict, groupid: str, error_msg: str, module: AnsibleModule):
"""
API call to update a group
:param update_body: Body for the update query
:type update_body: dict
:param groupid: ID of the group which should be updated
:type groupid: str
:param error_msg: Message used if an API request failes
:type error_msg: str
:param module: Ansible module for error handling
:type module: AnsibleModule
:return: API response of the update query
:rtype: dict
"""

api_url = f"{self.api_endpoint_groups}/{groupid}"
error_msg = "Failed to update group."
response = self.api_call(module, api_url, "PUT", body=update_body, error_msg=error_msg)

if response['data']['name'] != update_body['data']['name']:
Expand All @@ -261,20 +263,21 @@ def update_group(self, update_body: dict, groupid: str, module: AnsibleModule):

return response

def delete_group(self, group_id: str, module: AnsibleModule):
def delete_group(self, group_id: str, error_msg: str, module: AnsibleModule):
"""
API call to delete a group
:param group_id: ID of the group which should be deleted
:type group_id: str
:type error_msg: Message used if an API request failes
:type error_msg: str
:param module: Ansible module for error handling
:type module: AnsibleModule
:return: API response of the delete-query
:rtype: dict
"""

api_url = f"{self.api_endpoint_groups}/{group_id}"
error_msg = "Failed to delete group."
response = self.api_call(module, api_url, "DELETE", error_msg=error_msg)

if not response['data']['success']:
Expand Down Expand Up @@ -349,14 +352,16 @@ def run_module():
# Inheritance and policy should not be maintained by this module.
# Removing the key because if not the module will update the inherintance property
del desired_state_group['data']['inherits']
groups_obj.update_group(desired_state_group, group_id, module)
error_msg = f"Failed to update group {group_name}."
groups_obj.update_group(desired_state_group, group_id, error_msg, module)
basic_message.append(f"Group {group_name} updated")
diffs.append({'changes': dict(diff), 'groupName': group_name})
else:
basic_message.append("Can not convert dynamic to static group and vice versa. Nothing changed.")
else:
# Group does not exist. Creating the group
groups_obj.create_group(desired_state_group, module)
error_msg = f"Failed to create group {group_name}."
groups_obj.create_group(desired_state_group, error_msg, module)
basic_message.append(f"Group {group_name} created.")
diffs.append({'changes': "Group created", 'groupName': group_name})
else:
Expand All @@ -366,8 +371,9 @@ def run_module():
current_group = list(filter(lambda filterobj: filterobj['name'] == group_name, current_groups))
if current_group:
# if group exists delete it
error_msg = f"Failed to delete group {group_name}."
group_id = current_group[0]['id']
groups_obj.delete_group(group_id, module)
groups_obj.delete_group(group_id, error_msg, module)
basic_message.append(f"Group {group_name} deleted.")
diffs.append({'changes': 'Group deleted', 'groupName': group_name})

Expand Down

0 comments on commit c8874ce

Please sign in to comment.