diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5a0c5466f544a892f68d72d30e634908ef2a5871..be396e5ebbe19296c51d8a12c831ffaf6d178056 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,7 +7,7 @@ run-tox-pipeline: stage: tox tags: - docker-executor - image: python:3.10 + image: python:3.12 # Change pip's cache directory to be inside the project directory since we can # only cache local items. diff --git a/README.md b/README.md index 5026025e275a6e41d2d107405c6dd55da4dd1084..9f77eea3c43a8126ecbeb8495bf45b2ba6190119 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,11 @@ Fill in environment variables and run the command to generate the Ansible invent ```bash # Rewritting environment variables ## URL of the WFO API (change IP only) -export api_url=http://127.0.0.1:8080/api/v1/subscriptions/routers +export aig_api_url=http://127.0.0.1:8080/api/v1/subscriptions/routers ## Token provided by each GSO instance admin (varies per environment) -export gso_api_key=<fill_with_gso_token> +export aig_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 +export aig_host_vars_dir=/path/to/base/host_vars/dir # Execution of the command to generate the Ansible inventory files ansible_inventory_generator @@ -60,4 +60,4 @@ pip install -U pip setuptools wheel ### 403 error when running the `ansible_inventory_generator` command -This tool cannot reach the GSO API because it is authenticated. Use a correct gso_api_key environment variable. +This tool cannot reach the GSO API because it is authenticated. Use a correct aig_gso_api_key environment variable. diff --git a/ansible_inventory_generator/app.py b/ansible_inventory_generator/app.py index 1591e861b7c8073b40bbeeb64eb80a391e26c561..6c732dc35d8e8b32ddcc2e5825430194eb8cece4 100644 --- a/ansible_inventory_generator/app.py +++ b/ansible_inventory_generator/app.py @@ -26,10 +26,10 @@ def represent_none(self: BaseRepresenter, _: Any) -> ScalarNode: yaml.add_representer(type(None), represent_none) -def write_host_vars(host_name: str, router_data: dict, host_vars_dir: Path) -> None: +def write_host_vars(host_name: str, router_data: dict, aig_host_vars_dir: Path) -> None: """Write host variables to a file.""" settings = load_settings() - host_dir = host_vars_dir / host_name + host_dir = aig_host_vars_dir / host_name host_dir.mkdir(exist_ok=True) vars_file_path = host_dir / settings.vars_file_name @@ -56,8 +56,8 @@ def write_hosts_file(groups: dict, hosts_file: Path) -> None: def generate_host_vars_and_hosts_file(router_subscriptions: list, temp_dir: Path) -> None: """Process router subscriptions data.""" groups: dict[str, list[str]] = {} - host_vars_dir = temp_dir / "host_vars" - host_vars_dir.mkdir() + aig_host_vars_dir = temp_dir / "host_vars" + aig_host_vars_dir.mkdir() for router_subscription in router_subscriptions: router = router_subscription.get("router", {}) @@ -65,7 +65,7 @@ def generate_host_vars_and_hosts_file(router_subscriptions: list, temp_dir: Path if not host_name: continue - write_host_vars(host_name, router, host_vars_dir) + write_host_vars(host_name, router, aig_host_vars_dir) vendor_group = router.get("vendor", "").lower() role_group = f"{router.get('router_role', '')}_routers" @@ -101,7 +101,7 @@ def generate_inventory_from_api() -> None: settings = load_settings() """Generate Ansible inventory from API.""" response = requests.get( - settings.api_url, timeout=API_TIMEOUT_SEC, headers={"Authorization": f"Bearer {settings.gso_api_key}"} + settings.aig_api_url, timeout=API_TIMEOUT_SEC, headers={"Authorization": f"Bearer {settings.aig_gso_api_key}"} ) response.raise_for_status() try: @@ -111,7 +111,7 @@ def generate_inventory_from_api() -> None: sys.exit(1) temp_dir = Path(tempfile.mkdtemp()) - old_host_vars_dir = Path(settings.host_vars_dir) + old_host_vars_dir = Path(settings.aig_host_vars_dir) with safe_write(temp_dir, old_host_vars_dir) as temp_dir: generate_host_vars_and_hosts_file(router_subscriptions, temp_dir) diff --git a/ansible_inventory_generator/config.py b/ansible_inventory_generator/config.py index 8029c7a20563b3a03f56452cdd037dc75c3021aa..f2f5539d6ca30541e0a0169cb050f14e974df134 100644 --- a/ansible_inventory_generator/config.py +++ b/ansible_inventory_generator/config.py @@ -2,9 +2,9 @@ from pydantic.v1 import BaseSettings class Settings(BaseSettings): - api_url: str - gso_api_key: str - host_vars_dir: str + aig_api_url: str + aig_gso_api_key: str + aig_host_vars_dir: str vars_file_name: str = "gso.yaml" class Config: diff --git a/example.env b/example.env index 59005596d63d1d67b373cb73b9e6efd39b2667ce..3404d69e8ae5ce94b8aa9f80a0ed680fea3a282c 100644 --- a/example.env +++ b/example.env @@ -1,5 +1,5 @@ -api_url=http://127.0.0.1:8080/api/v1/subscriptions/routers -host_vars_dir=/path/to/base/hostvars/dir +aig_api_url=http://127.0.0.1:8080/api/v1/subscriptions/routers +aig_host_vars_dir=/path/to/base/hostvars/dir vars_file_name=wfo_vars.yaml hosts_file_dir=/path/to/base/hosts/dir -gso_api_key=askjdhaskhdaksjhdkajshdkashd +aig_gso_api_key=askjdhaskhdaksjhdkajshdkashd diff --git a/tests/test_inventory_generator.py b/tests/test_inventory_generator.py index abe34f603c76cb200a4cb08032b047de808b8a40..67719ee776b8a55504f45bd46704af4ec7a8fb0b 100644 --- a/tests/test_inventory_generator.py +++ b/tests/test_inventory_generator.py @@ -154,10 +154,10 @@ def setup_test(tmp_dir, mocked_subscription_api_data): patch("requests.get") as mock_get, ): mock_load_settings.return_value = Settings( - api_url="http://test", - host_vars_dir=str(tmp_dir), + aig_api_url="http://test", + aig_host_vars_dir=str(tmp_dir), vars_file_name="wfo_vars.yaml", - gso_api_key="random_gso_api_key", + aig_gso_api_key="random_gso_api_key", ) mock_get.return_value.json.return_value = mocked_subscription_api_data