Skip to content

Commit 12f40f9

Browse files
authored
Add support for custom headers in all Netbox modules (#1433)
* Add custom header parameter to all module Add custom header parameter based on @clinta work in #1327 * 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` * Reformat plugins/module_utils/netbox_utils.py * Fix changelog message * Update add default value for headers parameter in _connect_netbox_api function * Fix Tests * FIx changelog * Fix default value
1 parent becc534 commit 12f40f9

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- Add support for custom headers for all modules

docs/getting_started/contributing/modules/architecture.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,11 @@ We set several instance attributes that are used within other methods throughout
284284
url = self.module.params["netbox_url"]
285285
token = self.module.params["netbox_token"]
286286
ssl_verify = self.module.params["validate_certs"]
287+
headers = self.module.params["headers"]
287288
288289
# Attempt to initiate connection to NetBox
289290
if nb_client is None:
290-
self.nb = self._connect_netbox_api(url, token, ssl_verify)
291+
self.nb = self._connect_netbox_api(url, token, ssl_verify, headers)
291292
else:
292293
self.nb = nb_client
293294
try:

plugins/doc_fragments/common.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,8 @@ class ModuleDocFragment(object):
5050
- Certificate path
5151
required: false
5252
type: raw
53+
headers:
54+
description: Dictionary of headers to be passed to the NetBox API.
55+
required: false
56+
type: dict
5357
"""

plugins/module_utils/netbox_utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,7 @@
723723
query_params=dict(required=False, type="list", elements="str"),
724724
validate_certs=dict(type="raw", default=True),
725725
cert=dict(type="raw", required=False),
726+
headers=dict(type="dict", required=False),
726727
)
727728

728729

@@ -751,10 +752,11 @@ def __init__(self, module, endpoint, nb_client=None):
751752
token = self.module.params["netbox_token"]
752753
ssl_verify = self.module.params["validate_certs"]
753754
cert = self.module.params["cert"]
755+
headers = self.module.params["headers"]
754756

755757
# Attempt to initiate connection to NetBox
756758
if nb_client is None:
757-
self.nb = self._connect_netbox_api(url, token, ssl_verify, cert)
759+
self.nb = self._connect_netbox_api(url, token, ssl_verify, cert, headers)
758760
else:
759761
self.nb = nb_client
760762
try:
@@ -804,10 +806,14 @@ def _version_check_greater(self, greater, lesser, greater_or_equal=False):
804806

805807
return False
806808

807-
def _connect_netbox_api(self, url, token, ssl_verify, cert):
809+
def _connect_netbox_api(self, url, token, ssl_verify, cert, headers=None):
808810
try:
809811
session = requests.Session()
810812
session.verify = ssl_verify
813+
if isinstance(headers, str):
814+
headers = json.load(headers)
815+
if isinstance(headers, dict):
816+
session.headers.update(headers)
811817
if cert:
812818
session.cert = tuple(i for i in cert)
813819
nb = pynetbox.api(url, token=token)

tests/unit/module_utils/test_netbox_base_class.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def fixture_arg_spec():
6363
"state": "present",
6464
"validate_certs": False,
6565
"cert": None,
66+
"headers": None,
6667
}
6768

6869

0 commit comments

Comments
 (0)