Skip to content

Commit

Permalink
SOARHELP-2878: Handle max limit in all scenarios(last page, not last …
Browse files Browse the repository at this point in the history
…page)
  • Loading branch information
ishans-crest committed Feb 12, 2024
1 parent 739cd36 commit df8733b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
2 changes: 1 addition & 1 deletion ciscoise.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"publisher": "Splunk",
"type": "network security",
"main_module": "ciscoise_connector.py",
"app_version": "3.0.5",
"app_version": "3.0.3",
"utctime_updated": "2022-03-11T04:16:56.000000Z",
"package_name": "phantom_ciscoise",
"product_vendor": "Cisco Systems",
Expand Down
56 changes: 20 additions & 36 deletions ciscoise_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class CiscoISEConnector(BaseConnector):
ACTION_ID_DELETE_POLICY = "delete_policy"

def __init__(self):
self.my_state = {}
# Call the BaseConnectors init first
super(CiscoISEConnector, self).__init__()

Expand Down Expand Up @@ -116,15 +115,15 @@ def make_another_call(*args, **kwargs):

return make_another_call

def _call_ers_api(self, endpoint, action_result, data=None, allow_unknown=True, method="get", try_ha_device=False):
def _call_ers_api(self, endpoint, action_result, data=None, allow_unknown=True, method="get", try_ha_device=False, params=None):
auth_method = self._ers_auth or self._auth
if not auth_method:
return action_result.set_status(phantom.APP_ERROR, CISCOISE_ERS_CRED_MISSING), None
url = "{0}{1}".format(self._base_url, endpoint)
if try_ha_device:
url = "{0}{1}".format(self._ha_device_url, endpoint)

self.debug_print("url = {}".format(url))
self.debug_print("url for calling an ERS API: {}".format(url))

ret_data = None

Expand All @@ -142,7 +141,8 @@ def _call_ers_api(self, endpoint, action_result, data=None, allow_unknown=True,
json=data,
verify=verify,
headers=headers,
auth=auth_method
auth=auth_method,
params=params
)

except Exception as e:
Expand Down Expand Up @@ -427,54 +427,38 @@ def _terminate_session(self, param):

return action_result.set_status(phantom.APP_SUCCESS, CISCOISE_SUCC_SESSION_TERMINATED)

def _paginator(self, endpoint, action_result, param=None, limit=None):
def _paginator(self, endpoint, action_result, limit=None):

items_list = list()

if not param:
payload = {}

count = 1
# param["size"] = DEFAULT_MAX_RESULTS
# param["page"] = page
params = {}
params["size"] = DEFAULT_MAX_RESULTS

while True:
ret_val, items = self._call_ers_api(endpoint, action_result, data=payload)
self.my_state[count] = items
ret_val, items = self._call_ers_api(endpoint, action_result, param=params)
if phantom.is_fail(ret_val):
self.debug_print("Saving state for Cisco ISE 1: {}".format(self.my_state))
self.save_state(self.my_state)
self.debug_print("Call to ERS API Failed")
return None

items_list.extend(items.get("SearchResult", {}).get("resources"))

items_from_page = items.get("SearchResult", {}).get("resources")
items_list.extend(items_from_page)
self.debug_print("Retrieved {} records from the endpoint {}".format(len(items_from_page), endpoint))
next_page_dict = items.get("SearchResult", {}).get("nextPage")

if next_page_dict is not None:
endpoint = next_page_dict.get("href").replace(self._base_url, "")
self.debug_print("endpoint = {}".format(endpoint))
self.debug_print("Next page available")
else:
break
if limit and len(items_list) >= limit:
self.debug_print("Reached to the final page and max limit reached")
return items_list[:limit]
else:
self.debug_print("Max limit not reached, but no more records left to retrieve")
return items_list

if limit and len(items_list) >= limit:
self.debug_print("Saving state for Cisco ISE 2: {}".format(self.my_state))
self.debug_print("Next page available. But max limit reached.")
self.save_state(self.my_state)
return items_list[:limit]

# if len(items.get("SearchResult", {}).get("resources")) < DEFAULT_MAX_RESULTS:
# break

if len(items_list) == items.get("SearchResult", {}).get("total"):
break

count = count + 1
# payload["page"] = page

self.debug_print("Saving state for Cisco ISE 3: {}".format(self.my_state))
self.save_state(self.my_state)

return items_list

def _list_resources(self, param):

action_result = self.add_action_result(ActionResult(dict(param)))
Expand Down
2 changes: 1 addition & 1 deletion ciscoise_consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
CISCOISE_ERROR_INVALID_PARAM = "Please provide a non-zero positive integer in {param}"
CISCOISE_MAP_IP_ABSENT_ERROR = "Please provide either mac address or ip address"
CISCOISE_ERS_CRED_MISSING = "ERS credentials in asset configuration are required for this action"
DEFAULT_MAX_RESULTS = 20
DEFAULT_MAX_RESULTS = 25

# Json reply schema
IS_MAC_QUARAN_RESP_SCHEMA = {
Expand Down

0 comments on commit df8733b

Please sign in to comment.