| 1 |
# This code is part of Ansible, but is an independent component. |
| 2 |
# This particular file snippet, and this file snippet only, is BSD licensed. |
| 3 |
# Modules you write using this snippet, which is embedded dynamically by Ansible |
| 4 |
# still belong to the author of the module, and may assign their own license |
| 5 |
# to the complete work. |
| 6 |
# |
| 7 |
# (c) 2022 Red Hat Inc. |
| 8 |
# |
| 9 |
# Redistribution and use in source and binary forms, with or without modification, |
| 10 |
# are permitted provided that the following conditions are met: |
| 11 |
# |
| 12 |
# * Redistributions of source code must retain the above copyright |
| 13 |
# notice, this list of conditions and the following disclaimer. |
| 14 |
# * Redistributions in binary form must reproduce the above copyright notice, |
| 15 |
# this list of conditions and the following disclaimer in the documentation |
| 16 |
# and/or other materials provided with the distribution. |
| 17 |
# |
| 18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 |
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 |
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 |
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 26 |
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
# |
| 28 |
|
| 29 |
from __future__ import absolute_import, division, print_function |
| 30 |
|
| 31 |
__metaclass__ = type |
| 32 |
|
| 33 |
import time |
| 34 |
from ansible.module_utils.six import iteritems |
| 35 |
from ansible.module_utils.connection import ConnectionError |
| 36 |
from ansible.module_utils.connection import Connection |
| 37 |
from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import ( |
| 38 |
utils, |
| 39 |
) |
| 40 |
|
| 41 |
BASE_HEADERS = { |
| 42 |
"Content-Type": "application/json", |
| 43 |
"User-Agent": "Ansible", |
| 44 |
} |
| 45 |
|
| 46 |
checkpoint_argument_spec_for_action_module = dict( |
| 47 |
auto_publish_session=dict(type="bool", default=False), |
| 48 |
wait_for_task_timeout=dict(type="int", default=30), |
| 49 |
version=dict(type="str"), |
| 50 |
) |
| 51 |
|
| 52 |
checkpoint_argument_spec_for_objects = dict( |
| 53 |
auto_publish_session=dict(type="bool", default=False), |
| 54 |
wait_for_task=dict(type="bool", default=True), |
| 55 |
wait_for_task_timeout=dict(type="int", default=30), |
| 56 |
state=dict(type="str", choices=["present", "absent"], default="present"), |
| 57 |
version=dict(type="str"), |
| 58 |
) |
| 59 |
|
| 60 |
checkpoint_argument_spec_for_facts = dict(version=dict(type="str")) |
| 61 |
|
| 62 |
checkpoint_argument_spec_for_commands = dict( |
| 63 |
wait_for_task=dict(type="bool", default=True), |
| 64 |
wait_for_task_timeout=dict(type="int", default=30), |
| 65 |
version=dict(type="str"), |
| 66 |
auto_publish_session=dict(type="bool", default=False), |
| 67 |
) |
| 68 |
|
| 69 |
delete_params = [ |
| 70 |
"name", |
| 71 |
"uid", |
| 72 |
"layer", |
| 73 |
"exception-group-name", |
| 74 |
"rule-name", |
| 75 |
"package", |
| 76 |
"ignore-errors", |
| 77 |
"ignore-warnings", |
| 78 |
"gateway-uid", |
| 79 |
"url" |
| 80 |
] |
| 81 |
|
| 82 |
remove_from_set_payload = { |
| 83 |
"lsm-cluster": [ |
| 84 |
"security-profile", |
| 85 |
"name-prefix", |
| 86 |
"name-suffix", |
| 87 |
"main-ip-address", |
| 88 |
], |
| 89 |
"md-permissions-profile": ["permission-level"], |
| 90 |
"access-section": ["position"], |
| 91 |
"nat-section": ["position"], |
| 92 |
"https-section": ["position"], |
| 93 |
"mobile-access-section": ["position"], |
| 94 |
"mobile-access-profile-section": ["position"], |
| 95 |
} |
| 96 |
|
| 97 |
remove_from_add_payload = {"lsm-cluster": ["name"]} |
| 98 |
|
| 99 |
|
| 100 |
def _fail_json(msg): |
| 101 |
"""Replace the AnsibleModule fail_json here |
| 102 |
:param msg: The message for the failure |
| 103 |
:type msg: str |
| 104 |
""" |
| 105 |
raise Exception(msg) |
| 106 |
|
| 107 |
|
| 108 |
def map_params_to_obj(module_params, key_transform): |
| 109 |
"""The fn to convert the api returned params to module params |
| 110 |
:param module_params: Module params |
| 111 |
:param key_transform: Dict with module equivalent API params |
| 112 |
:rtype: A dict |
| 113 |
:returns: dict with module prams transformed having API expected params |
| 114 |
""" |
| 115 |
obj = {} |
| 116 |
for k, v in iteritems(key_transform): |
| 117 |
if k in module_params and ( |
| 118 |
module_params.get(k) |
| 119 |
or module_params.get(k) == 0 |
| 120 |
or module_params.get(k) is False |
| 121 |
): |
| 122 |
val = module_params.pop(k) |
| 123 |
if isinstance(val, list): |
| 124 |
temp = [] |
| 125 |
for each in val: |
| 126 |
if isinstance(each, dict): |
| 127 |
temp.append(map_params_to_obj(each, key_transform)) |
| 128 |
if temp: |
| 129 |
val = temp |
| 130 |
if isinstance(val, dict): |
| 131 |
temp_child = {} |
| 132 |
for each_k, each_v in iteritems(val): |
| 133 |
if "_" in each_k: |
| 134 |
temp_param = "-".join(each_k.split("_")) |
| 135 |
if isinstance(each_v, dict): |
| 136 |
temp_dict = map_params_to_obj( |
| 137 |
each_v, key_transform |
| 138 |
) |
| 139 |
each_v = temp_dict |
| 140 |
temp_child.update({temp_param: each_v}) |
| 141 |
else: |
| 142 |
temp_child.update({each_k: each_v}) |
| 143 |
obj[v] = temp_child |
| 144 |
else: |
| 145 |
obj[v] = val |
| 146 |
if module_params: |
| 147 |
obj.update(module_params) |
| 148 |
return obj |
| 149 |
|
| 150 |
|
| 151 |
def map_obj_to_params(module_return_params, key_transform, return_param): |
| 152 |
"""The fn to convert the api returned params to module params |
| 153 |
:param module_return_params: API returned response params |
| 154 |
:param key_transform: Module params |
| 155 |
:rtype: A dict |
| 156 |
:returns: dict with api returned value to module param value |
| 157 |
""" |
| 158 |
temp = {} |
| 159 |
if module_return_params.get(return_param): |
| 160 |
temp[return_param] = [] |
| 161 |
for each in module_return_params[return_param]: |
| 162 |
api_temp = {} |
| 163 |
for k, v in iteritems(key_transform): |
| 164 |
if v in each and ( |
| 165 |
each.get(v) or each.get(v) == 0 or each.get(v) is False |
| 166 |
): |
| 167 |
api_temp[k] = each.pop(v) |
| 168 |
if each: |
| 169 |
api_temp.update(each) |
| 170 |
temp[return_param].append(api_temp) |
| 171 |
else: |
| 172 |
for k, v in iteritems(key_transform): |
| 173 |
if v in module_return_params and ( |
| 174 |
module_return_params.get(v) |
| 175 |
or module_return_params.get(v) == 0 |
| 176 |
or module_return_params.get(v) is False |
| 177 |
): |
| 178 |
if isinstance(module_return_params[v], dict): |
| 179 |
temp_child = {} |
| 180 |
for each_k, each_v in iteritems(module_return_params[v]): |
| 181 |
if "-" in each_k: |
| 182 |
temp_param = "_".join(each_k.split("-")) |
| 183 |
if temp_param in key_transform: |
| 184 |
temp_child.update({temp_param: each_v}) |
| 185 |
else: |
| 186 |
temp_child.update({each_k: each_v}) |
| 187 |
temp[k] = temp_child |
| 188 |
module_return_params.pop(v) |
| 189 |
else: |
| 190 |
temp[k] = module_return_params.pop(v) |
| 191 |
if module_return_params: |
| 192 |
temp.update(module_return_params) |
| 193 |
return temp |
| 194 |
|
| 195 |
|
| 196 |
def verify_want_have_diff(want, have, remove_key_from_diff): |
| 197 |
for each in remove_key_from_diff: |
| 198 |
if each in want: |
| 199 |
del want[each] |
| 200 |
diff = utils.dict_diff(have, want) |
| 201 |
return diff |
| 202 |
|
| 203 |
|
| 204 |
def remove_unwanted_key(payload, remove_keys): |
| 205 |
for each in remove_keys: |
| 206 |
if each in payload: |
| 207 |
del payload[each] |
| 208 |
return payload |
| 209 |
|
| 210 |
|
| 211 |
def sync_show_params_with_add_params(search_result, key_transform): |
| 212 |
temp = {} |
| 213 |
remove_keys = ["type", "meta-info"] |
| 214 |
for k, v in iteritems(search_result): |
| 215 |
if k in remove_keys: |
| 216 |
continue |
| 217 |
if isinstance(v, dict): |
| 218 |
if v.get("name"): |
| 219 |
temp.update({k: v["name"]}) |
| 220 |
else: |
| 221 |
temp_child = {} |
| 222 |
for each_k, each_v in iteritems(v): |
| 223 |
if isinstance(each_v, dict): |
| 224 |
if each_v.get("name"): |
| 225 |
temp_child.update({each_k: each_v["name"]}) |
| 226 |
else: |
| 227 |
temp_child.update({each_k: each_v}) |
| 228 |
temp.update({k: temp_child}) |
| 229 |
elif isinstance(v, list): |
| 230 |
temp[k] = [] |
| 231 |
for each in v: |
| 232 |
if each.get("name"): |
| 233 |
temp[k].append(each["name"]) |
| 234 |
else: |
| 235 |
temp.update(each) |
| 236 |
else: |
| 237 |
temp.update({k: v}) |
| 238 |
temp = map_obj_to_params(temp, key_transform, "") |
| 239 |
return temp |
| 240 |
|
| 241 |
|
| 242 |
# parse failure message with code and response |
| 243 |
def parse_fail_message(code, response): |
| 244 |
return "Checkpoint device returned error {0} with message {1}".format( |
| 245 |
code, response |
| 246 |
) |
| 247 |
|
| 248 |
|
| 249 |
# send the request to checkpoint |
| 250 |
def send_request(connection, version, url, payload=None): |
| 251 |
code, response = connection.send_request( |
| 252 |
"/web_api/" + version + url, payload |
| 253 |
) |
| 254 |
|
| 255 |
return code, response |
| 256 |
|
| 257 |
|
| 258 |
# get the payload from the user parameters |
| 259 |
def is_checkpoint_param(parameter): |
| 260 |
if ( |
| 261 |
parameter == "auto_publish_session" |
| 262 |
or parameter == "state" |
| 263 |
or parameter == "wait_for_task" |
| 264 |
or parameter == "wait_for_task_timeout" |
| 265 |
or parameter == "version" |
| 266 |
): |
| 267 |
return False |
| 268 |
return True |
| 269 |
|
| 270 |
|
| 271 |
def is_no_changes_command(command): |
| 272 |
no_changes_command_prefixes = ["show", "export-access-rulebase"] |
| 273 |
for prefix in no_changes_command_prefixes: |
| 274 |
if command.startswith(prefix): |
| 275 |
return True |
| 276 |
return False |
| 277 |
|
| 278 |
|
| 279 |
def contains_show_identifier_param(payload): |
| 280 |
identifier_params = ["name", "uid", "assigned-domain", "task-id", "signature", "url", "best-practice-id"] |
| 281 |
for param in identifier_params: |
| 282 |
if payload.get(param) is not None: |
| 283 |
return True |
| 284 |
return False |
| 285 |
|
| 286 |
|
| 287 |
# build the payload from the parameters which has value (not None), and they are parameter of checkpoint API as well |
| 288 |
def get_payload_from_parameters(params): |
| 289 |
payload = {} |
| 290 |
for parameter in params: |
| 291 |
parameter_value = params[parameter] |
| 292 |
if parameter_value is not None and is_checkpoint_param(parameter): |
| 293 |
if isinstance(parameter_value, dict): |
| 294 |
payload[ |
| 295 |
parameter.replace("_", "-") |
| 296 |
] = get_payload_from_parameters(parameter_value) |
| 297 |
elif ( |
| 298 |
isinstance(parameter_value, list) |
| 299 |
and len(parameter_value) != 0 |
| 300 |
and isinstance(parameter_value[0], dict) |
| 301 |
): |
| 302 |
payload_list = [] |
| 303 |
for element_dict in parameter_value: |
| 304 |
payload_list.append( |
| 305 |
get_payload_from_parameters(element_dict) |
| 306 |
) |
| 307 |
payload[parameter.replace("_", "-")] = payload_list |
| 308 |
else: |
| 309 |
# special handle for this param in order to avoid two params called "version" |
| 310 |
if ( |
| 311 |
parameter == "gateway_version" |
| 312 |
or parameter == "cluster_version" |
| 313 |
or parameter == "server_version" |
| 314 |
or parameter == "check_point_host_version" |
| 315 |
or parameter == "target_version" |
| 316 |
or parameter == "vsx_version" |
| 317 |
): |
| 318 |
parameter = "version" |
| 319 |
|
| 320 |
# message & syslog_facility are internally used by Ansible, so need to avoid param duplicity |
| 321 |
elif parameter == "login_message": |
| 322 |
parameter = "message" |
| 323 |
|
| 324 |
payload[parameter.replace("_", "-")] = parameter_value |
| 325 |
|
| 326 |
return payload |
| 327 |
|
| 328 |
|
| 329 |
# wait for task |
| 330 |
def wait_for_task(module, version, connection, task_id): |
| 331 |
task_id_payload = {"task-id": task_id, "details-level": "full"} |
| 332 |
task_complete = False |
| 333 |
minutes_until_timeout = 30 |
| 334 |
if ( |
| 335 |
module.params["wait_for_task_timeout"] is not None |
| 336 |
and module.params["wait_for_task_timeout"] >= 0 |
| 337 |
): |
| 338 |
minutes_until_timeout = module.params["wait_for_task_timeout"] |
| 339 |
max_num_iterations = minutes_until_timeout * 30 |
| 340 |
current_iteration = 0 |
| 341 |
|
| 342 |
# As long as there is a task in progress |
| 343 |
while not task_complete and current_iteration < max_num_iterations: |
| 344 |
current_iteration += 1 |
| 345 |
# Check the status of the task |
| 346 |
code, response = send_request( |
| 347 |
connection, version, "show-task", task_id_payload |
| 348 |
) |
| 349 |
|
| 350 |
attempts_counter = 0 |
| 351 |
while code != 200: |
| 352 |
if attempts_counter < 5: |
| 353 |
attempts_counter += 1 |
| 354 |
time.sleep(2) |
| 355 |
code, response = send_request( |
| 356 |
connection, version, "show-task", task_id_payload |
| 357 |
) |
| 358 |
else: |
| 359 |
response["message"] = ( |
| 360 |
"ERROR: Failed to handle asynchronous tasks as synchronous, tasks result is" |
| 361 |
" undefined. " + response["message"] |
| 362 |
) |
| 363 |
module.fail_json(msg=parse_fail_message(code, response)) |
| 364 |
|
| 365 |
# Count the number of tasks that are not in-progress |
| 366 |
completed_tasks = 0 |
| 367 |
for task in response["tasks"]: |
| 368 |
if task["status"] == "failed": |
| 369 |
( |
| 370 |
status_description, |
| 371 |
comments, |
| 372 |
) = get_status_description_and_comments(task) |
| 373 |
if comments and status_description: |
| 374 |
module.fail_json( |
| 375 |
msg="Task {0} with task id {1} failed. Message: {2} with description: {3} - " |
| 376 |
"Look at the logs for more details ".format( |
| 377 |
task["task-name"], |
| 378 |
task["task-id"], |
| 379 |
comments, |
| 380 |
status_description, |
| 381 |
) |
| 382 |
) |
| 383 |
elif comments: |
| 384 |
module.fail_json( |
| 385 |
msg="Task {0} with task id {1} failed. Message: {2} - Look at the logs for more details ".format( |
| 386 |
task["task-name"], task["task-id"], comments |
| 387 |
) |
| 388 |
) |
| 389 |
elif status_description: |
| 390 |
module.fail_json( |
| 391 |
msg="Task {0} with task id {1} failed. Message: {2} - Look at the logs for more " |
| 392 |
"details ".format( |
| 393 |
task["task-name"], |
| 394 |
task["task-id"], |
| 395 |
status_description, |
| 396 |
) |
| 397 |
) |
| 398 |
else: |
| 399 |
module.fail_json( |
| 400 |
msg="Task {0} with task id {1} failed. Look at the logs for more details".format( |
| 401 |
task["task-name"], task["task-id"] |
| 402 |
) |
| 403 |
) |
| 404 |
if task["status"] == "in progress": |
| 405 |
break |
| 406 |
completed_tasks += 1 |
| 407 |
|
| 408 |
# Are we done? check if all tasks are completed |
| 409 |
if completed_tasks == len(response["tasks"]) and completed_tasks != 0: |
| 410 |
task_complete = True |
| 411 |
else: |
| 412 |
time.sleep(2) # Wait for two seconds |
| 413 |
if not task_complete: |
| 414 |
module.fail_json( |
| 415 |
msg="ERROR: Timeout. Task-id: {0}.".format( |
| 416 |
task_id_payload["task-id"] |
| 417 |
) |
| 418 |
) |
| 419 |
else: |
| 420 |
return response |
| 421 |
|
| 422 |
|
| 423 |
# Getting a status description and comments of task failure details |
| 424 |
def get_status_description_and_comments(task): |
| 425 |
status_description = None |
| 426 |
comments = None |
| 427 |
if "comments" in task and task["comments"]: |
| 428 |
comments = task["comments"] |
| 429 |
if "task-details" in task and task["task-details"]: |
| 430 |
task_details = task["task-details"][0] |
| 431 |
if "statusDescription" in task_details: |
| 432 |
status_description = task_details["statusDescription"] |
| 433 |
return status_description, comments |
| 434 |
|
| 435 |
|
| 436 |
# if failed occurred, in some cases we want to discard changes before exiting. We also notify the user about the `discard` |
| 437 |
def discard_and_fail(module, code, response, connection, version): |
| 438 |
discard_code, discard_response = send_request( |
| 439 |
connection, version, "discard" |
| 440 |
) |
| 441 |
if discard_code != 200: |
| 442 |
try: |
| 443 |
module.fail_json( |
| 444 |
msg=parse_fail_message(code, response) |
| 445 |
+ " Failed to discard session {0}" |
| 446 |
" with error {1} with message {2}".format( |
| 447 |
connection.get_session_uid(), |
| 448 |
discard_code, |
| 449 |
discard_response, |
| 450 |
) |
| 451 |
) |
| 452 |
except Exception: |
| 453 |
# Read-only mode without UID |
| 454 |
module.fail_json( |
| 455 |
msg=parse_fail_message(code, response) |
| 456 |
+ " Failed to discard session" |
| 457 |
" with error {0} with message {1}".format( |
| 458 |
discard_code, discard_response |
| 459 |
) |
| 460 |
) |
| 461 |
|
| 462 |
module.fail_json( |
| 463 |
msg=parse_fail_message(code, response) |
| 464 |
+ " Unpublished changes were discarded" |
| 465 |
) |
| 466 |
|
| 467 |
|
| 468 |
# handle publish command, and wait for it to end if the user asked so |
| 469 |
def handle_publish(module, connection, version): |
| 470 |
if ( |
| 471 |
"auto_publish_session" in module.params |
| 472 |
and module.params["auto_publish_session"] |
| 473 |
): |
| 474 |
publish_code, publish_response = send_request( |
| 475 |
connection, version, "publish" |
| 476 |
) |
| 477 |
if publish_code != 200: |
| 478 |
discard_and_fail( |
| 479 |
module, publish_code, publish_response, connection, version |
| 480 |
) |
| 481 |
if module.params["wait_for_task"]: |
| 482 |
wait_for_task( |
| 483 |
module, version, connection, publish_response["task-id"] |
| 484 |
) |
| 485 |
|
| 486 |
|
| 487 |
# if user insert a specific version, we add it to the url |
| 488 |
def get_version(module): |
| 489 |
return ( |
| 490 |
("v" + module.params["version"] + "/") |
| 491 |
if module.params.get("version") |
| 492 |
else "" |
| 493 |
) |
| 494 |
|
| 495 |
|
| 496 |
# if code is 400 (bad request) or 500 (internal error) - fail |
| 497 |
def handle_equals_failure(module, equals_code, equals_response): |
| 498 |
if equals_code == 400 or equals_code == 500: |
| 499 |
module.fail_json(msg=parse_fail_message(equals_code, equals_response)) |
| 500 |
if ( |
| 501 |
equals_code == 404 |
| 502 |
and equals_response["code"] == "generic_err_command_not_found" |
| 503 |
): |
| 504 |
module.fail_json( |
| 505 |
msg="Relevant hotfix is not installed on Check Point server. See sk114661 on Check Point Support Center." |
| 506 |
) |
| 507 |
|
| 508 |
|
| 509 |
# handle call |
| 510 |
def handle_call( |
| 511 |
connection, |
| 512 |
version, |
| 513 |
call, |
| 514 |
payload, |
| 515 |
module, |
| 516 |
to_publish, |
| 517 |
to_discard_on_failure, |
| 518 |
): |
| 519 |
code, response = send_request(connection, version, call, payload) |
| 520 |
if code != 200: |
| 521 |
if to_discard_on_failure: |
| 522 |
discard_and_fail(module, code, response, connection, version) |
| 523 |
else: |
| 524 |
module.fail_json(msg=parse_fail_message(code, response)) |
| 525 |
else: |
| 526 |
if "wait_for_task" in module.params and module.params["wait_for_task"]: |
| 527 |
if "task-id" in response: |
| 528 |
response = wait_for_task( |
| 529 |
module, version, connection, response["task-id"] |
| 530 |
) |
| 531 |
elif "tasks" in response: |
| 532 |
for task in response["tasks"]: |
| 533 |
if "task-id" in task: |
| 534 |
task_id = task["task-id"] |
| 535 |
response[task_id] = wait_for_task( |
| 536 |
module, version, connection, task["task-id"] |
| 537 |
) |
| 538 |
del response["tasks"] |
| 539 |
if to_publish: |
| 540 |
handle_publish(module, connection, version) |
| 541 |
return response |
| 542 |
|
| 543 |
|
| 544 |
# handle a command |
| 545 |
def api_command(module, command): |
| 546 |
payload = get_payload_from_parameters(module.params) |
| 547 |
connection = Connection(module._socket_path) |
| 548 |
version = get_version(module) |
| 549 |
|
| 550 |
code, response = send_request(connection, version, command, payload) |
| 551 |
result = {"changed": True} |
| 552 |
|
| 553 |
if is_no_changes_command(command): |
| 554 |
result['changed'] = False |
| 555 |
|
| 556 |
if code == 200: |
| 557 |
if module.params["wait_for_task"]: |
| 558 |
if "task-id" in response: |
| 559 |
response = wait_for_task( |
| 560 |
module, version, connection, response["task-id"] |
| 561 |
) |
| 562 |
elif "tasks" in response: |
| 563 |
for task in response["tasks"]: |
| 564 |
if "task-id" in task: |
| 565 |
task_id = task["task-id"] |
| 566 |
response[task_id] = wait_for_task( |
| 567 |
module, version, connection, task["task-id"] |
| 568 |
) |
| 569 |
del response["tasks"] |
| 570 |
|
| 571 |
result[command] = response |
| 572 |
|
| 573 |
handle_publish(module, connection, version) |
| 574 |
else: |
| 575 |
if is_no_changes_command(command): |
| 576 |
module.fail_json(msg=parse_fail_message(code, response)) |
| 577 |
else: |
| 578 |
discard_and_fail(module, code, response, connection, version) |
| 579 |
|
| 580 |
return result |
| 581 |
|
| 582 |
|
| 583 |
# handle api call facts |
| 584 |
def api_call_facts(module, api_call_object, api_call_object_plural_version): |
| 585 |
payload = get_payload_from_parameters(module.params) |
| 586 |
connection = Connection(module._socket_path) |
| 587 |
version = get_version(module) |
| 588 |
|
| 589 |
# if there isn't an identifier param, the API command will be in plural version (e.g. show-hosts instead of show-host) |
| 590 |
if not contains_show_identifier_param(payload): |
| 591 |
api_call_object = api_call_object_plural_version |
| 592 |
|
| 593 |
response = handle_call( |
| 594 |
connection, |
| 595 |
version, |
| 596 |
"show-" + api_call_object, |
| 597 |
payload, |
| 598 |
module, |
| 599 |
False, |
| 600 |
False, |
| 601 |
) |
| 602 |
result = {api_call_object.replace("-", "_"): response} |
| 603 |
return result |
| 604 |
|
| 605 |
|
| 606 |
# handle delete |
| 607 |
def handle_delete( |
| 608 |
equals_code, |
| 609 |
payload, |
| 610 |
delete_params, |
| 611 |
connection, |
| 612 |
version, |
| 613 |
api_call_object, |
| 614 |
module, |
| 615 |
result, |
| 616 |
): |
| 617 |
# else equals_code is 404 and no need to delete because he doesn't exist |
| 618 |
if equals_code == 200: |
| 619 |
if module.check_mode: |
| 620 |
result["changed"] = True |
| 621 |
result["message"] = "This would delete the object" |
| 622 |
return |
| 623 |
payload_for_delete = extract_payload_with_some_params( |
| 624 |
payload, delete_params |
| 625 |
) |
| 626 |
response = handle_call( |
| 627 |
connection, |
| 628 |
version, |
| 629 |
"delete-" + api_call_object, |
| 630 |
payload_for_delete, |
| 631 |
module, |
| 632 |
True, |
| 633 |
True, |
| 634 |
) |
| 635 |
result["changed"] = True |
| 636 |
else: |
| 637 |
if module.check_mode: |
| 638 |
result["changed"] = False |
| 639 |
result["message"] = "The object does not exist, no deletion would occur" |
| 640 |
|
| 641 |
|
| 642 |
# handle the call and set the result with 'changed' and the response |
| 643 |
def handle_call_and_set_result( |
| 644 |
connection, version, call, payload, module, result |
| 645 |
): |
| 646 |
response = handle_call( |
| 647 |
connection, version, call, payload, module, True, True |
| 648 |
) |
| 649 |
result["changed"] = True |
| 650 |
result[call] = response |
| 651 |
|
| 652 |
|
| 653 |
# handle api call |
| 654 |
def api_call(module, api_call_object): |
| 655 |
payload = get_payload_from_parameters(module.params) |
| 656 |
connection = Connection(module._socket_path) |
| 657 |
version = get_version(module) |
| 658 |
|
| 659 |
result = {"changed": False} |
| 660 |
|
| 661 |
payload_for_equals = {"type": api_call_object, "params": payload} |
| 662 |
equals_code, equals_response = send_request( |
| 663 |
connection, version, "equals", payload_for_equals |
| 664 |
) |
| 665 |
handle_equals_failure(module, equals_code, equals_response) |
| 666 |
|
| 667 |
if module.params["state"] == "present": |
| 668 |
if equals_code == 200: |
| 669 |
# else objects are equals and there is no need for set request |
| 670 |
if not equals_response["equals"]: |
| 671 |
if module.check_mode: |
| 672 |
return {"changed": True, "message": "This would edit the object"} |
| 673 |
build_payload( |
| 674 |
api_call_object, payload, remove_from_set_payload |
| 675 |
) |
| 676 |
handle_call_and_set_result( |
| 677 |
connection, |
| 678 |
version, |
| 679 |
"set-" + api_call_object, |
| 680 |
payload, |
| 681 |
module, |
| 682 |
result, |
| 683 |
) |
| 684 |
else: |
| 685 |
if module.check_mode: |
| 686 |
return {"changed": False, "message": "Object exists with desired configuration"} |
| 687 |
elif equals_code == 404: |
| 688 |
if module.check_mode: |
| 689 |
return {"changed": True, "message": "This would create the object"} |
| 690 |
build_payload(api_call_object, payload, remove_from_add_payload) |
| 691 |
handle_call_and_set_result( |
| 692 |
connection, |
| 693 |
version, |
| 694 |
"add-" + api_call_object, |
| 695 |
payload, |
| 696 |
module, |
| 697 |
result, |
| 698 |
) |
| 699 |
elif module.params["state"] == "absent": |
| 700 |
handle_delete( |
| 701 |
equals_code, |
| 702 |
payload, |
| 703 |
delete_params, |
| 704 |
connection, |
| 705 |
version, |
| 706 |
api_call_object, |
| 707 |
module, |
| 708 |
result, |
| 709 |
) |
| 710 |
if not module.check_mode: |
| 711 |
result["checkpoint_session_uid"] = connection.get_session_uid() |
| 712 |
return result |
| 713 |
|
| 714 |
|
| 715 |
# returns a generator of the entire rulebase. show_rulebase_identifier_payload can be either package or layer |
| 716 |
def get_rulebase_generator( |
| 717 |
connection, version, show_rulebase_identifier_payload, show_rulebase_command, rules_amount |
| 718 |
): |
| 719 |
offset = 0 |
| 720 |
limit = 100 |
| 721 |
while True: |
| 722 |
payload_for_show_rulebase = { |
| 723 |
"limit": limit, |
| 724 |
"offset": offset, |
| 725 |
} |
| 726 |
payload_for_show_rulebase.update(show_rulebase_identifier_payload) |
| 727 |
# in case there are empty sections after the last rule, we need them to appear in the reply and the limit might |
| 728 |
# cut them out |
| 729 |
if offset + limit >= rules_amount: |
| 730 |
del payload_for_show_rulebase["limit"] |
| 731 |
code, response = send_request( |
| 732 |
connection, |
| 733 |
version, |
| 734 |
show_rulebase_command, |
| 735 |
payload_for_show_rulebase, |
| 736 |
) |
| 737 |
offset = response["to"] |
| 738 |
total = response["total"] |
| 739 |
rulebase = response["rulebase"] |
| 740 |
yield rulebase |
| 741 |
if total <= offset: |
| 742 |
return |
| 743 |
|
| 744 |
|
| 745 |
# get 'to' or 'from' of given section |
| 746 |
def get_edge_position_in_section( |
| 747 |
connection, version, identifier, section_name, edge |
| 748 |
): |
| 749 |
code, response = send_request( |
| 750 |
connection, |
| 751 |
version, |
| 752 |
"show-layer-structure", |
| 753 |
{"name": identifier, "details-level": "uid"}, |
| 754 |
) |
| 755 |
if 'code' in response and response["code"] == "generic_err_command_not_found": |
| 756 |
raise ValueError( |
| 757 |
"The use of the relative_position field with a section as its value is available only for" |
| 758 |
" version 1.7.1 with JHF take 42 and above" |
| 759 |
) |
| 760 |
sections_in_layer = response["root-section"]["children"] |
| 761 |
for section in sections_in_layer: |
| 762 |
if section["name"] == section_name: |
| 763 |
return int(section[edge + "-rule"]) |
| 764 |
|
| 765 |
return None |
| 766 |
|
| 767 |
|
| 768 |
# return the total amount of rules in the rulebase of the given layer |
| 769 |
def get_rules_amount(connection, version, show_rulebase_payload, show_rulebase_command): |
| 770 |
payload = {"limit": 0} |
| 771 |
payload.update(show_rulebase_payload) |
| 772 |
code, response = send_request( |
| 773 |
connection, |
| 774 |
version, |
| 775 |
show_rulebase_command, |
| 776 |
payload, |
| 777 |
) |
| 778 |
return int(response["total"]) |
| 779 |
|
| 780 |
|
| 781 |
def keep_searching_rulebase( |
| 782 |
position, current_section, relative_position, relative_position_is_section |
| 783 |
): |
| 784 |
position_not_found = position is None |
| 785 |
if relative_position_is_section and "above" not in relative_position: |
| 786 |
# if 'above' in relative_position then get_number_and_section_from_relative_position returns the previous section |
| 787 |
# so there isn't a need to further search for the relative section |
| 788 |
relative_section = list(relative_position.values())[0] |
| 789 |
return position_not_found or current_section != relative_section |
| 790 |
# if relative position is a rule then get_number_and_section_from_relative_position has already entered the section |
| 791 |
# (if exists) that the relative rule is in |
| 792 |
return position_not_found |
| 793 |
|
| 794 |
|
| 795 |
def relative_position_is_section( |
| 796 |
connection, version, api_call_object, layer_or_package_payload, relative_position |
| 797 |
): |
| 798 |
if "top" in relative_position or "bottom" in relative_position: |
| 799 |
return True |
| 800 |
|
| 801 |
show_section_command = "show-access-section" if 'access' in api_call_object else "show-nat-section" |
| 802 |
relative_position_value = list(relative_position.values())[0] |
| 803 |
payload = {"name": relative_position_value} |
| 804 |
payload.update(layer_or_package_payload) |
| 805 |
code, response = send_request( |
| 806 |
connection, |
| 807 |
version, |
| 808 |
show_section_command, |
| 809 |
payload, |
| 810 |
) |
| 811 |
if code == 200: |
| 812 |
return True |
| 813 |
return False |
| 814 |
|
| 815 |
|
| 816 |
def get_number_and_section_from_relative_position( |
| 817 |
payload, |
| 818 |
connection, |
| 819 |
version, |
| 820 |
rulebase, |
| 821 |
above_relative_position, |
| 822 |
pos_before_relative_empty_section, |
| 823 |
api_call_object, |
| 824 |
prev_section=None, |
| 825 |
current_section=None, |
| 826 |
): |
| 827 |
section_name = current_section |
| 828 |
position = None |
| 829 |
for rules in rulebase: |
| 830 |
if "rulebase" in rules: |
| 831 |
# cases relevant for relative-position=section |
| 832 |
if ( |
| 833 |
"above" in payload["position"] |
| 834 |
and rules["name"] == payload["position"]["above"] |
| 835 |
): |
| 836 |
if len(rules["rulebase"]) == 0: |
| 837 |
position = ( |
| 838 |
pos_before_relative_empty_section |
| 839 |
if above_relative_position |
| 840 |
else pos_before_relative_empty_section + 1 |
| 841 |
) |
| 842 |
else: |
| 843 |
# if the entire section isn't present in rulebase, the 'from' value of the section might not be |
| 844 |
# the first position in the section, which is why we use get_edge_position_in_section |
| 845 |
from_value = get_edge_position_in_section( |
| 846 |
connection, |
| 847 |
version, |
| 848 |
list(get_relevant_layer_or_package_identifier(api_call_object, payload).values())[0], |
| 849 |
rules["name"], |
| 850 |
"from", |
| 851 |
) |
| 852 |
if from_value is not None: # section exists in rulebase |
| 853 |
position = ( |
| 854 |
max(from_value - 1, 1) |
| 855 |
if above_relative_position |
| 856 |
else from_value |
| 857 |
) |
| 858 |
return ( |
| 859 |
position, |
| 860 |
section_name, |
| 861 |
above_relative_position, |
| 862 |
pos_before_relative_empty_section, |
| 863 |
prev_section, |
| 864 |
) |
| 865 |
|
| 866 |
# we update this only after the 'above' case since the section that should be returned in that case isn't |
| 867 |
# the one we are currently iterating over (but the one beforehand) |
| 868 |
prev_section = section_name |
| 869 |
section_name = rules["name"] |
| 870 |
|
| 871 |
if ( |
| 872 |
"bottom" in payload["position"] |
| 873 |
and rules["name"] == payload["position"]["bottom"] |
| 874 |
): |
| 875 |
if len(rules["rulebase"]) == 0: |
| 876 |
position = ( |
| 877 |
pos_before_relative_empty_section |
| 878 |
if above_relative_position |
| 879 |
else pos_before_relative_empty_section + 1 |
| 880 |
) |
| 881 |
else: |
| 882 |
# if the entire section isn't present in rulebase, the 'to' value of the section might not be the |
| 883 |
# last position in the section, which is why we use get_edge_position_in_section |
| 884 |
to_value = get_edge_position_in_section( |
| 885 |
connection, |
| 886 |
version, |
| 887 |
list(get_relevant_layer_or_package_identifier(api_call_object, payload).values())[0], |
| 888 |
section_name, |
| 889 |
"to", |
| 890 |
) |
| 891 |
if to_value is not None and to_value == int( |
| 892 |
rules["to"] |
| 893 |
): # meaning the entire section is present in rulebase |
| 894 |
# is the rule already at the bottom of the section. Can infer this only if the entire section is |
| 895 |
# present in rulebase |
| 896 |
is_bottom = ( |
| 897 |
rules["rulebase"][-1]["name"] == payload["name"] |
| 898 |
) |
| 899 |
position = ( |
| 900 |
to_value |
| 901 |
if (above_relative_position or is_bottom) |
| 902 |
else to_value + 1 |
| 903 |
) |
| 904 |
# else: need to keep searching the rulebase, so position=None is returned |
| 905 |
return ( |
| 906 |
position, |
| 907 |
section_name, |
| 908 |
above_relative_position, |
| 909 |
pos_before_relative_empty_section, |
| 910 |
prev_section, |
| 911 |
) |
| 912 |
|
| 913 |
# setting a rule 'below' a section is equivalent to setting the rule at the top of that section |
| 914 |
if ( |
| 915 |
"below" in payload["position"] |
| 916 |
and section_name == payload["position"]["below"] |
| 917 |
) or ( |
| 918 |
"top" in payload["position"] |
| 919 |
and section_name == payload["position"]["top"] |
| 920 |
): |
| 921 |
if len(rules["rulebase"]) == 0: |
| 922 |
position = ( |
| 923 |
pos_before_relative_empty_section |
| 924 |
if above_relative_position |
| 925 |
else pos_before_relative_empty_section + 1 |
| 926 |
) |
| 927 |
else: |
| 928 |
# is the rule already at the top of the section |
| 929 |
is_top = rules["rulebase"][0]["name"] == payload["name"] |
| 930 |
position = ( |
| 931 |
max(int(rules["from"]) - 1, 1) |
| 932 |
if (above_relative_position or not is_top) |
| 933 |
else int(rules["from"]) |
| 934 |
) |
| 935 |
return ( |
| 936 |
position, |
| 937 |
section_name, |
| 938 |
above_relative_position, |
| 939 |
pos_before_relative_empty_section, |
| 940 |
prev_section, |
| 941 |
) |
| 942 |
|
| 943 |
if len(rules["rulebase"]) != 0: |
| 944 |
# if search_entire_rulebase=True: even if rules['rulebase'] is cut (due to query limit) this will |
| 945 |
# eventually be updated to the correct value in further calls |
| 946 |
pos_before_relative_empty_section = int(rules["to"]) |
| 947 |
|
| 948 |
rules = rules["rulebase"] |
| 949 |
for rule in rules: |
| 950 |
if payload["name"] == rule["name"]: |
| 951 |
above_relative_position = True |
| 952 |
# cases relevant for relative-position=rule |
| 953 |
if ( |
| 954 |
"below" in payload["position"] |
| 955 |
and rule["name"] == payload["position"]["below"] |
| 956 |
): |
| 957 |
position = ( |
| 958 |
int(rule["rule-number"]) |
| 959 |
if above_relative_position |
| 960 |
else int(rule["rule-number"]) + 1 |
| 961 |
) |
| 962 |
return ( |
| 963 |
position, |
| 964 |
section_name, |
| 965 |
above_relative_position, |
| 966 |
pos_before_relative_empty_section, |
| 967 |
prev_section, |
| 968 |
) |
| 969 |
elif ( |
| 970 |
"above" in payload["position"] |
| 971 |
and rule["name"] == payload["position"]["above"] |
| 972 |
): |
| 973 |
position = ( |
| 974 |
max(int(rule["rule-number"]) - 1, 1) |
| 975 |
if above_relative_position |
| 976 |
else int(rule["rule-number"]) |
| 977 |
) |
| 978 |
return ( |
| 979 |
position, |
| 980 |
section_name, |
| 981 |
above_relative_position, |
| 982 |
pos_before_relative_empty_section, |
| 983 |
prev_section, |
| 984 |
) |
| 985 |
|
| 986 |
else: # cases relevant for relative-position=rule |
| 987 |
if payload["name"] == rules["name"]: |
| 988 |
above_relative_position = True |
| 989 |
if ( |
| 990 |
"below" in payload["position"] |
| 991 |
and rules["name"] == payload["position"]["below"] |
| 992 |
): |
| 993 |
position = ( |
| 994 |
int(rules["rule-number"]) |
| 995 |
if above_relative_position |
| 996 |
else int(rules["rule-number"]) + 1 |
| 997 |
) |
| 998 |
return ( |
| 999 |
position, |
| 1000 |
section_name, |
| 1001 |
above_relative_position, |
| 1002 |
pos_before_relative_empty_section, |
| 1003 |
prev_section, |
| 1004 |
) |
| 1005 |
elif ( |
| 1006 |
"above" in payload["position"] |
| 1007 |
and rules["name"] == payload["position"]["above"] |
| 1008 |
): |
| 1009 |
position = ( |
| 1010 |
max(int(rules["rule-number"]) - 1, 1) |
| 1011 |
if above_relative_position |
| 1012 |
else int(rules["rule-number"]) |
| 1013 |
) |
| 1014 |
return ( |
| 1015 |
position, |
| 1016 |
section_name, |
| 1017 |
above_relative_position, |
| 1018 |
pos_before_relative_empty_section, |
| 1019 |
prev_section, |
| 1020 |
) |
| 1021 |
|
| 1022 |
return ( |
| 1023 |
position, |
| 1024 |
section_name, |
| 1025 |
above_relative_position, |
| 1026 |
pos_before_relative_empty_section, |
| 1027 |
prev_section, |
| 1028 |
) # None, None, False/True, x>=1, None |
| 1029 |
|
| 1030 |
|
| 1031 |
# get the position in integer format and the section it is. |
| 1032 |
def get_number_and_section_from_position( |
| 1033 |
payload, connection, version, api_call_object |
| 1034 |
): |
| 1035 |
show_rulebase_command = get_relevant_show_rulebase_command(api_call_object) |
| 1036 |
if "position" in payload: |
| 1037 |
section_name = None |
| 1038 |
if not isinstance(payload["position"], dict): |
| 1039 |
position = payload["position"] |
| 1040 |
if position == "top": |
| 1041 |
position = 1 |
| 1042 |
return position, section_name |
| 1043 |
elif position == "bottom": |
| 1044 |
show_rulebase_payload = get_relevant_show_rulebase_identifier_payload(api_call_object, payload) |
| 1045 |
position = get_rules_amount( |
| 1046 |
connection, |
| 1047 |
version, |
| 1048 |
show_rulebase_payload, |
| 1049 |
show_rulebase_command, |
| 1050 |
) |
| 1051 |
show_rulebase_payload.update({"offset": position - 1}) |
| 1052 |
code, response = send_request( |
| 1053 |
connection, |
| 1054 |
version, |
| 1055 |
show_rulebase_command, |
| 1056 |
show_rulebase_payload, |
| 1057 |
) |
| 1058 |
rulebase = reversed(response["rulebase"]) |
| 1059 |
else: # is a number so we need to get the section (if exists) of the rule in that position |
| 1060 |
position = int(position) |
| 1061 |
payload_for_show_obj_rulebase = build_rulebase_payload( |
| 1062 |
api_call_object, payload, position |
| 1063 |
) |
| 1064 |
code, response = send_request( |
| 1065 |
connection, |
| 1066 |
version, |
| 1067 |
show_rulebase_command, |
| 1068 |
payload_for_show_obj_rulebase, |
| 1069 |
) |
| 1070 |
rulebase = response["rulebase"] |
| 1071 |
if position > response["total"]: |
| 1072 |
raise ValueError( |
| 1073 |
"The given position " |
| 1074 |
+ str(position) |
| 1075 |
+ " of rule " |
| 1076 |
+ payload["name"] |
| 1077 |
+ "exceeds the total amount of rules in the rulebase" |
| 1078 |
) |
| 1079 |
# in case position=1 and there are empty sections at the beginning of the rulebase we want to skip them |
| 1080 |
i = 0 |
| 1081 |
for rules in rulebase: |
| 1082 |
if "rulebase" in rules and len(rules["rulebase"]) == 0: |
| 1083 |
i += 1 |
| 1084 |
rulebase = rulebase[i:] |
| 1085 |
|
| 1086 |
for rules in rulebase: |
| 1087 |
if "rulebase" in rules: |
| 1088 |
section_name = rules["name"] |
| 1089 |
return position, section_name |
| 1090 |
else: |
| 1091 |
return position, section_name # section = None |
| 1092 |
|
| 1093 |
else: |
| 1094 |
search_entire_rulebase = payload["search-entire-rulebase"] |
| 1095 |
position = None |
| 1096 |
# is the rule we're getting its position number above the rule it is relatively positioned to |
| 1097 |
above_relative_position = False |
| 1098 |
# no from-to in empty sections so can't infer the position from them -> need to keep track of the position |
| 1099 |
# before the empty relative section |
| 1100 |
pos_before_relative_empty_section = 1 |
| 1101 |
show_rulebase_payload = get_relevant_show_rulebase_identifier_payload(api_call_object, payload) |
| 1102 |
if not search_entire_rulebase: |
| 1103 |
code, response = send_request( |
| 1104 |
connection, |
| 1105 |
version, |
| 1106 |
show_rulebase_command, |
| 1107 |
show_rulebase_payload, |
| 1108 |
) |
| 1109 |
rulebase = response["rulebase"] |
| 1110 |
( |
| 1111 |
position, |
| 1112 |
section_name, |
| 1113 |
above_relative_position, |
| 1114 |
pos_before_relative_empty_section, |
| 1115 |
prev_section |
| 1116 |
) = get_number_and_section_from_relative_position( |
| 1117 |
payload, |
| 1118 |
connection, |
| 1119 |
version, |
| 1120 |
rulebase, |
| 1121 |
above_relative_position, |
| 1122 |
pos_before_relative_empty_section, |
| 1123 |
api_call_object, |
| 1124 |
) |
| 1125 |
else: |
| 1126 |
layer_or_package_payload = get_relevant_layer_or_package_identifier(api_call_object, payload) |
| 1127 |
rules_amount = get_rules_amount( |
| 1128 |
connection, |
| 1129 |
version, |
| 1130 |
show_rulebase_payload, |
| 1131 |
show_rulebase_command, |
| 1132 |
) |
| 1133 |
relative_pos_is_section = relative_position_is_section( |
| 1134 |
connection, version, api_call_object, layer_or_package_payload, payload["position"] |
| 1135 |
) |
| 1136 |
rulebase_generator = get_rulebase_generator( |
| 1137 |
connection, |
| 1138 |
version, |
| 1139 |
show_rulebase_payload, |
| 1140 |
show_rulebase_command, |
| 1141 |
rules_amount, |
| 1142 |
) |
| 1143 |
# need to keep track of the previous section in case the iteration starts with a new section and |
| 1144 |
# we want to set the rule above a section - so the section the rule should be at is the previous one |
| 1145 |
prev_section = None |
| 1146 |
for rulebase in rulebase_generator: |
| 1147 |
( |
| 1148 |
position, |
| 1149 |
section_name, |
| 1150 |
above_relative_position, |
| 1151 |
pos_before_relative_empty_section, |
| 1152 |
prev_section, |
| 1153 |
) = get_number_and_section_from_relative_position( |
| 1154 |
payload, |
| 1155 |
connection, |
| 1156 |
version, |
| 1157 |
rulebase, |
| 1158 |
above_relative_position, |
| 1159 |
pos_before_relative_empty_section, |
| 1160 |
api_call_object, |
| 1161 |
prev_section, |
| 1162 |
section_name, |
| 1163 |
) |
| 1164 |
if not keep_searching_rulebase( |
| 1165 |
position, |
| 1166 |
section_name, |
| 1167 |
payload["position"], |
| 1168 |
relative_pos_is_section, |
| 1169 |
): |
| 1170 |
break |
| 1171 |
|
| 1172 |
return position, section_name |
| 1173 |
return None, None |
| 1174 |
|
| 1175 |
|
| 1176 |
# build the show rulebase payload |
| 1177 |
def build_rulebase_payload(api_call_object, payload, position_number): |
| 1178 |
show_rulebase_required_identifiers_payload = get_relevant_show_rulebase_identifier_payload(api_call_object, payload) |
| 1179 |
show_rulebase_required_identifiers_payload.update({'offset': position_number - 1, 'limit': 1}) |
| 1180 |
return show_rulebase_required_identifiers_payload |
| 1181 |
|
| 1182 |
|
| 1183 |
def build_rulebase_command(api_call_object): |
| 1184 |
rulebase_command = "show-" + api_call_object + "base" |
| 1185 |
|
| 1186 |
if api_call_object == "threat-exception": |
| 1187 |
rulebase_command = "show-threat-rule-exception-rulebase" |
| 1188 |
|
| 1189 |
return rulebase_command |
| 1190 |
|
| 1191 |
|
| 1192 |
# remove from payload unrecognized params (used for cases where add payload differs from that of a set) |
| 1193 |
def build_payload(api_call_object, payload, params_to_remove): |
| 1194 |
if api_call_object in params_to_remove: |
| 1195 |
for param in params_to_remove[api_call_object]: |
| 1196 |
del payload[param] |
| 1197 |
|
| 1198 |
return payload |
| 1199 |
|
| 1200 |
|
| 1201 |
# extract first rule from given rulebase response and the section it is in. |
| 1202 |
def extract_rule_and_section_from_rulebase_response(response): |
| 1203 |
section_name = None |
| 1204 |
rule = response["rulebase"][0] |
| 1205 |
i = 0 |
| 1206 |
# skip empty sections (possible when offset=0) |
| 1207 |
while "rulebase" in rule and len(rule["rulebase"]) == 0: |
| 1208 |
i += 1 |
| 1209 |
rule = response["rulebase"][i] |
| 1210 |
|
| 1211 |
while "rulebase" in rule: |
| 1212 |
section_name = rule["name"] |
| 1213 |
rule = rule["rulebase"][0] |
| 1214 |
|
| 1215 |
return rule, section_name |
| 1216 |
|
| 1217 |
|
| 1218 |
def get_relevant_show_rulebase_command(api_call_object): |
| 1219 |
if api_call_object == "access-rule": |
| 1220 |
return "show-access-rulebase" |
| 1221 |
elif api_call_object == "threat-rule": |
| 1222 |
return "show-threat-rulebase" |
| 1223 |
elif api_call_object == "threat-exception": |
| 1224 |
return "show-threat-rule-exception-rulebase" |
| 1225 |
elif api_call_object == 'nat-rule': |
| 1226 |
return 'show-nat-rulebase' |
| 1227 |
elif api_call_object == 'https-rule': |
| 1228 |
return 'show-https-rulebase' |
| 1229 |
elif api_call_object == 'mobile-access-rule': |
| 1230 |
return 'show-mobile-access-rulebase' |
| 1231 |
elif api_call_object == 'mobile-access-profile-rule': |
| 1232 |
return 'show-mobile-access-profile-rulebase' |
| 1233 |
|
| 1234 |
|
| 1235 |
# returns the show rulebase payload with the relevant required identifiers params |
| 1236 |
def get_relevant_show_rulebase_identifier_payload(api_call_object, payload): |
| 1237 |
show_rulebase_payload = {} |
| 1238 |
if api_call_object == 'nat-rule': |
| 1239 |
show_rulebase_payload = {'package': payload['package']} |
| 1240 |
|
| 1241 |
# mobile-access-x apis don't have an identifier in show rulebase command |
| 1242 |
elif 'mobile-access' not in api_call_object: |
| 1243 |
show_rulebase_payload = {'name': payload['layer']} |
| 1244 |
|
| 1245 |
if api_call_object == 'threat-exception': |
| 1246 |
show_rulebase_payload['rule-name'] = payload['rule-name'] |
| 1247 |
|
| 1248 |
return show_rulebase_payload |
| 1249 |
|
| 1250 |
|
| 1251 |
# returns the show section/rule payload with the relevant required identifying package/layer |
| 1252 |
def get_relevant_layer_or_package_identifier(api_call_object, payload): |
| 1253 |
if 'nat' in api_call_object: |
| 1254 |
identifier = {'package': payload['package']} |
| 1255 |
|
| 1256 |
else: |
| 1257 |
identifier = {'layer': payload['layer']} |
| 1258 |
|
| 1259 |
return identifier |
| 1260 |
|
| 1261 |
|
| 1262 |
# is the param position (if the user inserted it) equals between the object and the user input, as well as the section the rule is in |
| 1263 |
def is_equals_with_position_param( |
| 1264 |
payload, connection, version, api_call_object |
| 1265 |
): |
| 1266 |
( |
| 1267 |
position_number, |
| 1268 |
section_according_to_position, |
| 1269 |
) = get_number_and_section_from_position( |
| 1270 |
payload, connection, version, api_call_object |
| 1271 |
) |
| 1272 |
|
| 1273 |
# In this case the one of the following has occurred: |
| 1274 |
# 1) There is no position param, then it's equals in vacuous truth |
| 1275 |
# 2) search_entire_rulebase = False so it's possible the relative rule wasn't found in the default limit or maybe doesn't even exist |
| 1276 |
# 3) search_entire_rulebase = True and the relative rule/section doesn't exist |
| 1277 |
if position_number is None: |
| 1278 |
return True |
| 1279 |
|
| 1280 |
rulebase_payload = build_rulebase_payload( |
| 1281 |
api_call_object, payload, position_number |
| 1282 |
) |
| 1283 |
rulebase_command = build_rulebase_command(api_call_object) |
| 1284 |
|
| 1285 |
code, response = send_request( |
| 1286 |
connection, version, rulebase_command, rulebase_payload |
| 1287 |
) |
| 1288 |
rule, section = extract_rule_and_section_from_rulebase_response(response) |
| 1289 |
|
| 1290 |
# if the names of the exist rule and the user input rule are equals, as well as the section they're in, then it |
| 1291 |
# means that their positions are equals so I return True. and there is no way that there is another rule with this |
| 1292 |
# name cause otherwise the 'equals' command would fail |
| 1293 |
if ( |
| 1294 |
rule["name"] == payload["name"] |
| 1295 |
and section_according_to_position == section |
| 1296 |
): |
| 1297 |
return True |
| 1298 |
else: |
| 1299 |
return False |
| 1300 |
|
| 1301 |
|
| 1302 |
# get copy of the payload without some of the params |
| 1303 |
def extract_payload_without_some_params(payload, params_to_remove): |
| 1304 |
copy_payload = dict(payload) |
| 1305 |
for param in params_to_remove: |
| 1306 |
if param in copy_payload: |
| 1307 |
del copy_payload[param] |
| 1308 |
return copy_payload |
| 1309 |
|
| 1310 |
|
| 1311 |
# get copy of the payload with only some of the params |
| 1312 |
def extract_payload_with_some_params(payload, params_to_insert): |
| 1313 |
copy_payload = {} |
| 1314 |
for param in params_to_insert: |
| 1315 |
if param in payload: |
| 1316 |
copy_payload[param] = payload[param] |
| 1317 |
return copy_payload |
| 1318 |
|
| 1319 |
|
| 1320 |
# is equals with all the params including action and position |
| 1321 |
def is_equals_with_all_params( |
| 1322 |
payload, connection, version, api_call_object, is_access_rule |
| 1323 |
): |
| 1324 |
if is_access_rule and "action" in payload: |
| 1325 |
payload_for_show = extract_payload_with_some_params( |
| 1326 |
payload, ["name", "uid", "layer"] |
| 1327 |
) |
| 1328 |
code, response = send_request( |
| 1329 |
connection, version, "show-" + api_call_object, payload_for_show |
| 1330 |
) |
| 1331 |
exist_action = response["action"]["name"] |
| 1332 |
if exist_action.lower() != payload["action"].lower(): |
| 1333 |
if ( |
| 1334 |
payload["action"].lower() != "Apply Layer".lower() |
| 1335 |
or exist_action.lower() != "Inner Layer".lower() |
| 1336 |
): |
| 1337 |
return False |
| 1338 |
|
| 1339 |
# here the action is equals, so check the position param |
| 1340 |
if not is_equals_with_position_param( |
| 1341 |
payload, connection, version, api_call_object |
| 1342 |
): |
| 1343 |
return False |
| 1344 |
|
| 1345 |
return True |
| 1346 |
|
| 1347 |
|
| 1348 |
# handle api call for rule |
| 1349 |
def api_call_for_rule(module, api_call_object): |
| 1350 |
is_access_rule = True if "access" in api_call_object else False |
| 1351 |
payload = get_payload_from_parameters(module.params) |
| 1352 |
connection = Connection(module._socket_path) |
| 1353 |
version = get_version(module) |
| 1354 |
|
| 1355 |
result = {"changed": False} |
| 1356 |
|
| 1357 |
if is_access_rule: |
| 1358 |
copy_payload_without_some_params = extract_payload_without_some_params( |
| 1359 |
payload, ["action", "position", "search_entire_rulebase"] |
| 1360 |
) |
| 1361 |
else: |
| 1362 |
copy_payload_without_some_params = extract_payload_without_some_params( |
| 1363 |
payload, ["position"] |
| 1364 |
) |
| 1365 |
payload_for_equals = { |
| 1366 |
"type": api_call_object, |
| 1367 |
"params": copy_payload_without_some_params, |
| 1368 |
} |
| 1369 |
equals_code, equals_response = send_request( |
| 1370 |
connection, version, "equals", payload_for_equals |
| 1371 |
) |
| 1372 |
result["checkpoint_session_uid"] = connection.get_session_uid() |
| 1373 |
handle_equals_failure(module, equals_code, equals_response) |
| 1374 |
|
| 1375 |
if module.params["state"] == "present": |
| 1376 |
if equals_code == 200: |
| 1377 |
if equals_response["equals"]: |
| 1378 |
if not is_equals_with_all_params( |
| 1379 |
payload, |
| 1380 |
connection, |
| 1381 |
version, |
| 1382 |
api_call_object, |
| 1383 |
is_access_rule, |
| 1384 |
): |
| 1385 |
equals_response["equals"] = False |
| 1386 |
# else objects are equals and there is no need for set request |
| 1387 |
if not equals_response["equals"]: |
| 1388 |
if module.check_mode: |
| 1389 |
return {"changed": True, "message": "This would edit the object"} |
| 1390 |
# if user insert param 'position' and needed to use the 'set' command, change the param name to 'new-position' |
| 1391 |
if "position" in payload: |
| 1392 |
payload["new-position"] = payload["position"] |
| 1393 |
del payload["position"] |
| 1394 |
if "search-entire-rulebase" in payload: |
| 1395 |
del payload["search-entire-rulebase"] |
| 1396 |
handle_call_and_set_result( |
| 1397 |
connection, |
| 1398 |
version, |
| 1399 |
"set-" + api_call_object, |
| 1400 |
payload, |
| 1401 |
module, |
| 1402 |
result, |
| 1403 |
) |
| 1404 |
else: |
| 1405 |
if module.check_mode: |
| 1406 |
return {"changed": False, "message": "Object exists with desired configuration"} |
| 1407 |
elif equals_code == 404: |
| 1408 |
if module.check_mode: |
| 1409 |
return {"changed": True, "message": "This would create the object"} |
| 1410 |
if "search-entire-rulebase" in payload: |
| 1411 |
del payload["search-entire-rulebase"] |
| 1412 |
handle_call_and_set_result( |
| 1413 |
connection, |
| 1414 |
version, |
| 1415 |
"add-" + api_call_object, |
| 1416 |
payload, |
| 1417 |
module, |
| 1418 |
result, |
| 1419 |
) |
| 1420 |
elif module.params["state"] == "absent": |
| 1421 |
handle_delete( |
| 1422 |
equals_code, |
| 1423 |
payload, |
| 1424 |
delete_params, |
| 1425 |
connection, |
| 1426 |
version, |
| 1427 |
api_call_object, |
| 1428 |
module, |
| 1429 |
result, |
| 1430 |
) |
| 1431 |
if not module.check_mode: |
| 1432 |
result["checkpoint_session_uid"] = connection.get_session_uid() |
| 1433 |
return result |
| 1434 |
|
| 1435 |
|
| 1436 |
# check if call is in plural form |
| 1437 |
def call_is_plural(api_call_object, payload): |
| 1438 |
if ( |
| 1439 |
(payload.get("name") is not None or payload.get("rule-number") is not None) |
| 1440 |
and ("nat" in api_call_object or "mobile-access" in api_call_object) |
| 1441 |
): |
| 1442 |
return False |
| 1443 |
if ((payload.get("layer") is None and ("access" in api_call_object or "threat" |
| 1444 |
in api_call_object or "https" in api_call_object)) |
| 1445 |
or |
| 1446 |
(payload.get("package") is not None and "nat" in api_call_object)): |
| 1447 |
return True |
| 1448 |
return False |
| 1449 |
|
| 1450 |
|
| 1451 |
# handle api call facts for rule |
| 1452 |
def api_call_facts_for_rule( |
| 1453 |
module, api_call_object, api_call_object_plural_version |
| 1454 |
): |
| 1455 |
payload = get_payload_from_parameters(module.params) |
| 1456 |
connection = Connection(module._socket_path) |
| 1457 |
version = get_version(module) |
| 1458 |
|
| 1459 |
# if there is no layer, the API command will be in plural version (e.g. show-https-rulebase instead of show-https-rule) |
| 1460 |
if call_is_plural(api_call_object, payload): |
| 1461 |
api_call_object = api_call_object_plural_version |
| 1462 |
|
| 1463 |
response = handle_call( |
| 1464 |
connection, |
| 1465 |
version, |
| 1466 |
"show-" + api_call_object, |
| 1467 |
payload, |
| 1468 |
module, |
| 1469 |
False, |
| 1470 |
False, |
| 1471 |
) |
| 1472 |
result = {api_call_object: response} |
| 1473 |
return result |
| 1474 |
|
| 1475 |
|
| 1476 |
# The code from here till EOF will be deprecated when Rikis' modules will be deprecated |
| 1477 |
# checkpoint_argument_spec = dict( |
| 1478 |
# auto_publish_session=dict(type="bool", default=True), |
| 1479 |
# policy_package=dict(type="str", default="standard"), |
| 1480 |
# auto_install_policy=dict(type="bool", default=True), |
| 1481 |
# targets=dict(type="list"), |
| 1482 |
# ) |
| 1483 |
|
| 1484 |
|
| 1485 |
def publish(connection, uid=None): |
| 1486 |
payload = None |
| 1487 |
|
| 1488 |
if uid: |
| 1489 |
payload = {"uid": uid} |
| 1490 |
|
| 1491 |
connection.send_request("/web_api/publish", payload) |
| 1492 |
|
| 1493 |
|
| 1494 |
def discard(connection, uid=None): |
| 1495 |
payload = None |
| 1496 |
|
| 1497 |
if uid: |
| 1498 |
payload = {"uid": uid} |
| 1499 |
|
| 1500 |
connection.send_request("/web_api/discard", payload) |
| 1501 |
|
| 1502 |
|
| 1503 |
def install_policy(connection, policy_package, targets): |
| 1504 |
payload = {"policy-package": policy_package, "targets": targets} |
| 1505 |
|
| 1506 |
connection.send_request("/web_api/install-policy", payload) |
| 1507 |
|
| 1508 |
|
| 1509 |
def prepare_rule_params_for_execute_module( |
| 1510 |
rule, module_args, position, below_rule_name |
| 1511 |
): |
| 1512 |
rule["layer"] = module_args["layer"] |
| 1513 |
if "details_level" in module_args.keys(): |
| 1514 |
rule["details_level"] = module_args["details_level"] |
| 1515 |
if "state" not in rule.keys() or ( |
| 1516 |
"state" in rule.keys() and rule["state"] != "absent" |
| 1517 |
): |
| 1518 |
if below_rule_name: |
| 1519 |
relative_position = { |
| 1520 |
"relative_position": {"below": below_rule_name} |
| 1521 |
} |
| 1522 |
rule.update(relative_position) |
| 1523 |
else: |
| 1524 |
rule["position"] = position |
| 1525 |
position = position + 1 |
| 1526 |
below_rule_name = rule["name"] |
| 1527 |
|
| 1528 |
return rule, position, below_rule_name |
| 1529 |
|
| 1530 |
|
| 1531 |
def check_if_to_publish_for_action(result, module_args): |
| 1532 |
to_publish = ( |
| 1533 |
( |
| 1534 |
"auto_publish_session" in module_args.keys() |
| 1535 |
and module_args["auto_publish_session"] |
| 1536 |
) |
| 1537 |
and ("changed" in result.keys() and result["changed"] is True) |
| 1538 |
and ("failed" not in result.keys() or result["failed"] is False) |
| 1539 |
) |
| 1540 |
return to_publish |
| 1541 |
|
| 1542 |
|
| 1543 |
class CheckPointRequest(object): |
| 1544 |
def __init__( |
| 1545 |
self, |
| 1546 |
module=None, |
| 1547 |
connection=None, |
| 1548 |
headers=None, |
| 1549 |
not_rest_data_keys=None, |
| 1550 |
task_vars=None, |
| 1551 |
): |
| 1552 |
self.module = module |
| 1553 |
if module: |
| 1554 |
# This will be removed, once all of the available modules |
| 1555 |
# are moved to use action plugin design, as otherwise test |
| 1556 |
# would start to complain without the implementation. |
| 1557 |
self.connection = Connection(self.module._socket_path) |
| 1558 |
elif connection: |
| 1559 |
self.connection = connection |
| 1560 |
try: |
| 1561 |
self.connection.load_platform_plugins( |
| 1562 |
"check_point.mgmt.checkpoint" |
| 1563 |
) |
| 1564 |
self.connection.set_options(var_options=task_vars) |
| 1565 |
except ConnectionError: |
| 1566 |
raise |
| 1567 |
# This allows us to exclude specific argspec keys from being included by |
| 1568 |
# the rest data that don't follow the deepsec_* naming convention |
| 1569 |
if not_rest_data_keys: |
| 1570 |
self.not_rest_data_keys = not_rest_data_keys |
| 1571 |
else: |
| 1572 |
self.not_rest_data_keys = [] |
| 1573 |
self.not_rest_data_keys.append("validate_certs") |
| 1574 |
self.headers = headers if headers else BASE_HEADERS |
| 1575 |
|
| 1576 |
# wait for task |
| 1577 |
def wait_for_task(self, version, connection, task_id): |
| 1578 |
task_id_payload = {"task-id": task_id, "details-level": "full"} |
| 1579 |
task_complete = False |
| 1580 |
minutes_until_timeout = 30 |
| 1581 |
# if module.params['wait_for_task_timeout'] is not None and module.params['wait_for_task_timeout'] >= 0: |
| 1582 |
# minutes_until_timeout = module.params['wait_for_task_timeout'] |
| 1583 |
max_num_iterations = minutes_until_timeout * 30 |
| 1584 |
current_iteration = 0 |
| 1585 |
|
| 1586 |
# As long as there is a task in progress |
| 1587 |
while not task_complete and current_iteration < max_num_iterations: |
| 1588 |
current_iteration += 1 |
| 1589 |
# Check the status of the task |
| 1590 |
code, response = send_request( |
| 1591 |
connection, version, "show-task", task_id_payload |
| 1592 |
) |
| 1593 |
|
| 1594 |
attempts_counter = 0 |
| 1595 |
while code != 200: |
| 1596 |
if attempts_counter < 5: |
| 1597 |
attempts_counter += 1 |
| 1598 |
time.sleep(2) |
| 1599 |
code, response = send_request( |
| 1600 |
connection, version, "show-task", task_id_payload |
| 1601 |
) |
| 1602 |
else: |
| 1603 |
response["message"] = ( |
| 1604 |
"ERROR: Failed to handle asynchronous tasks as synchronous, tasks result is" |
| 1605 |
" undefined. " + response["message"] |
| 1606 |
) |
| 1607 |
_fail_json(parse_fail_message(code, response)) |
| 1608 |
|
| 1609 |
# Count the number of tasks that are not in-progress |
| 1610 |
completed_tasks = 0 |
| 1611 |
for task in response["tasks"]: |
| 1612 |
if task["status"] == "failed": |
| 1613 |
_fail_json( |
| 1614 |
"Task {0} with task id {1} failed. Look at the logs for more details".format( |
| 1615 |
task["task-name"], task["task-id"] |
| 1616 |
) |
| 1617 |
) |
| 1618 |
if task["status"] == "in progress": |
| 1619 |
break |
| 1620 |
completed_tasks += 1 |
| 1621 |
|
| 1622 |
# Are we done? check if all tasks are completed |
| 1623 |
if completed_tasks == len(response["tasks"]): |
| 1624 |
task_complete = True |
| 1625 |
else: |
| 1626 |
time.sleep(2) # Wait for two seconds |
| 1627 |
if not task_complete: |
| 1628 |
_fail_json( |
| 1629 |
"ERROR: Timeout. Task-id: {0}.".format( |
| 1630 |
task_id_payload["task-id"] |
| 1631 |
) |
| 1632 |
) |
| 1633 |
else: |
| 1634 |
return response |
| 1635 |
|
| 1636 |
# if failed occurred, in some cases we want to discard changes before exiting. We also notify the user about the `discard` |
| 1637 |
def discard_and_fail( |
| 1638 |
self, code, response, connection, version, session_uid |
| 1639 |
): |
| 1640 |
discard_code, discard_response = send_request( |
| 1641 |
connection, version, "discard" |
| 1642 |
) |
| 1643 |
if discard_code != 200: |
| 1644 |
try: |
| 1645 |
_fail_json( |
| 1646 |
parse_fail_message(code, response) |
| 1647 |
+ " Failed to discard session {0}" |
| 1648 |
" with error {1} with message {2}".format( |
| 1649 |
session_uid, |
| 1650 |
discard_code, |
| 1651 |
discard_response, |
| 1652 |
) |
| 1653 |
) |
| 1654 |
except Exception: |
| 1655 |
# Read-only mode without UID |
| 1656 |
_fail_json( |
| 1657 |
parse_fail_message(code, response) |
| 1658 |
+ " Failed to discard session" |
| 1659 |
" with error {0} with message {1}".format( |
| 1660 |
discard_code, discard_response |
| 1661 |
) |
| 1662 |
) |
| 1663 |
|
| 1664 |
_fail_json( |
| 1665 |
"Checkpoint session with ID: {0}".format(session_uid) |
| 1666 |
+ ", " |
| 1667 |
+ parse_fail_message(code, response) |
| 1668 |
+ " Unpublished changes were discarded" |
| 1669 |
) |
| 1670 |
|
| 1671 |
# handle publish command, and wait for it to end if the user asked so |
| 1672 |
def handle_publish(self, connection, version, payload): |
| 1673 |
publish_code, publish_response = send_request( |
| 1674 |
connection, version, "publish" |
| 1675 |
) |
| 1676 |
if publish_code != 200: |
| 1677 |
self.discard_and_fail( |
| 1678 |
publish_code, publish_response, connection, version |
| 1679 |
) |
| 1680 |
if payload.get("wait_for_task"): |
| 1681 |
self.wait_for_task( |
| 1682 |
version, connection, publish_response["task-id"] |
| 1683 |
) |
| 1684 |
|
| 1685 |
# handle call |
| 1686 |
def handle_call( |
| 1687 |
self, |
| 1688 |
connection, |
| 1689 |
version, |
| 1690 |
api_url, |
| 1691 |
payload, |
| 1692 |
to_discard_on_failure, |
| 1693 |
session_uid=None, |
| 1694 |
to_publish=False, |
| 1695 |
): |
| 1696 |
code, response = send_request(connection, version, api_url, payload) |
| 1697 |
if code != 200: |
| 1698 |
if to_discard_on_failure: |
| 1699 |
self.discard_and_fail( |
| 1700 |
code, response, connection, version, session_uid |
| 1701 |
) |
| 1702 |
elif "object_not_found" not in response.get( |
| 1703 |
"code" |
| 1704 |
) and "not found" not in response.get("message"): |
| 1705 |
raise _fail_json( |
| 1706 |
"Checkpoint session with ID: {0}".format(session_uid) |
| 1707 |
+ ", " |
| 1708 |
+ parse_fail_message(code, response) |
| 1709 |
) |
| 1710 |
else: |
| 1711 |
if "wait_for_task" in payload and payload["wait_for_task"]: |
| 1712 |
if "task-id" in response: |
| 1713 |
response = self.wait_for_task( |
| 1714 |
version, connection, response["task-id"] |
| 1715 |
) |
| 1716 |
elif "tasks" in response: |
| 1717 |
for task in response["tasks"]: |
| 1718 |
if "task-id" in task: |
| 1719 |
task_id = task["task-id"] |
| 1720 |
response[task_id] = self.wait_for_task( |
| 1721 |
version, connection, task["task-id"] |
| 1722 |
) |
| 1723 |
del response["tasks"] |
| 1724 |
|
| 1725 |
if to_publish: |
| 1726 |
self.handle_publish(connection, version, payload) |
| 1727 |
return code, response |
| 1728 |
|
| 1729 |
# handle the call and set the result with 'changed' and teh response |
| 1730 |
def handle_add_and_set_result( |
| 1731 |
self, |
| 1732 |
connection, |
| 1733 |
version, |
| 1734 |
api_url, |
| 1735 |
payload, |
| 1736 |
session_uid, |
| 1737 |
auto_publish_session=False, |
| 1738 |
): |
| 1739 |
code, response = self.handle_call( |
| 1740 |
connection, |
| 1741 |
version, |
| 1742 |
api_url, |
| 1743 |
payload, |
| 1744 |
True, |
| 1745 |
session_uid, |
| 1746 |
auto_publish_session, |
| 1747 |
) |
| 1748 |
result = {"code": code, "response": response, "changed": True} |
| 1749 |
return result |
| 1750 |
|
| 1751 |
# handle delete |
| 1752 |
def handle_delete(self, connection, payload, api_call_object, version): |
| 1753 |
auto_publish = False |
| 1754 |
payload_for_equals = {"type": api_call_object, "params": payload} |
| 1755 |
equals_code, equals_response = send_request( |
| 1756 |
connection, version, "equals", payload_for_equals |
| 1757 |
) |
| 1758 |
session_uid = connection.get_session_uid() |
| 1759 |
if equals_code == 200: |
| 1760 |
if payload.get("auto_publish_session"): |
| 1761 |
auto_publish = payload["auto_publish_session"] |
| 1762 |
del payload["auto_publish_session"] |
| 1763 |
code, response = self.handle_call( |
| 1764 |
connection, |
| 1765 |
version, |
| 1766 |
"delete-" + api_call_object, |
| 1767 |
payload, |
| 1768 |
True, |
| 1769 |
session_uid, |
| 1770 |
auto_publish, |
| 1771 |
) |
| 1772 |
result = {"code": code, "response": response, "changed": True} |
| 1773 |
else: |
| 1774 |
# else equals_code is 404 and no need to delete because object doesn't exist |
| 1775 |
result = {"changed": False} |
| 1776 |
if result.get("response"): |
| 1777 |
result["checkpoint_session_uid"] = session_uid |
| 1778 |
return result |
| 1779 |
|
| 1780 |
# handle api call facts |
| 1781 |
def api_call_facts(self, connection, payload, api_call_object, version): |
| 1782 |
if payload.get("auto_publish_session"): |
| 1783 |
del payload["auto_publish_session"] |
| 1784 |
code, response = self.handle_call( |
| 1785 |
connection, version, api_call_object, payload, False |
| 1786 |
) |
| 1787 |
result = {"code": code, "response": response} |
| 1788 |
return result |
| 1789 |
|
| 1790 |
# handle api call |
| 1791 |
def api_call( |
| 1792 |
self, |
| 1793 |
connection, |
| 1794 |
payload, |
| 1795 |
remove_keys, |
| 1796 |
api_call_object, |
| 1797 |
state, |
| 1798 |
equals_response, |
| 1799 |
version, |
| 1800 |
delete_params, |
| 1801 |
): |
| 1802 |
result = {} |
| 1803 |
auto_publish_session = False |
| 1804 |
if payload.get("auto_publish_session"): |
| 1805 |
auto_publish_session = payload["auto_publish_session"] |
| 1806 |
del payload["auto_publish_session"] |
| 1807 |
session_uid = connection.get_session_uid() |
| 1808 |
if state == "merged": |
| 1809 |
if equals_response and equals_response.get("equals") is False: |
| 1810 |
payload = remove_unwanted_key(payload, remove_keys) |
| 1811 |
result = self.handle_add_and_set_result( |
| 1812 |
connection, |
| 1813 |
version, |
| 1814 |
"set-" + api_call_object, |
| 1815 |
payload, |
| 1816 |
session_uid, |
| 1817 |
auto_publish_session, |
| 1818 |
) |
| 1819 |
elif equals_response.get("code") or equals_response.get("message"): |
| 1820 |
result = self.handle_add_and_set_result( |
| 1821 |
connection, |
| 1822 |
version, |
| 1823 |
"add-" + api_call_object, |
| 1824 |
payload, |
| 1825 |
session_uid, |
| 1826 |
auto_publish_session, |
| 1827 |
) |
| 1828 |
elif state == "replaced": |
| 1829 |
if equals_response and equals_response.get("equals") is False: |
| 1830 |
code, response = self.handle_call( |
| 1831 |
connection, |
| 1832 |
version, |
| 1833 |
"delete-" + api_call_object, |
| 1834 |
delete_params, |
| 1835 |
True, |
| 1836 |
session_uid, |
| 1837 |
auto_publish_session, |
| 1838 |
) |
| 1839 |
result = self.handle_add_and_set_result( |
| 1840 |
connection, |
| 1841 |
version, |
| 1842 |
"add-" + api_call_object, |
| 1843 |
payload, |
| 1844 |
session_uid, |
| 1845 |
auto_publish_session, |
| 1846 |
) |
| 1847 |
elif equals_response.get("code") or equals_response.get("message"): |
| 1848 |
result = self.handle_add_and_set_result( |
| 1849 |
connection, |
| 1850 |
version, |
| 1851 |
"add-" + api_call_object, |
| 1852 |
payload, |
| 1853 |
session_uid, |
| 1854 |
auto_publish_session, |
| 1855 |
) |
| 1856 |
if result.get("response"): |
| 1857 |
result["checkpoint_session_uid"] = session_uid |
| 1858 |
|
| 1859 |
return result |
| 1860 |
|
| 1861 |
# if user insert a specific version, we add it to the url |
| 1862 |
def get_version(self, payload): |
| 1863 |
return ( |
| 1864 |
("v" + payload["version"] + "/") if payload.get("version") else "" |
| 1865 |
) |
| 1866 |
|
| 1867 |
def _httpapi_error_handle(self, api_obj, state, **kwargs): |
| 1868 |
# FIXME - make use of handle_httperror(self, exception) where applicable |
| 1869 |
# https://docs.ansible.com/ansible/latest/network/dev_guide/developing_plugins_network.html#developing-plugins-httpapi |
| 1870 |
try: |
| 1871 |
result = {} |
| 1872 |
version = self.get_version(kwargs["data"]) |
| 1873 |
if state == "gathered": |
| 1874 |
result = self.api_call_facts( |
| 1875 |
self.connection, kwargs["data"], "show-" + api_obj, version |
| 1876 |
) |
| 1877 |
elif state == "deleted": |
| 1878 |
result = self.handle_delete( |
| 1879 |
self.connection, kwargs["data"], api_obj, version |
| 1880 |
) |
| 1881 |
elif state == "merged" or state == "replaced": |
| 1882 |
payload_for_equals = { |
| 1883 |
"type": api_obj, |
| 1884 |
"params": kwargs["data"], |
| 1885 |
} |
| 1886 |
equals_code, equals_response = send_request( |
| 1887 |
self.connection, version, "equals", payload_for_equals |
| 1888 |
) |
| 1889 |
if equals_response.get("equals"): |
| 1890 |
result = { |
| 1891 |
"code": equals_code, |
| 1892 |
"response": equals_response, |
| 1893 |
"changed": False, |
| 1894 |
} |
| 1895 |
else: |
| 1896 |
result = self.api_call( |
| 1897 |
self.connection, |
| 1898 |
kwargs["data"], |
| 1899 |
kwargs["remove_keys"], |
| 1900 |
api_obj, |
| 1901 |
state, |
| 1902 |
equals_response, |
| 1903 |
version, |
| 1904 |
kwargs["delete_params"], |
| 1905 |
) |
| 1906 |
except ConnectionError as e: |
| 1907 |
raise _fail_json("connection error occurred: {0}".format(e)) |
| 1908 |
except ValueError as e: |
| 1909 |
raise _fail_json("certificate not found: {0}".format(e)) |
| 1910 |
# This fn. will return both code and response, once all of the available modules |
| 1911 |
# are moved to use action plugin design, as otherwise test |
| 1912 |
# would start to complain without the implementation. |
| 1913 |
return result |
| 1914 |
|
| 1915 |
def post(self, obj, state, **kwargs): |
| 1916 |
return self._httpapi_error_handle(obj, state, **kwargs) |