def insert_job(self, data: (ExportdJob, dict)) -> int: """ Insert new ExportdJob Object Args: data: init data Returns: Public ID of the new ExportdJob in database """ if isinstance(data, dict): try: new_object = ExportdJob(**data) except CMDBError as e: raise ExportdJobManagerInsertError(e) elif isinstance(data, ExportdJob): new_object = data try: ack = self.dbm.insert(collection=ExportdJob.COLLECTION, data=new_object.to_database()) if self._event_queue: event = Event( "cmdb.exportd.added", { "id": new_object.get_public_id(), "active": new_object.scheduling["event"]["active"] and new_object.get_active(), "user_id": new_object.get_author_id() }) self._event_queue.put(event) except CMDBError as e: raise ExportdJobManagerInsertError(e) return ack
def update_job(self, data: (dict, ExportdJob), request_user: UserModel, event_start=True) -> str: """ Update new ExportdJob Object Args: data: init data request_user: current user, to detect who triggered event event_start: Controls whether an event should be started Returns: Public ID of the ExportdJob in database """ if isinstance(data, dict): update_object = ExportdJob(**data) elif isinstance(data, ExportdJob): update_object = data else: raise ExportdJobManagerUpdateError(f'Could not update job with ID: {data.get_public_id()}') update_object.last_execute_date = datetime.now(timezone.utc) ack = self._update( collection=ExportdJob.COLLECTION, public_id=update_object.get_public_id(), data=update_object.to_database() ) if self._event_queue and event_start: state = update_object.scheduling["event"]["active"] and update_object.get_active() event = Event("cmdb.exportd.updated", {"id": update_object.get_public_id(), "active": state, "user_id": request_user.get_public_id(), "event": 'automatic'}) self._event_queue.put(event) return ack.acknowledged