Esempio n. 1
0
    def create_or_update_attr_value(self,
                                    key,
                                    value,
                                    ci,
                                    _no_attribute_policy=ExistPolicy.IGNORE):
        """
        add or update attribute value, then write history
        :param key: id, name or alias
        :param value:
        :param ci: instance object
        :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

        if attr.is_list:
            value_list = [
                self._validate(attr, i, value_table, ci)
                for i in handle_arg_list(value)
            ]
            if not value_list:
                self.__check_is_required(ci.type_id, attr, '')

            existed_attrs = value_table.get_by(attr_id=attr.id,
                                               ci_id=ci.id,
                                               to_dict=False)
            existed_values = [i.value for i in existed_attrs]
            added = set(value_list) - set(existed_values)
            deleted = set(existed_values) - set(value_list)
            for v in added:
                value_table.create(ci_id=ci.id, attr_id=attr.id, value=v)
                self._write_change(ci.id, attr.id, OperateType.ADD, None, v)

            for v in deleted:
                existed_attr = existed_attrs[existed_values.index(v)]
                existed_attr.delete()
                self._write_change(ci.id, attr.id, OperateType.DELETE, v, None)
        else:
            value = self._validate(attr, value, value_table, ci)
            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
            if existed_value is None:
                value_table.create(ci_id=ci.id, attr_id=attr.id, value=value)
                self._write_change(ci.id, attr.id, OperateType.ADD, None,
                                   value)
            else:
                existed_attr.update(value=value)
                self._write_change(ci.id, attr.id, OperateType.UPDATE,
                                   existed_value, value)
Esempio n. 2
0
    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]
        if not isinstance(value, list):
            value_list = [value]

        for v in value_list:
            v = self._validate(attr, v, value_table, ci_id)
            if not v and attr.value_type != ValueTypeEnum.TEXT:
                v = None

            if operate_type == OperateType.ADD:
                if v is not None:
                    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:
                if v is not None:
                    existed_attr.update(value=v)
                else:
                    existed_attr.delete()
                self._write_change(ci_id, attr.id, operate_type, existed_value,
                                   v)