From 76e656b773203608f8c8cf057231a06b4f16f458 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoit=20Ferr=C3=A9?= Date: Fri, 27 Jun 2025 17:57:16 +0200 Subject: [PATCH 1/8] Add custom header parameter to all module Add custom header parameter based on @clinta work in #1327 --- changelogs/fragments/add-custom-headers.yml | 2 ++ plugins/doc_fragments/common.py | 4 ++++ plugins/module_utils/netbox_utils.py | 12 ++++++++++-- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/add-custom-headers.yml diff --git a/changelogs/fragments/add-custom-headers.yml b/changelogs/fragments/add-custom-headers.yml new file mode 100644 index 000000000..985a3f12c --- /dev/null +++ b/changelogs/fragments/add-custom-headers.yml @@ -0,0 +1,2 @@ +minor-changes: + - Add support for custom headers for all modules \ No newline at end of file diff --git a/plugins/doc_fragments/common.py b/plugins/doc_fragments/common.py index 47a7895d9..4bb63c73e 100644 --- a/plugins/doc_fragments/common.py +++ b/plugins/doc_fragments/common.py @@ -50,4 +50,8 @@ class ModuleDocFragment(object): - Certificate path required: false type: raw + headers: + description: Dictionary of headers to be passed to the NetBox API. + required: false + type: dict """ diff --git a/plugins/module_utils/netbox_utils.py b/plugins/module_utils/netbox_utils.py index 08a756234..de3fef36e 100644 --- a/plugins/module_utils/netbox_utils.py +++ b/plugins/module_utils/netbox_utils.py @@ -723,6 +723,7 @@ query_params=dict(required=False, type="list", elements="str"), validate_certs=dict(type="raw", default=True), cert=dict(type="raw", required=False), + headers=dict(type="dict", required=False), ) @@ -751,10 +752,11 @@ def __init__(self, module, endpoint, nb_client=None): token = self.module.params["netbox_token"] ssl_verify = self.module.params["validate_certs"] cert = self.module.params["cert"] + headers = self.module.params["headers"] # Attempt to initiate connection to NetBox if nb_client is None: - self.nb = self._connect_netbox_api(url, token, ssl_verify, cert) + self.nb = self._connect_netbox_api(url, token, ssl_verify, cert, headers) else: self.nb = nb_client try: @@ -804,9 +806,15 @@ def _version_check_greater(self, greater, lesser, greater_or_equal=False): return False - def _connect_netbox_api(self, url, token, ssl_verify, cert): + def _connect_netbox_api(self, url, token, ssl_verify, cert, headers): try: session = requests.Session() + + if isinstance(headers, str): + headers = json.load(headers) + if isinstance(headers, dict): + session.headers.update(headers) + session.verify = ssl_verify if cert: session.cert = tuple(i for i in cert) From f58251256f75a000fc6a9558262a714005249184 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoit=20Ferr=C3=A9?= Date: Mon, 30 Jun 2025 20:14:58 +0200 Subject: [PATCH 2/8] Reflect *_connect_netbox_api* function signature change in the docs Reflect signature change in the doc file: `modified: docs/getting_started/contributing/modules/architecture.rst` --- docs/getting_started/contributing/modules/architecture.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/getting_started/contributing/modules/architecture.rst b/docs/getting_started/contributing/modules/architecture.rst index edeac7a56..e00ad0696 100644 --- a/docs/getting_started/contributing/modules/architecture.rst +++ b/docs/getting_started/contributing/modules/architecture.rst @@ -284,10 +284,11 @@ We set several instance attributes that are used within other methods throughout url = self.module.params["netbox_url"] token = self.module.params["netbox_token"] ssl_verify = self.module.params["validate_certs"] + headers = self.module.params["headers"] # Attempt to initiate connection to NetBox if nb_client is None: - self.nb = self._connect_netbox_api(url, token, ssl_verify) + self.nb = self._connect_netbox_api(url, token, ssl_verify, headers) else: self.nb = nb_client try: From 13737cbe6804474f15a09ae455c906eae9ad80d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoit=20Ferr=C3=A9?= Date: Mon, 30 Jun 2025 20:49:37 +0200 Subject: [PATCH 3/8] Reformat plugins/module_utils/netbox_utils.py --- plugins/module_utils/netbox_utils.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/module_utils/netbox_utils.py b/plugins/module_utils/netbox_utils.py index de3fef36e..a942af293 100644 --- a/plugins/module_utils/netbox_utils.py +++ b/plugins/module_utils/netbox_utils.py @@ -809,13 +809,11 @@ def _version_check_greater(self, greater, lesser, greater_or_equal=False): def _connect_netbox_api(self, url, token, ssl_verify, cert, headers): try: session = requests.Session() - + session.verify = ssl_verify if isinstance(headers, str): headers = json.load(headers) if isinstance(headers, dict): session.headers.update(headers) - - session.verify = ssl_verify if cert: session.cert = tuple(i for i in cert) nb = pynetbox.api(url, token=token) From 3886e04a92912f414db660b9942091b68aefe12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoit=20Ferr=C3=A9?= Date: Mon, 30 Jun 2025 20:51:53 +0200 Subject: [PATCH 4/8] Fix changelog message --- changelogs/fragments/1433-add-custom-headers.yml | 2 ++ changelogs/fragments/add-custom-headers.yml | 2 -- 2 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/1433-add-custom-headers.yml delete mode 100644 changelogs/fragments/add-custom-headers.yml diff --git a/changelogs/fragments/1433-add-custom-headers.yml b/changelogs/fragments/1433-add-custom-headers.yml new file mode 100644 index 000000000..3f85f8ee0 --- /dev/null +++ b/changelogs/fragments/1433-add-custom-headers.yml @@ -0,0 +1,2 @@ +minor-changes: + - Add support for custom headers for all modules diff --git a/changelogs/fragments/add-custom-headers.yml b/changelogs/fragments/add-custom-headers.yml deleted file mode 100644 index 985a3f12c..000000000 --- a/changelogs/fragments/add-custom-headers.yml +++ /dev/null @@ -1,2 +0,0 @@ -minor-changes: - - Add support for custom headers for all modules \ No newline at end of file From 2a20d4700be51c5651061770897b209d70df65bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoit=20Ferr=C3=A9?= Date: Mon, 30 Jun 2025 20:58:18 +0200 Subject: [PATCH 5/8] Update add default value for headers parameter in _connect_netbox_api function --- plugins/module_utils/netbox_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/module_utils/netbox_utils.py b/plugins/module_utils/netbox_utils.py index a942af293..d8fc46d03 100644 --- a/plugins/module_utils/netbox_utils.py +++ b/plugins/module_utils/netbox_utils.py @@ -806,7 +806,7 @@ def _version_check_greater(self, greater, lesser, greater_or_equal=False): return False - def _connect_netbox_api(self, url, token, ssl_verify, cert, headers): + def _connect_netbox_api(self, url, token, ssl_verify, cert, headers={}): try: session = requests.Session() session.verify = ssl_verify From d4a28934f29556078df044cd3c61f95777d2bcfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoit=20Ferr=C3=A9?= Date: Mon, 30 Jun 2025 21:14:55 +0200 Subject: [PATCH 6/8] Fix Tests --- tests/unit/module_utils/test_netbox_base_class.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/module_utils/test_netbox_base_class.py b/tests/unit/module_utils/test_netbox_base_class.py index 40bbadaa7..d20a78ca4 100644 --- a/tests/unit/module_utils/test_netbox_base_class.py +++ b/tests/unit/module_utils/test_netbox_base_class.py @@ -63,6 +63,7 @@ def fixture_arg_spec(): "state": "present", "validate_certs": False, "cert": None, + "headers": None, } From 888831528b7479ad0a750699816ab5eb7be57bdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benoit=20Ferr=C3=A9?= Date: Tue, 1 Jul 2025 17:31:44 +0200 Subject: [PATCH 7/8] FIx changelog --- changelogs/fragments/1433-add-custom-headers.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/1433-add-custom-headers.yml b/changelogs/fragments/1433-add-custom-headers.yml index 3f85f8ee0..25b67d974 100644 --- a/changelogs/fragments/1433-add-custom-headers.yml +++ b/changelogs/fragments/1433-add-custom-headers.yml @@ -1,2 +1,2 @@ -minor-changes: +minor_changes: - Add support for custom headers for all modules From e226a2432eb7d2ea2ed5c1f45e094628ab078632 Mon Sep 17 00:00:00 2001 From: benoit-ferre Date: Tue, 8 Jul 2025 20:35:16 +0200 Subject: [PATCH 8/8] Fix default value --- plugins/module_utils/netbox_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/module_utils/netbox_utils.py b/plugins/module_utils/netbox_utils.py index d8fc46d03..2d5549849 100644 --- a/plugins/module_utils/netbox_utils.py +++ b/plugins/module_utils/netbox_utils.py @@ -806,7 +806,7 @@ def _version_check_greater(self, greater, lesser, greater_or_equal=False): return False - def _connect_netbox_api(self, url, token, ssl_verify, cert, headers={}): + def _connect_netbox_api(self, url, token, ssl_verify, cert, headers=None): try: session = requests.Session() session.verify = ssl_verify