Fix check_worfklow_status to add additional condition regarding status.phase

Change-Id: I539ca5506cd44dad791844c1f5611ac530a5fbbf
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
diff --git a/osm_lcm/k8s.py b/osm_lcm/k8s.py
index 87b7160..952760e 100644
--- a/osm_lcm/k8s.py
+++ b/osm_lcm/k8s.py
@@ -352,12 +352,12 @@
             )
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -600,12 +600,12 @@
             )
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -786,12 +786,12 @@
             )
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -874,12 +874,12 @@
             self.db.set_one("clusters", {"_id": db_cluster["_id"]}, db_cluster)
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -962,12 +962,12 @@
             )
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -1094,12 +1094,12 @@
             )
             return
 
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
         self.logger.info(
-            "workflow_status is :{} and workflow_msg is :{}".format(
+            "workflow_status is: {} and workflow_msg is: {}".format(
                 workflow_status, workflow_msg
             )
         )
@@ -1202,7 +1202,7 @@
                 f"clean_status is :{clean_status} and clean_msg is :{clean_msg}"
             )
             return
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
         workflow_status, workflow_msg = await self.odu.check_workflow_status(
             workflow_name
         )
@@ -1499,7 +1499,7 @@
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "create_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1528,7 +1528,7 @@
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "delete_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1575,7 +1575,7 @@
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "create_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1606,7 +1606,7 @@
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "delete_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1655,7 +1655,7 @@
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "create_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1686,7 +1686,7 @@
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "delete_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1735,7 +1735,7 @@
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "create_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
@@ -1766,7 +1766,7 @@
         workflow_res, workflow_name = await self.odu.launch_workflow(
             "delete_profile", op_id, op_params, content
         )
-        self.logger.info("workflow_name is :{}".format(workflow_name))
+        self.logger.info("workflow_name is: {}".format(workflow_name))
 
         workflow_status = await self.check_workflow_and_update_db(
             op_id, workflow_name, content
diff --git a/osm_lcm/odu_libs/workflows.py b/osm_lcm/odu_libs/workflows.py
index 1b5d5cc..84a2b9e 100644
--- a/osm_lcm/odu_libs/workflows.py
+++ b/osm_lcm/odu_libs/workflows.py
@@ -26,7 +26,8 @@
     if not workflow_name:
         return False, "Workflow was not launched"
     try:
-        return await self.readiness_loop(
+        # First check if the workflow ends successfully
+        completed, message = await self.readiness_loop(
             item="workflow",
             name=workflow_name,
             namespace="osm-workflows",
@@ -37,8 +38,23 @@
             deleted=False,
             timeout=300,
         )
+        if completed:
+            # Then check if the workflow has a failed task
+            return await self.readiness_loop(
+                item="workflow",
+                name=workflow_name,
+                namespace="osm-workflows",
+                condition={
+                    "jsonpath_filter": "status.phase",
+                    "value": "Succeeded",
+                },
+                deleted=False,
+                timeout=0,
+            )
+        else:
+            return False, f"Workflow was not completed: {message}"
     except Exception as e:
-        return False, f"Unexpected exception: {e}"
+        return False, f"Workflow could not be completed. Unexpected exception: {e}"
 
 
 async def readiness_loop(
@@ -90,6 +106,8 @@
     counter = 1
     retry_time = self._odu_checkloop_retry_time
     max_iterations = ceil(timeout / retry_time)
+    if max_iterations < 1:
+        max_iterations = 1
     api_group = item_api_map[item]["api_group"]
     api_plural = item_api_map[item]["api_plural"]
     api_version = item_api_map[item]["api_version"]
@@ -150,7 +168,8 @@
                     )
         except Exception as e:
             self.logger.error(f"Exception: {e}")
-        await asyncio.sleep(retry_time)
+        if counter < max_iterations:
+            await asyncio.sleep(retry_time)
         counter += 1
     return (
         False,