Skip to content

Add RUNNING_CONFIG_MAPPER and get_running_config_command with tests #649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/user/include_jinja_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
| regex_split | netutils.regex.regex_split |
| regex_sub | netutils.regex.regex_sub |
| longest_prefix_match | netutils.route.longest_prefix_match |
| get_running_config_command | netutils.running_config.get_running_config_command |
| uptime_seconds_to_string | netutils.time.uptime_seconds_to_string |
| uptime_string_to_seconds | netutils.time.uptime_string_to_seconds |
| vlanconfig_to_list | netutils.vlan.vlanconfig_to_list |
Expand Down
10 changes: 10 additions & 0 deletions netutils/lib_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,16 @@
"vyos": "vyos",
}

# Running config command
RUNNING_CONFIG_MAPPER: t.Dict[str, str] = {
"cisco_ios": "show run",
"cisco_nxos": "show run",
"cisco_xr": "show run",
"juniper_junos": "show configuration | display set",
"arista_eos": "show run",
"checkpoint_gaia": 'clish -c "show configuration"',
}

# PYTNC | Normalized
PYNTC_LIB_MAPPER: t.Dict[str, str] = {
"arista_eos_eapi": "arista_eos",
Expand Down
16 changes: 16 additions & 0 deletions netutils/running_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Configuration command lookup."""

from netutils.lib_mapper import RUNNING_CONFIG_MAPPER


def get_running_config_command(platform: str) -> str:
"""
Get the running config command for a specific network platform.

Args:
platform: Platform name, like 'cisco_ios' or 'juniper_junos'.

Returns:
The corresponding command as a string, or 'show run' by default.
"""
return RUNNING_CONFIG_MAPPER.get(platform.lower(), "show run")
1 change: 1 addition & 0 deletions netutils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"version_metadata": "os_version.version_metadata",
"get_nist_urls": "nist.get_nist_urls",
"get_nist_vendor_platform_urls": "nist.get_nist_vendor_platform_urls",
"get_running_config_command": "running_config.get_running_config_command",
}


Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_running_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Tests for the running configuration command mapping."""

import pytest
from netutils import lib_mapper
from netutils.running_config import get_running_config_command


def test_running_config_mapper_keys_are_known():
"""Ensure all keys in RUNNING_CONFIG_MAPPER are in MAIN_LIB_MAPPER values."""
unknown_keys = [key for key in lib_mapper.RUNNING_CONFIG_MAPPER if key not in lib_mapper.MAIN_LIB_MAPPER.values()]
assert not unknown_keys, f"Unexpected keys in RUNNING_CONFIG_MAPPER: {unknown_keys}"


@pytest.mark.parametrize("platform,expected_command", list(lib_mapper.RUNNING_CONFIG_MAPPER.items()))
def test_get_running_config_command_known_platforms(platform, expected_command):
"""Test get_running_config_command returns correct command for known platforms."""
assert get_running_config_command(platform) == expected_command
assert get_running_config_command(platform.upper()) == expected_command # test case insensitivity


def test_get_running_config_command_unknown_platform():
"""Test get_running_config_command returns default 'show run' for unknown platforms."""
assert get_running_config_command("unknown_platform") == "show run"
assert get_running_config_command("checkpoint_gaiAA") == "show run"
Loading