Skip to content

Commit 25d8b06

Browse files
authored
To add CKP access_layers RM against the legacy access_layer and access_layer_facts module (#93)
* add access_layer_rm * fix reviews * remove unwanted if cond * remove pycache * fix reviews * add deprecation notice * fix logic * fix syntax * fix gathered all bug * fix plural api logic * add order param
1 parent 2320d82 commit 25d8b06

File tree

18 files changed

+1526
-2
lines changed

18 files changed

+1526
-2
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2022 Red Hat
3+
# GNU General Public License v3.0+
4+
# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5+
"""
6+
The module file for cp_mgmt_add_access_layers
7+
"""
8+
9+
from __future__ import absolute_import, division, print_function
10+
11+
__metaclass__ = type
12+
13+
from ansible.plugins.action import ActionBase
14+
from ansible.module_utils.connection import Connection
15+
16+
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import (
17+
utils,
18+
)
19+
from ansible_collections.check_point.mgmt.plugins.module_utils.checkpoint import (
20+
CheckPointRequest,
21+
map_params_to_obj,
22+
sync_show_params_with_add_params,
23+
remove_unwanted_key,
24+
contains_show_identifier_param,
25+
)
26+
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import (
27+
AnsibleArgSpecValidator,
28+
)
29+
from ansible_collections.check_point.mgmt.plugins.modules.cp_mgmt_access_layers import (
30+
DOCUMENTATION,
31+
)
32+
33+
34+
class ActionModule(ActionBase):
35+
"""action module"""
36+
37+
def __init__(self, *args, **kwargs):
38+
super(ActionModule, self).__init__(*args, **kwargs)
39+
self._result = None
40+
self.api_call_object = "access-layer"
41+
self.api_call_object_plural_version = "access-layers"
42+
self.module_return = "mgmt_access_layers"
43+
self.key_transform = {
44+
"add_default_rule": "add-default-rule",
45+
"applications_and_url_filtering": "applications-and-url-filtering",
46+
"content_awareness": "content-awareness",
47+
"detect_using_x_forward_for": "detect-using-x-forward-for",
48+
"implicit_cleanup_action": "implicit-cleanup-action",
49+
"mobile_access": "mobile-access",
50+
"details_level": "details-level",
51+
"ignore_warnings": "ignore-warnings",
52+
"ignore_errors": "ignore-errors",
53+
}
54+
55+
def _check_argspec(self):
56+
aav = AnsibleArgSpecValidator(
57+
data=self._task.args,
58+
schema=DOCUMENTATION,
59+
schema_format="doc",
60+
name=self._task.action,
61+
)
62+
valid, errors, self._task.args = aav.validate()
63+
if not valid:
64+
self._result["failed"] = True
65+
self._result["msg"] = errors
66+
67+
def search_for_existing_rules(
68+
self, conn_request, api_call_object, search_payload=None, state=None
69+
):
70+
result = conn_request.post(api_call_object, state, data=search_payload)
71+
return result
72+
73+
def search_for_resource_name(self, conn_request, payload):
74+
search_result = []
75+
search_payload = utils.remove_empties(payload)
76+
if not contains_show_identifier_param(search_payload):
77+
search_result = self.search_for_existing_rules(
78+
conn_request,
79+
self.api_call_object_plural_version,
80+
search_payload,
81+
"gathered",
82+
)
83+
else:
84+
search_result = self.search_for_existing_rules(
85+
conn_request, self.api_call_object, search_payload, "gathered"
86+
)
87+
search_result = sync_show_params_with_add_params(
88+
search_result["response"], self.key_transform
89+
)
90+
if (
91+
search_result.get("code")
92+
and "object_not_found" in search_result.get("code")
93+
and "not found" in search_result.get("message")
94+
):
95+
search_result = {}
96+
return search_result
97+
98+
def delete_module_api_config(self, conn_request, module_config_params):
99+
config = {}
100+
before = {}
101+
after = {}
102+
changed = False
103+
result = {}
104+
payload = utils.remove_empties(module_config_params)
105+
remove_from_response = ["uid", "read-only", "domain"]
106+
search_result = self.search_for_resource_name(conn_request, payload)
107+
if search_result:
108+
search_result = remove_unwanted_key(
109+
search_result, remove_from_response
110+
)
111+
before = search_result
112+
result = conn_request.post(
113+
self.api_call_object, self._task.args["state"], data=payload
114+
)
115+
if before:
116+
config.update({"before": before, "after": after})
117+
else:
118+
config.update({"before": before})
119+
if result.get("changed"):
120+
changed = True
121+
return config, changed
122+
123+
def configure_module_api(self, conn_request, module_config_params):
124+
config = {}
125+
before = {}
126+
after = {}
127+
changed = False
128+
result = {}
129+
# Add to the THIS list for the value which needs to be excluded
130+
# from HAVE params when compared to WANT param like 'ID' can be
131+
# part of HAVE param but may not be part of your WANT param
132+
remove_from_response = ["uid", "read-only", "domain"]
133+
remove_from_set = ["add-default-rule"]
134+
payload = utils.remove_empties(module_config_params)
135+
if payload.get("name"):
136+
search_payload = {"name": payload["name"]}
137+
search_result = self.search_for_resource_name(
138+
conn_request, search_payload
139+
)
140+
if search_result:
141+
search_result = remove_unwanted_key(
142+
search_result, remove_from_response
143+
)
144+
before = search_result
145+
payload = map_params_to_obj(payload, self.key_transform)
146+
delete_params = {
147+
"name": payload["name"],
148+
}
149+
result = conn_request.post(
150+
self.api_call_object,
151+
self._task.args["state"],
152+
data=payload,
153+
remove_keys=remove_from_set,
154+
delete_params=delete_params,
155+
)
156+
if result.get("changed"):
157+
search_result = sync_show_params_with_add_params(
158+
result["response"], self.key_transform
159+
)
160+
search_result = remove_unwanted_key(
161+
search_result, remove_from_response
162+
)
163+
after = search_result
164+
changed = True
165+
config.update({"before": before, "after": after})
166+
167+
return config, changed
168+
169+
def run(self, tmp=None, task_vars=None):
170+
self._supports_check_mode = True
171+
self._result = super(ActionModule, self).run(tmp, task_vars)
172+
self._check_argspec()
173+
if self._result.get("failed"):
174+
return self._result
175+
conn = Connection(self._connection.socket_path)
176+
conn_request = CheckPointRequest(connection=conn, task_vars=task_vars)
177+
if self._task.args["state"] == "gathered":
178+
if self._task.args.get("config"):
179+
self._result["gathered"] = self.search_for_resource_name(
180+
conn_request, self._task.args["config"]
181+
)
182+
else:
183+
self._result["gathered"] = self.search_for_resource_name(
184+
conn_request, dict()
185+
)
186+
elif (
187+
self._task.args["state"] == "merged"
188+
or self._task.args["state"] == "replaced"
189+
):
190+
if self._task.args.get("config"):
191+
(
192+
self._result[self.module_return],
193+
self._result["changed"],
194+
) = self.configure_module_api(
195+
conn_request, self._task.args["config"]
196+
)
197+
elif self._task.args["state"] == "deleted":
198+
if self._task.args.get("config"):
199+
(
200+
self._result[self.module_return],
201+
self._result["changed"],
202+
) = self.delete_module_api_config(
203+
conn_request, self._task.args["config"]
204+
)
205+
206+
return self._result

0 commit comments

Comments
 (0)