Feature 10996: Adds ns-op-cancel command
[osm/osmclient.git] / osmclient / sol005 / ns.py
index c84d090..e8442f9 100644 (file)
@@ -40,7 +40,7 @@ class Ns(object):
         )
 
     # NS '--wait' option
-    def _wait(self, id, wait_time, deleteFlag=False):
+    def _wait(self, id, wait_time, deleteFlag=False, entity="NS"):
         self._logger.debug("")
         # Endpoint to get operation status
         apiUrlStatus = "{}{}{}".format(
@@ -50,7 +50,7 @@ class Ns(object):
         if isinstance(wait_time, bool):
             wait_time = WaitForStatus.TIMEOUT_NS_OPERATION
         WaitForStatus.wait_for_status(
-            "NS",
+            entity,
             str(id),
             wait_time,
             apiUrlStatus,
@@ -431,6 +431,33 @@ class Ns(object):
             message = "failed to exec operation {}:\nerror:\n{}".format(name, str(exc))
             raise ClientException(message)
 
+    def cancel_op(self, operation_id, cancel_mode, wait=False):
+        """Cancels an LCM operation"""
+        self._client.get_token()
+        self._apiResource = "/ns_lcm_op_occs"
+        self._apiBase = "{}{}{}".format(
+            self._apiName, self._apiVersion, self._apiResource
+        )
+        endpoint = "{}/{}/cancel".format(self._apiBase, operation_id)
+        op_data = {"cancelMode": cancel_mode}
+        try:
+            http_code, resp = self._http.post_cmd(
+                endpoint=endpoint, postfields_dict=op_data
+            )
+            if http_code == 202:
+                if wait:
+                    self._wait(operation_id, wait, deleteFlag=True, entity="OPCANCEL")
+                else:
+                    print("Cancellation in progress")
+            else:
+                msg = resp or ""
+                raise ClientException(msg)
+        except ClientException as exc:
+            message = "failed to exec operation {}:\nerror:\n{}".format(
+                operation_id, str(exc)
+            )
+            raise ClientException(message)
+
     def scale_vnf(
         self,
         ns_name,