Skip to content

Commit 4766db2

Browse files
authored
Merge pull request #25 from chkp-ameera/master
fix Idempotency and some other ansible requests
2 parents 08b1706 + 140286f commit 4766db2

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

plugins/module_utils/checkpoint.py

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@
3333
import time
3434

3535
from ansible.module_utils.connection import Connection
36+
from enum import Enum
3637

38+
# class syntax
39+
class RequestType(Enum):
40+
BEFORE_REQUEST = 1
41+
AFTER_REQUEST = 2
3742

3843
checkpoint_argument_spec_for_async = dict(
3944
wait_for_task=dict(type='bool', default=True)
@@ -69,7 +74,10 @@ def idempotency_check(old_val, new_val):
6974

7075
# if user insert a specific version, we add it to the url
7176
def get_version(module):
72-
return ('v' + module.params['version'] + '/') if module.params.get('version') else ''
77+
res = ('v' + module.params['version'] + '/') if module.params.get('version') else ''
78+
del module.params['version']
79+
return res
80+
7381

7482

7583
# send the request to checkpoint
@@ -81,28 +89,38 @@ def send_request(connection, version, url, payload=None):
8189
# get the payload from the user parameters
8290
def is_checkpoint_param(parameter):
8391
if parameter == 'state' or \
84-
parameter == 'wait_for_task' or \
85-
parameter == 'version':
92+
parameter == 'wait_for_task':
8693
return False
8794
return True
8895

8996

9097
# build the payload from the parameters which has value (not None), and they are parameter of checkpoint API as well
91-
def replace_chkp_params(params, old, new):
98+
def replace_chkp_params(params, request_type):
9299
payload = {}
93-
# we used a dedicated 'msg' parametr because we can not use 'message' parameter
94-
# as 'message' is used internally in Ansible Core engine
95-
if "msg" in params:
96-
params["message"] = params.pop("msg")
100+
old = ""
101+
new = ""
102+
if request_type == RequestType.BEFORE_REQUEST:
103+
old = "_"
104+
new = "-"
105+
# we used a dedicated 'msg' parametr because we can not use 'message' parameter
106+
# as 'message' is used internally in Ansible Core engine
107+
if "msg" in params:
108+
params["message"] = params.pop("msg")
109+
elif request_type == RequestType.AFTER_REQUEST:
110+
old = "-"
111+
new = "_"
112+
if "message" in params:
113+
params["msg"] = params.pop("message")
114+
97115
for parameter in params:
98116
parameter_value = params[parameter]
99117
if parameter_value is not None and is_checkpoint_param(parameter):
100118
if isinstance(parameter_value, dict):
101-
payload[parameter.replace(old, new)] = replace_chkp_params(parameter_value, old, new)
119+
payload[parameter.replace(old, new)] = replace_chkp_params(parameter_value, request_type)
102120
elif isinstance(parameter_value, list) and len(parameter_value) != 0 and isinstance(parameter_value[0], dict):
103121
payload_list = []
104122
for element_dict in parameter_value:
105-
payload_list.append(replace_chkp_params(element_dict, old, new))
123+
payload_list.append(replace_chkp_params(element_dict, request_type))
106124
payload[parameter.replace(old, new)] = payload_list
107125
else:
108126
payload[parameter.replace(old, new)] = parameter_value
@@ -158,11 +176,11 @@ def wait_for_task(module, version, task_id):
158176

159177
# handle api call
160178
def api_call(module, target_version, api_call_object):
161-
payload = replace_chkp_params(module.params, "_", "-")
179+
payload = replace_chkp_params(module.params, RequestType.BEFORE_REQUEST)
162180
connection = Connection(module._socket_path)
163181
code, response = send_request(connection, target_version, api_call_object, payload)
164182

165-
response = replace_chkp_params(response, "-", "_")
183+
response = replace_chkp_params(response, RequestType.AFTER_REQUEST)
166184
return code, response
167185

168186

@@ -225,17 +243,21 @@ def chkp_api_call(module, api_call_object, has_add_api, ignore=None, show_params
225243
if not is_checkpoint_param(key):
226244
del params_dict[key]
227245

228-
if idempotency_check(res, params_dict) is True:
229-
return {
230-
api_call_object.replace('-', '_'): res,
231-
"changed": False
232-
}
233-
code, res = api_call(module, target_version, api_call_object="set-{0}".format(api_call_object))
234-
if code != 200 and has_add_api is True:
235-
if add_params:
236-
[module.params.pop(key) for key in show_params if key not in add_params]
237-
module.params.update(add_params)
238-
code, res = api_call(module, target_version, api_call_object="add-{0}".format(api_call_object))
246+
if code == 200:
247+
if idempotency_check(res, params_dict) is True:
248+
return {
249+
api_call_object.replace('-', '_'): res,
250+
"changed": False
251+
}
252+
code, res = api_call(module, target_version, api_call_object="set-{0}".format(api_call_object))
253+
else:
254+
if has_add_api is True:
255+
if add_params:
256+
[module.params.pop(key) for key in show_params if key not in add_params]
257+
module.params.update(add_params)
258+
code, res = api_call(module, target_version, api_call_object="add-{0}".format(api_call_object))
259+
else: # some requests like static-route don't have add, try set instead
260+
code, res = api_call(module, target_version, api_call_object="set-{0}".format(api_call_object))
239261

240262
if code != 200:
241263
module.fail_json(msg=parse_fail_message(code, res))

plugins/modules/cp_gaia_static_route.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def main():
154154
api_call_object = 'static-route'
155155
show_params = ["address", "mask_length"]
156156

157-
res = chkp_api_call(module, api_call_object, True, show_params=show_params)
157+
res = chkp_api_call(module, api_call_object, False, show_params=show_params)
158158
module.exit_json(**res)
159159

160160

0 commit comments

Comments
 (0)