Support base64 encoded credentials for VIM creation
[osm/osmclient.git] / osmclient / sol005 / ns.py
index 00d68d6..29232c1 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,
@@ -153,12 +153,26 @@ 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,
         nsr_name,
         account,
         config=None,
+        ns_config_template=None,
         ssh_keys=None,
         description="default description",
         admin_status="ENABLED",
@@ -212,6 +226,16 @@ class Ns(object):
                     ns["ssh_keys"].append(f.read())
         if timeout:
             ns["timeout_ns_deploy"] = timeout
+        if ns_config_template:
+            _, resp = self._http.get2_cmd(
+                "/nsd{}/ns_config_template".format(self._apiVersion)
+            )
+            resp = json.loads(resp)
+            for rep in resp:
+                if ns_config_template == rep.get("name"):
+                    ns["nsConfigTemplateId"] = rep.get("_id")
+                if ns_config_template == rep.get("_id"):
+                    ns["nsConfigTemplateId"] = rep.get("_id")
         if config:
             ns_config = yaml.safe_load(config)
             if "vim-network-name" in ns_config:
@@ -278,10 +302,7 @@ class Ns(object):
             )
             headers = self._client._headers
             headers["Content-Type"] = "application/yaml"
-            http_header = [
-                "{}: {}".format(key, val) for (key, val) in list(headers.items())
-            ]
-            self._http.set_http_header(http_header)
+            self._http.set_http_header(headers)
             http_code, resp = self._http.post_cmd(
                 endpoint=self._apiBase, postfields_dict=ns
             )
@@ -434,6 +455,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,
@@ -589,7 +637,7 @@ class Ns(object):
                 http_code, resp = self._http.get2_cmd(
                     "{}/{}".format(self._apiBase, uuid)
                 )
-            if not uuid:
+            else:  # if not uuid
                 http_code, resp = self._http.get2_cmd(
                     "{}/{}/{}/{}".format(self._apiBase, uuid, project_name, ns_id)
                 )