From 94cdb424779096e6188530fab04518cc3d2f79f2 Mon Sep 17 00:00:00 2001 From: pycook Date: Mon, 11 Sep 2023 19:15:31 +0800 Subject: [PATCH] fix upload template and add /api/v0.1/attributes//calc_computed_attribute --- cmdb-api/api/lib/cmdb/attribute.py | 6 ++++-- cmdb-api/api/lib/cmdb/ci_type.py | 32 +++++++++++++++++----------- cmdb-api/api/views/cmdb/attribute.py | 12 +++++------ 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/cmdb-api/api/lib/cmdb/attribute.py b/cmdb-api/api/lib/cmdb/attribute.py index be0654c..f80927a 100644 --- a/cmdb-api/api/lib/cmdb/attribute.py +++ b/cmdb-api/api/lib/cmdb/attribute.py @@ -163,13 +163,15 @@ class AttributeManager(object): if RoleEnum.CONFIG not in session.get("acl", {}).get("parentRoles", []) and not is_app_admin('cmdb'): return abort(403, ErrFormat.role_required.format(RoleEnum.CONFIG)) - @staticmethod - def calc_computed_attribute(attr_id): + @classmethod + def calc_computed_attribute(cls, attr_id): """ calculate computed attribute for all ci :param attr_id: :return: """ + cls.can_create_computed_attribute() + from api.tasks.cmdb import calc_computed_attribute calc_computed_attribute.apply_async(args=(attr_id, current_user.uid), queue=CMDB_QUEUE) diff --git a/cmdb-api/api/lib/cmdb/ci_type.py b/cmdb-api/api/lib/cmdb/ci_type.py index 0f28d97..349d5ee 100644 --- a/cmdb-api/api/lib/cmdb/ci_type.py +++ b/cmdb-api/api/lib/cmdb/ci_type.py @@ -1,4 +1,4 @@ -# -*- coding:utf-8 -*- +# -*- coding:utf-8 -*- import copy import datetime @@ -114,7 +114,7 @@ class CITypeManager(object): @kwargs_required("name") def add(cls, **kwargs): - unique_key = kwargs.pop("unique_key", None) + unique_key = kwargs.pop("unique_key", None) or kwargs.pop("unique_id", None) unique_key = AttributeCache.get(unique_key) or abort(404, ErrFormat.unique_key_not_define) kwargs["alias"] = kwargs["name"] if not kwargs.get("alias") else kwargs["alias"] @@ -276,10 +276,10 @@ class CITypeGroupManager(object): def update(gid, name, type_ids): """ update part - :param gid: - :param name: - :param type_ids: - :return: + :param gid: + :param name: + :param type_ids: + :return: """ existed = CITypeGroup.get_by_id(gid) or abort( 404, ErrFormat.ci_type_group_not_found.format("id={}".format(gid))) @@ -386,10 +386,10 @@ class CITypeAttributeManager(object): def add(cls, type_id, attr_ids=None, **kwargs): """ add attributes to CIType - :param type_id: + :param type_id: :param attr_ids: list - :param kwargs: - :return: + :param kwargs: + :return: """ attr_ids = list(set(attr_ids)) @@ -416,9 +416,9 @@ class CITypeAttributeManager(object): def update(cls, type_id, attributes): """ update attributes to CIType - :param type_id: + :param type_id: :param attributes: list - :return: + :return: """ cls._check(type_id, [i.get('attr_id') for i in attributes]) @@ -446,9 +446,9 @@ class CITypeAttributeManager(object): def delete(cls, type_id, attr_ids=None): """ delete attributes from CIType - :param type_id: + :param type_id: :param attr_ids: list - :return: + :return: """ from api.tasks.cmdb import ci_cache @@ -823,6 +823,12 @@ class CITypeTemplateManager(object): for added_id in set(id2obj_dicts.keys()) - set(existed_ids): if cls == CIType: CITypeManager.add(**id2obj_dicts[added_id]) + elif cls == CITypeRelation: + CITypeRelationManager.add(id2obj_dicts[added_id].get('parent_id'), + id2obj_dicts[added_id].get('child_id'), + id2obj_dicts[added_id].get('relation_type_id'), + id2obj_dicts[added_id].get('constraint'), + ) else: cls.create(flush=True, **id2obj_dicts[added_id]) diff --git a/cmdb-api/api/views/cmdb/attribute.py b/cmdb-api/api/views/cmdb/attribute.py index 2a3edf2..fffbcdc 100644 --- a/cmdb-api/api/views/cmdb/attribute.py +++ b/cmdb-api/api/views/cmdb/attribute.py @@ -56,12 +56,7 @@ class AttributeView(APIView): @args_required("name") @args_validate(AttributeManager.cls) - def post(self, attr_id=None): - if request.url.endswith("/calc_computed_attribute"): - AttributeManager.calc_computed_attribute(attr_id) - - return self.jsonify(attr_id=attr_id) - + def post(self): choice_value = handle_arg_list(request.values.get("choice_value")) params = request.values params["choice_value"] = choice_value @@ -74,6 +69,11 @@ class AttributeView(APIView): @args_validate(AttributeManager.cls) def put(self, attr_id): + if request.url.endswith("/calc_computed_attribute"): + AttributeManager.calc_computed_attribute(attr_id) + + return self.jsonify(attr_id=attr_id) + choice_value = handle_arg_list(request.values.get("choice_value")) params = request.values params["choice_value"] = choice_value