def update_type(request_user: User): from bson import json_util add_data_dump = json.dumps(request.json) try: new_type_data = json.loads(add_data_dump, object_hook=json_util.object_hook) except TypeError as e: LOGGER.warning(e) abort(400) try: update_type_instance = CmdbType(**new_type_data) except CMDBError: return abort(400) try: old_fields = object_manager.get_type( update_type_instance.get_public_id()).get_fields() new_fields = update_type_instance.get_fields() if [item for item in old_fields if item not in new_fields]: update_type_instance.clean_db = False if [item for item in new_fields if item not in old_fields]: update_type_instance.clean_db = False except CMDBError: return abort(500) try: object_manager.update_type(update_type_instance) except CMDBError: return abort(500) resp = make_response(update_type_instance) return resp
def test_add_type(self, object_manager): _render_data = { 'summary': [], 'external': [], 'sections': [] } type_instance_new_1 = CmdbType(name='example2', active=True, author_id=1, creation_time=datetime.utcnow(), render_meta=_render_data, fields=[], public_id=2) # CmdbObjectType insert ack = object_manager.insert_type(type_instance_new_1) assert ack == 2 type_instance_new_2 = object_manager.get_type(public_id=2) assert type_instance_new_2.get_name() == 'example2' # dict insert type_instance_new_3 = CmdbType(name='example3', active=True, author_id=1, creation_time=datetime.utcnow(), render_meta=_render_data, fields=[], public_id=3) object_manager.insert_type(type_instance_new_3.__dict__) type_instance_new_3 = object_manager.get_type(public_id=3) assert type_instance_new_3.get_name() == 'example3' # insert error test type_instance_new_4 = CmdbType(name='example3', active=True, author_id=1, creation_time=datetime.utcnow(), render_meta=_render_data, fields=[], public_id=type_instance_new_3.get_public_id()) with pytest.raises(TypeAlreadyExists): object_manager.insert_type(type_instance_new_4)
def update_type(self, data: (CmdbType, dict)): if isinstance(data, CmdbType): update_type = data elif isinstance(data, dict): update_type = CmdbType(**data) else: raise WrongInputFormatError(CmdbType, data, "Possible data: dict or CmdbType") ack = self._update(collection=CmdbType.COLLECTION, public_id=update_type.get_public_id(), data=update_type.to_database()) if self._event_queue: event = Event("cmdb.core.objecttype.updated", {"id": update_type.get_public_id()}) self._event_queue.put(event) return ack
def insert_type(self, data: (CmdbType, dict)): if isinstance(data, CmdbType): new_type = data elif isinstance(data, dict): new_type = CmdbType(**data) else: raise WrongInputFormatError(CmdbType, data, "Possible data: dict or CmdbType") try: ack = self._insert(collection=CmdbType.COLLECTION, data=new_type.to_database()) LOGGER.debug(f"Inserted new type with ack {ack}") if self._event_queue: event = Event("cmdb.core.objecttype.added", {"id": new_type.get_public_id()}) self._event_queue.put(event) except PublicIDAlreadyExists: raise TypeAlreadyExists(type_id=new_type.get_public_id()) except (CMDBError, InsertError): raise TypeInsertError(new_type.get_public_id()) return ack