From 44a9612c6f0e363801c6802ee57300e749580fa8 Mon Sep 17 00:00:00 2001 From: bixiaoyan Date: Tue, 10 Dec 2024 10:17:58 +0800 Subject: [PATCH] Optimize get location function --- models/resource.go | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/models/resource.go b/models/resource.go index 22aa492..1bd8fc6 100644 --- a/models/resource.go +++ b/models/resource.go @@ -1,6 +1,6 @@ /* * Copyright (c) KylinSoft Co., Ltd. 2024.All rights reserved. - * ha-api licensed under the Mulan Permissive Software License, Version 2. + * ha-api licensed under the Mulan Permissive Software License, Version 2. * See LICENSE file for more details. * Author: yangzhao_kl * Date: Fri Jan 8 20:56:40 2021 +0800 @@ -104,26 +104,11 @@ func GetResourceConstraints(rscID, relation string) (map[string]interface{}, err if rsc == rscID { rscConstraint := map[string]string{} score := resourceLocation.SelectAttrValue("score", "") - if score == "-INFINITY" || score == "-infinity" { - continue - } - if score == "INFINITY" || score == "infinity" { + if score == "-INFINITY" || score == "-infinity" || score == "INFINITY" || score == "infinity" { continue } rscConstraint["node"] = resourceLocation.SelectAttrValue("node", "") - if score == "20000" { - rscConstraint["level"] = "Master Node" - } else if score == "16000" { - //TODO implements func turnScoreToLevel - rscConstraint["level"] = "Slave 1" - } else if score == "15000" { - rscConstraint["level"] = "Slave 2" - } else if score == "14000" { - rscConstraint["level"] = "Slave 3" - } else if score == "13000" { - rscConstraint["level"] = "Slave 4" - } - + rscConstraint["level"] = getLevelFromScore(score) resourceLocations = append(resourceLocations, rscConstraint) } @@ -184,6 +169,23 @@ func GetResourceConstraints(rscID, relation string) (map[string]interface{}, err return retData, nil } +func getLevelFromScore(score string) string { + switch score { + case "20000": + return "Master Node" + case "16000": + return "Slave 1" + case "15000": + return "Slave 2" + case "14000": + return "Slave 3" + case "13000": + return "Slave 4" + default: + return "" + } +} + func GetResourceFailedMessage() map[string]map[string]string { out, err := utils.RunCommand(utils.CmdClusterStatusAsXML) failInfo := map[string]map[string]string{} -- Gitee