| 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_hosts |
| 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 |
map_obj_to_params, |
| 23 |
sync_show_params_with_add_params, |
| 24 |
remove_unwanted_key, |
| 25 |
contains_show_identifier_param, |
| 26 |
) |
| 27 |
from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import ( |
| 28 |
AnsibleArgSpecValidator, |
| 29 |
) |
| 30 |
from ansible_collections.check_point.mgmt.plugins.modules.cp_mgmt_hosts import ( |
| 31 |
DOCUMENTATION, |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
class ActionModule(ActionBase): |
| 36 |
"""action module""" |
| 37 |
|
| 38 |
def __init__(self, *args, **kwargs): |
| 39 |
super(ActionModule, self).__init__(*args, **kwargs) |
| 40 |
self._result = None |
| 41 |
self.api_call_object = "host" |
| 42 |
self.api_call_object_plural_version = "hosts" |
| 43 |
self.module_return = "mgmt_hosts" |
| 44 |
self.key_transform = { |
| 45 |
"ip_address": "ip-address", |
| 46 |
"ipv4_address": "ipv4-address", |
| 47 |
"ipv6_address": "ipv6-address", |
| 48 |
"interfaces": "interfaces", |
| 49 |
"nat_settings": "nat-settings", |
| 50 |
"hide_behind": "hide-behind", |
| 51 |
"install_on": "install-on", |
| 52 |
"host_servers": "host-servers", |
| 53 |
"dns_server": "dns-server", |
| 54 |
"mail_server": "mail-server", |
| 55 |
"web_server": "web-server", |
| 56 |
"web_server_config": "web-server-config", |
| 57 |
"additional_ports": "additional-ports", |
| 58 |
"application_engines": "application-engines", |
| 59 |
"listen_standard_port": "listen-standard-port", |
| 60 |
"operating_system": "operating-system", |
| 61 |
"protected_by": "protected-by", |
| 62 |
"details_level": "details-level", |
| 63 |
"ignore_warnings": "ignore-warnings", |
| 64 |
"ignore_errors": "ignore-errors", |
| 65 |
"mask_length": "mask-length", |
| 66 |
"mask_length4": "mask-length4", |
| 67 |
"mask_length6": "mask-length4", |
| 68 |
"subnet_mask": "subnet-mask", |
| 69 |
} |
| 70 |
|
| 71 |
def _check_argspec(self): |
| 72 |
aav = AnsibleArgSpecValidator( |
| 73 |
data=self._task.args, |
| 74 |
schema=DOCUMENTATION, |
| 75 |
schema_format="doc", |
| 76 |
name=self._task.action, |
| 77 |
) |
| 78 |
valid, errors, self._task.args = aav.validate() |
| 79 |
if not valid: |
| 80 |
self._result["failed"] = True |
| 81 |
self._result["msg"] = errors |
| 82 |
|
| 83 |
def search_for_existing_rules( |
| 84 |
self, conn_request, api_call_object, search_payload=None, state=None |
| 85 |
): |
| 86 |
result = conn_request.post(api_call_object, state, data=search_payload) |
| 87 |
return result |
| 88 |
|
| 89 |
def search_for_resource_name(self, conn_request, payload): |
| 90 |
search_result = [] |
| 91 |
round_trip = False |
| 92 |
|
| 93 |
search_payload = utils.remove_empties(payload) |
| 94 |
if search_payload.get("round_trip"): |
| 95 |
round_trip = True |
| 96 |
if search_payload.get("round_trip") is not None: |
| 97 |
del search_payload["round_trip"] |
| 98 |
search_payload = map_params_to_obj(search_payload, self.key_transform) |
| 99 |
if not contains_show_identifier_param(search_payload): |
| 100 |
search_result = self.search_for_existing_rules( |
| 101 |
conn_request, |
| 102 |
self.api_call_object_plural_version, |
| 103 |
search_payload, |
| 104 |
"gathered", |
| 105 |
) |
| 106 |
if search_result.get("code") == 200: |
| 107 |
search_result = search_result["response"]["objects"] |
| 108 |
return search_result |
| 109 |
else: |
| 110 |
search_result = self.search_for_existing_rules( |
| 111 |
conn_request, self.api_call_object, search_payload, "gathered" |
| 112 |
) |
| 113 |
if round_trip: |
| 114 |
search_result = sync_show_params_with_add_params( |
| 115 |
search_result["response"], self.key_transform |
| 116 |
) |
| 117 |
elif search_result.get("code") and search_result["code"] == 200: |
| 118 |
search_result = search_result["response"] |
| 119 |
search_result = map_obj_to_params( |
| 120 |
search_result, |
| 121 |
self.key_transform, |
| 122 |
self.module_return, |
| 123 |
) |
| 124 |
if search_result.get("code") and search_result["code"] != 200: |
| 125 |
if ( |
| 126 |
search_result.get("response") |
| 127 |
and "object_not_found" in search_result["response"]["code"] |
| 128 |
and "not found" in search_result["response"]["message"] |
| 129 |
): |
| 130 |
search_result = {} |
| 131 |
elif "object_not_found" in search_result.get( |
| 132 |
"code" |
| 133 |
) and "not found" in search_result.get("message"): |
| 134 |
search_result = {} |
| 135 |
return search_result |
| 136 |
|
| 137 |
def delete_module_api_config(self, conn_request, module_config_params): |
| 138 |
config = {} |
| 139 |
before = {} |
| 140 |
after = {} |
| 141 |
result = {} |
| 142 |
changed = False |
| 143 |
round_trip = False |
| 144 |
ckp_session_uid = None |
| 145 |
payload = utils.remove_empties(module_config_params) |
| 146 |
if payload.get("round_trip"): |
| 147 |
round_trip = True |
| 148 |
del payload["round_trip"] |
| 149 |
remove_from_response = ["uid", "read-only", "domain"] |
| 150 |
if round_trip: |
| 151 |
search_payload = {"name": payload["name"], "round_trip": True} |
| 152 |
else: |
| 153 |
search_payload = {"name": payload["name"]} |
| 154 |
search_result = self.search_for_resource_name( |
| 155 |
conn_request, search_payload |
| 156 |
) |
| 157 |
if search_result: |
| 158 |
if round_trip: |
| 159 |
search_result = remove_unwanted_key( |
| 160 |
search_result, remove_from_response |
| 161 |
) |
| 162 |
before = search_result |
| 163 |
result = conn_request.post( |
| 164 |
self.api_call_object, self._task.args["state"], data=payload |
| 165 |
) |
| 166 |
if before: |
| 167 |
config.update({"before": before, "after": after}) |
| 168 |
else: |
| 169 |
config.update({"before": before}) |
| 170 |
if result.get("changed"): |
| 171 |
changed = True |
| 172 |
ckp_session_uid = result["checkpoint_session_uid"] |
| 173 |
return config, changed, ckp_session_uid |
| 174 |
|
| 175 |
def configure_module_api(self, conn_request, module_config_params): |
| 176 |
config = {} |
| 177 |
before = {} |
| 178 |
after = {} |
| 179 |
result = {} |
| 180 |
changed = False |
| 181 |
round_trip = False |
| 182 |
ckp_session_uid = None |
| 183 |
# Add to the THIS list for the value which needs to be excluded |
| 184 |
# from HAVE params when compared to WANT param like 'ID' can be |
| 185 |
# part of HAVE param but may not be part of your WANT param |
| 186 |
remove_from_response = ["uid", "read-only", "domain"] |
| 187 |
remove_from_set = [] |
| 188 |
payload = utils.remove_empties(module_config_params) |
| 189 |
if payload.get("round_trip"): |
| 190 |
round_trip = True |
| 191 |
del payload["round_trip"] |
| 192 |
if payload.get("name"): |
| 193 |
if round_trip: |
| 194 |
search_payload = {"name": payload["name"], "round_trip": True} |
| 195 |
else: |
| 196 |
search_payload = {"name": payload["name"]} |
| 197 |
search_result = self.search_for_resource_name( |
| 198 |
conn_request, search_payload |
| 199 |
) |
| 200 |
if search_result: |
| 201 |
if round_trip: |
| 202 |
search_result = remove_unwanted_key( |
| 203 |
search_result, remove_from_response |
| 204 |
) |
| 205 |
before = search_result |
| 206 |
payload = map_params_to_obj(payload, self.key_transform) |
| 207 |
delete_params = { |
| 208 |
"name": payload["name"], |
| 209 |
} |
| 210 |
result = conn_request.post( |
| 211 |
self.api_call_object, |
| 212 |
self._task.args["state"], |
| 213 |
data=payload, |
| 214 |
remove_keys=remove_from_set, |
| 215 |
delete_params=delete_params, |
| 216 |
) |
| 217 |
if result.get("changed"): |
| 218 |
if round_trip: |
| 219 |
search_result = sync_show_params_with_add_params( |
| 220 |
result["response"], self.key_transform |
| 221 |
) |
| 222 |
else: |
| 223 |
search_result = map_obj_to_params( |
| 224 |
result["response"], |
| 225 |
self.key_transform, |
| 226 |
self.module_return, |
| 227 |
) |
| 228 |
if round_trip: |
| 229 |
search_result = remove_unwanted_key( |
| 230 |
search_result, remove_from_response |
| 231 |
) |
| 232 |
after = search_result |
| 233 |
ckp_session_uid = result["checkpoint_session_uid"] |
| 234 |
changed = True |
| 235 |
config.update({"before": before, "after": after}) |
| 236 |
|
| 237 |
return config, changed, ckp_session_uid |
| 238 |
|
| 239 |
def run(self, tmp=None, task_vars=None): |
| 240 |
self._supports_check_mode = True |
| 241 |
self._result = super(ActionModule, self).run(tmp, task_vars) |
| 242 |
self._check_argspec() |
| 243 |
self._result["checkpoint_session_uid"] = None |
| 244 |
if self._result.get("failed"): |
| 245 |
return self._result |
| 246 |
conn = Connection(self._connection.socket_path) |
| 247 |
conn_request = CheckPointRequest(connection=conn, task_vars=task_vars) |
| 248 |
if self._task.args["state"] == "gathered": |
| 249 |
if self._task.args.get("config"): |
| 250 |
self._result["gathered"] = self.search_for_resource_name( |
| 251 |
conn_request, self._task.args["config"] |
| 252 |
) |
| 253 |
else: |
| 254 |
self._result["gathered"] = self.search_for_resource_name( |
| 255 |
conn_request, dict() |
| 256 |
) |
| 257 |
elif ( |
| 258 |
self._task.args["state"] == "merged" |
| 259 |
or self._task.args["state"] == "replaced" |
| 260 |
): |
| 261 |
if self._task.args.get("config"): |
| 262 |
( |
| 263 |
self._result[self.module_return], |
| 264 |
self._result["changed"], |
| 265 |
self._result["checkpoint_session_uid"], |
| 266 |
) = self.configure_module_api( |
| 267 |
conn_request, self._task.args["config"] |
| 268 |
) |
| 269 |
elif self._task.args["state"] == "deleted": |
| 270 |
if self._task.args.get("config"): |
| 271 |
( |
| 272 |
self._result[self.module_return], |
| 273 |
self._result["changed"], |
| 274 |
self._result["checkpoint_session_uid"], |
| 275 |
) = self.delete_module_api_config( |
| 276 |
conn_request, self._task.args["config"] |
| 277 |
) |
| 278 |
if self._result.get("checkpoint_session_uid") is None: |
| 279 |
del self._result["checkpoint_session_uid"] |
| 280 |
|
| 281 |
return self._result |