Exemple #1
0
    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
Exemple #2
0
    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