diff --git a/cmdb-api/api/lib/cmdb/auto_discovery/auto_discovery.py b/cmdb-api/api/lib/cmdb/auto_discovery/auto_discovery.py index 276eb3a..a0323fb 100644 --- a/cmdb-api/api/lib/cmdb/auto_discovery/auto_discovery.py +++ b/cmdb-api/api/lib/cmdb/auto_discovery/auto_discovery.py @@ -331,14 +331,14 @@ class AutoDiscoveryCICRUD(DBMixin): @staticmethod def get_attributes_by_type_id(type_id): from api.lib.cmdb.ci_type import CITypeAttributeManager - attributes = [i[1] for i in CITypeAttributeManager.get_all_attributes(type_id) or []] + attributes = [i for i in CITypeAttributeManager.get_attributes_by_type_id(type_id) or []] attr_names = set() adts = AutoDiscoveryCITypeCRUD.get_by_type_id(type_id) for adt in adts: attr_names |= set((adt.attributes or {}).values()) - return [attr.to_dict() for attr in attributes if attr.name in attr_names] + return [attr for attr in attributes if attr['name'] in attr_names] @classmethod def search(cls, page, page_size, fl=None, **kwargs): diff --git a/cmdb-api/api/lib/cmdb/resp_format.py b/cmdb-api/api/lib/cmdb/resp_format.py index b9f6b37..7e89cbe 100644 --- a/cmdb-api/api/lib/cmdb/resp_format.py +++ b/cmdb-api/api/lib/cmdb/resp_format.py @@ -96,7 +96,7 @@ class ErrFormat(CommonErrFormat): # 属性 {} 的值必须是唯一的, 当前值 {} 已存在 attribute_value_unique_required = _l("The value of attribute {} must be unique, {} already exists") attribute_value_required = _l("Attribute {} value must exist") # 属性 {} 值必须存在 - + attribute_value_out_of_range = _l("Out of range value, the maximum value is 2147483647") # 新增或者修改属性值未知错误: {} attribute_value_unknown_error = _l("Unknown error when adding or modifying attribute value: {}") diff --git a/cmdb-api/api/lib/cmdb/utils.py b/cmdb-api/api/lib/cmdb/utils.py index 1239b1c..9358d23 100644 --- a/cmdb-api/api/lib/cmdb/utils.py +++ b/cmdb-api/api/lib/cmdb/utils.py @@ -11,12 +11,21 @@ import six import api.models.cmdb as model from api.lib.cmdb.cache import AttributeCache from api.lib.cmdb.const import ValueTypeEnum +from api.lib.cmdb.resp_format import ErrFormat TIME_RE = re.compile(r'(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d') +class ValueDeserializeError(Exception): + pass + + def string2int(x): - return int(float(x)) + v = int(float(x)) + if v > 2147483647: + raise ValueDeserializeError(ErrFormat.attribute_value_out_of_range) + + return v def str2datetime(x): diff --git a/cmdb-api/api/lib/cmdb/value.py b/cmdb-api/api/lib/cmdb/value.py index bf988bd..66594ce 100644 --- a/cmdb-api/api/lib/cmdb/value.py +++ b/cmdb-api/api/lib/cmdb/value.py @@ -5,14 +5,16 @@ from __future__ import unicode_literals import copy import imp -import jinja2 import os import re import tempfile + +import jinja2 from flask import abort from flask import current_app from jinja2schema import infer from jinja2schema import to_json_schema +from werkzeug.exceptions import BadRequest from api.extensions import db from api.lib.cmdb.attribute import AttributeManager @@ -23,6 +25,7 @@ from api.lib.cmdb.const import ValueTypeEnum from api.lib.cmdb.history import AttributeHistoryManger from api.lib.cmdb.resp_format import ErrFormat from api.lib.cmdb.utils import TableMap +from api.lib.cmdb.utils import ValueDeserializeError from api.lib.cmdb.utils import ValueTypeMap from api.lib.utils import handle_arg_list from api.models.cmdb import CI @@ -80,7 +83,7 @@ class AttributeValueManager(object): return res @staticmethod - def _deserialize_value(value_type, value): + def _deserialize_value(alias, value_type, value): if not value: return value @@ -88,6 +91,8 @@ class AttributeValueManager(object): try: v = deserialize(value) return v + except ValueDeserializeError as e: + return abort(400, ErrFormat.attribute_value_invalid2.format(alias, e)) except ValueError: return abort(400, ErrFormat.attribute_value_invalid.format(value)) @@ -124,7 +129,7 @@ class AttributeValueManager(object): def _validate(self, attr, value, value_table, ci=None, type_id=None, ci_id=None, type_attr=None): ci = ci or {} - v = self._deserialize_value(attr.value_type, value) + v = self._deserialize_value(attr.alias, attr.value_type, value) attr.is_choice and value and self._check_is_choice(attr, attr.value_type, v) attr.is_unique and self._check_is_unique( @@ -240,6 +245,8 @@ class AttributeValueManager(object): value = self._validate(attr, value, value_table, ci=None, type_id=type_id, ci_id=ci_id, type_attr=ci_attr2type_attr.get(attr.id)) ci_dict[key] = value + except BadRequest as e: + raise except Exception as e: current_app.logger.warning(str(e)) diff --git a/cmdb-api/api/translations/zh/LC_MESSAGES/messages.mo b/cmdb-api/api/translations/zh/LC_MESSAGES/messages.mo index 09b8c79..c742637 100644 Binary files a/cmdb-api/api/translations/zh/LC_MESSAGES/messages.mo and b/cmdb-api/api/translations/zh/LC_MESSAGES/messages.mo differ diff --git a/cmdb-api/api/translations/zh/LC_MESSAGES/messages.po b/cmdb-api/api/translations/zh/LC_MESSAGES/messages.po index 7e7b157..a668075 100644 --- a/cmdb-api/api/translations/zh/LC_MESSAGES/messages.po +++ b/cmdb-api/api/translations/zh/LC_MESSAGES/messages.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: PROJECT VERSION\n" "Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2024-03-01 13:49+0800\n" +"POT-Creation-Date: 2024-03-29 10:42+0800\n" "PO-Revision-Date: 2023-12-25 20:21+0800\n" "Last-Translator: FULL NAME \n" "Language: zh\n" @@ -322,7 +322,7 @@ msgstr "无效的属性值: {}" #: api/lib/cmdb/resp_format.py:94 msgid "{} Invalid value: {}" -msgstr "无效的值: {}" +msgstr "{} 无效的值: {}" #: api/lib/cmdb/resp_format.py:95 msgid "{} is not in the predefined values" @@ -336,6 +336,10 @@ msgstr "属性 {} 的值必须是唯一的, 当前值 {} 已存在" msgid "Attribute {} value must exist" msgstr "属性 {} 值必须存在" +#: api/lib/cmdb/resp_format.py:99 +msgid "Out of range value, the maximum value is 2147483647" +msgstr "超过最大值限制, 最大值是2147483647" + #: api/lib/cmdb/resp_format.py:101 msgid "Unknown error when adding or modifying attribute value: {}" msgstr "新增或者修改属性值未知错误: {}"