Skip to content

Commit 76e656b

Browse files
committed
Add custom header parameter to all module
Add custom header parameter based on @clinta work in netbox-community#1327
1 parent f57ef66 commit 76e656b

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
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

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: 10 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,9 +806,15 @@ 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):
808810
try:
809811
session = requests.Session()
812+
813+
if isinstance(headers, str):
814+
headers = json.load(headers)
815+
if isinstance(headers, dict):
816+
session.headers.update(headers)
817+
810818
session.verify = ssl_verify
811819
if cert:
812820
session.cert = tuple(i for i in cert)

0 commit comments

Comments
 (0)