예제 #1
0
    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
예제 #2
0
파일: ci.py 프로젝트: xuweiwei2011/cmdb-1
    def add(cls, first_ci_id, second_ci_id, more=None, relation_type_id=None):

        relation_type_id = relation_type_id or cls._get_default_relation_type()

        CIManager.confirm_ci_existed(first_ci_id)
        CIManager.confirm_ci_existed(second_ci_id)

        existed = CIRelation.get_by(first_ci_id=first_ci_id,
                                    second_ci_id=second_ci_id,
                                    to_dict=False,
                                    first=True)
        if existed is not None:
            if existed.relation_type_id != relation_type_id:
                existed.update(relation_type_id=relation_type_id)
                CIRelationHistoryManager().add(existed, OperateType.UPDATE)
        else:
            existed = CIRelation.create(first_ci_id=first_ci_id,
                                        second_ci_id=second_ci_id,
                                        relation_type_id=relation_type_id)
            CIRelationHistoryManager().add(existed, OperateType.ADD)

            ci_relation_cache.apply_async(args=(first_ci_id, second_ci_id), queue=CMDB_QUEUE)

        if more is not None:
            existed.upadte(more=more)

        return existed.id
예제 #3
0
파일: ci.py 프로젝트: zzszmyf/cmdb
    def add(cls,
            first_ci_id,
            second_ci_id,
            more=None,
            relation_type_id=None,
            many_to_one=False):

        first_ci = CIManager.confirm_ci_existed(first_ci_id)
        second_ci = CIManager.confirm_ci_existed(second_ci_id)

        existed = CIRelation.get_by(first_ci_id=first_ci_id,
                                    second_ci_id=second_ci_id,
                                    to_dict=False,
                                    first=True)
        if existed is not None:
            if existed.relation_type_id != relation_type_id and relation_type_id is not None:
                existed.update(relation_type_id=relation_type_id)

                CIRelationHistoryManager().add(existed, OperateType.UPDATE)
        else:
            if relation_type_id is None:
                type_relation = CITypeRelation.get_by(
                    parent_id=first_ci.type_id,
                    child_id=second_ci.type_id,
                    first=True,
                    to_dict=False)
                relation_type_id = type_relation and type_relation.relation_type_id
                relation_type_id or abort(
                    404, "Relation {0} <-> {1} is not found".format(
                        first_ci.ci_type.name, second_ci.ci_type.name))

            if many_to_one:
                for item in CIRelation.get_by(
                        second_ci_id=second_ci_id,
                        relation_type_id=relation_type_id,
                        to_dict=False):
                    item.soft_delete()
                    his_manager = CIRelationHistoryManager()
                    his_manager.add(item, operate_type=OperateType.DELETE)

                    ci_relation_delete.apply_async(args=(item.first_ci_id,
                                                         item.second_ci_id),
                                                   queue=CMDB_QUEUE)

            existed = CIRelation.create(first_ci_id=first_ci_id,
                                        second_ci_id=second_ci_id,
                                        relation_type_id=relation_type_id)

            CIRelationHistoryManager().add(existed, OperateType.ADD)

            ci_relation_cache.apply_async(args=(first_ci_id, second_ci_id),
                                          queue=CMDB_QUEUE)

        if more is not None:
            existed.upadte(more=more)

        return existed.id
예제 #4
0
def test_delete_ci_relation_by_id(session, client):
    cr_id = init_ci_relation()
    url = "/api/v0.1/ci_relations/{cr_id}".format(cr_id=cr_id)
    resp = client.delete(url)
    assert resp.status_code == 200
    cr = CIRelation.get_by_id(cr_id)
    assert cr is None
예제 #5
0
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()
예제 #6
0
파일: ci.py 프로젝트: shuke163/cmdb
    def delete(cr_id):
        cr = CIRelation.get_by_id(cr_id) or abort(404, "CIRelation <{0}> is not existed".format(cr_id))
        cr.soft_delete()

        his_manager = CIRelationHistoryManager()
        his_manager.add(cr, operate_type=OperateType.DELETE)

        return cr_id
