| 1 |
# (c) 2020, Alexei Znamensky <[email protected]> |
| 2 |
# Copyright (c) 2020, Ansible Project |
| 3 |
# Simplified BSD License (see LICENSES/BSD-2-Clause.txt or https://opensource.org/licenses/BSD-2-Clause) |
| 4 |
# SPDX-License-Identifier: BSD-2-Clause |
| 5 |
|
| 6 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 7 |
# Do not use this from other collections or standalone plugins/modules! |
| 8 |
|
| 9 |
from __future__ import annotations |
| 10 |
|
| 11 |
import traceback |
| 12 |
from contextlib import contextmanager |
| 13 |
from functools import wraps |
| 14 |
|
| 15 |
from ansible_collections.community.general.plugins.module_utils._mh.exceptions import ( |
| 16 |
ModuleHelperException, |
| 17 |
_UnhandledSentinel, |
| 18 |
) |
| 19 |
|
| 20 |
_unhandled_exceptions: tuple[type[Exception], ...] = (_UnhandledSentinel,) |
| 21 |
|
| 22 |
|
| 23 |
def cause_changes(when=None): |
| 24 |
def deco(func): |
| 25 |
@wraps(func) |
| 26 |
def wrapper(self, *args, **kwargs): |
| 27 |
try: |
| 28 |
func(self, *args, **kwargs) |
| 29 |
if when == "success": |
| 30 |
self.changed = True |
| 31 |
except Exception: |
| 32 |
if when == "failure": |
| 33 |
self.changed = True |
| 34 |
raise |
| 35 |
finally: |
| 36 |
if when == "always": |
| 37 |
self.changed = True |
| 38 |
|
| 39 |
return wrapper |
| 40 |
|
| 41 |
return deco |
| 42 |
|
| 43 |
|
| 44 |
@contextmanager |
| 45 |
def no_handle_exceptions(*exceptions: type[Exception]): |
| 46 |
global _unhandled_exceptions |
| 47 |
current = _unhandled_exceptions |
| 48 |
_unhandled_exceptions = tuple(exceptions) |
| 49 |
try: |
| 50 |
yield |
| 51 |
finally: |
| 52 |
_unhandled_exceptions = current |
| 53 |
|
| 54 |
|
| 55 |
def module_fails_on_exception(func): |
| 56 |
conflict_list = ("msg", "exception", "output", "vars", "changed") |
| 57 |
|
| 58 |
@wraps(func) |
| 59 |
def wrapper(self, *args, **kwargs): |
| 60 |
def fix_key(k): |
| 61 |
return k if k not in conflict_list else f"_{k}" |
| 62 |
|
| 63 |
def fix_var_conflicts(output): |
| 64 |
result = {fix_key(k): v for k, v in output.items()} |
| 65 |
return result |
| 66 |
|
| 67 |
try: |
| 68 |
func(self, *args, **kwargs) |
| 69 |
except _unhandled_exceptions: |
| 70 |
# re-raise exception without further processing |
| 71 |
raise |
| 72 |
except ModuleHelperException as e: |
| 73 |
if e.update_output: |
| 74 |
self.update_output(e.update_output) |
| 75 |
# patchy solution to resolve conflict with output variables |
| 76 |
output = fix_var_conflicts(self.output) |
| 77 |
self.module.fail_json( |
| 78 |
msg=e.msg, exception=traceback.format_exc(), output=self.output, vars=self.vars.output(), **output |
| 79 |
) |
| 80 |
except Exception as e: |
| 81 |
# patchy solution to resolve conflict with output variables |
| 82 |
output = fix_var_conflicts(self.output) |
| 83 |
msg = f"Module failed with exception: {str(e).strip()}" |
| 84 |
self.module.fail_json( |
| 85 |
msg=msg, exception=traceback.format_exc(), output=self.output, vars=self.vars.output(), **output |
| 86 |
) |
| 87 |
|
| 88 |
return wrapper |
| 89 |
|
| 90 |
|
| 91 |
def check_mode_skip(func): |
| 92 |
@wraps(func) |
| 93 |
def wrapper(self, *args, **kwargs): |
| 94 |
if not self.module.check_mode: |
| 95 |
return func(self, *args, **kwargs) |
| 96 |
|
| 97 |
return wrapper |
| 98 |
|
| 99 |
|
| 100 |
def check_mode_skip_returns(callable=None, value=None): |
| 101 |
def deco(func): |
| 102 |
if callable is not None: |
| 103 |
|
| 104 |
@wraps(func) |
| 105 |
def wrapper_callable(self, *args, **kwargs): |
| 106 |
if self.module.check_mode: |
| 107 |
return callable(self, *args, **kwargs) |
| 108 |
return func(self, *args, **kwargs) |
| 109 |
|
| 110 |
return wrapper_callable |
| 111 |
|
| 112 |
else: |
| 113 |
|
| 114 |
@wraps(func) |
| 115 |
def wrapper_value(self, *args, **kwargs): |
| 116 |
if self.module.check_mode: |
| 117 |
return value |
| 118 |
return func(self, *args, **kwargs) |
| 119 |
|
| 120 |
return wrapper_value |
| 121 |
|
| 122 |
return deco |