Addition of PaaS
[osm/NBI.git] / osm_nbi / admin_topics.py
index 0006917..daeb260 100644 (file)
@@ -37,6 +37,8 @@ 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,
@@ -370,14 +372,21 @@ 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"])
 
-        # encrypt passwords
+    def _encrypt_password(self, content, schema_version):
         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")
@@ -390,13 +399,33 @@ class CommonVimWimSdn(BaseTopic):
                         salt=content["_id"],
                     )
 
-        content["_admin"]["operationalState"] = "PROCESSING"
-
+    def _insert_create_operation(self, content):
         # create operation
         content["_admin"]["operations"] = [self._create_operation("create")]
         content["_admin"]["current_operation"] = None
-
-        return "{}:0".format(content["_id"])
+        # create Resource in Openstack based VIM
+        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},
+                }
+                storage = {
+                    "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},
+                }
+                content["resources"] = {
+                    "compute": compute,
+                    "storage": storage,
+                    "network": network,
+                }
 
     def delete(self, session, _id, dry_run=False, not_send_msg=None):
         """
@@ -522,7 +551,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 = {}
 
 
@@ -700,6 +729,39 @@ 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"
@@ -922,12 +984,13 @@ class UserTopicAuth(UserTopic):
         except ValidationError as e:
             raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
 
-    def show(self, session, _id, api_req=False):
+    def show(self, session, _id, filter_q=None, api_req=False):
         """
         Get complete information on an topic
 
         :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
         :param _id: server internal id or username
+        :param filter_q: dict: query parameter
         :param api_req: True if this call is serving an external API request. False if serving internal request.
         :return: dictionary, raise exception if not found.
         """
@@ -1046,10 +1109,7 @@ 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)
@@ -1066,10 +1126,7 @@ 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
@@ -1082,10 +1139,7 @@ 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
@@ -1097,6 +1151,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,
                 }
@@ -1295,12 +1350,13 @@ class ProjectTopicAuth(ProjectTopic):
         except ValidationError as e:
             raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY)
 
-    def show(self, session, _id, api_req=False):
+    def show(self, session, _id, filter_q=None, api_req=False):
         """
         Get complete information on an topic
 
         :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
         :param _id: server internal id
+        :param filter_q: dict: query parameter
         :param api_req: True if this call is serving an external API request. False if serving internal request.
         :return: dictionary, raise exception if not found.
         """
@@ -1594,12 +1650,13 @@ class RoleTopicAuth(BaseTopic):
             final_content["permissions"]["admin"] = False
         return None
 
-    def show(self, session, _id, api_req=False):
+    def show(self, session, _id, filter_q=None, api_req=False):
         """
         Get complete information on an topic
 
         :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
         :param _id: server internal id
+        :param filter_q: dict: query parameter
         :param api_req: True if this call is serving an external API request. False if serving internal request.
         :return: dictionary, raise exception if not found.
         """