#!/usr/bin/env python3 """ Simple test script for Proxmox API connection Tests only the Proxmox connection without loading full application """ import sys from pathlib import Path # Add src to path sys.path.insert(0, str(Path(__file__).parent.parent / "src")) from datacenter_docs.utils.config import get_settings def test_proxmox_connection(): """Test Proxmox API connection""" print("=" * 80) print("PROXMOX API CONNECTION TEST") print("=" * 80) # Load settings settings = get_settings() print("\nšŸ“‹ Configuration:") print(f" Host: {settings.PROXMOX_HOST}") print(f" Port: {settings.PROXMOX_PORT}") print(f" User: {settings.PROXMOX_USER}") print(f" Token Name: {settings.PROXMOX_TOKEN_NAME}") print(f" Token Value: {'*' * 8}{settings.PROXMOX_TOKEN_VALUE[-4:] if settings.PROXMOX_TOKEN_VALUE else 'NOT SET'}") print(f" Verify SSL: {settings.PROXMOX_VERIFY_SSL}") print(f" Timeout: {settings.PROXMOX_TIMEOUT}s") # Check if configured if not settings.PROXMOX_HOST or settings.PROXMOX_HOST == "proxmox.example.com": print("\nāŒ ERROR: Proxmox host not configured") print(" Please set PROXMOX_HOST in .env file") return False # Try to import proxmoxer try: from proxmoxer import ProxmoxAPI except ImportError: print("\nāŒ ERROR: proxmoxer library not installed") print(" Install with: pip install proxmoxer") return False print("\nšŸ”Œ Connecting to Proxmox...") try: # Prepare connection parameters auth_params = { "host": settings.PROXMOX_HOST, "port": settings.PROXMOX_PORT, "verify_ssl": settings.PROXMOX_VERIFY_SSL, "timeout": settings.PROXMOX_TIMEOUT, } # API Token authentication if settings.PROXMOX_TOKEN_NAME and settings.PROXMOX_TOKEN_VALUE: print(f" Using API token authentication: {settings.PROXMOX_USER}!{settings.PROXMOX_TOKEN_NAME}") auth_params["user"] = settings.PROXMOX_USER auth_params["token_name"] = settings.PROXMOX_TOKEN_NAME auth_params["token_value"] = settings.PROXMOX_TOKEN_VALUE # Password authentication elif settings.PROXMOX_PASSWORD: print(f" Using password authentication: {settings.PROXMOX_USER}") auth_params["user"] = settings.PROXMOX_USER auth_params["password"] = settings.PROXMOX_PASSWORD else: print("\nāŒ ERROR: No authentication credentials configured") print(" Set either PROXMOX_TOKEN_NAME/VALUE or PROXMOX_PASSWORD") return False # Connect proxmox = ProxmoxAPI(**auth_params) # Test connection by getting version version = proxmox.version.get() print(f"\nāœ… Connection successful!") print(f" Proxmox VE version: {version.get('version')}") print(f" Release: {version.get('release', 'unknown')}") # Get cluster status print("\nšŸ“Š Cluster Information:") try: cluster_status = proxmox.cluster.status.get() for item in cluster_status: if item.get("type") == "cluster": print(f" Cluster Name: {item.get('name')}") print(f" Quorate: {'Yes' if item.get('quorate') else 'No'}") print(f" Nodes: {item.get('nodes')}") break except Exception as e: print(f" āš ļø Could not get cluster info: {e}") # Get nodes print("\nšŸ–„ļø Nodes:") try: nodes = proxmox.nodes.get() for node in nodes: status_icon = "🟢" if node.get("status") == "online" else "šŸ”“" print(f" {status_icon} {node.get('node')}: {node.get('status')}") except Exception as e: print(f" āš ļø Could not get nodes: {e}") # Get VM count print("\nšŸ’» Virtual Machines:") try: total_vms = 0 total_containers = 0 for node in nodes: node_name = node["node"] try: vms = proxmox.nodes(node_name).qemu.get() containers = proxmox.nodes(node_name).lxc.get() total_vms += len(vms) total_containers += len(containers) print(f" Node {node_name}: {len(vms)} VMs, {len(containers)} containers") except Exception: pass print(f"\n šŸ“Š TOTAL: {total_vms} VMs, {total_containers} containers") except Exception as e: print(f" āš ļø Could not get VMs: {e}") print("\n" + "=" * 80) print("āœ… ALL TESTS PASSED - Proxmox connection is working!") print("=" * 80) return True except Exception as e: print(f"\nāŒ CONNECTION FAILED") print(f" Error: {e}") print(f" Type: {type(e).__name__}") if "401" in str(e): print("\nšŸ’” Troubleshooting:") print(" - Check that PROXMOX_USER is correct (should include realm: user@pam or user@pve)") print(" - Verify PROXMOX_TOKEN_NAME matches the token ID in Proxmox") print(" - Verify PROXMOX_TOKEN_VALUE is correct") print(" - Check token has proper permissions (PVEAuditor role on path /)") elif "SSL" in str(e): print("\nšŸ’” Troubleshooting:") print(" - Try setting PROXMOX_VERIFY_SSL=false in .env") print(" - Or install valid SSL certificate on Proxmox") elif "refused" in str(e).lower(): print("\nšŸ’” Troubleshooting:") print(" - Check PROXMOX_HOST is correct") print(" - Check PROXMOX_PORT is correct (default: 8006)") print(" - Verify firewall allows access to Proxmox API") print(" - Check Proxmox API service is running: systemctl status pveproxy") print("\n" + "=" * 80) return False if __name__ == "__main__": success = test_proxmox_connection() sys.exit(0 if success else 1)