Fix checks for KSU create and delete operations and for cluster delete operations

Change-Id: I9fc2a68d636100295409b7d3ad875e54ba368d64
Signed-off-by: garciadeblas <gerardo.garciadeblas@telefonica.com>
diff --git a/osm_lcm/odu_libs/workflows.py b/osm_lcm/odu_libs/workflows.py
index 80527ed..d1b9582 100644
--- a/osm_lcm/odu_libs/workflows.py
+++ b/osm_lcm/odu_libs/workflows.py
@@ -30,16 +30,21 @@
             name=workflow_name,
             namespace="osm-workflows",
             flag="Completed",
+            deleted=False,
             timeout=300,
         )
     except Exception as e:
         return False, f"Unexpected exception: {e}"
 
 
-async def readiness_loop(self, item, name, namespace, flag, timeout):
+async def readiness_loop(
+    self, item, name, namespace, flag, deleted, timeout, kubectl=None
+):
+    if kubectl is None:
+        kubectl = self._kubectl
     self.logger.info("readiness_loop Enter")
     self.logger.info(
-        f"{item} {name}. Namespace: '{namespace}'. Flag: {flag}. Timeout: {timeout}"
+        f"{item} {name}. Namespace: '{namespace}'. Flag: {flag}. Deleted: {deleted}. Timeout: {timeout}"
     )
     item_api_map = {
         "workflow": {
@@ -86,30 +91,52 @@
     api_version = item_api_map[item]["api_version"]
 
     while counter <= max_iterations:
-        generic_object = await self._kubectl.get_generic_object(
-            api_group=api_group,
-            api_plural=api_plural,
-            api_version=api_version,
-            namespace=namespace,
-            name=name,
-        )
-        if generic_object:
-            # self.logger.debug(f"{yaml.safe_dump(generic_object)}")
-            conditions = generic_object.get("status", {}).get("conditions", [])
-        else:
-            self.logger.info(
-                f"Could not find {api_plural}. Name: {name}. Namespace: '{namespace}'. API: {api_group}/{api_version}"
+        try:
+            self.logger.info(f"Iteration {counter}/{max_iterations}")
+            generic_object = await kubectl.get_generic_object(
+                api_group=api_group,
+                api_plural=api_plural,
+                api_version=api_version,
+                namespace=namespace,
+                name=name,
             )
-            conditions = []
-        self.logger.info(
-            f"Iteration {counter}/{max_iterations}. {item} status conditions: {conditions}"
-        )
-        result = next((item for item in conditions if item["type"] == flag), {})
-        if result.get("status", "False") == "True":
-            self.logger.info(
-                f"{item} {name} reached the status {flag} in {counter} iterations (aprox {counter*retry_time} seconds)"
-            )
-            return True, "COMPLETED"
+            if deleted:
+                if generic_object:
+                    self.logger.info(
+                        f"Found {api_plural}. Name: {name}. Namespace: '{namespace}'. API: {api_group}/{api_version}"
+                    )
+                else:
+                    self.logger.info(
+                        f"{item} {name} deleted after {counter} iterations (aprox {counter*retry_time} seconds)"
+                    )
+                    return True, "COMPLETED"
+            else:
+                if not flag:
+                    return True, "Nothing to check"
+                if generic_object:
+                    # self.logger.debug(f"{yaml.safe_dump(generic_object)}")
+                    conditions = generic_object.get("status", {}).get("conditions", [])
+                    self.logger.info(f"{item} status conditions: {conditions}")
+                else:
+                    self.logger.info(
+                        f"Could not find {api_plural}. Name: {name}. Namespace: '{namespace}'. API: {api_group}/{api_version}"
+                    )
+                    conditions = []
+                result = next((item for item in conditions if item["type"] == flag), {})
+                if result.get("status", "False") == "True":
+                    self.logger.info(
+                        f"{item} {name} reached the status {flag} in {counter} iterations (aprox {counter*retry_time} seconds)"
+                    )
+                    return True, "COMPLETED"
+                # TODO: Implement generic condition with jsonpath filter
+                # jsonpath_expr = parse(condition["jsonpath_filter"])
+                # match = jsonpath_expr.find(generic_object)
+                # if match:
+                #     value = match[0].value
+                #     if condition["function"](value, condition["value"]):
+                #         return True, "COMPLETED"
+        except Exception as e:
+            self.logger.error(f"Exception: {e}")
         await asyncio.sleep(retry_time)
         counter += 1
     return (