-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_netbox_api.py
More file actions
93 lines (79 loc) · 3.16 KB
/
check_netbox_api.py
File metadata and controls
93 lines (79 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
"""
# ==============================================================================
# File: check_netbox_api.py
# Part of the sovereign-stack project.
# Version: See version.py
#
# Sovereign Stack - NetBox API Debugger
# Validates connectivity, authorization, and data payload structure
# between the infra-scanner and the NetBox instance.
#
# Copyright (C) 2026 Henk van Hoek
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see [https://www.gnu.org/licenses/](https://www.gnu.org/licenses/).
# ==============================================================================
"""
import os
import logging
import pynetbox
import requests
from dotenv import load_dotenv
# Configure detailed logging
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger("NetBoxDebug")
load_dotenv()
NETBOX_URL = os.getenv("NETBOX_URL")
NETBOX_TOKEN = os.getenv("NETBOX_API_TOKEN")
def test_connection():
logger.info(f"Initiating connection test to: {NETBOX_URL}")
if not NETBOX_URL or not NETBOX_TOKEN:
logger.error("Missing NETBOX_URL or NETBOX_TOKEN in .env file.")
return
try:
# Initialize client
nb = pynetbox.api(NETBOX_URL, token=NETBOX_TOKEN)
# Debug: Show masked token for verification
masked_token = f"{NETBOX_TOKEN[:4]}...{NETBOX_TOKEN[-4:]}"
logger.debug(f"Using Token: {masked_token}")
# 1. Test Status (Basic connectivity)
logger.debug("Attempting to fetch NetBox status...")
status = nb.status()
logger.info(
f"Successfully connected! NetBox version: {status.get('netbox-version')}"
)
# 2. Test Authorization (Write permissions check)
logger.debug("Checking authorization for Virtualization objects...")
try:
# We only 'list' to check read-access first
_ = list(nb.virtualization.clusters.all(limit=1))
logger.info("Read access to Virtualization: OK")
except pynetbox.RequestError as e:
logger.error(f"Authorization failed or insufficient permissions: {e}")
return
# 3. Payload Preview (What would be sent)
test_payload = {
"name": "Debug-VM-Test",
"cluster": 1, # Placeholder ID
"status": "active",
}
logger.debug(f"Example VM Payload: {test_payload}")
except requests.exceptions.ConnectionError as ce:
logger.error(f"Network Unreachable: {ce}")
except Exception as ge:
logger.error(f"An unexpected error occurred: {ge}")
if __name__ == "__main__":
test_connection()