From dc569c32a59c3713a74815645a93d51b18281783 Mon Sep 17 00:00:00 2001 From: pycook Date: Tue, 26 Mar 2024 10:58:11 +0800 Subject: [PATCH] feat(api): support service tree editing (#437) --- cmdb-api/api/lib/cmdb/preference.py | 19 ++++++++++++++++--- cmdb-api/api/models/cmdb.py | 1 + cmdb-api/api/views/cmdb/preference.py | 14 ++++++++++---- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/cmdb-api/api/lib/cmdb/preference.py b/cmdb-api/api/lib/cmdb/preference.py index 757608e..f5659d5 100644 --- a/cmdb-api/api/lib/cmdb/preference.py +++ b/cmdb-api/api/lib/cmdb/preference.py @@ -238,11 +238,13 @@ class PreferenceManager(object): views = _views view2cr_ids = dict() + name2view = dict() result = dict() name2id = list() for view in views: view2cr_ids.setdefault(view['name'], []).extend(view['cr_ids']) name2id.append([view['name'], view['id']]) + name2view[view['name']] = view id2type = dict() for view_name in view2cr_ids: @@ -286,6 +288,8 @@ class PreferenceManager(object): topo_flatten=topo_flatten, level2constraint=level2constraint, leaf=leaf, + option=name2view[view_name]['option'], + is_public=name2view[view_name]['is_public'], leaf2show_types=leaf2show_types, node2show_types=node2show_types, show_types=[CITypeCache.get(j).to_dict() @@ -297,14 +301,18 @@ class PreferenceManager(object): return result, id2type, sorted(name2id, key=lambda x: x[1]) @classmethod - def create_or_update_relation_view(cls, name, cr_ids, is_public=False): + def create_or_update_relation_view(cls, name=None, cr_ids=None, _id=None, is_public=False, option=None): if not cr_ids: return abort(400, ErrFormat.preference_relation_view_node_required) - existed = PreferenceRelationView.get_by(name=name, to_dict=False, first=True) + if _id is None: + existed = PreferenceRelationView.get_by(name=name, to_dict=False, first=True) + else: + existed = PreferenceRelationView.get_by_id(_id) current_app.logger.debug(existed) if existed is None: - PreferenceRelationView.create(name=name, cr_ids=cr_ids, uid=current_user.uid, is_public=is_public) + PreferenceRelationView.create(name=name, cr_ids=cr_ids, uid=current_user.uid, + is_public=is_public, option=option) if current_app.config.get("USE_ACL"): ACLManager().add_resource(name, ResourceTypeEnum.RELATION_VIEW) @@ -312,6 +320,11 @@ class PreferenceManager(object): RoleEnum.CMDB_READ_ALL, ResourceTypeEnum.RELATION_VIEW, permissions=[PermEnum.READ]) + else: + if existed.name != name and current_app.config.get("USE_ACL"): + ACLManager().update_resource(existed.name, name, ResourceTypeEnum.RELATION_VIEW) + + existed.update(name=name, cr_ids=cr_ids, is_public=is_public, option=option) return cls.get_relation_view() diff --git a/cmdb-api/api/models/cmdb.py b/cmdb-api/api/models/cmdb.py index b282d01..a585eb3 100644 --- a/cmdb-api/api/models/cmdb.py +++ b/cmdb-api/api/models/cmdb.py @@ -460,6 +460,7 @@ class PreferenceRelationView(Model): name = db.Column(db.String(64), index=True, nullable=False) cr_ids = db.Column(db.JSON) # [{parent_id: x, child_id: y}] is_public = db.Column(db.Boolean, default=False) + option = db.Column(db.JSON) class PreferenceSearchOption(Model): diff --git a/cmdb-api/api/views/cmdb/preference.py b/cmdb-api/api/views/cmdb/preference.py index 681319d..3da2fa7 100644 --- a/cmdb-api/api/views/cmdb/preference.py +++ b/cmdb-api/api/views/cmdb/preference.py @@ -97,7 +97,7 @@ class PreferenceTreeApiView(APIView): class PreferenceRelationApiView(APIView): - url_prefix = "/preference/relation/view" + url_prefix = ("/preference/relation/view", "/preference/relation/view/") def get(self): views, id2type, name2id = PreferenceManager.get_relation_view() @@ -110,14 +110,20 @@ class PreferenceRelationApiView(APIView): @args_validate(PreferenceManager.pref_rel_cls) def post(self): name = request.values.get("name") + is_public = request.values.get("is_public") in current_app.config.get('BOOL_TRUE') cr_ids = request.values.get("cr_ids") - views, id2type, name2id = PreferenceManager.create_or_update_relation_view(name, cr_ids) + option = request.values.get("option") or None + views, id2type, name2id = PreferenceManager.create_or_update_relation_view(name, cr_ids, is_public=is_public, + option=option) return self.jsonify(views=views, id2type=id2type, name2id=name2id) @role_required(RoleEnum.CONFIG) - def put(self): - return self.post() + @args_required("name") + def put(self, _id): + views, id2type, name2id = PreferenceManager.create_or_update_relation_view(_id=_id, **request.values) + + return self.jsonify(views=views, id2type=id2type, name2id=name2id) @role_required(RoleEnum.CONFIG) @args_required("name")