From d85715793fd6a82f8db14ad817052ed1a865e0df Mon Sep 17 00:00:00 2001 From: lovvvve Date: Thu, 28 Jan 2021 16:27:56 +0800 Subject: [PATCH 1/6] Update ci.py feat(CiManager.add()): Check the attribute is in the ci_type attributes list --- cmdb-api/api/lib/cmdb/ci.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmdb-api/api/lib/cmdb/ci.py b/cmdb-api/api/lib/cmdb/ci.py index 88530d0..bfa0bf9 100644 --- a/cmdb-api/api/lib/cmdb/ci.py +++ b/cmdb-api/api/lib/cmdb/ci.py @@ -187,8 +187,12 @@ class CIManager(object): return abort(404, 'CI <{0}> does not exist'.format(unique_value)) ci = CI.create(type_id=ci_type.id) + ci_type_attrs_name = [attr["name"] for attr in CITypeAttributeManager().get_attributes_by_type_id(ci_type.id)] value_manager = AttributeValueManager() for p, v in ci_dict.items(): + if p not in ci_type_attrs_name: + current_app.logger.warning(f'ci_type: {ci_type_name} not has attribute {p}, please check!') + continue try: value_manager.create_or_update_attr_value(p, v, ci, _no_attribute_policy) except BadRequest as e: From 0d8b41b64a6801b98dda65c6e639d0aee4b54ea4 Mon Sep 17 00:00:00 2001 From: lovvvve Date: Thu, 28 Jan 2021 16:30:17 +0800 Subject: [PATCH 2/6] Update value.py feat(AttributeValueManager.create_or_update_attr_value()): AttributeValue update skip The same value --- cmdb-api/api/lib/cmdb/value.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cmdb-api/api/lib/cmdb/value.py b/cmdb-api/api/lib/cmdb/value.py index 2b53a22..967a2fc 100644 --- a/cmdb-api/api/lib/cmdb/value.py +++ b/cmdb-api/api/lib/cmdb/value.py @@ -160,12 +160,13 @@ class AttributeValueManager(object): self._write_change(ci.id, attr.id, OperateType.ADD, None, value) else: - if not value and attr.value_type != ValueTypeEnum.TEXT: - existed_attr.delete() - else: - existed_attr.update(value=value) + if existed_value != value: + if value is not 0 and not value and attr.value_type != ValueTypeEnum.TEXT: + existed_attr.delete() + else: + existed_attr.update(value=value) - self._write_change(ci.id, attr.id, OperateType.UPDATE, existed_value, value) + AttributeValueManager._write_change(ci.id, attr.id, OperateType.UPDATE, existed_value, value) @staticmethod def delete_attr_value(attr_id, ci_id): From cfcb0924786f7e80378529a945d012a58f75dbe6 Mon Sep 17 00:00:00 2001 From: lovvvve Date: Thu, 28 Jan 2021 16:32:53 +0800 Subject: [PATCH 3/6] Update value.py feat(AttributeValueManager.create_or_update_attr_value()): AttributeValue update skip The same value --- cmdb-api/api/lib/cmdb/value.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmdb-api/api/lib/cmdb/value.py b/cmdb-api/api/lib/cmdb/value.py index 967a2fc..590fa8e 100644 --- a/cmdb-api/api/lib/cmdb/value.py +++ b/cmdb-api/api/lib/cmdb/value.py @@ -166,7 +166,7 @@ class AttributeValueManager(object): else: existed_attr.update(value=value) - AttributeValueManager._write_change(ci.id, attr.id, OperateType.UPDATE, existed_value, value) + self._write_change(ci.id, attr.id, OperateType.UPDATE, existed_value, value) @staticmethod def delete_attr_value(attr_id, ci_id): From 9268da2ffa55fa4f1e3d5f067276e3084398afe9 Mon Sep 17 00:00:00 2001 From: lovvvve Date: Thu, 28 Jan 2021 16:46:19 +0800 Subject: [PATCH 4/6] Update value.py --- cmdb-api/api/lib/cmdb/value.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmdb-api/api/lib/cmdb/value.py b/cmdb-api/api/lib/cmdb/value.py index 590fa8e..4976e8f 100644 --- a/cmdb-api/api/lib/cmdb/value.py +++ b/cmdb-api/api/lib/cmdb/value.py @@ -161,7 +161,7 @@ class AttributeValueManager(object): self._write_change(ci.id, attr.id, OperateType.ADD, None, value) else: if existed_value != value: - if value is not 0 and not value and attr.value_type != ValueTypeEnum.TEXT: + if value == 0 and not value and attr.value_type != ValueTypeEnum.TEXT: existed_attr.delete() else: existed_attr.update(value=value) From a7debc1b3b63ff9793143d82a01d3b50a939de0b Mon Sep 17 00:00:00 2001 From: lovvvve Date: Thu, 28 Jan 2021 16:56:04 +0800 Subject: [PATCH 5/6] Update ci.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 兼容 py2 --- cmdb-api/api/lib/cmdb/ci.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmdb-api/api/lib/cmdb/ci.py b/cmdb-api/api/lib/cmdb/ci.py index bfa0bf9..ad84126 100644 --- a/cmdb-api/api/lib/cmdb/ci.py +++ b/cmdb-api/api/lib/cmdb/ci.py @@ -191,7 +191,7 @@ class CIManager(object): value_manager = AttributeValueManager() for p, v in ci_dict.items(): if p not in ci_type_attrs_name: - current_app.logger.warning(f'ci_type: {ci_type_name} not has attribute {p}, please check!') + current_app.logger.warning('ci_type: {0} not has attribute {1}, please check!'.format(ci_type_name, p)) continue try: value_manager.create_or_update_attr_value(p, v, ci, _no_attribute_policy) From 3cf234d49e3f9cac6586f4df30356be0cf2aa0a3 Mon Sep 17 00:00:00 2001 From: lovvvve Date: Thu, 28 Jan 2021 16:57:20 +0800 Subject: [PATCH 6/6] Update value.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit value type 是 int 或 float 时 value 值等于 0 是会删除 的 BUG --- cmdb-api/api/lib/cmdb/value.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmdb-api/api/lib/cmdb/value.py b/cmdb-api/api/lib/cmdb/value.py index 4976e8f..10c54a0 100644 --- a/cmdb-api/api/lib/cmdb/value.py +++ b/cmdb-api/api/lib/cmdb/value.py @@ -161,7 +161,7 @@ class AttributeValueManager(object): self._write_change(ci.id, attr.id, OperateType.ADD, None, value) else: if existed_value != value: - if value == 0 and not value and attr.value_type != ValueTypeEnum.TEXT: + if value != 0 and not value and attr.value_type != ValueTypeEnum.TEXT: existed_attr.delete() else: existed_attr.update(value=value)