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 delete(cls, type_id): ci_type = cls.check_is_existed(type_id) if CI.get_by(type_id=type_id, first=True, to_dict=False) is not None: return abort(400, "cannot delete, because CI instance exists") for item in CITypeRelation.get_by(parent_id=type_id, to_dict=False): item.soft_delete() for item in CITypeRelation.get_by(child_id=type_id, to_dict=False): item.soft_delete() for item in PreferenceTreeView.get_by(type_id=type_id, to_dict=False): item.soft_delete() for item in PreferenceShowAttributes.get_by(type_id=type_id, to_dict=False): item.soft_delete() ci_type.soft_delete() CITypeCache.clean(type_id) if current_app.config.get("USE_ACL"): from api.lib.perm.acl.acl import ACLManager from api.lib.cmdb.const import ResourceTypeEnum, RoleEnum, PermEnum ACLManager().del_resource(ci_type.name, ResourceTypeEnum.CI)
def init_cache(): db.session.remove() if current_app.config.get("USE_ES"): from api.extensions import es from api.models.cmdb import Attribute from api.lib.cmdb.utils import ValueTypeMap attributes = Attribute.get_by(to_dict=False) for attr in attributes: other = dict() other['index'] = True if attr.is_index else False if attr.value_type == ValueTypeEnum.TEXT: other['analyzer'] = 'ik_max_word' other['search_analyzer'] = 'ik_smart' if attr.is_index: other["fields"] = { "keyword": { "type": "keyword", "ignore_above": 256 } } try: es.update_mapping(attr.name, ValueTypeMap.es_type[attr.value_type], other) except Exception as e: print(e) cis = CI.get_by(to_dict=False) for ci in cis: if current_app.config.get("USE_ES"): res = es.get_index_id(ci.id) if res: continue else: res = rd.get([ci.id], REDIS_PREFIX_CI) if res and list(filter(lambda x: x, res)): continue m = api.lib.cmdb.ci.CIManager() ci_dict = m.get_ci_by_id_from_db(ci.id, need_children=False, use_master=False) if current_app.config.get("USE_ES"): es.create(ci_dict) else: rd.create_or_update({ci.id: json.dumps(ci_dict)}, REDIS_PREFIX_CI) ci_relations = CIRelation.get_by(to_dict=False) relations = dict() for cr in ci_relations: relations.setdefault(cr.first_ci_id, {}).update( {cr.second_ci_id: cr.second_ci.type_id}) for i in relations: relations[i] = json.dumps(relations[i]) if relations: rd.create_or_update(relations, REDIS_PREFIX_CI_RELATION) db.session.remove()
def get(self, ci_id=None): from api.tasks.cmdb import ci_cache from api.lib.cmdb.const import CMDB_QUEUE if ci_id is not None: ci_cache.apply_async([ci_id], queue=CMDB_QUEUE) else: cis = CI.get_by(to_dict=False) for ci in cis: ci_cache.apply_async([ci.id], queue=CMDB_QUEUE) return self.jsonify(code=200)
def delete(cls, type_id): ci_type = cls.check_is_existed(type_id) if CI.get_by(type_id=type_id, first=True, to_dict=False) is not False: return abort(400, "cannot delete, because CI instance exists") for item in CITypeRelation.get_by(parent_id=type_id, to_dict=False): item.soft_delete() for item in CITypeRelation.get_by(child_id=type_id, to_dict=False): item.soft_delete() for item in PreferenceTreeView.get_by(type_id=type_id, to_dict=False): item.soft_delete() for item in PreferenceShowAttributes.get_by(type_id=type_id, to_dict=False): item.soft_delete() ci_type.soft_delete() CITypeCache.clean(type_id)