Skip to content
Snippets Groups Projects
Commit 74b15958 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
1 merge request!4restructure and fix router_vendor
Pipeline #88717 passed
...@@ -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
......
...@@ -29,13 +29,10 @@ Fill in environment variables and run the command to generate the Ansible invent ...@@ -29,13 +29,10 @@ Fill in environment variables and run the command to generate the Ansible invent
# Rewritting environment variables # Rewritting environment variables
## URL of the WFO API (change IP only) ## URL of the WFO API (change IP only)
export api_url=http://127.0.0.1:8080/api/v1/subscriptions/routers export api_url=http://127.0.0.1:8080/api/v1/subscriptions/routers
## Full paths where this tool will save variables and inventory files
export host_vars_dir=/path/to/base/hostvars/dir
export hosts_file_dir=/path/to/base/hosts/dir
## Full path where this tool will save the WFO variables
export vars_file_name=wfo_vars.yaml
## Token provided by each GSO instance admin (varies per environment) ## Token provided by each GSO instance admin (varies per environment)
export gso_api_key=<fill_with_gso_token> export gso_api_key=<fill_with_gso_token>
## Full paths where AIG will save host_vars
export host_vars_dir=/path/to/base/host_vars/dir
# Execution of the command to generate the Ansible inventory files # Execution of the command to generate the Ansible inventory files
ansible_inventory_generator ansible_inventory_generator
...@@ -63,6 +60,4 @@ pip install -U pip setuptools wheel ...@@ -63,6 +60,4 @@ pip install -U pip setuptools wheel
### 403 error when running the `ansible_inventory_generator` command ### 403 error when running the `ansible_inventory_generator` command
This tool cannot reach the WFO API because it is authenticated. This tool cannot reach the GSO API because it is authenticated. Use a correct gso_api_key environment variable.
Correct it by redeploying WFO by first running `export OAUTH2_ACTIVE=false` and then re-running WFO.
Alternatively, change `export api_url` to an authenticated instance of GSO.
\ No newline at end of file
...@@ -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.yaml 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",
...@@ -149,14 +149,14 @@ def mocked_subscription_api_data(): ...@@ -149,14 +149,14 @@ def mocked_subscription_api_data():
@pytest.fixture() @pytest.fixture()
def setup_test(tmp_dir, mocked_subscription_api_data): def setup_test(tmp_dir, mocked_subscription_api_data):
with patch("ansible_inventory_generator.app.load_settings") as mock_load_settings, patch( with (
"requests.get" patch("ansible_inventory_generator.app.load_settings") as mock_load_settings,
) as mock_get: patch("requests.get") as mock_get,
):
mock_load_settings.return_value = Settings( mock_load_settings.return_value = Settings(
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 +170,8 @@ def setup_test(tmp_dir, mocked_subscription_api_data): ...@@ -170,8 +170,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 +218,8 @@ def test_inventory_generation_handles_exceptions_and_preserves_file_integrity( ...@@ -218,8 +218,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