def delete(ci_id): ci = CI.get_by_id(ci_id) or abort( 404, "CI <{0}> is not found".format(ci_id)) attrs = CITypeAttribute.get_by(type_id=ci.type_id, to_dict=False) attr_names = set( [AttributeCache.get(attr.attr_id).name for attr in attrs]) for attr_name in attr_names: value_table = TableMap(attr_name=attr_name).table for item in value_table.get_by(ci_id=ci_id, to_dict=False): item.delete() for item in CIRelation.get_by(first_ci_id=ci_id, to_dict=False): item.delete() for item in CIRelation.get_by(second_ci_id=ci_id, to_dict=False): item.delete() ci.delete() # TODO: soft delete AttributeHistoryManger.add(ci_id, [(None, OperateType.DELETE, None, None)]) ci_delete.apply_async([ci.id], queue=CMDB_QUEUE) return ci_id
def get(cls, key): if key is None: return attrs = cache.get(cls.PREFIX_NAME.format(key)) attrs = attrs or cache.get(cls.PREFIX_ID.format(key)) if not attrs: attrs = CITypeAttribute.get_by(type_id=key, to_dict=False) if not attrs: ci_type = CIType.get_by(name=key, first=True, to_dict=False) if ci_type is not None: attrs = CITypeAttribute.get_by(type_id=ci_type.id, to_dict=False) if attrs is not None: cls.set(key, attrs) return attrs
def delete(cls, type_id, attr_ids=None): """ delete attributes from CIType :param type_id: :param attr_ids: list :return: """ from api.tasks.cmdb import ci_cache cls._check(type_id, attr_ids) for attr_id in attr_ids: existed = CITypeAttribute.get_by(type_id=type_id, attr_id=attr_id, first=True, to_dict=False) if existed is not None: existed.soft_delete() for ci in CI.get_by(type_id=type_id, to_dict=False): AttributeValueManager.delete_attr_value(attr_id, ci.id) ci_cache.apply_async([ci.id], queue=CMDB_QUEUE) CITypeAttributeCache.clean(type_id, attr_id) CITypeAttributesCache.clean(type_id)
def get(cls, key): if key is None: return attrs = cache.get("CITypeAttribute::Name::{0}".format(key)) \ or cache.get("CITypeAttribute::ID::{0}".format(key)) if not attrs: attrs = CITypeAttribute.get_by(type_id=key, to_dict=False) if not attrs: ci_type = CIType.get_by(name=key, first=True, to_dict=False) if ci_type is not None: attrs = CITypeAttribute.get_by(type_id=ci_type.id, to_dict=False) if attrs is not None: cls.set(key, attrs) return attrs
def get(cls, type_id, attr_id): attr = cache.get(cls.PREFIX_ID.format(type_id, attr_id)) attr = attr or cache.get(cls.PREFIX_ID.format(type_id, attr_id)) if not attr: attr = CITypeAttribute.get_by(type_id=type_id, attr_id=attr_id, first=True, to_dict=False) if attr is not None: cls.set(type_id, attr_id, attr) return attr
def delete(cls, type_id, attr_ids=None): """ delete attributes from CIType :param type_id: :param attr_ids: list :return: """ cls._check(type_id, attr_ids) for attr_id in attr_ids: existed = CITypeAttribute.get_by(type_id=type_id, attr_id=attr_id, first=True, to_dict=False) if existed is not None: existed.soft_delete() CITypeAttributeCache.clean(type_id)
def update(cls, type_id, attributes): """ update attributes to CIType :param type_id: :param attributes: list :return: """ cls._check(type_id, [i.get('attr_id') for i in attributes]) for attr in attributes: existed = CITypeAttribute.get_by(type_id=type_id, attr_id=attr.get("attr_id"), first=True, to_dict=False) if existed is None: continue existed.update(**attr) CITypeAttributeCache.clean(type_id)
def delete(_id): attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id)) name = attr.name if attr.is_choice: choice_table = type_map["choice"].get(attr.value_type) db.session.query(choice_table).filter(choice_table.attr_id == _id).delete() # FIXME: session conflict db.session.flush() AttributeCache.clean(attr) attr.soft_delete() for i in CITypeAttribute.get_by(attr_id=_id, to_dict=False): i.soft_delete() for i in PreferenceShowAttributes.get_by(attr_id=_id, to_dict=False): i.soft_delete() return name
def update(cls, type_id, **kwargs): ci_type = cls.check_is_existed(type_id) unique_key = kwargs.pop("unique_key", None) unique_key = AttributeCache.get(unique_key) if unique_key is not None: kwargs["unique_id"] = unique_key.id type_attr = CITypeAttribute.get_by(type_id=type_id, attr_id=unique_key.id, first=True, to_dict=False) if type_attr is None: CITypeAttributeManager.add(type_id, [unique_key.id], is_required=True) ci_type.update(**kwargs) CITypeCache.clean(type_id) return type_id
def add(cls, type_id, attr_ids=None, **kwargs): """ add attributes to CIType :param type_id: :param attr_ids: list :param kwargs: :return: """ cls._check(type_id, attr_ids) for attr_id in attr_ids: existed = CITypeAttribute.get_by(type_id=type_id, attr_id=attr_id, first=True, to_dict=False) if existed is not None: continue current_app.logger.debug(attr_id) CITypeAttribute.create(type_id=type_id, attr_id=attr_id, **kwargs) CITypeAttributesCache.clean(type_id)