From: tierno Date: Thu, 12 Apr 2018 15:48:49 +0000 (+0200) Subject: Added database control version X-Git-Tag: v4.0.0~18 X-Git-Url: https://osm.etsi.org/gitweb/?a=commitdiff_plain;h=refs%2Fchanges%2F75%2F5975%2F1;p=osm%2FNBI.git Added database control version Change-Id: I3c4f4e0aa446cc6f242293ddb8e39cdb90bc4575 Signed-off-by: tierno --- diff --git a/osm_nbi/engine.py b/osm_nbi/engine.py index e55c7ec..f8c7404 100644 --- a/osm_nbi/engine.py +++ b/osm_nbi/engine.py @@ -736,18 +736,47 @@ class Engine(object): def create_admin(self): """ - Creates a new user admin/admin into database. Only allowed if database is empty. Useful for initialization - :return: _id identity of the inserted data. + Creates a new user admin/admin into database if database is empty. Useful for initialization + :return: _id identity of the inserted data, or None """ users = self.db.get_one("users", fail_on_empty=False, fail_on_more=False) if users: - raise EngineException("Unauthorized. Database users is not empty", HTTPStatus.UNAUTHORIZED) + return None + # raise EngineException("Unauthorized. Database users is not empty", HTTPStatus.UNAUTHORIZED) indata = {"username": "admin", "password": "admin", "projects": ["admin"]} fake_session = {"project_id": "admin", "username": "admin"} self._format_new_data(fake_session, "users", indata) _id = self.db.create("users", indata) return _id + def init_db(self, target_version='1.0'): + """ + Init database if empty. If not empty it checks that database version is ok. + If empty, it creates a new user admin/admin at 'users' and a new entry at 'version' + :return: None if ok, exception if error or if the version is different. + """ + version = self.db.get_one("versions", fail_on_empty=False, fail_on_more=False) + if not version: + # create user admin + self.create_admin() + # create database version + version_data = { + "_id": '1.0', # version text + "version": 1000, # version number + "date": "2018-04-12", # version date + "description": "initial design", # changes in this version + 'status': 'ENABLED' # ENABLED, DISABLED (migration in process), ERROR, + } + self.db.create("version", version_data) + elif version["_id"] != target_version: + # TODO implement migration process + raise EngineException("Wrong database version '{}'. Expected '{}'".format( + version["_id"], target_version), HTTPStatus.INTERNAL_SERVER_ERROR) + elif version["status"] != 'ENABLED': + raise EngineException("Wrong database status '{}'".format( + version["status"]), HTTPStatus.INTERNAL_SERVER_ERROR) + return + def _edit_item(self, session, item, id, content, indata={}, kwargs=None): if indata: indata = self._remove_envelop(item, indata) diff --git a/osm_nbi/nbi.py b/osm_nbi/nbi.py index 0a344fb..0520ef8 100644 --- a/osm_nbi/nbi.py +++ b/osm_nbi/nbi.py @@ -21,6 +21,7 @@ from os import environ __author__ = "Alfonso Tierno " __version__ = "0.3" version_date = "Apr 2018" +database_version = '1.0' """ North Bound Interface (O: OSM specific; 5,X: SOL005 not implemented yet; O5: SOL005 implemented) @@ -788,7 +789,7 @@ def _start_service(): # TODO add more entries, e.g.: storage cherrypy.tree.apps['/osm'].root.engine.start(engine_config) try: - cherrypy.tree.apps['/osm'].root.engine.create_admin() + cherrypy.tree.apps['/osm'].root.engine.init_db(target_version=database_version) except EngineException: pass # getenv('OSMOPENMANO_TENANT', None)