Add Juju PaaS as a VIM
[osm/NBI.git] / osm_nbi / admin_topics.py
index daeb260..b2def67 100644 (file)
@@ -37,8 +37,6 @@ from osm_nbi.validation import (
     k8srepo_edit_schema,
     vca_new_schema,
     vca_edit_schema,
-    paas_new_schema,
-    paas_edit_schema,
     osmrepo_new_schema,
     osmrepo_edit_schema,
     validate_input,
@@ -347,7 +345,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(
@@ -372,21 +369,14 @@ class CommonVimWimSdn(BaseTopic):
         """
         super().format_on_new(content, project_id=project_id, make_public=make_public)
         content["schema_version"] = schema_version = "1.11"
-        self._encrypt_password(content, schema_version)
-        self._encrypt_config_fields(content, schema_version)
-        content["_admin"]["operationalState"] = "PROCESSING"
-        self._insert_create_operation(content)
-        return "{}:0".format(content["_id"])
 
-    def _encrypt_password(self, content, schema_version):
+        # encrypt passwords
         if content.get(self.password_to_encrypt):
             content[self.password_to_encrypt] = self.db.encrypt(
                 content[self.password_to_encrypt],
                 schema_version=schema_version,
                 salt=content["_id"],
             )
-
-    def _encrypt_config_fields(self, content, schema_version):
         config_to_encrypt_keys = self.config_to_encrypt.get(
             schema_version
         ) or self.config_to_encrypt.get("default")
@@ -399,7 +389,8 @@ class CommonVimWimSdn(BaseTopic):
                         salt=content["_id"],
                     )
 
-    def _insert_create_operation(self, content):
+        content["_admin"]["operationalState"] = "PROCESSING"
+
         # create operation
         content["_admin"]["operations"] = [self._create_operation("create")]
         content["_admin"]["current_operation"] = None
@@ -427,6 +418,8 @@ class CommonVimWimSdn(BaseTopic):
                     "network": network,
                 }
 
+        return "{}:0".format(content["_id"])
+
     def delete(self, session, _id, dry_run=False, not_send_msg=None):
         """
         Delete item by its internal _id
@@ -525,6 +518,30 @@ class VimAccountTopic(CommonVimWimSdn):
             "vrops_password",
         ),
     }
+    valid_paas_providers = ["juju"]
+
+    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 self._is_paas_vim_type(indata):
+            self._check_paas_provider_is_valid(indata)
+
+    def _check_paas_provider_is_valid(self, indata):
+        try:
+            paas_provider = indata["config"]["paas_provider"]
+            if paas_provider in self.valid_paas_providers:
+                return
+        except Exception:
+            pass
+        raise EngineException(
+            "Invalid paas_provider for VIM account '{}'.".format(indata["name"]),
+            HTTPStatus.UNPROCESSABLE_ENTITY,
+        )
 
     def check_conflict_on_del(self, session, _id, db_content):
         """
@@ -544,6 +561,11 @@ 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):
+            return
+        super()._send_msg(action, content, not_send_msg)
+
 
 class WimAccountTopic(CommonVimWimSdn):
     topic = "wim_accounts"
@@ -729,39 +751,6 @@ class VcaTopic(CommonVimWimSdn):
         super().check_conflict_on_del(session, _id, db_content)
 
 
-class PaasTopic(CommonVimWimSdn):
-    topic = "paas"
-    topic_msg = "paas"
-    schema_new = paas_new_schema
-    schema_edit = paas_edit_schema
-    multiproject = True
-    password_to_encrypt = "secret"
-    config_to_encrypt = {}
-
-    def format_on_edit(self, final_content, edit_content):
-        oid = super().format_on_edit(final_content, edit_content)
-        final_content["_admin"]["operationalState"] = "PROCESSING"
-        final_content["_admin"]["detailed-status"] = "Editing"
-        return oid
-
-    def _check_if_used_by_ns(self):
-        pass
-
-    def check_conflict_on_del(self, session, _id, db_content):
-        """
-        Check if deletion can be done because of dependencies if it is not force.
-        :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
-        :param _id: internal _id
-        :param db_content: The database content of this item _id
-        :return: None if ok or raises EngineException with the conflict
-        """
-        if session["force"]:
-            return
-        self._check_if_used_by_ns()
-
-        super().check_conflict_on_del(session, _id, db_content)
-
-
 class K8sRepoTopic(CommonVimWimSdn):
     topic = "k8srepos"
     topic_msg = "k8srepo"
@@ -1109,8 +1098,10 @@ class UserTopicAuth(UserTopic):
                     if to_add["project"] in (
                         mapping["project"],
                         mapping["project_name"],
-                    ) and to_add["role"] in (mapping["role"], mapping["role_name"]):
-
+                    ) and to_add["role"] in (
+                        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
@@ -1126,7 +1117,10 @@ class UserTopicAuth(UserTopic):
                         if to_set["project"] in (
                             mapping["project"],
                             mapping["project_name"],
-                        ) and to_set["role"] in (mapping["role"], mapping["role_name"]):
+                        ) and to_set["role"] in (
+                            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
@@ -1139,7 +1133,10 @@ class UserTopicAuth(UserTopic):
                         if to_set["project"] in (
                             mapping["project"],
                             mapping["project_name"],
-                        ) and to_set["role"] in (mapping["role"], mapping["role_name"]):
+                        ) and to_set["role"] in (
+                            mapping["role"],
+                            mapping["role_name"],
+                        ):
                             break
                     else:
                         # delete