PyYAML fix
[osm/NBI.git] / osm_nbi / admin_topics.py
index c2767c8..f70c497 100644 (file)
@@ -47,6 +47,7 @@ from osm_nbi.base_topic import BaseTopic, EngineException
 from osm_nbi.authconn import AuthconnNotFoundException, AuthconnConflictException
 from osm_common.dbbase import deep_update_rfc7396
 import copy
+from osm_nbi.temporal.nbi_temporal import NbiTemporal
 
 __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
 
@@ -323,6 +324,18 @@ class CommonVimWimSdn(BaseTopic):
 
         return final_content
 
+    def _validate_input_edit(self, input, content, force=False):
+        """
+        Validates input user content for an edition. It uses jsonschema. Some overrides will use pyangbind
+        :param input: user input content for the new topic
+        :param force: may be used for being more tolerant
+        :return: The same input content, or a changed version of it.
+        """
+
+        if "vim_type" in content:
+            input["vim_type"] = content["vim_type"]
+        return super()._validate_input_edit(input, content, force)
+
     def format_on_edit(self, final_content, edit_content):
         """
         Modifies final_content inserting admin information upon edition
@@ -345,7 +358,6 @@ class CommonVimWimSdn(BaseTopic):
                 schema_version
             ) or self.config_to_encrypt.get("default")
             if edit_content.get("config") and config_to_encrypt_keys:
-
                 for p in config_to_encrypt_keys:
                     if edit_content["config"].get(p):
                         final_content["config"][p] = self.db.encrypt(
@@ -399,48 +411,25 @@ class CommonVimWimSdn(BaseTopic):
         if content.get("vim_type"):
             if content["vim_type"] == "openstack":
                 compute = {
-                    "ram": {
-                        "total": None,
-                        "used": None
-                    },
-                    "vcpus": {
-                        "total": None,
-                        "used": None
-                    },
-                    "instances": {
-                        "total": None,
-                        "used": None
-                    }
+                    "ram": {"total": None, "used": None},
+                    "vcpus": {"total": None, "used": None},
+                    "instances": {"total": None, "used": None},
                 }
                 storage = {
-                    "volumes": {
-                        "total": None,
-                        "used": None
-                    },
-                    "snapshots": {
-                        "total": None,
-                        "used": None
-                    },
-                    "storage": {
-                        "total": None,
-                        "used": None
-                    }
+                    "volumes": {"total": None, "used": None},
+                    "snapshots": {"total": None, "used": None},
+                    "storage": {"total": None, "used": None},
                 }
                 network = {
-                    "networks": {
-                        "total": None,
-                        "used": None
-                    },
-                    "subnets": {
-                        "total": None,
-                        "used": None
-                    },
-                    "floating_ips": {
-                        "total": None,
-                        "used": None
-                    }
+                    "networks": {"total": None, "used": None},
+                    "subnets": {"total": None, "used": None},
+                    "floating_ips": {"total": None, "used": None},
+                }
+                content["resources"] = {
+                    "compute": compute,
+                    "storage": storage,
+                    "network": network,
                 }
-                content["resources"] = {"compute": compute, "storage": storage, "network": network}
 
         return "{}:0".format(content["_id"])
 
@@ -504,8 +493,15 @@ class CommonVimWimSdn(BaseTopic):
         if session["force"]:
             self.db.del_one(self.topic, {"_id": _id})
             op_id = None
+            message = {"_id": _id, "op_id": op_id}
+            # The vim_type is a temporary hack to shim in temporal workflows in the create
+            if "vim_type" in db_content:
+                message["vim_type"] = db_content["vim_type"]
+
             self._send_msg(
-                "deleted", {"_id": _id, "op_id": op_id}, not_send_msg=not_send_msg
+                "deleted",
+                message,
+                not_send_msg=not_send_msg,
             )
         else:
             update_dict = {"_admin.to_delete": True}
@@ -520,8 +516,15 @@ class CommonVimWimSdn(BaseTopic):
             op_id = "{}:{}".format(
                 db_content["_id"], len(db_content["_admin"]["operations"])
             )
+            message = {"_id": _id, "op_id": op_id}
+            # The vim_type is a temporary hack to shim in temporal workflows in the create
+            if "vim_type" in db_content:
+                message["vim_type"] = db_content["vim_type"]
+
             self._send_msg(
-                "delete", {"_id": _id, "op_id": op_id}, not_send_msg=not_send_msg
+                "delete",
+                message,
+                not_send_msg=not_send_msg,
             )
         return op_id
 
@@ -542,6 +545,45 @@ class VimAccountTopic(CommonVimWimSdn):
             "vrops_password",
         ),
     }
+    valid_paas_providers = ["juju"]
+    temporal = NbiTemporal()
+
+    def check_conflict_on_new(self, session, indata):
+        super().check_conflict_on_new(session, indata)
+        self._check_paas_account(indata)
+
+    def _is_paas_vim_type(self, indata):
+        return indata.get("vim_type") and indata["vim_type"] == "paas"
+
+    def _check_paas_account(self, indata):
+        if not self._is_paas_vim_type(indata):
+            return
+        if not self._is_valid_paas_config(indata.get("config")):
+            raise EngineException(
+                "Invalid config for VIM account '{}'.".format(indata["name"]),
+                HTTPStatus.UNPROCESSABLE_ENTITY,
+            )
+
+    def _is_valid_paas_config(self, config) -> bool:
+        if not config:
+            return False
+        paas_provider = config.get("paas_provider")
+        is_valid_paas_provider = paas_provider in self.valid_paas_providers
+        if paas_provider == "juju":
+            return self._is_valid_juju_paas_config(config)
+        return is_valid_paas_provider
+
+    def _is_valid_juju_paas_config(self, config) -> bool:
+        if not config:
+            return False
+        config_keys = [
+            "paas_provider",
+            "ca_cert_content",
+            "cloud",
+            "cloud_credentials",
+            "authorized_keys",
+        ]
+        return all(key in config for key in config_keys)
 
     def check_conflict_on_del(self, session, _id, db_content):
         """
@@ -561,6 +603,13 @@ class VimAccountTopic(CommonVimWimSdn):
             )
         super().check_conflict_on_del(session, _id, db_content)
 
+    def _send_msg(self, action, content, not_send_msg=None):
+        if self._is_paas_vim_type(content):
+            self.temporal.start_vim_workflow(action, content)
+            return
+
+        super()._send_msg(action, content, not_send_msg)
+
 
 class WimAccountTopic(CommonVimWimSdn):
     topic = "wim_accounts"
@@ -568,7 +617,7 @@ class WimAccountTopic(CommonVimWimSdn):
     schema_new = wim_account_new_schema
     schema_edit = wim_account_edit_schema
     multiproject = True
-    password_to_encrypt = "wim_password"
+    password_to_encrypt = "password"
     config_to_encrypt = {}
 
 
@@ -1097,7 +1146,6 @@ class UserTopicAuth(UserTopic):
                         mapping["role"],
                         mapping["role_name"],
                     ):
-
                         if mapping in mappings_to_remove:  # do not remove
                             mappings_to_remove.remove(mapping)
                         break  # do not add, it is already at user
@@ -1144,6 +1192,7 @@ class UserTopicAuth(UserTopic):
                     "_id": _id,
                     "username": indata.get("username"),
                     "password": indata.get("password"),
+                    "old_password": indata.get("old_password"),
                     "add_project_role_mappings": mappings_to_add,
                     "remove_project_role_mappings": mappings_to_remove,
                 }