From c861f390c7da203253d786aa6b1c1d1e9b63cfc9 Mon Sep 17 00:00:00 2001 From: fit2bot <68588906+fit2bot@users.noreply.github.com> Date: Mon, 12 Dec 2022 11:43:19 +0800 Subject: [PATCH] perf: k8s account ignore (#9188) Co-authored-by: feng <1304903146@qq.com> --- apps/assets/utils/k8s.py | 2 +- .../api/user_permission/tree/node_with_asset.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/apps/assets/utils/k8s.py b/apps/assets/utils/k8s.py index 19134440f..3c5452272 100644 --- a/apps/assets/utils/k8s.py +++ b/apps/assets/utils/k8s.py @@ -118,7 +118,7 @@ class KubernetesTree: def as_account_tree_node(self, account, parent_info): username = account.username - name = f'{account.name}({account.username})' + name = str(account) pid = urlencode({'asset_id': self.tree_id}) i = self.create_tree_id(pid, 'account', username) parent_info.update({'account': username}) diff --git a/apps/perms/api/user_permission/tree/node_with_asset.py b/apps/perms/api/user_permission/tree/node_with_asset.py index fd07972ff..98b7cd261 100644 --- a/apps/perms/api/user_permission/tree/node_with_asset.py +++ b/apps/perms/api/user_permission/tree/node_with_asset.py @@ -8,7 +8,7 @@ from rest_framework.request import Request from rest_framework.response import Response from rest_framework.generics import get_object_or_404 -from assets.models import Asset +from assets.models import Asset, Account from assets.utils import KubernetesTree from assets.api import SerializeToTreeNodeMixin from perms.hands import Node @@ -141,12 +141,19 @@ class UserGrantedK8sAsTreeApi( asset = get_object_or_404(Asset, **kwargs) return asset + def get_accounts(self, asset): + util = PermAccountUtil() + accounts = util.get_permed_accounts_for_user(self.user, asset) + ignore_username = [Account.AliasAccount.INPUT, Account.AliasAccount.USER] + accounts = filter(lambda x: x.username not in ignore_username, accounts) + accounts = list(accounts) + return accounts + def list(self, request: Request, *args, **kwargs): tree_id = request.query_params.get('tree_id') key = request.query_params.get('key', {}) tree = [] - util = PermAccountUtil() parent_info = dict(parse_qsl(key)) account_username = parent_info.get('account') @@ -155,7 +162,7 @@ class UserGrantedK8sAsTreeApi( if tree_id and not account_username: asset = self.asset(asset_id) - accounts = util.get_permed_accounts_for_user(self.user, asset) + accounts = self.get_accounts(asset) asset_node = KubernetesTree(tree_id).as_asset_tree_node(asset) tree.append(asset_node) for account in accounts: