From 0be549c34c2b79d0cc2271cdf8e2436ae2e8f2bd Mon Sep 17 00:00:00 2001 From: iSecloud <869820505@qq.com> Date: Mon, 15 Nov 2021 16:36:44 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E9=80=9A=E8=BF=87=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=8E=B7=E5=BE=97=E6=93=8D=E4=BD=9C=E7=B3=BB=E7=BB=9F=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B(close=20#230)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/node_man/constants.py | 2 +- apps/node_man/handlers/meta.py | 19 +++++++++++++++---- apps/node_man/tests/test_meta.py | 7 +------ dev_log/2.1.356/kiozhang_202111171615.yaml | 3 +++ frontend/src/config/config.ts | 15 ++++----------- frontend/src/router/index.ts | 9 +++++++++ frontend/src/store/modules/agent.ts | 6 ++---- frontend/src/store/modules/main.ts | 18 +++++++++++++++++- frontend/src/types/index.ts | 2 +- frontend/src/views/agent/agent-list.vue | 9 +++------ .../plugin/plugin-list/plugin-list-table.vue | 8 +++----- .../views/plugin/plugin-list/plugin-list.vue | 5 ----- .../plugin-rule-create/perform-preview.vue | 9 +++------ 13 files changed, 62 insertions(+), 50 deletions(-) create mode 100644 dev_log/2.1.356/kiozhang_202111171615.yaml diff --git a/apps/node_man/constants.py b/apps/node_man/constants.py index a8666e0da..ac6058758 100644 --- a/apps/node_man/constants.py +++ b/apps/node_man/constants.py @@ -103,7 +103,7 @@ def get_optional_items(cls) -> List[str]: OS_TUPLE = ("LINUX", "WINDOWS", "AIX", "SOLARIS") OS_CHOICES = tuple_choices(OS_TUPLE) OsType = choices_to_namedtuple(OS_CHOICES) -OS_CHN = {"WINDOWS": "Windows", "LINUX": "Linux", "AIX": "Aix", "SOLARIS": "Solaris"} +OS_CHN = {os_type: os_type if os_type == OsType.AIX else os_type.capitalize() for os_type in OS_TUPLE} BK_OS_TYPE = {"LINUX": "1", "WINDOWS": "2", "AIX": "3", "SOLARIS": "5"} # 操作系统->系统账户映射表 diff --git a/apps/node_man/handlers/meta.py b/apps/node_man/handlers/meta.py index a355f2bc4..274d01894 100644 --- a/apps/node_man/handlers/meta.py +++ b/apps/node_man/handlers/meta.py @@ -9,6 +9,7 @@ specific language governing permissions and limitations under the License. """ import re +from typing import Tuple from django.conf import settings from django.db import connection @@ -145,7 +146,7 @@ def fetch_host_condition(self): for index, item in enumerate(sublist): col_map[index].add(item) - os_types_children = [{"name": constants.OS_CHN.get(os, os), "id": os} for os in os_types if os != ""] + os_types_children = self.fetch_os_type_children(tuple(os_types)) statuses_children = [ {"name": constants.PROC_STATUS_CHN.get(status, status), "id": status} for status in statuses if status != "" ] @@ -354,9 +355,7 @@ def fetch_plugin_host_condition(self): special_os_type = [constants.OsType.AIX, constants.OsType.SOLARIS] if os_type in special_os_type and settings.BKAPP_RUN_ENV == constants.BkappRunEnvType.CE.value: continue - os_dict["children"].append( - {"id": os_type, "name": os_type if os_type == constants.OsType.AIX else os_type.capitalize()} - ) + os_dict["children"].append({"id": os_type, "name": constants.OS_CHN.get(os_type, os_type)}) ret_value.append(os_dict) @@ -437,6 +436,15 @@ def fetch_plugin_version_condition(): return plugin_result + @staticmethod + def fetch_os_type_children(os_types: Tuple = constants.OsType): + os_type_children = [] + for os_type in os_types: + if os_type == "": + continue + os_type_children.append({"id": os_type, "name": constants.OS_CHN.get(os_type, os_type)}) + return os_type_children + def filter_condition(self, category): """ 获取过滤条件 @@ -460,6 +468,9 @@ def filter_condition(self, category): elif category == "plugin_host": ret = self.fetch_plugin_host_condition() return ret + elif category == "os_type": + ret = self.fetch_os_type_children() + return ret def search(self, key): """ diff --git a/apps/node_man/tests/test_meta.py b/apps/node_man/tests/test_meta.py index 14cd017f4..c7fda208e 100644 --- a/apps/node_man/tests/test_meta.py +++ b/apps/node_man/tests/test_meta.py @@ -194,12 +194,7 @@ def test_fetch_plugin_list_condition_no_permission(self, *args, **kwargs): { "name": "操作系统", "id": "os_type", - "children": [ - {"id": "LINUX", "name": "Linux"}, - {"id": "WINDOWS", "name": "Windows"}, - {"id": "AIX", "name": "AIX"}, - {"id": "SOLARIS", "name": "Solaris"}, - ], + "children": MetaHandler.fetch_os_type_children(), }, ) self.assertEqual( diff --git a/dev_log/2.1.356/kiozhang_202111171615.yaml b/dev_log/2.1.356/kiozhang_202111171615.yaml new file mode 100644 index 000000000..c68a62854 --- /dev/null +++ b/dev_log/2.1.356/kiozhang_202111171615.yaml @@ -0,0 +1,3 @@ +--- +feature: + - "通过接口获得操作系统类型 (closed #230)" diff --git a/frontend/src/config/config.ts b/frontend/src/config/config.ts index 91da76dd6..ac90944c0 100644 --- a/frontend/src/config/config.ts +++ b/frontend/src/config/config.ts @@ -1,3 +1,5 @@ +import { MainStore } from '@/store'; + export interface IAuth { id: string name: string @@ -34,17 +36,8 @@ const getAuthentication = () => { } return auths; }; -const getSysOptions = () => { - const options: {id: string, name: string }[] = [ - { id: 'WINDOWS', name: 'Windows' }, - { id: 'LINUX', name: 'Linux' }, - ]; - if (window.PROJECT_CONFIG.BKAPP_RUN_ENV !== 'ce') { - options.push({ id: 'AIX', name: 'AIX' }, { id: 'SOLARIS', name: 'Solaris' }); - } - return options; -}; + export const authentication = getAuthentication(); export const defaultPort = window.PROJECT_CONFIG.DEFAULT_SSH_PORT ? Number(window.PROJECT_CONFIG.DEFAULT_SSH_PORT) : 22; -export const sysOptions = getSysOptions(); +export const sysOptions = MainStore.osList; export default authentication; diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 0d925333f..e190a4b03 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -45,6 +45,7 @@ const router = new VueRouter({ routes, }); +const loadOsRoute = ['agentSetup', 'agentImport', 'agentEdit']; const cancelRequest = async () => { const allRequest = http.queue.get() as any[]; const requestQueue = allRequest.filter(request => request.cancelWhenRouteChange); @@ -72,6 +73,14 @@ const beforeRouterMethod = async (to: Route, next: any) => { // 重置业务权限 MainStore.updateBizAction(authority ? authority.page : ''); await cancelRequest(); + if (!MainStore.osList) { + if (loadOsRoute.includes(to.name)) { + const list = await MainStore.getOsList(); + MainStore.updateOsList(list); + } else { + MainStore.getOsList().then(list => MainStore.updateOsList(list)); + } + } next(); }; diff --git a/frontend/src/store/modules/agent.ts b/frontend/src/store/modules/agent.ts index 2fa3f1897..cf6416155 100644 --- a/frontend/src/store/modules/agent.ts +++ b/frontend/src/store/modules/agent.ts @@ -162,10 +162,8 @@ export default class AgentStore extends VuexModule { * @param {*} params */ @Action - public async getFilterCondition() { - let data: ISearchItem[] = await getFilterCondition({ - category: 'host', - }).catch(() => []); + public async getFilterCondition(category = 'host') { + let data: ISearchItem[] = await getFilterCondition({ category }).catch(() => []); data = data.map((item) => { if (item.children && item.children.length) { item.multiable = true; diff --git a/frontend/src/store/modules/main.ts b/frontend/src/store/modules/main.ts index e8c28ded7..fd93a27af 100644 --- a/frontend/src/store/modules/main.ts +++ b/frontend/src/store/modules/main.ts @@ -3,7 +3,7 @@ import Vue from 'vue'; import http from '@/api'; import navList from '@/router/navigation-config'; import { retrieveBiz, fetchTopo } from '@/api/modules/cmdb'; -import { retrieveGlobalSettings } from '@/api/modules/meta'; +import { retrieveGlobalSettings, getFilterCondition } from '@/api/modules/meta'; import { fetchPublicKeys } from '@/api/modules/rsa'; import { fetchPermission, @@ -65,6 +65,8 @@ export default class Main extends VuexModule { // cache view public cacheViews: string[] = []; public routerBackName = ''; + public osList: any = null; + public osMap: Dictionary = null; // 公共 mutations /** @@ -274,6 +276,15 @@ export default class Main extends VuexModule { public updateRouterBackName(name = '') { this.routerBackName = name; } + @Mutation + public updateOsList(list = []) { + this.osList = list; + this.osMap = list.reduce((map, item) => { + map[item.id] = item.name; + return map; + }, {}); + } + /** * 获取用户信息 * @@ -379,4 +390,9 @@ export default class Main extends VuexModule { const data: IKeyItem[] = await fetchPublicKeys(params).catch(() => []); return data.find(item => item.name === 'DEFAULT') || {}; } + @Action + public async getOsList(category = 'os_type'): Promise { + const res = await getFilterCondition({ category }).catch(() => []); + return res || []; + } } diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.ts index cd4f45e41..1e3aa76d3 100644 --- a/frontend/src/types/index.ts +++ b/frontend/src/types/index.ts @@ -34,7 +34,7 @@ export interface IAuthApply { } export type RequestMethods = 'delete' | 'get' | 'head' | 'post' | 'put' | 'patch'; -export type IOs = 'LINUX' | 'WINDOWS' | 'AIX'; +export type IOs = 'LINUX' | 'WINDOWS' | 'AIX' | 'SOLARIS'; export interface IUserConfig { // http 请求默认 id diff --git a/frontend/src/views/agent/agent-list.vue b/frontend/src/views/agent/agent-list.vue index 0289d8947..d2feb51bc 100644 --- a/frontend/src/views/agent/agent-list.vue +++ b/frontend/src/views/agent/agent-list.vue @@ -691,12 +691,6 @@ export default class AgentList extends Mixins(pollMixin, TableHeaderMixins, auth terminated: this.$t('异常'), unknown: this.$t('未知'), }; - private osMap = { - LINUX: 'Linux', - WINDOWS: 'Windows', - AIX: 'AIX', - SOLARIS: 'Solaris', - }; // 批量操作 private operate: IOperateItem[] = [ { @@ -768,6 +762,9 @@ export default class AgentList extends Mixins(pollMixin, TableHeaderMixins, auth private operateBiz: IBizValue[] =[]; // 有操作权限的业务 private cloudAgentNum = 0; // 从云区域点击跳转过来的主机数量,区分是否因为权限问题看不到主机 + private get osMap() { + return MainStore.osMap; + } private get fontSize() { return MainStore.fontSize; } diff --git a/frontend/src/views/plugin/plugin-list/plugin-list-table.vue b/frontend/src/views/plugin/plugin-list/plugin-list-table.vue index 7d5d36083..3a0ad893e 100644 --- a/frontend/src/views/plugin/plugin-list/plugin-list-table.vue +++ b/frontend/src/views/plugin/plugin-list/plugin-list-table.vue @@ -202,11 +202,6 @@ export default class PluginRuleTable extends Mixins(FormLabelMixin, HeaderRender private currentHostId = -1; private currentHostStatus = ''; private showSlider = false; - private osMap = { - LINUX: 'Linux', - WINDOWS: 'Windows', - AIX: 'AIX', - }; // 本地存储Key private localMark = 'plugin_list_table'; private filterField: ITabelFliter[] = []; @@ -220,6 +215,9 @@ export default class PluginRuleTable extends Mixins(FormLabelMixin, HeaderRender failed: this.$t('异常'), // 失败 }; + private get osMap() { + return MainStore.osMap; + } private get selectionCount() { if (this.checkType === 'current') { return this.selections.length; diff --git a/frontend/src/views/plugin/plugin-list/plugin-list.vue b/frontend/src/views/plugin/plugin-list/plugin-list.vue index 66822b663..86ff9833f 100644 --- a/frontend/src/views/plugin/plugin-list/plugin-list.vue +++ b/frontend/src/views/plugin/plugin-list/plugin-list.vue @@ -140,11 +140,6 @@ export default class PluginList extends Mixins(HeaderFilterMixins) { private runningCount = 0; private checkValue: CheckValueEnum = 0; private hasOldRouteParams = false; - private osMap = { - LINUX: 'Linux', - WINDOWS: 'Windows', - AIX: 'AIX', - }; private strategyValue: Array = []; // 插件id number, 策略名称 string private sortData: ISortData = { head: '', diff --git a/frontend/src/views/plugin/plugin-rule/plugin-rule-create/perform-preview.vue b/frontend/src/views/plugin/plugin-rule/plugin-rule-create/perform-preview.vue index 392aa7456..fcba5f7c4 100644 --- a/frontend/src/views/plugin/plugin-rule/plugin-rule-create/perform-preview.vue +++ b/frontend/src/views/plugin/plugin-rule/plugin-rule-create/perform-preview.vue @@ -296,11 +296,6 @@ export default class PerformPreview extends Mixins(HeaderRenderMixin, HeaderFilt head: '', sort_type: '', }; - private osMap: Dictionary = { - LINUX: 'Linux', - WINDOWS: 'Windows', - AIX: 'AIX', - }; private statusMap = { running: window.i18n.t('正常'), terminated: window.i18n.t('异常'), @@ -308,7 +303,9 @@ export default class PerformPreview extends Mixins(HeaderRenderMixin, HeaderFilt }; public filterData: ISearchItem[] = []; - + private get osMap() { + return MainStore.osMap; + } private get statisticsList() { return [ { id: 'running', count: this.runningNum, path: '正常agent个数' },