X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FNBI.git;a=blobdiff_plain;f=osm_nbi%2Fbase_topic.py;h=c8a7665e30be46ac463b47b794f3a49a9231be0d;hp=72707ad55d7c7fbb042afa7e189f3adaa1ffa335;hb=d77ba6fb46f75d52475978e6e1272b2169edd56c;hpb=65ca36d13f895d0a361d59a5962029d6e3ef7a99 diff --git a/osm_nbi/base_topic.py b/osm_nbi/base_topic.py index 72707ad..c8a7665 100644 --- a/osm_nbi/base_topic.py +++ b/osm_nbi/base_topic.py @@ -60,7 +60,9 @@ class BaseTopic: # Alternative ID Fields for some Topics alt_id_field = { "projects": "name", - "users": "username" + "users": "username", + "roles": "name", + "roles_operations": "name" } def __init__(self, db, fs, msg): @@ -72,7 +74,7 @@ class BaseTopic: @staticmethod def id_field(topic, value): """Returns ID Field for given topic and field value""" - if topic in ["projects", "users"] and not is_valid_uuid(value): + if topic in BaseTopic.alt_id_field.keys() and not is_valid_uuid(value): return BaseTopic.alt_id_field[topic] else: return "_id" @@ -191,7 +193,10 @@ class BaseTopic: :param _id: If not None, ignore this entry that are going to change :return: None or raises EngineException """ - _filter = self._get_project_filter(session) + if not self.multiproject: + _filter = {} + else: + _filter = self._get_project_filter(session) _filter["name"] = name if _id: _filter["_id.neq"] = _id @@ -234,11 +239,12 @@ class BaseTopic: content.pop("_admin", None) self.msg.write(self.topic_msg, action, content) - def check_conflict_on_del(self, session, _id): + def check_conflict_on_del(self, session, _id, db_content): """ Check if deletion can be done because of dependencies if it is not force. To override :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 """ pass @@ -286,7 +292,10 @@ class BaseTopic: :param _id: server internal id :return: dictionary, raise exception if not found. """ - filter_db = self._get_project_filter(session) + if not self.multiproject: + filter_db = {} + else: + filter_db = self._get_project_filter(session) # To allow project&user addressing by name AS WELL AS _id filter_db[BaseTopic.id_field(self.topic, _id)] = _id return self.db.get_one(self.topic, filter_db) @@ -313,8 +322,8 @@ class BaseTopic: """ if not filter_q: filter_q = {} - - filter_q.update(self._get_project_filter(session)) + if self.multiproject: + filter_q.update(self._get_project_filter(session)) # TODO transform data for SOL005 URL requests. Transform filtering # TODO implement "field-type" query string SOL005 @@ -369,15 +378,19 @@ class BaseTopic: # TODO add admin to filter, validate rights if not filter_q: filter_q = {} - filter_q.update(self._get_project_filter(session)) + if self.multiproject: + filter_q.update(self._get_project_filter(session)) return self.db.del_list(self.topic, filter_q) - def delete_extra(self, session, _id): + def delete_extra(self, session, _id, db_content): """ Delete other things apart from database entry of a item _id. e.g.: other associated elements at database and other file system storage :param session: contains "username", "admin", "force", "public", "project_id", "set_project" :param _id: server internal id + :param db_content: The database content of the _id. It is already deleted when reached this method, but the + content is needed in same cases + :return: None if ok or raises EngineException with the problem """ pass @@ -389,14 +402,19 @@ class BaseTopic: :param dry_run: make checking but do not delete :return: dictionary with deleted item _id. It raises EngineException on error: not found, conflict, ... """ + + # To allow addressing projects and users by name AS WELL AS by _id + filter_q = {BaseTopic.id_field(self.topic, _id): _id} + item_content = self.db.get_one(self.topic, filter_q) + # TODO add admin to filter, validate rights # data = self.get_item(topic, _id) - self.check_conflict_on_del(session, _id) - filter_q = self._get_project_filter(session) - # To allow project addressing by name AS WELL AS _id - filter_q[BaseTopic.id_field(self.topic, _id)] = _id + self.check_conflict_on_del(session, _id, item_content) if dry_run: return None + + if self.multiproject: + filter_q.update(self._get_project_filter(session)) if self.multiproject and session["project_id"]: # remove reference from project_read. If not last delete self.db.set_one(self.topic, filter_q, update_dict=None, @@ -408,7 +426,7 @@ class BaseTopic: return v else: v = self.db.del_one(self.topic, filter_q) - self.delete_extra(session, _id) + self.delete_extra(session, _id, item_content) self._send_msg("deleted", {"_id": _id}) return v