Skip to content

Commit

Permalink
Use unmanaged_config_sections to determine if we need to get
Browse files Browse the repository at this point in the history
running_config for platform/devtype combo, this way we don't need
a new config variable that needs to be synced with templates
  • Loading branch information
indy-independence committed Oct 30, 2024
1 parent 7c969fe commit 35f44c0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/cnaas_nms/app_settings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import List, Optional
from typing import Optional

import yaml
from pydantic import field_validator
Expand Down Expand Up @@ -57,7 +57,6 @@ class ApiSettings(BaseSettings):
COMMIT_CONFIRMED_TIMEOUT: int = 300
COMMIT_CONFIRMED_WAIT: int = 1
SETTINGS_OVERRIDE: Optional[dict] = None
DEVICE_TYPES_WITH_INCLUDE_RUNNING_CONFIG: List[str] = []

@field_validator("MGMTDOMAIN_PRIMARY_IP_VERSION")
@classmethod
Expand Down Expand Up @@ -119,7 +118,6 @@ def construct_api_settings() -> ApiSettings:
COMMIT_CONFIRMED_TIMEOUT=config.get("commit_confirmed_timeout", 300),
COMMIT_CONFIRMED_WAIT=config.get("commit_confirmed_wait", 1),
SETTINGS_OVERRIDE=config.get("settings_override", None),
DEVICE_TYPES_WITH_INCLUDE_RUNNING_CONFIG=config.get("device_types_with_include_running_config", []),
)
else:
return ApiSettings()
Expand Down
16 changes: 12 additions & 4 deletions src/cnaas_nms/devicehandler/sync_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,23 @@ def populate_device_vars(
)
device_variables = {**device_variables, **fabric_device_variables}

# if device type in api_settings.DEVICE_TYPES_WITH_INCLUDE_RUNNING_CONFIG
for dt_str in api_settings.DEVICE_TYPES_WITH_INCLUDE_RUNNING_CONFIG:
if dev.device_type.name.lower() == dt_str.lower():
# if platform/devtype has unmanaged config sections, get running_config and add to device_variables
local_repo_path = get_template_repo_path(hostname)
mapfile = os.path.join(local_repo_path, dev.platform, "mapping.yml")
if not os.path.isfile(mapfile):
raise RepoStructureException("File {} not found in template repo".format(mapfile))
with open(mapfile, "r") as f:
mapping = yaml.safe_load(f)
if (
"unmanaged_config_sections" in mapping[devtype.name]
and type(mapping[devtype.name]["unmanaged_config_sections"]) is list
):
task.host.open_connection("napalm", configuration=task.nornir.config)
res = task.run(task=napalm_get, getters=["config"])
task.host.close_connection("napalm")

running_config = dict(res.result)["config"]["running"]
# Remove the first task result, which is the napalm_get result, since it's not needed anymore
# Remove the first task result, which is the napalm_get result, since it's not needed for final job result
del task.results[0]
if running_config is None:
raise Exception(f"Failed to get running configuration for {dev.hostname}")
Expand Down

0 comments on commit 35f44c0

Please sign in to comment.