예제 #7
0
파일: ci.py 프로젝트: xuweiwei2011/cmdb-1
    def delete_2(cls, first_ci_id, second_ci_id):
        cr = CIRelation.get_by(first_ci_id=first_ci_id,
                               second_ci_id=second_ci_id,
                               to_dict=False,
                               first=True)

        ci_relation_delete.apply_async(args=(first_ci_id, second_ci_id), queue=CMDB_QUEUE)

        return cls.delete(cr.id)
예제 #8
0
def test_delte_ci_relation_by_ci_id(session, client):
    cr_id = init_ci_relation()
    cis = CI.query.all()

    url = "/api/v0.1/ci_relations/{}/{}".format(cis[0].id, cis[1].id)
    resp = client.delete(url)
    assert resp.status_code == 200
    cr = CIRelation.get_by_id(cr_id)
    assert cr is None
예제 #9
0
파일: ci.py 프로젝트: xuweiwei2011/cmdb-1
    def delete(cr_id):
        cr = CIRelation.get_by_id(cr_id) or abort(404, "CIRelation <{0}> is not existed".format(cr_id))
        cr.delete()

        his_manager = CIRelationHistoryManager()
        his_manager.add(cr, operate_type=OperateType.DELETE)

        ci_relation_delete.apply_async(args=(cr.first_ci_id, cr.second_ci_id), queue=CMDB_QUEUE)

        return cr_id
예제 #10
0
def test_create_ci_relation(session, client):
    init_ci_type_relation(1)
    ci_types = CIType.query.all()
    cis = init_ci_with_type(ci_types)

    url = "/api/v0.1/ci_relations/{}/{}".format(cis[0]['ci_id'], cis[1]['ci_id'])

    resp = client.post(url)
    assert resp.status_code == 200
    cr_id = resp.json['cr_id']
    cr = CIRelation.get_by_id(cr_id)
    assert cr is not None
예제 #11
0
파일: ci.py 프로젝트: xuweiwei2011/cmdb-1
    def get_children(cls, ci_id, ret_key=RetKey.NAME):
        second_cis = CIRelation.get_by(first_ci_id=ci_id, to_dict=False)
        second_ci_ids = (second_ci.second_ci_id for second_ci in second_cis)
        ci_type2ci_ids = dict()
        for ci_id in second_ci_ids:
            type_id = CI.get_by_id(ci_id).type_id
            ci_type2ci_ids.setdefault(type_id, []).append(ci_id)

        res = {}
        for type_id in ci_type2ci_ids:
            ci_type = CITypeCache.get(type_id)
            children = CIManager.get_cis_by_ids(list(map(str, ci_type2ci_ids[type_id])), ret_key=ret_key)
            res[ci_type.name] = children
        return res
예제 #12
0
def ci_relation_cache(parent_id, child_id):
    children = rd.get([parent_id], REDIS_PREFIX_CI_RELATION)[0]
    children = json.loads(children) if children is not None else {}

    cr = CIRelation.get_by(first_ci_id=parent_id,
                           second_ci_id=child_id,
                           first=True,
                           to_dict=False)
    if str(child_id) not in children:
        children[str(child_id)] = cr.second_ci.type_id

    rd.create_or_update({parent_id: json.dumps(children)},
                        REDIS_PREFIX_CI_RELATION)

    current_app.logger.info("ADD ci relation cache: {0} -> {1}".format(
        parent_id, child_id))
예제 #13
0
 def delete_2(cls, first_ci_id, second_ci_id):
     cr = CIRelation.get_by(first_ci_id=first_ci_id,
                            second_ci_id=second_ci_id,
                            to_dict=False,
                            first=True)
     return cls.delete(cr.cr_id)