def delete_type(public_id: int): """ HTTP `DELETE` route for delete a single type resource. Args: public_id (int): Public ID of the deletable type Raises: ManagerGetError: When the type with the `public_id` was not found. ManagerDeleteError: When something went wrong during the deletion. Notes: Deleting the type will also delete all objects in this type! Returns: DeleteSingleResponse: Delete result with the deleted type as data. """ type_manager = TypeManager(database_manager=current_app.database_manager) from cmdb.framework.cmdb_object_manager import CmdbObjectManager deprecated_object_manager = CmdbObjectManager( database_manager=current_app.database_manager) try: objects_ids = [ object_.get_public_id() for object_ in deprecated_object_manager.get_objects_by_type(public_id) ] deprecated_object_manager.delete_many_objects({'type_id': public_id}, objects_ids, None) deleted_type = type_manager.delete(public_id=PublicID(public_id)) api_response = DeleteSingleResponse( raw=TypeModel.to_json(deleted_type), model=TypeModel.MODEL) except ManagerGetError as err: return abort(404, err.message) except ManagerDeleteError as err: return abort(400, err.message) except Exception as err: return abort(400, str(err)) return api_response.make_response()
def update_unstructured_objects(public_id: int, request_user: UserModel): """ HTTP `PUT`/`PATCH` route for a multi resources which will be formatted based on the TypeModel Args: public_id (int): Public ID of the type. Raises: ManagerGetError: When the type with the `public_id` was not found. ManagerUpdateError: When something went wrong during the update. Returns: UpdateMultiResponse: Which includes the json data of multiple updated objects. """ type_manager = TypeManager(database_manager=current_app.database_manager) object_manager = CmdbObjectManager( database_manager=current_app.database_manager) try: update_type_instance = type_manager.get(public_id) type_fields = update_type_instance.fields objects_by_type = object_manager.get_objects_by_type(type_id=public_id) for obj in objects_by_type: incorrect = [] correct = [] obj_fields = obj.get_all_fields() for t_field in type_fields: name = t_field["name"] for field in obj_fields: if name == field["name"]: correct.append(field["name"]) else: incorrect.append(field["name"]) removed_type_fields = [ item for item in incorrect if not item in correct ] for field in removed_type_fields: object_manager.remove_object_fields( filter_query={'public_id': obj.public_id}, update={'$pull': { 'fields': { "name": field } }}) objects_by_type = object_manager.get_objects_by_type(type_id=public_id) for obj in objects_by_type: for t_field in type_fields: name = t_field["name"] value = None if [ item for item in obj.get_all_fields() if item["name"] == name ]: continue if "value" in t_field: value = t_field["value"] object_manager.update_object_fields( filter={'public_id': obj.public_id}, update={ '$addToSet': { 'fields': { "name": name, "value": value } } }) except ManagerUpdateError as err: return abort(400, err.message) api_response = UpdateMultiResponse([], url=request.url, model='Object') return api_response.make_response()