Update from master
[osm/NBI.git] / osm_nbi / nbi.py
index 80548ce..1cd13b7 100644 (file)
@@ -32,6 +32,7 @@ from osm_nbi.validation import ValidationError
 from osm_common.dbbase import DbException
 from osm_common.fsbase import FsException
 from osm_common.msgbase import MsgException
+from osm_common.wftemporal import WFTemporal
 from http import HTTPStatus
 from codecs import getreader
 from os import environ, path
@@ -115,8 +116,6 @@ URL: /osm                                                       GET     POST
                 /<id>                                           O                       O       O
             /k8sclusters                                        O       O
                 /<id>                                           O                       O       O
-            /paas                                               O5      O5
-                /<id>                                           O5                      O5      O5
             /k8srepos                                           O       O
                 /<id>                                           O                               O
             /osmrepos                                           O       O
@@ -290,14 +289,6 @@ valid_url_methods = {
                     "ROLE_PERMISSION": "vca:id:",
                 },
             },
-            "paas": {
-                "METHODS": ("GET", "POST"),
-                "ROLE_PERMISSION": "paas:",
-                "<ID>": {
-                    "METHODS": ("GET", "DELETE", "PATCH"),
-                    "ROLE_PERMISSION": "paas:id:",
-                },
-            },
             "k8srepos": {
                 "METHODS": ("GET", "POST"),
                 "ROLE_PERMISSION": "k8srepos:",
@@ -669,6 +660,7 @@ class Server(object):
         self.engine = Engine(self.authenticator)
 
     def _format_in(self, kwargs):
+        error_text = ""  # error_text must be initialized outside try
         try:
             indata = None
             if cherrypy.request.body.length:
@@ -681,9 +673,7 @@ class Server(object):
                         cherrypy.request.headers.pop("Content-File-MD5", None)
                     elif "application/yaml" in cherrypy.request.headers["Content-Type"]:
                         error_text = "Invalid yaml format "
-                        indata = yaml.load(
-                            cherrypy.request.body, Loader=yaml.SafeLoader
-                        )
+                        indata = yaml.safe_load(cherrypy.request.body)
                         cherrypy.request.headers.pop("Content-File-MD5", None)
                     elif (
                         "application/binary" in cherrypy.request.headers["Content-Type"]
@@ -713,13 +703,11 @@ class Server(object):
                         #                          "Only 'Content-Type' of type 'application/json' or
                         # 'application/yaml' for input format are available")
                         error_text = "Invalid yaml format "
-                        indata = yaml.load(
-                            cherrypy.request.body, Loader=yaml.SafeLoader
-                        )
+                        indata = yaml.safe_load(cherrypy.request.body)
                         cherrypy.request.headers.pop("Content-File-MD5", None)
                 else:
                     error_text = "Invalid yaml format "
-                    indata = yaml.load(cherrypy.request.body, Loader=yaml.SafeLoader)
+                    indata = yaml.safe_load(cherrypy.request.body)
                     cherrypy.request.headers.pop("Content-File-MD5", None)
             if not indata:
                 indata = {}
@@ -734,7 +722,7 @@ class Server(object):
                         kwargs[k] = None
                     elif format_yaml:
                         try:
-                            kwargs[k] = yaml.load(v, Loader=yaml.SafeLoader)
+                            kwargs[k] = yaml.safe_load(v)
                         except Exception:
                             pass
                     elif (
@@ -758,7 +746,7 @@ class Server(object):
                             v[index] = None
                         elif format_yaml:
                             try:
-                                v[index] = yaml.load(v[index], Loader=yaml.SafeLoader)
+                                v[index] = yaml.safe_load(v[index])
                             except Exception:
                                 pass
 
@@ -913,7 +901,7 @@ class Server(object):
         project_name=None,
         ns_id=None,
         *args,
-        **kwargs
+        **kwargs,
     ):
         if topic == "alarms":
             try:
@@ -986,7 +974,7 @@ class Server(object):
                         return self._format_out(str(alarm_list))
                 # to handle patch request for alarm update
                 elif cherrypy.request.method == "PATCH":
-                    data = yaml.load(cherrypy.request.body, Loader=yaml.SafeLoader)
+                    data = yaml.safe_load(cherrypy.request.body)
                     try:
                         # check if uuid is valid
                         self.engine.db.get_one("alarms", {"uuid": data.get("uuid")})
@@ -1010,7 +998,6 @@ class Server(object):
                         )
                     return self._format_out("Alarm updated")
             except Exception as e:
-                cherrypy.response.status = e.http_code.value
                 if isinstance(
                     e,
                     (
@@ -1071,7 +1058,7 @@ class Server(object):
             outdata = token_info = self.authenticator.new_token(
                 token_info, indata, cherrypy.request.remote
             )
-            cherrypy.session["Authorization"] = outdata["_id"]
+            cherrypy.session["Authorization"] = outdata["_id"]  # pylint: disable=E1101
             self._set_location_header("admin", "v1", "tokens", outdata["_id"])
             # for logging
             self._format_login(token_info)
@@ -1094,7 +1081,7 @@ class Server(object):
                 token_id = token_info["_id"]
             outdata = self.authenticator.del_token(token_id)
             token_info = None
-            cherrypy.session["Authorization"] = "logout"
+            cherrypy.session["Authorization"] = "logout"  # pylint: disable=E1101
             # cherrypy.response.cookie["Authorization"] = token_id
             # cherrypy.response.cookie["Authorization"]['expires'] = 0
         else:
@@ -1122,7 +1109,8 @@ class Server(object):
         elif args and args[0] == "init":
             try:
                 # self.engine.load_dbase(cherrypy.request.app.config)
-                self.engine.create_admin()
+                pid = self.authenticator.create_admin_project()
+                self.authenticator.create_admin_user(pid)
                 return "Done. User 'admin', password 'admin' created"
             except Exception:
                 cherrypy.response.status = HTTPStatus.FORBIDDEN.value
@@ -1180,13 +1168,13 @@ class Server(object):
             return_text = "<html><pre>{} ->\n".format(main_topic)
             try:
                 if cherrypy.request.method == "POST":
-                    to_send = yaml.load(cherrypy.request.body, Loader=yaml.SafeLoader)
+                    to_send = yaml.safe_load(cherrypy.request.body)
                     for k, v in to_send.items():
                         self.engine.msg.write(main_topic, k, v)
                         return_text += "  {}: {}\n".format(k, v)
                 elif cherrypy.request.method == "GET":
                     for k, v in kwargs.items():
-                        v_dict = yaml.load(v, Loader=yaml.SafeLoader)
+                        v_dict = yaml.safe_load(v)
                         self.engine.msg.write(main_topic, k, v_dict)
                         return_text += "  {}: {}\n".format(k, v_dict)
             except Exception as e:
@@ -1200,10 +1188,12 @@ class Server(object):
             + "  headers: {}\n".format(cherrypy.request.headers)
             + "  path_info: {}\n".format(cherrypy.request.path_info)
             + "  query_string: {}\n".format(cherrypy.request.query_string)
-            + "  session: {}\n".format(cherrypy.session)
+            + "  session: {}\n".format(cherrypy.session)  # pylint: disable=E1101
             + "  cookie: {}\n".format(cherrypy.request.cookie)
             + "  method: {}\n".format(cherrypy.request.method)
-            + "  session: {}\n".format(cherrypy.session.get("fieldname"))
+            + "  session: {}\n".format(
+                cherrypy.session.get("fieldname")  # pylint: disable=E1101
+            )
             + "  body:\n"
         )
         return_text += "    length: {}\n".format(cherrypy.request.body.length)
@@ -1392,7 +1382,7 @@ class Server(object):
         _id=None,
         item=None,
         *args,
-        **kwargs
+        **kwargs,
     ):
         token_info = None
         outdata = None
@@ -1796,7 +1786,6 @@ class Server(object):
                         self.engine.db.del_list(
                             rollback_item["topic"],
                             rollback_item["filter"],
-                            fail_on_empty=False,
                         )
                     else:
                         self.engine.db.del_one(
@@ -1862,7 +1851,7 @@ def _start_service():
                 update_dict["server.socket_host"] = v
             elif k1 in ("server", "test", "auth", "log"):
                 update_dict[k1 + "." + k2] = v
-            elif k1 in ("message", "database", "storage", "authentication"):
+            elif k1 in ("message", "database", "storage", "authentication", "temporal"):
                 # k2 = k2.replace('_', '.')
                 if k2 in ("port", "db_port"):
                     engine_config[k1][k2] = int(v)
@@ -1872,7 +1861,9 @@ def _start_service():
         except ValueError as e:
             cherrypy.log.error("Ignoring environ '{}': " + str(e))
         except Exception as e:
-            cherrypy.log.warn("skipping environ '{}' on exception '{}'".format(k, e))
+            cherrypy.log(
+                "WARNING: skipping environ '{}' on exception '{}'".format(k, e)
+            )
 
     if update_dict:
         cherrypy.config.update(update_dict)
@@ -1946,6 +1937,10 @@ def _start_service():
     subscription_thread.start()
     # Do not capture except SubscriptionException
 
+    WFTemporal.temporal_api = (
+        f'{engine_config["temporal"]["host"]}:{engine_config["temporal"]["port"]}'
+    )
+
     backend = engine_config["authentication"]["backend"]
     cherrypy.log.error(
         "Starting OSM NBI Version '{} {}' with '{}' authentication backend".format(