| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| tierno | d125caf | 2018-11-22 16:05:54 +0000 | [diff] [blame] | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 12 | # implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 16 | import logging |
| 17 | from uuid import uuid4 |
| 18 | from http import HTTPStatus |
| 19 | from time import time |
| 20 | from osm_common.dbbase import deep_update_rfc7396 |
| tierno | 23acf40 | 2019-08-28 13:36:34 +0000 | [diff] [blame] | 21 | from osm_nbi.validation import validate_input, ValidationError, is_valid_uuid |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 22 | |
| 23 | __author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>" |
| 24 | |
| 25 | |
| 26 | class EngineException(Exception): |
| 27 | |
| 28 | def __init__(self, message, http_code=HTTPStatus.BAD_REQUEST): |
| 29 | self.http_code = http_code |
| tierno | 23acf40 | 2019-08-28 13:36:34 +0000 | [diff] [blame] | 30 | super(Exception, self).__init__(message) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 31 | |
| 32 | |
| 33 | def get_iterable(input_var): |
| 34 | """ |
| 35 | Returns an iterable, in case input_var is None it just returns an empty tuple |
| 36 | :param input_var: can be a list, tuple or None |
| 37 | :return: input_var or () if it is None |
| 38 | """ |
| 39 | if input_var is None: |
| 40 | return () |
| 41 | return input_var |
| 42 | |
| 43 | |
| 44 | def versiontuple(v): |
| 45 | """utility for compare dot separate versions. Fills with zeros to proper number comparison""" |
| 46 | filled = [] |
| 47 | for point in v.split("."): |
| 48 | filled.append(point.zfill(8)) |
| 49 | return tuple(filled) |
| 50 | |
| 51 | |
| 52 | class BaseTopic: |
| 53 | # static variables for all instance classes |
| 54 | topic = None # to_override |
| 55 | topic_msg = None # to_override |
| 56 | schema_new = None # to_override |
| 57 | schema_edit = None # to_override |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 58 | multiproject = True # True if this Topic can be shared by several projects. Then it contains _admin.projects_read |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 59 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 60 | default_quota = 500 |
| 61 | |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 62 | # Alternative ID Fields for some Topics |
| 63 | alt_id_field = { |
| 64 | "projects": "name", |
| tierno | cf042d3 | 2019-06-13 09:06:40 +0000 | [diff] [blame] | 65 | "users": "username", |
| delacruzramo | 01b15d3 | 2019-07-02 14:37:47 +0200 | [diff] [blame] | 66 | "roles": "name" |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 69 | def __init__(self, db, fs, msg, auth): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 70 | self.db = db |
| 71 | self.fs = fs |
| 72 | self.msg = msg |
| 73 | self.logger = logging.getLogger("nbi.engine") |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 74 | self.auth = auth |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 75 | |
| 76 | @staticmethod |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 77 | def id_field(topic, value): |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 78 | """Returns ID Field for given topic and field value""" |
| delacruzramo | ceb8baf | 2019-06-21 14:25:38 +0200 | [diff] [blame] | 79 | if topic in BaseTopic.alt_id_field.keys() and not is_valid_uuid(value): |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 80 | return BaseTopic.alt_id_field[topic] |
| 81 | else: |
| 82 | return "_id" |
| 83 | |
| 84 | @staticmethod |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 85 | def _remove_envelop(indata=None): |
| 86 | if not indata: |
| 87 | return {} |
| 88 | return indata |
| 89 | |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 90 | def check_quota(self, session): |
| 91 | """ |
| 92 | Check whether topic quota is exceeded by the given project |
| 93 | Used by relevant topics' 'new' function to decide whether or not creation of the new item should be allowed |
| 94 | :param projects: projects (tuple) for which quota should be checked |
| 95 | :param override: boolean. If true, don't raise ValidationError even though quota be exceeded |
| 96 | :return: None |
| 97 | :raise: |
| 98 | DbException if project not found |
| 99 | ValidationError if quota exceeded and not overridden |
| 100 | """ |
| 101 | if session["force"] or session["admin"]: |
| 102 | return |
| 103 | projects = session["project_id"] |
| 104 | for project in projects: |
| 105 | proj = self.auth.get_project(project) |
| 106 | pid = proj["_id"] |
| 107 | quota = proj.get("quotas", {}).get(self.topic, self.default_quota) |
| 108 | count = self.db.count(self.topic, {"_admin.projects_read": pid}) |
| 109 | if count >= quota: |
| 110 | name = proj["name"] |
| 111 | raise ValidationError("{} quota ({}) exceeded for project {} ({})".format(self.topic, quota, name, pid)) |
| 112 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 113 | def _validate_input_new(self, input, force=False): |
| 114 | """ |
| 115 | Validates input user content for a new entry. It uses jsonschema. Some overrides will use pyangbind |
| 116 | :param input: user input content for the new topic |
| 117 | :param force: may be used for being more tolerant |
| 118 | :return: The same input content, or a changed version of it. |
| 119 | """ |
| 120 | if self.schema_new: |
| 121 | validate_input(input, self.schema_new) |
| 122 | return input |
| 123 | |
| 124 | def _validate_input_edit(self, input, force=False): |
| 125 | """ |
| 126 | Validates input user content for an edition. It uses jsonschema. Some overrides will use pyangbind |
| 127 | :param input: user input content for the new topic |
| 128 | :param force: may be used for being more tolerant |
| 129 | :return: The same input content, or a changed version of it. |
| 130 | """ |
| 131 | if self.schema_edit: |
| 132 | validate_input(input, self.schema_edit) |
| 133 | return input |
| 134 | |
| 135 | @staticmethod |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 136 | def _get_project_filter(session): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 137 | """ |
| 138 | Generates a filter dictionary for querying database, so that only allowed items for this project can be |
| 139 | addressed. Only propietary or public can be used. Allowed projects are at _admin.project_read/write. If it is |
| 140 | not present or contains ANY mean public. |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 141 | :param session: contains: |
| 142 | project_id: project list this session has rights to access. Can be empty, one or several |
| 143 | set_project: items created will contain this project list |
| 144 | force: True or False |
| 145 | public: True, False or None |
| 146 | method: "list", "show", "write", "delete" |
| 147 | admin: True or False |
| 148 | :return: dictionary with project filter |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 149 | """ |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 150 | p_filter = {} |
| 151 | project_filter_n = [] |
| 152 | project_filter = list(session["project_id"]) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 153 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 154 | if session["method"] not in ("list", "delete"): |
| 155 | if project_filter: |
| 156 | project_filter.append("ANY") |
| 157 | elif session["public"] is not None: |
| 158 | if session["public"]: |
| 159 | project_filter.append("ANY") |
| 160 | else: |
| 161 | project_filter_n.append("ANY") |
| 162 | |
| 163 | if session.get("PROJECT.ne"): |
| 164 | project_filter_n.append(session["PROJECT.ne"]) |
| 165 | |
| 166 | if project_filter: |
| 167 | if session["method"] in ("list", "show", "delete") or session.get("set_project"): |
| 168 | p_filter["_admin.projects_read.cont"] = project_filter |
| 169 | else: |
| 170 | p_filter["_admin.projects_write.cont"] = project_filter |
| 171 | if project_filter_n: |
| 172 | if session["method"] in ("list", "show", "delete") or session.get("set_project"): |
| 173 | p_filter["_admin.projects_read.ncont"] = project_filter_n |
| 174 | else: |
| 175 | p_filter["_admin.projects_write.ncont"] = project_filter_n |
| 176 | |
| 177 | return p_filter |
| 178 | |
| 179 | def check_conflict_on_new(self, session, indata): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 180 | """ |
| 181 | Check that the data to be inserted is valid |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 182 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 183 | :param indata: data to be inserted |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 184 | :return: None or raises EngineException |
| 185 | """ |
| 186 | pass |
| 187 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 188 | def check_conflict_on_edit(self, session, final_content, edit_content, _id): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 189 | """ |
| 190 | Check that the data to be edited/uploaded is valid |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 191 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 192 | :param final_content: data once modified. This method may change it. |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 193 | :param edit_content: incremental data that contains the modifications to apply |
| 194 | :param _id: internal _id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 195 | :return: None or raises EngineException |
| 196 | """ |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 197 | if not self.multiproject: |
| 198 | return |
| 199 | # Change public status |
| 200 | if session["public"] is not None: |
| 201 | if session["public"] and "ANY" not in final_content["_admin"]["projects_read"]: |
| 202 | final_content["_admin"]["projects_read"].append("ANY") |
| 203 | final_content["_admin"]["projects_write"].clear() |
| 204 | if not session["public"] and "ANY" in final_content["_admin"]["projects_read"]: |
| 205 | final_content["_admin"]["projects_read"].remove("ANY") |
| 206 | |
| 207 | # Change project status |
| 208 | if session.get("set_project"): |
| 209 | for p in session["set_project"]: |
| 210 | if p not in final_content["_admin"]["projects_read"]: |
| 211 | final_content["_admin"]["projects_read"].append(p) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 212 | |
| 213 | def check_unique_name(self, session, name, _id=None): |
| 214 | """ |
| 215 | Check that the name is unique for this project |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 216 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 217 | :param name: name to be checked |
| 218 | :param _id: If not None, ignore this entry that are going to change |
| 219 | :return: None or raises EngineException |
| 220 | """ |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 221 | if not self.multiproject: |
| 222 | _filter = {} |
| 223 | else: |
| 224 | _filter = self._get_project_filter(session) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 225 | _filter["name"] = name |
| 226 | if _id: |
| 227 | _filter["_id.neq"] = _id |
| 228 | if self.db.get_one(self.topic, _filter, fail_on_empty=False, fail_on_more=False): |
| 229 | raise EngineException("name '{}' already exists for {}".format(name, self.topic), HTTPStatus.CONFLICT) |
| 230 | |
| 231 | @staticmethod |
| 232 | def format_on_new(content, project_id=None, make_public=False): |
| 233 | """ |
| 234 | Modifies content descriptor to include _admin |
| 235 | :param content: descriptor to be modified |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 236 | :param project_id: if included, it add project read/write permissions. Can be None or a list |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 237 | :param make_public: if included it is generated as public for reading. |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 238 | :return: op_id: operation id on asynchronous operation, None otherwise. In addition content is modified |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 239 | """ |
| 240 | now = time() |
| 241 | if "_admin" not in content: |
| 242 | content["_admin"] = {} |
| 243 | if not content["_admin"].get("created"): |
| 244 | content["_admin"]["created"] = now |
| 245 | content["_admin"]["modified"] = now |
| 246 | if not content.get("_id"): |
| 247 | content["_id"] = str(uuid4()) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 248 | if project_id is not None: |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 249 | if not content["_admin"].get("projects_read"): |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 250 | content["_admin"]["projects_read"] = list(project_id) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 251 | if make_public: |
| 252 | content["_admin"]["projects_read"].append("ANY") |
| 253 | if not content["_admin"].get("projects_write"): |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 254 | content["_admin"]["projects_write"] = list(project_id) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 255 | return None |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 256 | |
| 257 | @staticmethod |
| 258 | def format_on_edit(final_content, edit_content): |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 259 | """ |
| 260 | Modifies final_content to admin information upon edition |
| 261 | :param final_content: final content to be stored at database |
| 262 | :param edit_content: user requested update content |
| 263 | :return: operation id, if this edit implies an asynchronous operation; None otherwise |
| 264 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 265 | if final_content.get("_admin"): |
| 266 | now = time() |
| 267 | final_content["_admin"]["modified"] = now |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 268 | return None |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 269 | |
| 270 | def _send_msg(self, action, content): |
| 271 | if self.topic_msg: |
| 272 | content.pop("_admin", None) |
| 273 | self.msg.write(self.topic_msg, action, content) |
| 274 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 275 | def check_conflict_on_del(self, session, _id, db_content): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 276 | """ |
| 277 | Check if deletion can be done because of dependencies if it is not force. To override |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 278 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 279 | :param _id: internal _id |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 280 | :param db_content: The database content of this item _id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 281 | :return: None if ok or raises EngineException with the conflict |
| 282 | """ |
| 283 | pass |
| 284 | |
| 285 | @staticmethod |
| 286 | def _update_input_with_kwargs(desc, kwargs): |
| 287 | """ |
| 288 | Update descriptor with the kwargs. It contains dot separated keys |
| 289 | :param desc: dictionary to be updated |
| 290 | :param kwargs: plain dictionary to be used for updating. |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 291 | :return: None, 'desc' is modified. It raises EngineException. |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 292 | """ |
| 293 | if not kwargs: |
| 294 | return |
| 295 | try: |
| 296 | for k, v in kwargs.items(): |
| 297 | update_content = desc |
| 298 | kitem_old = None |
| 299 | klist = k.split(".") |
| 300 | for kitem in klist: |
| 301 | if kitem_old is not None: |
| 302 | update_content = update_content[kitem_old] |
| 303 | if isinstance(update_content, dict): |
| 304 | kitem_old = kitem |
| 305 | elif isinstance(update_content, list): |
| 306 | kitem_old = int(kitem) |
| 307 | else: |
| 308 | raise EngineException( |
| 309 | "Invalid query string '{}'. Descriptor is not a list nor dict at '{}'".format(k, kitem)) |
| 310 | update_content[kitem_old] = v |
| 311 | except KeyError: |
| 312 | raise EngineException( |
| 313 | "Invalid query string '{}'. Descriptor does not contain '{}'".format(k, kitem_old)) |
| 314 | except ValueError: |
| 315 | raise EngineException("Invalid query string '{}'. Expected integer index list instead of '{}'".format( |
| 316 | k, kitem)) |
| 317 | except IndexError: |
| 318 | raise EngineException( |
| 319 | "Invalid query string '{}'. Index '{}' out of range".format(k, kitem_old)) |
| 320 | |
| 321 | def show(self, session, _id): |
| 322 | """ |
| 323 | Get complete information on an topic |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 324 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 325 | :param _id: server internal id |
| 326 | :return: dictionary, raise exception if not found. |
| 327 | """ |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 328 | if not self.multiproject: |
| 329 | filter_db = {} |
| 330 | else: |
| 331 | filter_db = self._get_project_filter(session) |
| delacruzramo | c061f56 | 2019-04-05 11:00:02 +0200 | [diff] [blame] | 332 | # To allow project&user addressing by name AS WELL AS _id |
| 333 | filter_db[BaseTopic.id_field(self.topic, _id)] = _id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 334 | return self.db.get_one(self.topic, filter_db) |
| 335 | # TODO transform data for SOL005 URL requests |
| 336 | # TODO remove _admin if not admin |
| 337 | |
| 338 | def get_file(self, session, _id, path=None, accept_header=None): |
| 339 | """ |
| 340 | Only implemented for descriptor topics. Return the file content of a descriptor |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 341 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 342 | :param _id: Identity of the item to get content |
| 343 | :param path: artifact path or "$DESCRIPTOR" or None |
| 344 | :param accept_header: Content of Accept header. Must contain applition/zip or/and text/plain |
| 345 | :return: opened file or raises an exception |
| 346 | """ |
| 347 | raise EngineException("Method get_file not valid for this topic", HTTPStatus.INTERNAL_SERVER_ERROR) |
| 348 | |
| 349 | def list(self, session, filter_q=None): |
| 350 | """ |
| 351 | Get a list of the topic that matches a filter |
| 352 | :param session: contains the used login username and working project |
| 353 | :param filter_q: filter of data to be applied |
| 354 | :return: The list, it can be empty if no one match the filter. |
| 355 | """ |
| 356 | if not filter_q: |
| 357 | filter_q = {} |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 358 | if self.multiproject: |
| 359 | filter_q.update(self._get_project_filter(session)) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 360 | |
| 361 | # TODO transform data for SOL005 URL requests. Transform filtering |
| 362 | # TODO implement "field-type" query string SOL005 |
| 363 | return self.db.get_list(self.topic, filter_q) |
| 364 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 365 | def new(self, rollback, session, indata=None, kwargs=None, headers=None): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 366 | """ |
| 367 | Creates a new entry into database. |
| 368 | :param rollback: list to append created items at database in case a rollback may to be done |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 369 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 370 | :param indata: data to be inserted |
| 371 | :param kwargs: used to override the indata descriptor |
| 372 | :param headers: http request headers |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 373 | :return: _id, op_id: |
| 374 | _id: identity of the inserted data. |
| 375 | op_id: operation id if this is asynchronous, None otherwise |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 376 | """ |
| 377 | try: |
| delacruzramo | 32bab47 | 2019-09-13 12:24:22 +0200 | [diff] [blame] | 378 | if self.multiproject: |
| 379 | self.check_quota(session) |
| 380 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 381 | content = self._remove_envelop(indata) |
| 382 | |
| 383 | # Override descriptor with query string kwargs |
| 384 | self._update_input_with_kwargs(content, kwargs) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 385 | content = self._validate_input_new(content, force=session["force"]) |
| 386 | self.check_conflict_on_new(session, content) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 387 | op_id = self.format_on_new(content, project_id=session["project_id"], make_public=session["public"]) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 388 | _id = self.db.create(self.topic, content) |
| 389 | rollback.append({"topic": self.topic, "_id": _id}) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 390 | if op_id: |
| 391 | content["op_id"] = op_id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 392 | self._send_msg("create", content) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 393 | return _id, op_id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 394 | except ValidationError as e: |
| 395 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |
| 396 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 397 | def upload_content(self, session, _id, indata, kwargs, headers): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 398 | """ |
| 399 | Only implemented for descriptor topics. Used for receiving content by chunks (with a transaction_id header |
| 400 | and/or gzip file. It will store and extract) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 401 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 402 | :param _id : the database id of entry to be updated |
| 403 | :param indata: http body request |
| 404 | :param kwargs: user query string to override parameters. NOT USED |
| 405 | :param headers: http request headers |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 406 | :return: True package has is completely uploaded or False if partial content has been uplodaed. |
| 407 | Raise exception on error |
| 408 | """ |
| 409 | raise EngineException("Method upload_content not valid for this topic", HTTPStatus.INTERNAL_SERVER_ERROR) |
| 410 | |
| 411 | def delete_list(self, session, filter_q=None): |
| 412 | """ |
| 413 | Delete a several entries of a topic. This is for internal usage and test only, not exposed to NBI API |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 414 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 415 | :param filter_q: filter of data to be applied |
| 416 | :return: The deleted list, it can be empty if no one match the filter. |
| 417 | """ |
| 418 | # TODO add admin to filter, validate rights |
| 419 | if not filter_q: |
| 420 | filter_q = {} |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 421 | if self.multiproject: |
| 422 | filter_q.update(self._get_project_filter(session)) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 423 | return self.db.del_list(self.topic, filter_q) |
| 424 | |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 425 | def delete_extra(self, session, _id, db_content): |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 426 | """ |
| 427 | Delete other things apart from database entry of a item _id. |
| 428 | e.g.: other associated elements at database and other file system storage |
| 429 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 430 | :param _id: server internal id |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 431 | :param db_content: The database content of the _id. It is already deleted when reached this method, but the |
| 432 | content is needed in same cases |
| 433 | :return: None if ok or raises EngineException with the problem |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 434 | """ |
| 435 | pass |
| 436 | |
| 437 | def delete(self, session, _id, dry_run=False): |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 438 | """ |
| 439 | Delete item by its internal _id |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 440 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 441 | :param _id: server internal id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 442 | :param dry_run: make checking but do not delete |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 443 | :return: operation id (None if there is not operation), raise exception if error or not found, conflict, ... |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 444 | """ |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 445 | |
| 446 | # To allow addressing projects and users by name AS WELL AS by _id |
| 447 | filter_q = {BaseTopic.id_field(self.topic, _id): _id} |
| 448 | item_content = self.db.get_one(self.topic, filter_q) |
| 449 | |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 450 | # TODO add admin to filter, validate rights |
| 451 | # data = self.get_item(topic, _id) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 452 | self.check_conflict_on_del(session, _id, item_content) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 453 | if dry_run: |
| 454 | return None |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 455 | |
| tierno | 1f029d8 | 2019-06-13 22:37:04 +0000 | [diff] [blame] | 456 | if self.multiproject: |
| 457 | filter_q.update(self._get_project_filter(session)) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 458 | if self.multiproject and session["project_id"]: |
| 459 | # remove reference from project_read. If not last delete |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 460 | # if this topic is not part of session["project_id"] no midification at database is done and an exception |
| 461 | # is raised |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 462 | self.db.set_one(self.topic, filter_q, update_dict=None, |
| 463 | pull={"_admin.projects_read": {"$in": session["project_id"]}}) |
| 464 | # try to delete if there is not any more reference from projects. Ignore if it is not deleted |
| 465 | filter_q = {'_id': _id, '_admin.projects_read': [[], ["ANY"]]} |
| 466 | v = self.db.del_one(self.topic, filter_q, fail_on_empty=False) |
| 467 | if not v or not v["deleted"]: |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 468 | return None |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 469 | else: |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 470 | self.db.del_one(self.topic, filter_q) |
| tierno | b4844ab | 2019-05-23 08:42:12 +0000 | [diff] [blame] | 471 | self.delete_extra(session, _id, item_content) |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 472 | self._send_msg("deleted", {"_id": _id}) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 473 | return None |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 474 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 475 | def edit(self, session, _id, indata=None, kwargs=None, content=None): |
| 476 | """ |
| 477 | Change the content of an item |
| 478 | :param session: contains "username", "admin", "force", "public", "project_id", "set_project" |
| 479 | :param _id: server internal id |
| 480 | :param indata: contains the changes to apply |
| 481 | :param kwargs: modifies indata |
| 482 | :param content: original content of the item |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 483 | :return: op_id: operation id if this is processed asynchronously, None otherwise |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 484 | """ |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 485 | indata = self._remove_envelop(indata) |
| 486 | |
| 487 | # Override descriptor with query string kwargs |
| 488 | if kwargs: |
| 489 | self._update_input_with_kwargs(indata, kwargs) |
| 490 | try: |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 491 | if indata and session.get("set_project"): |
| 492 | raise EngineException("Cannot edit content and set to project (query string SET_PROJECT) at same time", |
| 493 | HTTPStatus.UNPROCESSABLE_ENTITY) |
| 494 | indata = self._validate_input_edit(indata, force=session["force"]) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 495 | |
| 496 | # TODO self._check_edition(session, indata, _id, force) |
| 497 | if not content: |
| 498 | content = self.show(session, _id) |
| 499 | deep_update_rfc7396(content, indata) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 500 | |
| 501 | # To allow project addressing by name AS WELL AS _id. Get the _id, just in case the provided one is a name |
| 502 | _id = content.get("_id") or _id |
| 503 | |
| tierno | 65ca36d | 2019-02-12 19:27:52 +0100 | [diff] [blame] | 504 | self.check_conflict_on_edit(session, content, indata, _id=_id) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 505 | op_id = self.format_on_edit(content, indata) |
| 506 | |
| 507 | self.db.replace(self.topic, _id, content) |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 508 | |
| 509 | indata.pop("_admin", None) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 510 | if op_id: |
| 511 | indata["op_id"] = op_id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 512 | indata["_id"] = _id |
| 513 | self._send_msg("edit", indata) |
| tierno | bdebce9 | 2019-07-01 15:36:49 +0000 | [diff] [blame] | 514 | return op_id |
| tierno | b24258a | 2018-10-04 18:39:49 +0200 | [diff] [blame] | 515 | except ValidationError as e: |
| 516 | raise EngineException(e, HTTPStatus.UNPROCESSABLE_ENTITY) |