From 86c181270832a081de17c249edf7592d03102104 Mon Sep 17 00:00:00 2001
From: Karel van Klink <karel.vanklink@geant.org>
Date: Wed, 16 Aug 2023 09:51:25 +0200
Subject: [PATCH] migrate device to router, fix bug with playbook output

---
 lso/__init__.py                     | 4 ++--
 lso/playbook.py                     | 2 +-
 lso/routes/{device.py => router.py} | 9 +++------
 3 files changed, 6 insertions(+), 9 deletions(-)
 rename lso/routes/{device.py => router.py} (81%)

diff --git a/lso/__init__.py b/lso/__init__.py
index 831469b..7f48d92 100644
--- a/lso/__init__.py
+++ b/lso/__init__.py
@@ -5,7 +5,7 @@ from fastapi import FastAPI
 from fastapi.middleware.cors import CORSMiddleware
 
 from lso import config, environment
-from lso.routes import default, device, ip_trunk
+from lso.routes import default, router, ip_trunk
 
 
 def create_app() -> FastAPI:
@@ -26,7 +26,7 @@ def create_app() -> FastAPI:
     )
 
     app.include_router(default.router, prefix="/api")
-    app.include_router(device.router, prefix="/api/device")
+    app.include_router(router.router, prefix="/api/router")
     app.include_router(ip_trunk.router, prefix="/api/ip_trunk")
 
     # test that config params are loaded and available
diff --git a/lso/playbook.py b/lso/playbook.py
index 73fac95..7855588 100644
--- a/lso/playbook.py
+++ b/lso/playbook.py
@@ -94,7 +94,7 @@ def _run_playbook_proc(job_id: str, playbook_path: str, extra_vars: dict, invent
         try:
             task_output = json.loads(line)
             if "res" in task_output["event_data"] and (
-                task_output["event_data"]["res"]["changed"] is True or int(ansible_playbook_run.rc) != 0
+                int(ansible_playbook_run.rc) != 0 or task_output["event_data"]["res"]["changed"] is True
             ):
                 #  The line contains result data, and must either consist of a change, or the playbook failed, and we
                 #  want all steps, including those that didn't make changes.
diff --git a/lso/routes/device.py b/lso/routes/router.py
similarity index 81%
rename from lso/routes/device.py
rename to lso/routes/router.py
index 2e2b865..5c19d59 100644
--- a/lso/routes/device.py
+++ b/lso/routes/router.py
@@ -42,20 +42,17 @@ async def provision_node(params: NodeProvisioningParams) -> playbook.PlaybookLau
     :rtype: :class:`lso.playbook.PlaybookLaunchResponse`
     """
     extra_vars = {
-        "wfo_device_json": params.subscription,
+        "wfo_router_json": params.subscription,
         "dry_run": str(params.dry_run),
         "verb": "deploy",
         "commit_comment": "Base config deployed with WFO/LSO & Ansible",
     }
 
-    if params.subscription["device_type"] == "router":
-        playbook_path = os.path.join(config_params.ansible_playbooks_root_dir, "base_config.yaml")
-    else:
-        raise ValueError(f"Cannot find playbook path for device type " f"{params.subscription['device_type']}!!")
+    playbook_path = os.path.join(config_params.ansible_playbooks_root_dir, "base_config.yaml")
 
     return playbook.run_playbook(
         playbook_path=playbook_path,
-        inventory=f"{params.subscription['device']['device_fqdn']}",
+        inventory=f"{params.subscription['router']['router_fqdn']}",
         extra_vars=extra_vars,
         callback=params.callback,
     )
-- 
GitLab