1 # -*- coding: utf-8 -*-
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 from uuid
import uuid4
18 from http
import HTTPStatus
20 from osm_common
.dbbase
import deep_update_rfc7396
21 from osm_nbi
.validation
import validate_input
, ValidationError
, is_valid_uuid
22 from yaml
import safe_load
, YAMLError
24 __author__
= "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
27 class EngineException(Exception):
29 def __init__(self
, message
, http_code
=HTTPStatus
.BAD_REQUEST
):
30 self
.http_code
= http_code
31 super(Exception, self
).__init
__(message
)
34 def deep_get(target_dict
, key_list
):
36 Get a value from target_dict entering in the nested keys. If keys does not exist, it returns None
37 Example target_dict={a: {b: 5}}; key_list=[a,b] returns 5; both key_list=[a,b,c] and key_list=[f,h] return None
38 :param target_dict: dictionary to be read
39 :param key_list: list of keys to read from target_dict
40 :return: The wanted value if exist, None otherwise
43 if not isinstance(target_dict
, dict) or key
not in target_dict
:
45 target_dict
= target_dict
[key
]
49 def get_iterable(input_var
):
51 Returns an iterable, in case input_var is None it just returns an empty tuple
52 :param input_var: can be a list, tuple or None
53 :return: input_var or () if it is None
61 """utility for compare dot separate versions. Fills with zeros to proper number comparison"""
63 for point
in v
.split("."):
64 filled
.append(point
.zfill(8))
69 # static variables for all instance classes
70 topic
= None # to_override
71 topic_msg
= None # to_override
72 quota_name
= None # to_override. If not provided topic will be used for quota_name
73 schema_new
= None # to_override
74 schema_edit
= None # to_override
75 multiproject
= True # True if this Topic can be shared by several projects. Then it contains _admin.projects_read
79 # Alternative ID Fields for some Topics
86 def __init__(self
, db
, fs
, msg
, auth
):
90 self
.logger
= logging
.getLogger("nbi.engine")
94 def id_field(topic
, value
):
95 """Returns ID Field for given topic and field value"""
96 if topic
in BaseTopic
.alt_id_field
.keys() and not is_valid_uuid(value
):
97 return BaseTopic
.alt_id_field
[topic
]
102 def _remove_envelop(indata
=None):
107 def check_quota(self
, session
):
109 Check whether topic quota is exceeded by the given project
110 Used by relevant topics' 'new' function to decide whether or not creation of the new item should be allowed
111 :param session[project_id]: projects (tuple) for which quota should be checked
112 :param session[force]: boolean. If true, skip quota checking
115 DbException if project not found
116 ValidationError if quota exceeded in one of the projects
120 projects
= session
["project_id"]
121 for project
in projects
:
122 proj
= self
.auth
.get_project(project
)
124 quota_name
= self
.quota_name
or self
.topic
125 quota
= proj
.get("quotas", {}).get(quota_name
, self
.default_quota
)
126 count
= self
.db
.count(self
.topic
, {"_admin.projects_read": pid
})
129 raise ValidationError("quota ({}={}) exceeded for project {} ({})".format(quota_name
, quota
, name
, pid
),
130 http_code
=HTTPStatus
.UNAUTHORIZED
)
132 def _validate_input_new(self
, input, force
=False):
134 Validates input user content for a new entry. It uses jsonschema. Some overrides will use pyangbind
135 :param input: user input content for the new topic
136 :param force: may be used for being more tolerant
137 :return: The same input content, or a changed version of it.
140 validate_input(input, self
.schema_new
)
143 def _validate_input_edit(self
, input, content
, force
=False):
145 Validates input user content for an edition. It uses jsonschema. Some overrides will use pyangbind
146 :param input: user input content for the new topic
147 :param force: may be used for being more tolerant
148 :return: The same input content, or a changed version of it.
151 validate_input(input, self
.schema_edit
)
155 def _get_project_filter(session
):
157 Generates a filter dictionary for querying database, so that only allowed items for this project can be
158 addressed. Only proprietary or public can be used. Allowed projects are at _admin.project_read/write. If it is
159 not present or contains ANY mean public.
160 :param session: contains:
161 project_id: project list this session has rights to access. Can be empty, one or several
162 set_project: items created will contain this project list
164 public: True, False or None
165 method: "list", "show", "write", "delete"
167 :return: dictionary with project filter
170 project_filter_n
= []
171 project_filter
= list(session
["project_id"])
173 if session
["method"] not in ("list", "delete"):
175 project_filter
.append("ANY")
176 elif session
["public"] is not None:
177 if session
["public"]:
178 project_filter
.append("ANY")
180 project_filter_n
.append("ANY")
182 if session
.get("PROJECT.ne"):
183 project_filter_n
.append(session
["PROJECT.ne"])
186 if session
["method"] in ("list", "show", "delete") or session
.get("set_project"):
187 p_filter
["_admin.projects_read.cont"] = project_filter
189 p_filter
["_admin.projects_write.cont"] = project_filter
191 if session
["method"] in ("list", "show", "delete") or session
.get("set_project"):
192 p_filter
["_admin.projects_read.ncont"] = project_filter_n
194 p_filter
["_admin.projects_write.ncont"] = project_filter_n
198 def check_conflict_on_new(self
, session
, indata
):
200 Check that the data to be inserted is valid
201 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
202 :param indata: data to be inserted
203 :return: None or raises EngineException
207 def check_conflict_on_edit(self
, session
, final_content
, edit_content
, _id
):
209 Check that the data to be edited/uploaded is valid
210 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
211 :param final_content: data once modified. This method may change it.
212 :param edit_content: incremental data that contains the modifications to apply
213 :param _id: internal _id
214 :return: None or raises EngineException
216 if not self
.multiproject
:
218 # Change public status
219 if session
["public"] is not None:
220 if session
["public"] and "ANY" not in final_content
["_admin"]["projects_read"]:
221 final_content
["_admin"]["projects_read"].append("ANY")
222 final_content
["_admin"]["projects_write"].clear()
223 if not session
["public"] and "ANY" in final_content
["_admin"]["projects_read"]:
224 final_content
["_admin"]["projects_read"].remove("ANY")
226 # Change project status
227 if session
.get("set_project"):
228 for p
in session
["set_project"]:
229 if p
not in final_content
["_admin"]["projects_read"]:
230 final_content
["_admin"]["projects_read"].append(p
)
232 def check_unique_name(self
, session
, name
, _id
=None):
234 Check that the name is unique for this project
235 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
236 :param name: name to be checked
237 :param _id: If not None, ignore this entry that are going to change
238 :return: None or raises EngineException
240 if not self
.multiproject
:
243 _filter
= self
._get
_project
_filter
(session
)
244 _filter
["name"] = name
246 _filter
["_id.neq"] = _id
247 if self
.db
.get_one(self
.topic
, _filter
, fail_on_empty
=False, fail_on_more
=False):
248 raise EngineException("name '{}' already exists for {}".format(name
, self
.topic
), HTTPStatus
.CONFLICT
)
251 def format_on_new(content
, project_id
=None, make_public
=False):
253 Modifies content descriptor to include _admin
254 :param content: descriptor to be modified
255 :param project_id: if included, it add project read/write permissions. Can be None or a list
256 :param make_public: if included it is generated as public for reading.
257 :return: op_id: operation id on asynchronous operation, None otherwise. In addition content is modified
260 if "_admin" not in content
:
261 content
["_admin"] = {}
262 if not content
["_admin"].get("created"):
263 content
["_admin"]["created"] = now
264 content
["_admin"]["modified"] = now
265 if not content
.get("_id"):
266 content
["_id"] = str(uuid4())
267 if project_id
is not None:
268 if not content
["_admin"].get("projects_read"):
269 content
["_admin"]["projects_read"] = list(project_id
)
271 content
["_admin"]["projects_read"].append("ANY")
272 if not content
["_admin"].get("projects_write"):
273 content
["_admin"]["projects_write"] = list(project_id
)
277 def format_on_edit(final_content
, edit_content
):
279 Modifies final_content to admin information upon edition
280 :param final_content: final content to be stored at database
281 :param edit_content: user requested update content
282 :return: operation id, if this edit implies an asynchronous operation; None otherwise
284 if final_content
.get("_admin"):
286 final_content
["_admin"]["modified"] = now
289 def _send_msg(self
, action
, content
, not_send_msg
=None):
290 if self
.topic_msg
and not_send_msg
is not False:
291 content
.pop("_admin", None)
292 if isinstance(not_send_msg
, list):
293 not_send_msg
.append((self
.topic_msg
, action
, content
))
295 self
.msg
.write(self
.topic_msg
, action
, content
)
297 def check_conflict_on_del(self
, session
, _id
, db_content
):
299 Check if deletion can be done because of dependencies if it is not force. To override
300 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
301 :param _id: internal _id
302 :param db_content: The database content of this item _id
303 :return: None if ok or raises EngineException with the conflict
308 def _update_input_with_kwargs(desc
, kwargs
, yaml_format
=False):
310 Update descriptor with the kwargs. It contains dot separated keys
311 :param desc: dictionary to be updated
312 :param kwargs: plain dictionary to be used for updating.
313 :param yaml_format: get kwargs values as yaml format.
314 :return: None, 'desc' is modified. It raises EngineException.
319 for k
, v
in kwargs
.items():
320 update_content
= desc
324 if kitem_old
is not None:
325 update_content
= update_content
[kitem_old
]
326 if isinstance(update_content
, dict):
328 if not isinstance(update_content
.get(kitem_old
), (dict, list)):
329 update_content
[kitem_old
] = {}
330 elif isinstance(update_content
, list):
331 # key must be an index of the list, must be integer
332 kitem_old
= int(kitem
)
333 # if index greater than list, extend the list
334 if kitem_old
>= len(update_content
):
335 update_content
+= [None] * (kitem_old
- len(update_content
) + 1)
336 if not isinstance(update_content
[kitem_old
], (dict, list)):
337 update_content
[kitem_old
] = {}
339 raise EngineException(
340 "Invalid query string '{}'. Descriptor is not a list nor dict at '{}'".format(k
, kitem
))
342 del update_content
[kitem_old
]
344 update_content
[kitem_old
] = v
if not yaml_format
else safe_load(v
)
346 raise EngineException(
347 "Invalid query string '{}'. Descriptor does not contain '{}'".format(k
, kitem_old
))
349 raise EngineException("Invalid query string '{}'. Expected integer index list instead of '{}'".format(
352 raise EngineException(
353 "Invalid query string '{}'. Index '{}' out of range".format(k
, kitem_old
))
355 raise EngineException("Invalid query string '{}' yaml format".format(k
))
357 def show(self
, session
, _id
):
359 Get complete information on an topic
360 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
361 :param _id: server internal id
362 :return: dictionary, raise exception if not found.
364 if not self
.multiproject
:
367 filter_db
= self
._get
_project
_filter
(session
)
368 # To allow project&user addressing by name AS WELL AS _id
369 filter_db
[BaseTopic
.id_field(self
.topic
, _id
)] = _id
370 return self
.db
.get_one(self
.topic
, filter_db
)
371 # TODO transform data for SOL005 URL requests
372 # TODO remove _admin if not admin
374 def get_file(self
, session
, _id
, path
=None, accept_header
=None):
376 Only implemented for descriptor topics. Return the file content of a descriptor
377 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
378 :param _id: Identity of the item to get content
379 :param path: artifact path or "$DESCRIPTOR" or None
380 :param accept_header: Content of Accept header. Must contain applition/zip or/and text/plain
381 :return: opened file or raises an exception
383 raise EngineException("Method get_file not valid for this topic", HTTPStatus
.INTERNAL_SERVER_ERROR
)
385 def list(self
, session
, filter_q
=None):
387 Get a list of the topic that matches a filter
388 :param session: contains the used login username and working project
389 :param filter_q: filter of data to be applied
390 :return: The list, it can be empty if no one match the filter.
394 if self
.multiproject
:
395 filter_q
.update(self
._get
_project
_filter
(session
))
397 # TODO transform data for SOL005 URL requests. Transform filtering
398 # TODO implement "field-type" query string SOL005
399 return self
.db
.get_list(self
.topic
, filter_q
)
401 def new(self
, rollback
, session
, indata
=None, kwargs
=None, headers
=None):
403 Creates a new entry into database.
404 :param rollback: list to append created items at database in case a rollback may to be done
405 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
406 :param indata: data to be inserted
407 :param kwargs: used to override the indata descriptor
408 :param headers: http request headers
410 _id: identity of the inserted data.
411 op_id: operation id if this is asynchronous, None otherwise
414 if self
.multiproject
:
415 self
.check_quota(session
)
417 content
= self
._remove
_envelop
(indata
)
419 # Override descriptor with query string kwargs
420 self
._update
_input
_with
_kwargs
(content
, kwargs
)
421 content
= self
._validate
_input
_new
(content
, force
=session
["force"])
422 self
.check_conflict_on_new(session
, content
)
423 op_id
= self
.format_on_new(content
, project_id
=session
["project_id"], make_public
=session
["public"])
424 _id
= self
.db
.create(self
.topic
, content
)
425 rollback
.append({"topic": self
.topic
, "_id": _id
})
427 content
["op_id"] = op_id
428 self
._send
_msg
("created", content
)
430 except ValidationError
as e
:
431 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)
433 def upload_content(self
, session
, _id
, indata
, kwargs
, headers
):
435 Only implemented for descriptor topics. Used for receiving content by chunks (with a transaction_id header
436 and/or gzip file. It will store and extract)
437 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
438 :param _id : the database id of entry to be updated
439 :param indata: http body request
440 :param kwargs: user query string to override parameters. NOT USED
441 :param headers: http request headers
442 :return: True package has is completely uploaded or False if partial content has been uplodaed.
443 Raise exception on error
445 raise EngineException("Method upload_content not valid for this topic", HTTPStatus
.INTERNAL_SERVER_ERROR
)
447 def delete_list(self
, session
, filter_q
=None):
449 Delete a several entries of a topic. This is for internal usage and test only, not exposed to NBI API
450 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
451 :param filter_q: filter of data to be applied
452 :return: The deleted list, it can be empty if no one match the filter.
454 # TODO add admin to filter, validate rights
457 if self
.multiproject
:
458 filter_q
.update(self
._get
_project
_filter
(session
))
459 return self
.db
.del_list(self
.topic
, filter_q
)
461 def delete_extra(self
, session
, _id
, db_content
, not_send_msg
=None):
463 Delete other things apart from database entry of a item _id.
464 e.g.: other associated elements at database and other file system storage
465 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
466 :param _id: server internal id
467 :param db_content: The database content of the _id. It is already deleted when reached this method, but the
468 content is needed in same cases
469 :param not_send_msg: To not send message (False) or store content (list) instead
470 :return: None if ok or raises EngineException with the problem
474 def delete(self
, session
, _id
, dry_run
=False, not_send_msg
=None):
476 Delete item by its internal _id
477 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
478 :param _id: server internal id
479 :param dry_run: make checking but do not delete
480 :param not_send_msg: To not send message (False) or store content (list) instead
481 :return: operation id (None if there is not operation), raise exception if error or not found, conflict, ...
484 # To allow addressing projects and users by name AS WELL AS by _id
485 if not self
.multiproject
:
488 filter_q
= self
._get
_project
_filter
(session
)
489 filter_q
[self
.id_field(self
.topic
, _id
)] = _id
490 item_content
= self
.db
.get_one(self
.topic
, filter_q
)
492 self
.check_conflict_on_del(session
, _id
, item_content
)
496 if self
.multiproject
and session
["project_id"]:
497 # remove reference from project_read if there are more projects referencing it. If it last one,
498 # do not remove reference, but delete
499 other_projects_referencing
= next((p
for p
in item_content
["_admin"]["projects_read"]
500 if p
not in session
["project_id"] and p
!= "ANY"), None)
502 # check if there are projects referencing it (apart from ANY, that means, public)....
503 if other_projects_referencing
:
504 # remove references but not delete
505 update_dict_pull
= {"_admin.projects_read": session
["project_id"],
506 "_admin.projects_write": session
["project_id"]}
507 self
.db
.set_one(self
.topic
, filter_q
, update_dict
=None, pull_list
=update_dict_pull
)
510 can_write
= next((p
for p
in item_content
["_admin"]["projects_write"] if p
== "ANY" or
511 p
in session
["project_id"]), None)
513 raise EngineException("You have not write permission to delete it",
514 http_code
=HTTPStatus
.UNAUTHORIZED
)
517 self
.db
.del_one(self
.topic
, filter_q
)
518 self
.delete_extra(session
, _id
, item_content
, not_send_msg
=not_send_msg
)
519 self
._send
_msg
("deleted", {"_id": _id
}, not_send_msg
=not_send_msg
)
522 def edit(self
, session
, _id
, indata
=None, kwargs
=None, content
=None):
524 Change the content of an item
525 :param session: contains "username", "admin", "force", "public", "project_id", "set_project"
526 :param _id: server internal id
527 :param indata: contains the changes to apply
528 :param kwargs: modifies indata
529 :param content: original content of the item
530 :return: op_id: operation id if this is processed asynchronously, None otherwise
532 indata
= self
._remove
_envelop
(indata
)
534 # Override descriptor with query string kwargs
536 self
._update
_input
_with
_kwargs
(indata
, kwargs
)
538 if indata
and session
.get("set_project"):
539 raise EngineException("Cannot edit content and set to project (query string SET_PROJECT) at same time",
540 HTTPStatus
.UNPROCESSABLE_ENTITY
)
542 # TODO self._check_edition(session, indata, _id, force)
544 content
= self
.show(session
, _id
)
546 indata
= self
._validate
_input
_edit
(indata
, content
, force
=session
["force"])
548 deep_update_rfc7396(content
, indata
)
550 # To allow project addressing by name AS WELL AS _id. Get the _id, just in case the provided one is a name
551 _id
= content
.get("_id") or _id
553 self
.check_conflict_on_edit(session
, content
, indata
, _id
=_id
)
554 op_id
= self
.format_on_edit(content
, indata
)
556 self
.db
.replace(self
.topic
, _id
, content
)
558 indata
.pop("_admin", None)
560 indata
["op_id"] = op_id
562 self
._send
_msg
("edited", indata
)
564 except ValidationError
as e
:
565 raise EngineException(e
, HTTPStatus
.UNPROCESSABLE_ENTITY
)