From 62fa3eb7eda0664cfcc7e0161814059815ea83ca Mon Sep 17 00:00:00 2001 From: nyr Date: Thu, 28 Sep 2023 00:33:25 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0POST=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E9=9B=86=E7=BE=A4=EF=BC=8C=E5=BE=85=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/cluster.go | 20 ++++++++++++++++++++ models/manage_clusters.go | 32 +++++++++++++++++++++++++++++++- routers/router.go | 1 + 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/controllers/cluster.go b/controllers/cluster.go index ba6ce6e..1266529 100644 --- a/controllers/cluster.go +++ b/controllers/cluster.go @@ -46,6 +46,10 @@ type ClustersStatusController struct { web.Controller } +type ClusterRemoveController struct { + web.Controller +} + func (mcc *MultipleClustersController) Post() { logs.Debug("Handle post request in MultipleClustersController.") result := map[string]interface{}{} @@ -129,6 +133,22 @@ func (cd *ClusterDestroyController) Post() { cd.ServeJSON() } +func (crc *ClusterRemoveController) Post() { + logs.Debug("handle post request in ClustersController.") + result := map[string]interface{}{} + reqData := make(map[string]interface{}) + if err := json.Unmarshal(crc.Ctx.Input.RequestBody, &reqData); err != nil { + result = make(map[string]interface{}) + result["action"] = false + result["error"] = "invalid input data" + } else { + result = models.ClusterRemove(reqData) + } + + crc.Data["json"] = &result + crc.ServeJSON() +} + func (cc *ClustersController) Put() { logs.Debug("handle put request in ClustersController.") result := map[string]interface{}{} diff --git a/models/manage_clusters.go b/models/manage_clusters.go index 9828e0a..beebb6e 100644 --- a/models/manage_clusters.go +++ b/models/manage_clusters.go @@ -96,6 +96,17 @@ func (ci *ClusterInfo) SetVersion(version int) { ci.Version = version } +func (ci *ClusterInfo) DeleteCluster(clusterNameJson string) bool { + for i, c := range ci.Clusters { + cV := c.(map[string]interface{}) + if cV["cluster_name"] == clusterNameJson { + ci.Clusters = append(ci.Clusters[:i], ci.Clusters[i+1:]...) + return true + } + } + return false +} + // localClusterInfo retrieves the cluster information locally and returns it as a map. // If no cluster exists, an empty map is returned. func localClusterInfo() map[string]interface{} { @@ -172,7 +183,7 @@ func readFile(filename string) map[string]interface{} { return newDict } -//comment out due to type error as localconf could not be {}, it should be of type *ClusterInfo +// comment out due to type error as localconf could not be {}, it should be of type *ClusterInfo // SyncConfig synchronizes the local configuration with remote configuration. // Returns appropriate results indicating the synchronization status. func SyncConfig(remoteConf map[string]interface{}) map[string]interface{} { @@ -360,3 +371,22 @@ func ClustersDestroy() map[string]interface{} { res["message"] = string(out) return res } + +func ClusterRemove(clusterNameJson map[string]interface{}) map[string]interface{} { + //clusters := clusterNameJson["cluster_name"] + + clusters := make([]string, 0) + clusterNameJson["cluster name"] = clusters + localConf := getLocalConf() + removeRes := make([]bool, 0) + + for _, cluster := range clusters { + res := localConf.DeleteCluster(cluster) + removeRes = append(removeRes, res) + localConf.Save() + syncClusterConfFile(localConf) + } + return map[string]interface{}{ + "data": removeRes, + } +} diff --git a/routers/router.go b/routers/router.go index 83fa75f..25503b3 100644 --- a/routers/router.go +++ b/routers/router.go @@ -38,6 +38,7 @@ func init() { web.NSRouter("/managec/sync_config", &controllers.Sync_configController{}), web.NSRouter("/managec/cluster_setup", &controllers.ClusterSetupController{}), web.NSRouter("/managec/cluster_destroy", &controllers.ClusterDestroyController{}), + web.NSRouter("/managec/cluster_remove", &controllers.ClusterRemoveController{}), web.NSRouter("/haclusters/1/resources", &controllers.ResourceController{}), web.NSRouter("/haclusters/1/resources/:rscID/:action", &controllers.ResourceActionController{}), -- Gitee From be42ab694c508eed084f9961f4c0bf47d0650b6d Mon Sep 17 00:00:00 2001 From: nyr Date: Wed, 4 Oct 2023 22:25:07 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=BB=86=E8=8A=82=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=8Cpost=E7=A7=BB=E9=99=A4=E9=9B=86=E7=BE=A4=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/manage_clusters.go | 32 +++++++------------------------- models/nodes_management.go | 14 ++++++++++++++ routers/router.go | 1 + 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/models/manage_clusters.go b/models/manage_clusters.go index beebb6e..8a9c2d1 100644 --- a/models/manage_clusters.go +++ b/models/manage_clusters.go @@ -101,6 +101,7 @@ func (ci *ClusterInfo) DeleteCluster(clusterNameJson string) bool { cV := c.(map[string]interface{}) if cV["cluster_name"] == clusterNameJson { ci.Clusters = append(ci.Clusters[:i], ci.Clusters[i+1:]...) + fmt.Println(ci.Clusters) return true } } @@ -317,11 +318,6 @@ func ClusterAdd(nodeInfo map[string]interface{}) map[string]interface{} { "error": "添加集群失败"} } -type SetClusterInfo struct { - ClusterName string - Data []map[string]interface{} -} - // ClusterSetup performs the setup of a cluster using the provided cluster information. func ClusterSetup(clusterInfo map[string]interface{}) map[string]interface{} { authInfo := make(map[string]interface{}) @@ -358,30 +354,16 @@ func ClusterSetup(clusterInfo map[string]interface{}) map[string]interface{} { } } -func ClustersDestroy() map[string]interface{} { - res := map[string]interface{}{} - cmd := "pcs cluster destroy --all" - out, err := utils.RunCommand(cmd) - if err != nil { - res["action"] = false - res["error"] = string(out) - return res - } - res["action"] = true - res["message"] = string(out) - return res -} - func ClusterRemove(clusterNameJson map[string]interface{}) map[string]interface{} { - //clusters := clusterNameJson["cluster_name"] - - clusters := make([]string, 0) - clusterNameJson["cluster name"] = clusters + clusters := clusterNameJson["cluster_name"] + //clusters = clusters.([]interface{}) + //clusters := make([]string, 0) + //clusterNameJson["cluster name"] = clusters localConf := getLocalConf() removeRes := make([]bool, 0) - for _, cluster := range clusters { - res := localConf.DeleteCluster(cluster) + for _, cluster := range clusters.([]interface{}) { + res := localConf.DeleteCluster(cluster.(string)) removeRes = append(removeRes, res) localConf.Save() syncClusterConfFile(localConf) diff --git a/models/nodes_management.go b/models/nodes_management.go index 7adf7a5..9b3b3ab 100644 --- a/models/nodes_management.go +++ b/models/nodes_management.go @@ -168,6 +168,20 @@ func generateNodeCmdStr(nodesInfo []interface{}) string { return cmd.String() } +func ClustersDestroy() map[string]interface{} { + res := map[string]interface{}{} + cmd := "pcs cluster destroy --all" + out, err := utils.RunCommand(cmd) + if err != nil { + res["action"] = false + res["error"] = string(out) + return res + } + res["action"] = true + res["message"] = string(out) + return res +} + // isIPv4 checks if the provided string is a valid IPv4 address. // Returns true if the string is a valid IPv4 address, false otherwise. func isIPv4(ip string) bool { diff --git a/routers/router.go b/routers/router.go index 25503b3..772a503 100644 --- a/routers/router.go +++ b/routers/router.go @@ -32,6 +32,7 @@ func init() { ns := web.NewNamespace("/api/v1", web.NSRouter("/haclusters/1", &controllers.ClustersController{}), + web.NSRouter("/haclusters/1/cluster_status", &controllers.ClustersStatusController{}), web.NSRouter("/login", &controllers.LoginController{}), web.NSRouter("/logout", &controllers.LogoutController{}), web.NSRouter("/managec/cluster_add", &controllers.MultipleClustersController{}), -- Gitee From 8ac225bb7225ffb4200b1405773374c9f55a57b4 Mon Sep 17 00:00:00 2001 From: nyr Date: Sun, 8 Oct 2023 14:31:05 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E8=B0=83=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/manage_clusters.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/models/manage_clusters.go b/models/manage_clusters.go index 8a9c2d1..a5439fe 100644 --- a/models/manage_clusters.go +++ b/models/manage_clusters.go @@ -101,7 +101,6 @@ func (ci *ClusterInfo) DeleteCluster(clusterNameJson string) bool { cV := c.(map[string]interface{}) if cV["cluster_name"] == clusterNameJson { ci.Clusters = append(ci.Clusters[:i], ci.Clusters[i+1:]...) - fmt.Println(ci.Clusters) return true } } @@ -356,19 +355,22 @@ func ClusterSetup(clusterInfo map[string]interface{}) map[string]interface{} { func ClusterRemove(clusterNameJson map[string]interface{}) map[string]interface{} { clusters := clusterNameJson["cluster_name"] - //clusters = clusters.([]interface{}) - //clusters := make([]string, 0) - //clusterNameJson["cluster name"] = clusters localConf := getLocalConf() removeRes := make([]bool, 0) + faildCluster := make([]interface{}, 0) for _, cluster := range clusters.([]interface{}) { res := localConf.DeleteCluster(cluster.(string)) removeRes = append(removeRes, res) + if res == false { + faildCluster = append(faildCluster, cluster) + } localConf.Save() syncClusterConfFile(localConf) } return map[string]interface{}{ - "data": removeRes, + "action": true, + "faild_cluster": faildCluster, + "data": removeRes, } } -- Gitee