X-Git-Url: https://osm.etsi.org/gitweb/?p=osm%2FNBI.git;a=blobdiff_plain;f=osm_nbi%2Fbase_topic.py;h=095e26511e794d4c4a83c414ddb3ea36fc262fca;hp=2060c4c9f4090bdaa8d239d1195d6f03d38e7815;hb=e4a07d5db1e38e6a52788d5788c6cc3396e1052d;hpb=747c34e54f98aa70b3407ad0da54e5ad0cfb64a0;ds=sidebyside diff --git a/osm_nbi/base_topic.py b/osm_nbi/base_topic.py index 2060c4c..095e265 100644 --- a/osm_nbi/base_topic.py +++ b/osm_nbi/base_topic.py @@ -1,11 +1,24 @@ # -*- coding: utf-8 -*- +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import logging from uuid import uuid4 from http import HTTPStatus from time import time from osm_common.dbbase import deep_update_rfc7396 -from validation import validate_input, ValidationError +from validation import validate_input, ValidationError, is_valid_uuid __author__ = "Alfonso Tierno " @@ -43,12 +56,26 @@ class BaseTopic: schema_new = None # to_override schema_edit = None # to_override + # Alternative ID Fields for some Topics + alt_id_field = { + "projects": "name", + "users": "username" + } + def __init__(self, db, fs, msg): self.db = db self.fs = fs self.msg = msg self.logger = logging.getLogger("nbi.engine") + @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): + return BaseTopic.alt_id_field[topic] + else: + return "_id" + @staticmethod def _remove_envelop(indata=None): if not indata: @@ -188,7 +215,7 @@ class BaseTopic: Update descriptor with the kwargs. It contains dot separated keys :param desc: dictionary to be updated :param kwargs: plain dictionary to be used for updating. - :return: None, 'desc' is modified. It raises EngineException. + :return: None, 'desc' is modified. It raises EngineException. """ if not kwargs: return @@ -226,7 +253,8 @@ class BaseTopic: :return: dictionary, raise exception if not found. """ filter_db = self._get_project_filter(session, write=False, show_all=True) - filter_db["_id"] = _id + # 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) # TODO transform data for SOL005 URL requests # TODO remove _admin if not admin @@ -326,7 +354,8 @@ class BaseTopic: # data = self.get_item(topic, _id) self.check_conflict_on_del(session, _id, force) filter_q = self._get_project_filter(session, write=True, show_all=True) - filter_q["_id"] = _id + # To allow project addressing by name AS WELL AS _id + filter_q[BaseTopic.id_field(self.topic, _id)] = _id if not dry_run: v = self.db.del_one(self.topic, filter_q) self._send_msg("deleted", {"_id": _id}) @@ -348,7 +377,10 @@ class BaseTopic: deep_update_rfc7396(content, indata) self.check_conflict_on_edit(session, content, indata, _id=_id, force=force) self.format_on_edit(content, indata) - self.db.replace(self.topic, _id, content) + # To allow project addressing by name AS WELL AS _id + # self.db.replace(self.topic, _id, content) + cid = content.get("_id") + self.db.replace(self.topic, cid if cid else _id, content) indata.pop("_admin", None) indata["_id"] = _id