diff --git a/plugins/doc_fragments/common.py b/plugins/doc_fragments/common.py index d6d906c31..f358a2286 100644 --- a/plugins/doc_fragments/common.py +++ b/plugins/doc_fragments/common.py @@ -26,4 +26,13 @@ class ModuleDocFragment(object): description: Whether to validate the SSL certificate of the Checkmk server. default: true type: bool + proxy_url: + description: The URL of your proxy server. + type: str + proxy_user: + description: The username to authenticate against your proxy server. + type: str + proxy_pass: + description: The password to authenticate against your proxy server. + type: str """ diff --git a/plugins/module_utils/api.py b/plugins/module_utils/api.py index 4596a67ba..49d065988 100644 --- a/plugins/module_utils/api.py +++ b/plugins/module_utils/api.py @@ -11,6 +11,7 @@ __metaclass__ = type import json +from urllib.parse import urlparse from ansible.module_utils.urls import fetch_url from ansible_collections.checkmk.general.plugins.module_utils.types import RESULT @@ -34,6 +35,9 @@ def __init__(self, module, logger=None): site = self.params.get("site") user = self.params.get("automation_user") secret = self.params.get("automation_secret") + self.proxy_url = self.params.get("proxy_url") + self.proxy_user = self.params.get("proxy_user") + self.proxy_pass = self.params.get("proxy_pass") self.url = "%s/%s/check_mk/api/1.0" % (server, site) self.headers = { "Accept": "application/json", @@ -75,6 +79,19 @@ def _fetch( # TODO: If the REST API at some point actually cancels requests # properly, we can go back to the initial back-off mechanism. + if self.proxy_url: + proxy_uri = urlparse(self.proxy_url) + if self.proxy_user and self.proxy_pass: + proxy_uri = proxy_uri._replace( + netloc=f"{self.proxy_user}:{self.proxy_pass}@{proxy_uri.netloc}" + ) + + # Set up the proxy_env dictionary for fetch_url + proxy_env = { + "https": proxy_uri.geturl(), + "http": proxy_uri.geturl().replace("https", "http"), + } + num_of_retries = 1 timeout = 60 for i in range(num_of_retries): @@ -84,7 +101,8 @@ def _fetch( data=None if not data else self.module.jsonify(data), headers=self.headers, method=method, - use_proxy=None, + use_proxy=True, + proxy_env=proxy_env, timeout=timeout, ) diff --git a/plugins/module_utils/utils.py b/plugins/module_utils/utils.py index 9e3038d52..663c3698e 100644 --- a/plugins/module_utils/utils.py +++ b/plugins/module_utils/utils.py @@ -41,6 +41,19 @@ def base_argument_spec(): no_log=True, fallback=(env_fallback, ["CHECKMK_VAR_AUTOMATION_SECRET"]), ), + proxy_url=dict( + type="str", + fallback=(env_fallback, ["CHECKMK_VAR_PROXY_URL"]), + ), + proxy_user=dict( + type="str", + fallback=(env_fallback, ["CHECKMK_VAR_PROXY_USER"]), + ), + proxy_pass=dict( + type="str", + no_log=True, + fallback=(env_fallback, ["CHECKMK_VAR_PROXY_PASS"]), + ), )