Skip to content
Snippets Groups Projects
Commit 061bbfdd authored by Mohammad Torkashvand's avatar Mohammad Torkashvand
Browse files

restructure and fix router_vendor

parent f6a22c62
No related branches found
No related tags found
No related merge requests found
Pipeline #88712 failed
...@@ -13,6 +13,11 @@ docs/vale/styles/* ...@@ -13,6 +13,11 @@ docs/vale/styles/*
venv/ venv/
.venv/ .venv/
.mypy_cache
.pytest_cache
.ruff_cache
build/
# Ignore files generated by apidoc # Ignore files generated by apidoc
docs/source/lso.rst docs/source/lso.rst
docs/source/lso.*.rst docs/source/lso.*.rst
......
...@@ -56,7 +56,7 @@ def write_hosts_file(groups: dict, hosts_file: Path) -> None: ...@@ -56,7 +56,7 @@ def write_hosts_file(groups: dict, hosts_file: Path) -> None:
def generate_host_vars_and_hosts_file(router_subscriptions: list, temp_dir: Path) -> None: def generate_host_vars_and_hosts_file(router_subscriptions: list, temp_dir: Path) -> None:
"""Process router subscriptions data.""" """Process router subscriptions data."""
groups: dict[str, list[str]] = {} groups: dict[str, list[str]] = {}
host_vars_dir = temp_dir / "hostvars" host_vars_dir = temp_dir / "host_vars"
host_vars_dir.mkdir() host_vars_dir.mkdir()
for router_subscription in router_subscriptions: for router_subscription in router_subscriptions:
...@@ -67,7 +67,7 @@ def generate_host_vars_and_hosts_file(router_subscriptions: list, temp_dir: Path ...@@ -67,7 +67,7 @@ def generate_host_vars_and_hosts_file(router_subscriptions: list, temp_dir: Path
write_host_vars(host_name, router, host_vars_dir) write_host_vars(host_name, router, host_vars_dir)
vendor_group = router.get("router_vendor", "").lower() vendor_group = router.get("vendor", "").lower()
role_group = f"{router.get('router_role', '')}_routers" role_group = f"{router.get('router_role', '')}_routers"
groups.setdefault(vendor_group, []).append(host_name) groups.setdefault(vendor_group, []).append(host_name)
...@@ -78,7 +78,7 @@ def generate_host_vars_and_hosts_file(router_subscriptions: list, temp_dir: Path ...@@ -78,7 +78,7 @@ def generate_host_vars_and_hosts_file(router_subscriptions: list, temp_dir: Path
@contextmanager @contextmanager
def safe_write(temp_dir: Path, old_vars_dir: Path, old_hosts_file: Path) -> Generator[Path, None, None]: def safe_write(temp_dir: Path, old_host_vars_dir: Path) -> Generator[Path, None, None]:
temp_dir.mkdir(exist_ok=True) temp_dir.mkdir(exist_ok=True)
try: try:
...@@ -88,11 +88,11 @@ def safe_write(temp_dir: Path, old_vars_dir: Path, old_hosts_file: Path) -> Gene ...@@ -88,11 +88,11 @@ def safe_write(temp_dir: Path, old_vars_dir: Path, old_hosts_file: Path) -> Gene
typer.echo(f"Error: {e}") typer.echo(f"Error: {e}")
sys.exit(1) sys.exit(1)
else: else:
if old_vars_dir.exists(): if old_host_vars_dir.exists():
shutil.rmtree(old_vars_dir) shutil.rmtree(old_host_vars_dir)
shutil.copytree(temp_dir, old_vars_dir) # Copy new host vars dir shutil.copytree(temp_dir, old_host_vars_dir) # Copy new host vars dir
shutil.copy(temp_dir / "hosts.yaml", old_hosts_file) # Copy new hosts file shutil.copy(temp_dir / "hosts.yaml", old_host_vars_dir) # Copy new hosts file
shutil.rmtree(temp_dir) shutil.rmtree(temp_dir)
...@@ -112,9 +112,8 @@ def generate_inventory_from_api() -> None: ...@@ -112,9 +112,8 @@ def generate_inventory_from_api() -> None:
temp_dir = Path(tempfile.mkdtemp()) temp_dir = Path(tempfile.mkdtemp())
old_host_vars_dir = Path(settings.host_vars_dir) old_host_vars_dir = Path(settings.host_vars_dir)
old_hosts_file = Path(settings.hosts_file_dir)
with safe_write(temp_dir, old_host_vars_dir, old_hosts_file) as temp_dir: with safe_write(temp_dir, old_host_vars_dir) as temp_dir:
generate_host_vars_and_hosts_file(router_subscriptions, temp_dir) generate_host_vars_and_hosts_file(router_subscriptions, temp_dir)
......
...@@ -3,10 +3,9 @@ from pydantic.v1 import BaseSettings ...@@ -3,10 +3,9 @@ from pydantic.v1 import BaseSettings
class Settings(BaseSettings): class Settings(BaseSettings):
api_url: str api_url: str
host_vars_dir: str
vars_file_name: str
hosts_file_dir: str
gso_api_key: str gso_api_key: str
host_vars_dir: str
vars_file_name: str = 'gso.yaml'
class Config: class Config:
env_file = "../.env" env_file = "../.env"
......
...@@ -55,7 +55,7 @@ def mocked_subscription_api_data(): ...@@ -55,7 +55,7 @@ def mocked_subscription_api_data():
"router_si_ipv4_network": "62.40.97.222/31", "router_si_ipv4_network": "62.40.97.222/31",
"router_ias_lt_ipv4_network": "83.97.89.116/31", "router_ias_lt_ipv4_network": "83.97.89.116/31",
"router_ias_lt_ipv6_network": "2001:798:1::2a4/126", "router_ias_lt_ipv6_network": "2001:798:1::2a4/126",
"router_vendor": "nokia", "vendor": "nokia",
"router_role": "p", "router_role": "p",
"router_site": { "router_site": {
"name": "SiteBlock", "name": "SiteBlock",
...@@ -118,7 +118,7 @@ def mocked_subscription_api_data(): ...@@ -118,7 +118,7 @@ def mocked_subscription_api_data():
"router_si_ipv4_network": "62.40.97.222/31", "router_si_ipv4_network": "62.40.97.222/31",
"router_ias_lt_ipv4_network": "83.97.89.116/31", "router_ias_lt_ipv4_network": "83.97.89.116/31",
"router_ias_lt_ipv6_network": "2001:798:1::2a4/126", "router_ias_lt_ipv6_network": "2001:798:1::2a4/126",
"router_vendor": "juniper", "vendor": "juniper",
"router_role": "amt", "router_role": "amt",
"router_site": { "router_site": {
"name": "SiteBlock", "name": "SiteBlock",
...@@ -156,7 +156,6 @@ def setup_test(tmp_dir, mocked_subscription_api_data): ...@@ -156,7 +156,6 @@ def setup_test(tmp_dir, mocked_subscription_api_data):
api_url="http://test", api_url="http://test",
host_vars_dir=str(tmp_dir), host_vars_dir=str(tmp_dir),
vars_file_name="wfo_vars.yaml", vars_file_name="wfo_vars.yaml",
hosts_file_dir=str(tmp_dir),
gso_api_key="random_gso_api_key", gso_api_key="random_gso_api_key",
) )
...@@ -170,8 +169,8 @@ def setup_test(tmp_dir, mocked_subscription_api_data): ...@@ -170,8 +169,8 @@ def setup_test(tmp_dir, mocked_subscription_api_data):
def test_generate_inventory_from_api_valid_json(setup_test, tmp_dir, mocked_subscription_api_data): def test_generate_inventory_from_api_valid_json(setup_test, tmp_dir, mocked_subscription_api_data):
hosts_file = tmp_dir / "hosts.yaml" hosts_file = tmp_dir / "hosts.yaml"
amsterdam_host_vars = tmp_dir / "hostvars/amsterdam.nl.geant.net/wfo_vars.yaml" amsterdam_host_vars = tmp_dir / "host_vars/amsterdam.nl.geant.net/wfo_vars.yaml"
utrecht_host_vars = tmp_dir / "hostvars/utrecht.nl.geant.net/wfo_vars.yaml" utrecht_host_vars = tmp_dir / "host_vars/utrecht.nl.geant.net/wfo_vars.yaml"
assert not amsterdam_host_vars.exists() assert not amsterdam_host_vars.exists()
assert not utrecht_host_vars.exists() assert not utrecht_host_vars.exists()
...@@ -218,8 +217,8 @@ def test_inventory_generation_handles_exceptions_and_preserves_file_integrity( ...@@ -218,8 +217,8 @@ def test_inventory_generation_handles_exceptions_and_preserves_file_integrity(
): ):
paths = { paths = {
"hosts": tmp_dir / "hosts.yaml", "hosts": tmp_dir / "hosts.yaml",
"ams_vars": tmp_dir / "hostvars/amsterdam.nl.geant.net/wfo_vars.yaml", "ams_vars": tmp_dir / "host_vars/amsterdam.nl.geant.net/wfo_vars.yaml",
"utr_vars": tmp_dir / "hostvars/utrecht.nl.geant.net/wfo_vars.yaml", "utr_vars": tmp_dir / "host_vars/utrecht.nl.geant.net/wfo_vars.yaml",
} }
file_contents = { file_contents = {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment