feat(gns3-copilot): add get_gns3_server_host utility and integrate into Nornir tools

- Add `get_gns3_server_host()` function to `connector_factory.py` for extracting GNS3 server hostname from controller, config, or default URL
- Export new function in `__init__.py` for public API access
- Replace `os.getenv("GNS3_SERVER_HOST", "127.0.0.1")` calls with `get_gns3_server_host()` in Nornir configuration tools (`config_tools_nornir.py`, `display_tools_nornir.py`)
- Ensures consistent host detection across tools using the same priority logic as `get_gns3_connector`
This commit is contained in:
YueGuobin committed 2026-03-04 00:28:52 +08:00
1 parent 8917ef39c9
commit cabcf4f1cb
7 files changed
+54 -1390

No files matched your search

@@ -27,7 +27,11 @@ Main functions:
- get_gns3_connector_with_llm_config: Factory function to create connector AND retrieve LLM config
"""
from .connector_factory import get_gns3_connector, get_gns3_connector_with_llm_config
from .connector_factory import (
get_gns3_connector,
get_gns3_connector_with_llm_config,
get_gns3_server_host,
)
from .custom_gns3fy import (
CONSOLE_TYPES,
LINK_TYPES,
@@ -93,6 +97,7 @@ __all__ = [
"GNS3UpdateDrawingTool",
"get_gns3_connector",
"get_gns3_connector_with_llm_config",
"get_gns3_server_host",
"add_file_to_index",
"get_file_list",
]
@@ -298,3 +298,40 @@ def _detect_url_for_api() -> Optional[str]:
# Fallback
return DEFAULT_GNS3_URL
def get_gns3_server_host() -> str:
"""
Get GNS3 server hostname from Controller or Config.
This is a convenience function for extracting the hostname only,
useful for Nornir tools that need the GNS3 server address.
Uses the same priority order as get_gns3_connector:
1. Controller.instance().compute("local")
2. Config.instance().settings.Server
3. Fallback to DEFAULT_GNS3_URL host
Returns:
Hostname/IP address string
Example:
from gns3_copilot.gns3_client import get_gns3_server_host
host = get_gns3_server_host()
print(f"GNS3 server host: {host}")
"""
url = _detect_url_for_api()
# Extract host from URL
# URL format: protocol://host:port
try:
# Remove protocol prefix
host_part = url.split("://")[1]
# Extract host (before the port)
host = host_part.split(":")[0]
logger.debug("Extracted GNS3 server host: %s from URL: %s", host, url)
return host
except Exception as e:
logger.warning("Failed to extract host from URL %s: %s, using fallback", url, e)
return DEFAULT_GNS3_URL.split("://")[1].split(":")[0]