Add `osm ns-update` command 64/11864/7
authorDavid Garcia <david.garcia@canonical.com>
Wed, 6 Apr 2022 08:47:31 +0000 (10:47 +0200)
committerbeierlm <mark.beierl@canonical.com>
Mon, 2 May 2022 13:36:39 +0000 (15:36 +0200)
The ns-update command allows to update a network service so that all its
VNF instances are updated to the latest revision.

Feature 10908

Change-Id: Iffa73558b3dfe8d83d33644294cb6068c642dddf
Signed-off-by: David Garcia <david.garcia@canonical.com>
osmclient/scripts/osm.py
osmclient/sol005/ns.py

index 44bc7be..58b34c5 100755 (executable)
@@ -5551,6 +5551,53 @@ def vnf_scale(
     #     exit(1)
 
 
+@cli_osm.command(
+    name="ns-update", short_help="executes an update of a Network Service."
+)
+@click.argument("ns_name")
+@click.option(
+    "--updatetype", required=True, type=str, help="available types: CHANGE_VNFPKG"
+)
+@click.option(
+    "--config",
+    required=True,
+    type=str,
+    help="extra information for update operation as YAML/JSON inline string as --config"
+    " '{changeVnfPackageData:[{vnfInstanceId: xxx, vnfdId: yyy}]}'",
+)
+@click.option(
+    "--timeout", required=False, default=None, type=int, help="timeout in seconds"
+)
+@click.option(
+    "--wait",
+    required=False,
+    default=False,
+    is_flag=True,
+    help="do not return the control immediately, but keep it until the operation is completed, or timeout",
+)
+@click.pass_context
+def update(ctx, ns_name, updatetype, config, timeout, wait):
+    """Executes an update of a Network Service.
+
+    The update will check new revisions of the Network Functions that are part of the
+    Network Service, and it will update them if needed.
+    Sample update command: osm ns-update  ns_instance_id --updatetype CHANGE_VNFPKG
+    --config '{changeVnfPackageData: [{vnfInstanceId: id_x,vnfdId: id_y}]}' --timeout 300 --wait
+
+    NS_NAME: Network service instance name or ID.
+
+    """
+    op_data = {
+        "timeout": timeout,
+        "updateType": updatetype,
+    }
+    if config:
+        op_data["config"] = yaml.safe_load(config)
+
+    check_client_version(ctx.obj, ctx.command.name)
+    ctx.obj.ns.update(ns_name, op_data, wait=wait)
+
+
 @cli_osm.command(name="alarm-show", short_help="show alarm details")
 @click.argument("uuid")
 @click.pass_context
index f4366b2..e1a4f69 100644 (file)
@@ -471,6 +471,54 @@ class Ns(object):
             )
             raise ClientException(message)
 
+    def update(self, ns_name, data, wait=False):
+        """Update NS instance.
+
+        This function calls the NBI in order to perform an update operation
+        on a Network Service instance.
+
+        Args:
+            ns_name: (str)
+            data: (dict)
+            wait: (boolean)
+
+        Returns:
+            None
+
+        """
+        self._logger.debug("")
+        self._client.get_token()
+        try:
+            op_data = {"updateType": data.pop("updateType")}
+
+            # Check update parameters availability according to update type
+            if op_data["updateType"] == "CHANGE_VNFPKG":
+                if not (
+                    data["config"]["changeVnfPackageData"][0].get("vnfInstanceId")
+                    and data["config"]["changeVnfPackageData"][0].get("vnfdId")
+                ):
+                    raise ClientException("you must set both vnfInstanceId and vnfdId")
+
+            # Fill up op_data
+            op_data["changeVnfPackageData"] = {}
+            op_data["changeVnfPackageData"]["vnfInstanceId"] = data["config"][
+                "changeVnfPackageData"
+            ][0].get("vnfInstanceId")
+
+            op_data["changeVnfPackageData"]["vnfdId"] = data["config"][
+                "changeVnfPackageData"
+            ][0].get("vnfdId")
+
+            if data.get("timeout"):
+                op_data["timeout_ns_update"] = data["timeout"]
+
+            op_id = self.exec_op(ns_name, op_name="update", op_data=op_data, wait=wait)
+            print(str(op_id))
+
+        except ClientException as exc:
+            message = "failed to update ns {}:\nerror:\n{}".format(ns_name, str(exc))
+            raise ClientException(message)
+
     def create_alarm(self, alarm):
         self._logger.debug("")
         self._client.get_token()