Skip to content

Commit c91a1e9

Browse files
authored
Merge pull request #22 from chkp-ameera/master
add the ability to send gaia_api version within the request and add idempotincy check
2 parents ab125c2 + 90879da commit c91a1e9

File tree

76 files changed

+606
-248
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+606
-248
lines changed

CHANGELOG.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@ Check_Point.gaia Release Notes
44

55
.. contents:: Topics
66

7+
v4.1.0
8+
======
9+
10+
Release Summary
11+
---------------
12+
13+
this release 4.1.0 of ``check_point.gaia``, released on 2022-09-21.
14+
15+
Minor Changes
16+
---------------
17+
18+
add the ability to send gaia_api version within the request.
19+
20+
Bugfixes
21+
---------------
22+
23+
add idempotincy check before each present request. do not send the request if it the same configuration in the machine.
724

825
v4.0.0
926
======

galaxy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ dependencies: {}
4848
repository: https://github.com/CheckPointSW/CheckPointAnsibleGAIACollection
4949

5050
# The URL to any online docs
51-
documentation: https://docs.ansible.com/ansible/latest/collections/check_point/gaia/index.html
51+
documentation: https://github.com/CheckPointSW/CheckPointAnsibleGAIACollection/tree/master/docs/build/html/index.html
5252

5353
# The URL to the homepage of the collection/project
5454
homepage: https://github.com/CheckPointSW/CheckPointAnsibleGAIACollection

plugins/module_utils/checkpoint.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,39 @@
3939
wait_for_task=dict(type='bool', default=True)
4040
)
4141

42+
checkpoint_argument_spec_for_all = dict(
43+
version=dict(type='str')
44+
)
45+
4246

4347
# parse failure message with code and response
4448
def parse_fail_message(code, response):
4549
return 'Checkpoint device returned error {0} with message {1}'.format(code, response)
4650

4751

52+
def idempotency_check(old_val, new_val):
53+
if isinstance(new_val, dict):
54+
for key in new_val:
55+
if key in old_val:
56+
if idempotency_check(old_val[key], new_val[key]) is False:
57+
return False
58+
elif isinstance(new_val, list):
59+
if len(new_val) != len(old_val):
60+
return False
61+
for item in new_val:
62+
if item not in old_val:
63+
return False
64+
else:
65+
if new_val != old_val:
66+
return False
67+
return True
68+
69+
70+
# if user insert a specific version, we add it to the url
71+
def get_version(module):
72+
return ('v' + module.params['version'] + '/') if module.params.get('version') else ''
73+
74+
4875
# send the request to checkpoint
4976
def send_request(connection, version, url, payload=None):
5077
code, response = connection.send_request('/gaia_api/' + version + url, payload)
@@ -54,7 +81,8 @@ def send_request(connection, version, url, payload=None):
5481
# get the payload from the user parameters
5582
def is_checkpoint_param(parameter):
5683
if parameter == 'state' or \
57-
parameter == 'wait_for_task':
84+
parameter == 'wait_for_task' or \
85+
parameter == 'version':
5886
return False
5987
return True
6088

@@ -138,7 +166,8 @@ def api_call(module, target_version, api_call_object):
138166
return code, response
139167

140168

141-
def chkp_facts_api_call(module, target_version, api_call_object, is_multible):
169+
def chkp_facts_api_call(module, api_call_object, is_multible):
170+
target_version = get_version(module)
142171
if is_multible is True:
143172
show_single = False
144173
module_key_params = dict((k, v) for k, v in module.params.items() if v is not None)
@@ -164,7 +193,8 @@ def chkp_facts_api_call(module, target_version, api_call_object, is_multible):
164193
}
165194

166195

