예제 #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
파일: value.py 프로젝트: shuke163/cmdb
    def get_attr_values(self, fields, ci_id, ret_key="name", unique_key=None, use_master=False):
        """

        :param fields:
        :param ci_id:
        :param ret_key: It can be name or alias
        :param unique_key: primary attribute
        :param use_master: Only for master-slave read-write separation
        :return:
        """
        res = dict()
        for field in fields:
            attr = self._get_attr(field)
            if not attr:
                continue
            value_table = TableMap(attr_name=attr.name).table
            rs = value_table.get_by(ci_id=ci_id,
                                    attr_id=attr.id,
                                    use_master=use_master,
                                    to_dict=False)
            field_name = getattr(attr, ret_key)

            if attr.is_list:
                res[field_name] = [type_map["serialize"][attr.value_type](i.value) for i in rs]
            else:
                res[field_name] = type_map["serialize"][attr.value_type](rs[0].value) if rs else None

            if unique_key is not None and attr.id == unique_key.id and rs:
                res['unique'] = unique_key.name

        return res
예제 #3
0
파일: value.py 프로젝트: shuke163/cmdb
    def create_or_update_attr_value(self, key, value, ci_id, _no_attribute_policy=ExistPolicy.IGNORE):
        """
        add or update attribute value, then write history
        :param key: id, name or alias
        :param value:
        :param ci_id:
        :param _no_attribute_policy: ignore or reject
        :return:
        """
        attr = self._get_attr(key)
        if attr is None:
            if _no_attribute_policy == ExistPolicy.IGNORE:
                return
            if _no_attribute_policy == ExistPolicy.REJECT:
                return abort(400, 'attribute {0} does not exist'.format(key))

        value_table = TableMap(attr_name=attr.name).table
        existed_attr = value_table.get_by(attr_id=attr.id,
                                          ci_id=ci_id,
                                          first=True,
                                          to_dict=False)
        existed_value = existed_attr and existed_attr.value
        operate_type = OperateType.ADD if existed_attr is None else OperateType.UPDATE

        value_list = handle_arg_list(value) if attr.is_list else [value]

        for v in value_list:
            v = self._validate(attr, v, value_table, ci_id)
            if operate_type == OperateType.ADD:
                value_table.create(ci_id=ci_id, attr_id=attr.id, value=v)
                self._write_change(ci_id, attr.id, operate_type, None, v)
            elif existed_attr.value != v:
                existed_attr.update(value=v)
                self._write_change(ci_id, attr.id, operate_type, existed_value, v)
예제 #4
0
 def ci_is_exist(unique_key, unique_value):
     """
     
     :param unique_key: is a attribute
     :param unique_value: 
     :return: 
     """
     value_table = TableMap(attr_name=unique_key.name).table
     unique = value_table.get_by(attr_id=unique_key.id,
                                 value=unique_value,
                                 to_dict=False,
                                 first=True)
     if unique:
         return CI.get_by_id(unique.ci_id)
예제 #5
0
파일: ci.py 프로젝트: shuke163/cmdb
    def add_heartbeat(ci_type, unique_value):
        ci_type = CITypeManager().check_is_existed(ci_type)

        unique_key = AttributeCache.get(ci_type.unique_id)
        value_table = TableMap(attr_name=unique_key.name).table

        v = value_table.get_by(attr_id=unique_key.id,
                               value=unique_value,
                               to_dict=False,
                               first=True) \
            or abort(404, "not found")

        ci = CI.get_by_id(v.ci_id) or abort(404, "CI <{0}> is not found".format(v.ci_id))

        ci.update(heartbeat=datetime.datetime.now())