Feature 11011: Multiple NS deletion in OSM(osm client) 97/14397/3
authoradurti <adurti.v@tataelxsi.co.in>
Fri, 31 May 2024 09:09:05 +0000 (14:39 +0530)
committergarciadeblas <gerardo.garciadeblas@telefonica.com>
Thu, 18 Jul 2024 15:14:36 +0000 (17:14 +0200)
Change-Id: I57edad436dc466640041332eb68fda37215aa05f
Signed-off-by: adurti <adurti.v@tataelxsi.co.in>
osmclient/cli_commands/ns.py
osmclient/sol005/ns.py

index ad07a11..972bfa3 100755 (executable)
@@ -376,8 +376,18 @@ def ns_create(
     )
 
 
-@click.command(name="ns-delete", short_help="deletes a NS instance")
-@click.argument("name")
+def process_ns_multi_delete_params(ctx, param, value):
+    ns_id_list = []
+    for ns_id in value:
+        ns_id_list.append(ns_id)
+    ctx.params["ns_id_list"] = ns_id_list
+    return
+
+
+@click.command(name="ns-delete", short_help="deletes NS instances")
+@click.argument(
+    "names", nargs=-1, type=click.UNPROCESSED, callback=process_ns_multi_delete_params
+)
 @click.option(
     "--force", is_flag=True, help="forces the deletion bypassing pre-conditions"
 )
@@ -396,14 +406,26 @@ def ns_create(
     "until the operation is completed, or timeout",
 )
 @click.pass_context
-def ns_delete(ctx, name, force, config, wait):
-    """deletes a NS instance
+def ns_delete(ctx, names, ns_id_list, force, config, wait):
+    """deletes NS instances
 
-    NAME: name or ID of the NS instance to be deleted
+    NAMES: names or IDs of the NS instances to be deleted
     """
     logger.debug("")
-    if not force:
-        ctx.obj.ns.delete(name, config=config, wait=wait)
+    ns_id_list = ctx.params["ns_id_list"]
+    if len(ns_id_list) > 25:
+        raise ClientException("Cannot delete more than 25 instances")
+    if len(ns_id_list) > 1:
+        ns_id_dict = {}
+        ns_id_dict["ns_ids"] = []
+        for name in ns_id_list:
+            nsr = ctx.obj.ns.get(name)
+            ns_id_dict["ns_ids"].append(nsr["_id"])
+        ctx.obj.ns.multi_delete(ns_id_dict)
     else:
-        utils.check_client_version(ctx.obj, "--force")
-        ctx.obj.ns.delete(name, force, config=config, wait=wait)
+        name = ns_id_list[0]
+        if not force:
+            ctx.obj.ns.delete(name, config=config, wait=wait)
+        else:
+            utils.check_client_version(ctx.obj, "--force")
+            ctx.obj.ns.delete(name, force, config=config, wait=wait)
index 236674f..29232c1 100644 (file)
@@ -153,6 +153,19 @@ class Ns(object):
             #         msg = resp
             raise ClientException("failed to delete ns {} - {}".format(name, msg))
 
+    def multi_delete(self, ns_id_dict):
+        self._client.get_token()
+        self._apiResource = "/ns_instances_terminate"
+        self._apiBase = "{}{}{}".format(
+            self._apiName, self._apiVersion, self._apiResource
+        )
+        http_code, resp = self._http.post_cmd(self._apiBase, postfields_dict=ns_id_dict)
+        if http_code == 202:
+            print("Deletion in progress")
+        else:
+            msg = resp or ""
+            raise ClientException("failed to delete ns - {}".format(msg))
+
     def create(
         self,
         nsd_name,