Skip to content

Commit 890a15b

Browse files
committed
fix wait_for_task if there is a list of tasks
1 parent d31ae70 commit 890a15b

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

plugins/module_utils/checkpoint.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
version=dict(type='str')
5151
)
5252

53-
delete_params = ['name', 'uid', 'layer', 'exception-group-name', 'layer', 'rule-name']
53+
delete_params = ['name', 'uid', 'layer', 'exception-group-name', 'rule-name', 'package']
5454

5555

5656
# parse failure message with code and response
@@ -201,8 +201,9 @@ def api_command(module, command):
201201
if 'task-id' in response:
202202
wait_for_task(module, version, connection, response['task-id'])
203203
elif 'tasks' in response:
204-
for task_id in response['tasks']:
205-
wait_for_task(module, version, connection, task_id)
204+
for task in response['tasks']:
205+
if 'task-id' in task:
206+
wait_for_task(module, version, connection, task['task-id'])
206207

207208
result[command] = response
208209
else:
@@ -299,6 +300,33 @@ def get_number_from_position(payload, connection, version):
299300
return int(position)
300301

301302

303+
# build the show rulebase payload
304+
def build_rulebase_payload(api_call_object, payload, position_number):
305+
rulebase_payload = {'name': payload['layer'], 'offset': position_number - 1, 'limit': 1}
306+
307+
if api_call_object == 'threat-exception':
308+
rulebase_payload['rule-name'] = payload['rule-name']
309+
310+
return rulebase_payload
311+
312+
313+
def build_rulebase_command(api_call_object):
314+
rulebase_command = 'show-' + api_call_object.split('-')[0] + '-rulebase'
315+
316+
if api_call_object == 'threat-exception':
317+
rulebase_command = 'show-threat-rule-exception-rulebase'
318+
319+
return rulebase_command
320+
321+
322+
# extract rule from rulebase response
323+
def extract_rule_from_rulebase_response(response):
324+
rule = response['rulebase'][0]
325+
while 'rulebase' in rule:
326+
rule = rule['rulebase'][0]
327+
return rule
328+
329+
302330
# is the param position (if the user inserted it) equals between the object and the user input
303331
def is_equals_with_position_param(payload, connection, version, api_call_object):
304332
position_number = get_number_from_position(payload, connection, version)
@@ -307,24 +335,17 @@ def is_equals_with_position_param(payload, connection, version, api_call_object)
307335
if position_number is None:
308336
return True
309337

310-
payload_for_show_access_rulebase = {'name': payload['layer'], 'offset': position_number - 1, 'limit': 1}
311-
rulebase_command = 'show-' + api_call_object.split('-')[0] + '-rulebase'
312-
313-
# if it's threat-exception, we change a little the payload and the command
314-
if api_call_object == 'threat-exception':
315-
payload_for_show_access_rulebase['rule-name'] = payload['rule-name']
316-
rulebase_command = 'show-threat-rule-exception-rulebase'
338+
rulebase_payload = build_rulebase_payload(api_call_object, payload, position_number)
339+
rulebase_command = build_rulebase_command(api_call_object)
317340

318-
code, response = send_request(connection, version, rulebase_command, payload_for_show_access_rulebase)
341+
code, response = send_request(connection, version, rulebase_command, rulebase_payload)
319342

320343
# if true, it means there is no rule in the position that the user inserted, so I return false, and when we will try to set
321344
# the rule, the API server will get throw relevant error
322345
if response['total'] < position_number:
323346
return False
324347

325-
rule = response['rulebase'][0]
326-
while 'rulebase' in rule:
327-
rule = rule['rulebase'][0]
348+
rule = extract_rule_from_rulebase_response(response)
328349

329350
# if the names of the exist rule and the user input rule are equals, then it's means that their positions are equals so I
330351
# return True. and there is no way that there is another rule with this name cause otherwise the 'equals' command would fail
@@ -360,6 +381,7 @@ def is_equals_with_all_params(payload, connection, version, api_call_object, is_
360381
exist_action = response['action']['name']
361382
if exist_action != payload['action']:
362383
return False
384+
# here the action is equals, so check the position param
363385
if not is_equals_with_position_param(payload, connection, version, api_call_object):
364386
return False
365387

0 commit comments

Comments
 (0)