From 18990cc66417df4759d5a5a40dcb494137dad94f Mon Sep 17 00:00:00 2001 From: bixiaoyan Date: Fri, 14 Jun 2024 15:04:43 +0800 Subject: [PATCH] Add tag update function --- controllers/tag.go | 19 +++++++++++--- locale/zh_CN/LC_MESSAGES/ha-api.po | 5 +++- models/tag.go | 40 +++++++++++++++++++++++++++++- routers/router.go | 2 ++ 4 files changed, 60 insertions(+), 6 deletions(-) diff --git a/controllers/tag.go b/controllers/tag.go index 50831ee..7bc055f 100644 --- a/controllers/tag.go +++ b/controllers/tag.go @@ -2,7 +2,7 @@ * @Author: bixiaoyan bixiaoyan@kylinos.cn * @Date: 2024-03-13 15:04:41 * @LastEditors: bixiaoyan bixiaoyan@kylinos.cn - * @LastEditTime: 2024-03-25 11:39:36 + * @LastEditTime: 2024-06-14 15:03:57 * @FilePath: /ha-api/controllers/tag.go * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE */ @@ -33,19 +33,30 @@ type TagController struct { } func (tc *TagController) Get() { - tc.Data["json"] = models.GetTag() tc.ServeJSON() } func (tc *TagController) Post() { logs.Debug("handle Tag POST request") - jsonStr := tc.Ctx.Input.RequestBody tc.Data["json"] = models.SetTag(jsonStr) tc.ServeJSON() } - + +type TagUpdateController struct { + web.Controller +} + +func (tuc *TagUpdateController) Post() { + logs.Debug("handle TagUpdate POST request") + jsonStr := tuc.Ctx.Input.RequestBody + tagName := tuc.Ctx.Input.Param(":tag_name") + tuc.Data["json"] = models.UpdateTag(tagName, jsonStr) + tuc.ServeJSON() +} + + \ No newline at end of file diff --git a/locale/zh_CN/LC_MESSAGES/ha-api.po b/locale/zh_CN/LC_MESSAGES/ha-api.po index 1f8c51b..463ecdc 100644 --- a/locale/zh_CN/LC_MESSAGES/ha-api.po +++ b/locale/zh_CN/LC_MESSAGES/ha-api.po @@ -144,7 +144,7 @@ msgid "Generate script failed" msgstr "生成脚本失败" msgid "Tag get failed" -msgstr "tag信息获取失败" +msgstr "Tag信息获取失败" msgid "Parsing JSON failed" msgstr "JSON解析失败" @@ -152,6 +152,9 @@ msgstr "JSON解析失败" msgid "Add tag success" msgstr "Tag设置成功" +msgid "Update tag success" +msgstr "Tag编辑成功" + msgid "Node name error" msgstr "节点名称填写错误" diff --git a/models/tag.go b/models/tag.go index 02d26cd..cc1e101 100644 --- a/models/tag.go +++ b/models/tag.go @@ -18,7 +18,6 @@ import ( "encoding/json" "encoding/xml" "strings" - "gitee.com/openeuler/ha-api/utils" "github.com/chai2010/gettext-go" ) @@ -124,3 +123,42 @@ func SetTag(data []byte) TagPostResule { } return result } + +func UpdateTag(tagName string, data []byte) TagPostResule { + var result TagPostResule + // json数据解析 + if data == nil || len(data) == 0 { + result.Action = false + result.Error = gettext.Gettext("No input data") + return result + } + var TagData TagPostData + err := json.Unmarshal(data, &TagData) + if err != nil { + result.Action = false + result.Error = gettext.Gettext("Cannot convert data to json map") + return result + } + cmd_create := "pcs tag create " + string(tagName) + cmd_delete := "pcs tag delete " + string(tagName) + for _, res := range TagData.Tag_resource { + cmd_create = cmd_create + " " + string(res) + } + out1, err1 := utils.RunCommand(cmd_delete) + if err1 == nil { + out2, err2 := utils.RunCommand(cmd_create) + if err2 == nil { + result.Action = true + result.Info = gettext.Gettext("Update tag success") + } else { + result.Action = false + result.Error = string(out2) + } + + } else { + result.Action = false + result.Error = string(out1) + } + return result +} + diff --git a/routers/router.go b/routers/router.go index 37cb55b..5e79182 100644 --- a/routers/router.go +++ b/routers/router.go @@ -74,6 +74,7 @@ func init() { web.NSRouter("/:cluster_name/1/utilization", &controllers.UtilizationController{}), web.NSRouter("/:cluster_name/1/tag", &controllers.TagController{}), + web.NSRouter("/:cluster_name/1/tag/:tag_name", &controllers.TagUpdateController{}), web.NSRouter("/:cluster_name/1/rules", &controllers.RuleController{}), web.NSRouter("/:cluster_name/1/scripts", &controllers.ScriptsController{}), web.NSRouter("/remotescripts", &controllers.ScriptsRemoteController{}), @@ -118,6 +119,7 @@ func init() { web.NSRouter("/:cluster_name/1/utilization", &controllers.UtilizationController{}), web.NSRouter("/:cluster_name/1/tag", &controllers.TagController{}), + web.NSRouter("/:cluster_name/1/tag/:tag_name", &controllers.TagUpdateController{}), web.NSRouter("/:cluster_name/1/rules", &controllers.RuleController{}), web.NSRouter("/:cluster_name/1/scripts", &controllers.ScriptsController{}), ) -- Gitee