167-
def chkp_api_call(module, target_version, api_call_object, has_add_api, ignore=None, show_params=None, add_params=None):
196+
def chkp_api_call(module, api_call_object, has_add_api, ignore=None, show_params=None, add_params=None):
197+
target_version = get_version(module)
168198
changed = False
169199
if show_params is None:
170200
show_params = []
@@ -190,6 +220,16 @@ def chkp_api_call(module, target_version, api_call_object, has_add_api, ignore=N
190220
"changed": False
191221
}
192222
else: # handle set/add
223+
params_dict = module.params.copy()
224+
for key, value in module.params.items():
225+
if not is_checkpoint_param(key):
226+
del params_dict[key]
227+
228+
if idempotency_check(res, params_dict) is True:
229+
return {
230+
api_call_object.replace('-', '_'): res,
231+
"changed": False
232+
}
193233
code, res = api_call(module, target_version, api_call_object="set-{0}".format(api_call_object))
194234
if code != 200 and has_add_api is True:
195235
if add_params:
@@ -212,7 +252,8 @@ def chkp_api_call(module, target_version, api_call_object, has_add_api, ignore=N
212252

213253

214254
# for operation and async tasks
215-
def chkp_api_operation(module, target_version, api_call_object):
255+
def chkp_api_operation(module, api_call_object):
256+
target_version = get_version(module)
216257
code, response = api_call(module, target_version, api_call_object)
217258
result = {'changed': True}
218259
if code == 200:

plugins/modules/cp_gaia_allowed_clients.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
version_added: '3.0.0'
3232
requirements: ['supported starting from gaia_api >= 1.6']
3333
options:
34+
version:
35+
description: Gaia API version for example 1.6.
36+
required: False
37+
type: str
3438
allowed_networks:
3539
description: Configure allowed clients as network.
3640
required: False
@@ -76,7 +80,7 @@
7680
'''
7781

7882
from ansible.module_utils.basic import AnsibleModule
79-
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_api_call
83+
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_api_call, checkpoint_argument_spec_for_all
8084

8185

8286
def main():
@@ -91,12 +95,11 @@ def main():
9195
allowed_hosts=dict(type='list', elements='str'),
9296
allowed_any_host=dict(type='bool')
9397
)
94-
98+
fields.update(checkpoint_argument_spec_for_all)
9599
module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
96100
api_call_object = 'allowed-clients'
97-
gaia_api_version = 'v1.6/'
98101

99-
res = chkp_api_call(module, gaia_api_version, api_call_object, False)
102+
res = chkp_api_call(module, api_call_object, False)
100103
module.exit_json(**res)
101104

102105

plugins/modules/cp_gaia_allowed_clients_facts.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
description:
2828
- Show the configuration of allowed clients.
2929
module: cp_gaia_allowed_clients_facts
30+
options:
31+
version:
32+
description: Gaia API version for example 1.6.
33+
required: False
34+
type: str
3035
short_description: Show the configuration of allowed clients.
3136
version_added: '3.0.0'
3237
notes:
@@ -76,18 +81,18 @@
7681

7782

7883
from ansible.module_utils.basic import AnsibleModule
79-
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_facts_api_call
84+
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_facts_api_call, checkpoint_argument_spec_for_all
8085

8186

8287
def main():
8388
# arguments for the module:
8489
fields = dict()
90+
fields.update(checkpoint_argument_spec_for_all)
8591
module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
8692

8793
api_call_object = 'allowed-clients'
88-
gaia_api_version = 'v1.6/'
8994

90-
res = chkp_facts_api_call(module, gaia_api_version, api_call_object, False)
95+
res = chkp_facts_api_call(module, api_call_object, False)
9196
module.exit_json(ansible_facts=res["ansible_facts"])
9297

9398

plugins/modules/cp_gaia_api_versions_facts.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
author: Ameer Asli (@chkp-ameera)
2727
description:
2828
- Show api versions.
29+
options:
30+
version:
31+
description: Gaia API version for example 1.6.
32+
required: False
33+
type: str
2934
short_description: Show api versions.
3035
version_added: '3.0.0'
3136
notes:
@@ -59,17 +64,17 @@
5964
"""
6065

6166
from ansible.module_utils.basic import AnsibleModule
62-
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_facts_api_call
67+
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_facts_api_call, checkpoint_argument_spec_for_all
6368

6469

6570
def main():
6671
# arguments for the module:
6772
fields = dict()
73+
fields.update(checkpoint_argument_spec_for_all)
6874
module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
6975
api_call_object = 'api-versions'
70-
gaia_api_version = 'v1.6/'
7176

72-
res = chkp_facts_api_call(module, gaia_api_version, api_call_object, False)
77+
res = chkp_facts_api_call(module, api_call_object, False)
7378
module.exit_json(ansible_facts=res["ansible_facts"])
7479

7580

plugins/modules/cp_gaia_asset_facts.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
author: Ameer Asli (@chkp-ameera)
2727
description:
2828
- Show Asset.
29+
options:
30+
version:
31+
description: Gaia API version for example 1.6.
32+
required: False
33+
type: str
2934
short_description: Show Asset.
3035
version_added: '3.0.0'
3136
notes:
@@ -152,17 +157,17 @@
152157
"""
153158

154159
from ansible.module_utils.basic import AnsibleModule
155-
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_facts_api_call
160+
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_facts_api_call, checkpoint_argument_spec_for_all
156161

157162

158163
def main():
159164
# arguments for the module:
160165
fields = dict()
166+
fields.update(checkpoint_argument_spec_for_all)
161167
module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
162168
api_call_object = 'asset'
163-
gaia_api_version = 'v1.6/'
164169

165-
res = chkp_facts_api_call(module, gaia_api_version, api_call_object, False)
170+
res = chkp_facts_api_call(module, api_call_object, False)
166171
module.exit_json(ansible_facts=res["ansible_facts"])
167172

168173

plugins/modules/cp_gaia_banner.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
- Setting the banner message.
2929
module: cp_gaia_banner
3030
options:
31+
version:
32+
description: Gaia API version for example 1.6.
33+
required: False
34+
type: str
3135
msg:
3236
description: Banner message for the web, ssh and serial login. Empty string returns to default.
3337
required: false
@@ -59,7 +63,7 @@
5963
"""
6064

6165
from ansible.module_utils.basic import AnsibleModule
62-
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_api_call
66+
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_api_call, checkpoint_argument_spec_for_all
6367

6468

6569
def main():
@@ -68,11 +72,11 @@ def main():
6872
msg=dict(type='str', required=False, default="This system is for authorized use only."),
6973
enabled=dict(type='bool', required=False)
7074
)
75+
fields.update(checkpoint_argument_spec_for_all)
7176
module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
7277
api_call_object = 'banner'
73-
gaia_api_version = 'v1.6/'
7478

75-
res = chkp_api_call(module, gaia_api_version, api_call_object, False)
79+
res = chkp_api_call(module, api_call_object, False)
7680
module.exit_json(**res)
7781

7882

plugins/modules/cp_gaia_banner_facts.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
description:
2727
- Show banner message.
2828
module: cp_gaia_banner_facts
29+
options:
30+
version:
31+
description: Gaia API version for example 1.6.
32+
required: False
33+
type: str
2934
short_description: Show banner message settings.
3035
version_added: '3.0.0'
3136
notes:
@@ -59,17 +64,17 @@
5964
"""
6065

6166
from ansible.module_utils.basic import AnsibleModule
62-
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_facts_api_call
67+
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_facts_api_call, checkpoint_argument_spec_for_all
6368

6469

6570
def main():
6671
# arguments for the module:
6772
fields = dict()
73+
fields.update(checkpoint_argument_spec_for_all)
6874
module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
6975
api_call_object = "banner"
70-
gaia_api_version = 'v1.6/'
7176

72-
res = chkp_facts_api_call(module, gaia_api_version, api_call_object, False)
77+
res = chkp_facts_api_call(module, api_call_object, False)
7378
module.exit_json(ansible_facts=res["ansible_facts"])
7479

7580

plugins/modules/cp_gaia_bond_interface.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
notes:
3232
- Supports C(check_mode).
3333
options:
34+
version:
35+
description: Gaia API version for example 1.6.
36+
required: False
37+
type: str
3438
state:
3539
description: Ansible state which can be C(present) or C(absent).
3640
required: False
@@ -166,7 +170,7 @@
166170
"""
167171

168172
from ansible.module_utils.basic import AnsibleModule
169-
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_api_call
173+
from ansible_collections.check_point.gaia.plugins.module_utils.checkpoint import chkp_api_call, checkpoint_argument_spec_for_all
170174

171175

172176
def main():
@@ -202,16 +206,16 @@ def main():
202206
mii_interval=dict(type='int')
203207
)
204208

209+
fields.update(checkpoint_argument_spec_for_all)
205210
module = AnsibleModule(argument_spec=fields, supports_check_mode=True)
206211
api_call_object = 'bond-interface'
207-
gaia_api_version = 'v1.6/'
208212
ignore = ['status']
209213
show_params = ['name']
210214
add_params = {}
211215
if module.params['name'].startswith('bond') and len(module.params['name']) > 4:
212216
add_params = {'id': int(module.params['name'][4:])}
213217

214-
res = chkp_api_call(module, gaia_api_version, api_call_object, True, ignore=ignore, show_params=show_params, add_params=add_params)
218+
res = chkp_api_call(module, api_call_object, True, ignore=ignore, show_params=show_params, add_params=add_params)
215219
module.exit_json(**res)
216220

217221

0 commit comments

Comments
 (0)