From 4a638a68beaecc10a621a67941cbb015b4272868 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 6cb56287998657ddfd2fc0b6a907e19aaa7360ea 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 730f429801a80a56205a28ec18d358ff522b59f2 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 8959940713461e056af6708736a20faac9f82c97 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 7b8d1e2ec15dd0c534a3345aaad04e0571071d0b 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 34eb7ab1297e89c73150a5786cbc6f8c4fe86fec 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)