From 083fd8af3fd7ae29c41459a80f6f8768e8edaf2f Mon Sep 17 00:00:00 2001 From: zhengya Date: Tue, 22 Oct 2024 09:56:45 +0800 Subject: [PATCH] feature: modifies to new agent-group config api in cli --- cli/ctl/agent_group_config.go | 154 +++++++++++------- .../http/router/agent_group_config.go | 12 +- .../http/service/agent_group_config.go | 50 ++++-- 3 files changed, 139 insertions(+), 77 deletions(-) diff --git a/cli/ctl/agent_group_config.go b/cli/ctl/agent_group_config.go index dd8beef91d3..c1c922f764e 100644 --- a/cli/ctl/agent_group_config.go +++ b/cli/ctl/agent_group_config.go @@ -18,14 +18,11 @@ package ctl import ( "fmt" - "io/ioutil" "os" "github.com/spf13/cobra" - "sigs.k8s.io/yaml" "github.com/deepflowio/deepflow/cli/ctl/common" - "github.com/deepflowio/deepflow/cli/ctl/common/printutil" "github.com/deepflowio/deepflow/cli/ctl/common/table" ) @@ -51,7 +48,7 @@ func RegisterAgentGroupConfigCommand() *cobra.Command { var createFilename string create := &cobra.Command{ - Use: "create -f ", + Use: "create -f ", Short: "create config", Example: "deepflow-ctl agent-group-config create -f deepflow-config.yaml", Run: func(cmd *cobra.Command, args []string) { @@ -63,7 +60,7 @@ func RegisterAgentGroupConfigCommand() *cobra.Command { var updateFilename string update := &cobra.Command{ - Use: "update -f ", + Use: "update -f ", Short: "update agent-group config", Example: "deepflow-ctl agent-group-config update -f deepflow-config.yaml", Run: func(cmd *cobra.Command, args []string) { @@ -74,7 +71,7 @@ func RegisterAgentGroupConfigCommand() *cobra.Command { update.MarkFlagRequired("filename") delete := &cobra.Command{ - Use: "delete [agent-group ID]", + Use: "delete ", Short: "delete agent-group config", Example: "deepflow-ctl agent-group-config delete g-xxxxxx", Run: func(cmd *cobra.Command, args []string) { @@ -99,7 +96,7 @@ func RegisterAgentGroupConfigCommand() *cobra.Command { func exampleAgentGroupConfig(cmd *cobra.Command, args []string) { server := common.GetServerInfo(cmd) - url := fmt.Sprintf("http://%s:%d/v1/vtap-group-configuration/example/", server.IP, server.Port) + url := fmt.Sprintf("http://%s:%d/v1/agent-group-configuration/template/yaml", server.IP, server.Port) response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...) if err != nil { @@ -109,6 +106,30 @@ func exampleAgentGroupConfig(cmd *cobra.Command, args []string) { fmt.Println(response.Get("DATA").MustString()) } +type vtapInfo struct { + name string + shortUUID string +} + +func getAgentGroupInfos(cmd *cobra.Command, server *common.Server) (map[string]vtapInfo, error) { + url := fmt.Sprintf("http://%s:%d/v1/vtap-groups/", server.IP, server.Port) + response, err := common.CURLPerform("GET", url, nil, "", + []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...) + if err != nil { + return nil, err + } + + vtapLcuuidToInfo := map[string]vtapInfo{} + for i := range response.Get("DATA").MustArray() { + config := response.Get("DATA").GetIndex(i) + vtapLcuuidToInfo[config.Get("LCUUID").MustString()] = vtapInfo{ + name: config.Get("NAME").MustString(), + shortUUID: config.Get("SHORT_UUID").MustString(), + } + } + return vtapLcuuidToInfo, nil +} + func listAgentGroupConfig(cmd *cobra.Command, args []string, output string) { agentGroupShortUUID := "" if len(args) > 0 { @@ -116,12 +137,17 @@ func listAgentGroupConfig(cmd *cobra.Command, args []string, output string) { } server := common.GetServerInfo(cmd) - url := fmt.Sprintf("http://%s:%d/v1/vtap-group-configuration/", server.IP, server.Port) + url := fmt.Sprintf("http://%s:%d/v1/agent-group-configuration/", server.IP, server.Port) if output == "yaml" { if agentGroupShortUUID != "" { - url += fmt.Sprintf("filter/?vtap_group_id=%s", agentGroupShortUUID) + agentLcuuid, err := getAgentGroupLcuuid(cmd, server, agentGroupShortUUID) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return + } + url += fmt.Sprintf("%s/yaml", agentLcuuid) } else { - url += "advanced/" + url += "yaml" } response, err := common.CURLPerform("GET", url, nil, "", @@ -135,10 +161,20 @@ func listAgentGroupConfig(cmd *cobra.Command, args []string, output string) { fmt.Println(response.Get("DATA").MustString()) } else { for i := range response.Get("DATA").MustArray() { - fmt.Println(response.Get("DATA").GetIndex(i).MustString()) + config := response.Get("DATA").GetIndex(i) + fmt.Println() + fmt.Println("agent_group_lcuuid: ", config.Get("AGENT_GROUP_LCUUID").MustString()) + fmt.Println(config.Get("YAML").MustString()) } } } else { + vtapLcuuidToInfo, err := getAgentGroupInfos(cmd, server) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return + } + + url += "yaml" response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...) if err != nil { @@ -150,24 +186,48 @@ func listAgentGroupConfig(cmd *cobra.Command, args []string, output string) { t.SetHeader([]string{"NAME", "AGENT_GROUP_ID"}) tableItems := [][]string{} for i := range response.Get("DATA").MustArray() { - config := response.Get("DATA").GetIndex(i) - if agentGroupShortUUID != "" && config.Get("VTAP_GROUP_ID").MustString() != agentGroupShortUUID { - continue + agentGroupLcuuid := response.Get("DATA").GetIndex(i).Get("AGENT_GROUP_LCUUID") + if info, ok := vtapLcuuidToInfo[agentGroupLcuuid.MustString()]; ok { + tableItems = append(tableItems, []string{ + info.name, + info.shortUUID, + }) } - tableItems = append(tableItems, []string{ - config.Get("VTAP_GROUP_NAME").MustString(), - config.Get("VTAP_GROUP_ID").MustString(), - }) } t.AppendBulk(tableItems) t.Render() } } +func getAgentGroupLcuuid(cmd *cobra.Command, server *common.Server, shortUUID string) (string, error) { + url := fmt.Sprintf("http://%s:%d/v1/vtap-groups/?short_uuid=%s", server.IP, server.Port, shortUUID) + response, err := common.CURLPerform("GET", url, nil, "", + []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...) + if err != nil { + return "", err + } + + if len(response.Get("DATA").MustArray()) == 0 { + return "", fmt.Errorf("agent-group (%s) not exist\n", shortUUID) + } + return response.Get("DATA").GetIndex(0).Get("LCUUID").MustString(), nil +} + func createAgentGroupConfig(cmd *cobra.Command, args []string, createFilename string) { + if len(args) == 0 { + fmt.Fprintln(os.Stderr, "must specify agent-group ID.\nExample: %s", cmd.Example) + return + } server := common.GetServerInfo(cmd) - url := fmt.Sprintf("http://%s:%d/v1/vtap-group-configuration/advanced/", server.IP, server.Port) - yamlFile, err := ioutil.ReadFile(createFilename) + + agentGroupLcuuid, err := getAgentGroupLcuuid(cmd, server, args[0]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return + } + url := fmt.Sprintf("http://%s:%d/v1/agent-group-configuration/%s/yaml", server.IP, server.Port, agentGroupLcuuid) + + yamlFile, err := os.ReadFile(createFilename) if err != nil { fmt.Fprintln(os.Stderr, err) } @@ -180,51 +240,26 @@ func createAgentGroupConfig(cmd *cobra.Command, args []string, createFilename st } func updateAgentGroupConfig(cmd *cobra.Command, args []string, updateFilename string) { - yamlFile, err := os.ReadFile(updateFilename) - if err != nil { - fmt.Fprintln(os.Stderr, err) + if len(args) == 0 { + fmt.Fprintln(os.Stderr, "must specify agent-group ID.\nExample: %s", cmd.Example) return } + server := common.GetServerInfo(cmd) - updateMap := make(map[string]interface{}) - err = yaml.Unmarshal(yamlFile, &updateMap) + agentGroupLcuuid, err := getAgentGroupLcuuid(cmd, server, args[0]) if err != nil { fmt.Fprintln(os.Stderr, err) return } + url := fmt.Sprintf("http://%s:%d/v1/agent-group-configuration/%s/yaml", server.IP, server.Port, agentGroupLcuuid) - var agentGroupID interface{} - if id, ok := updateMap["vtap_group_id"]; ok { - agentGroupID = id - printutil.WarnWithColor("use agent_group_id instead of vtap_group_id") - } - id, ok := updateMap["agent_group_id"] - if ok { - agentGroupID = id - } - if agentGroupID == nil { - fmt.Fprintln(os.Stderr, "must specify agent_group_id") - return - } - server := common.GetServerInfo(cmd) - url := fmt.Sprintf("http://%s:%d/v1/vtap-group-configuration/?vtap_group_id=%s", server.IP, server.Port, agentGroupID) - // call vtap-group api, get lcuuid - response, err := common.CURLPerform("GET", url, nil, "", - []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...) + yamlFile, err := os.ReadFile(updateFilename) if err != nil { fmt.Fprintln(os.Stderr, err) return } - if len(response.Get("DATA").MustArray()) == 0 { - fmt.Fprintln(os.Stderr, "agent-group (%s) not exist\n") - } - group := response.Get("DATA").GetIndex(0) - lcuuid := group.Get("LCUUID").MustString() - - // call vtap-group config update api - url = fmt.Sprintf("http://%s:%d/v1/vtap-group-configuration/advanced/%s/", server.IP, server.Port, lcuuid) - _, err = common.CURLPerform("PATCH", url, nil, string(yamlFile), + _, err = common.CURLPerform("PUT", url, nil, string(yamlFile), []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...) if err != nil { fmt.Fprintln(os.Stderr, err) @@ -237,13 +272,16 @@ func deleteAgentGroupConfig(cmd *cobra.Command, args []string) { fmt.Fprintln(os.Stderr, "must specify agent-group ID.\nExample: %s", cmd.Example) return } - server := common.GetServerInfo(cmd) - url := fmt.Sprintf( - "http://%s:%d/v1/vtap-group-configuration/filter/?vtap_group_id=%s", - server.IP, server.Port, args[0], - ) - _, err := common.CURLPerform("DELETE", url, nil, "", + + agentGroupLcuuid, err := getAgentGroupLcuuid(cmd, server, args[0]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return + } + url := fmt.Sprintf("http://%s:%d/v1/agent-group-configuration/%s", server.IP, server.Port, agentGroupLcuuid) + + _, err = common.CURLPerform("DELETE", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...) if err != nil { fmt.Fprintln(os.Stderr, err) diff --git a/server/controller/http/router/agent_group_config.go b/server/controller/http/router/agent_group_config.go index 34c0146a1fa..85cbf5f094f 100644 --- a/server/controller/http/router/agent_group_config.go +++ b/server/controller/http/router/agent_group_config.go @@ -47,6 +47,7 @@ func (cgc *AgentGroupConfig) RegisterTo(e *gin.Engine) { e.POST("/v1/agent-group-configuration/:group-lcuuid/json", postJsonAgentGroupConfig(cgc.cfg)) e.PUT("/v1/agent-group-configuration/:group-lcuuid/json", putJsonAgentGroupConfig(cgc.cfg)) + e.GET("/v1/agent-group-configuration/yaml", getYAMLAgentGroupConfigs(cgc.cfg)) e.GET("/v1/agent-group-configuration/:group-lcuuid/yaml", getYAMLAgentGroupConfig(cgc.cfg)) e.POST("/v1/agent-group-configuration/:group-lcuuid/yaml", postYAMLAgentGroupConfig(cgc.cfg)) e.PUT("/v1/agent-group-configuration/:group-lcuuid/yaml", putYAMLAgentGroupConfig(cgc.cfg)) @@ -76,8 +77,8 @@ func getJsonAgentGroupConfig(cfg *config.ControllerConfig) gin.HandlerFunc { func getJsonAgentGroupConfigs(cfg *config.ControllerConfig) gin.HandlerFunc { return func(c *gin.Context) { - data, err := service.NewAgentGroupConfig(common.GetUserInfo(c), cfg).GetAgentGroupConfigs() - routercommon.JsonResponse(c, data, err) + data, err := service.NewAgentGroupConfig(common.GetUserInfo(c), cfg).GetAgentGroupConfigs(service.DataTypeJSON) + routercommon.JsonResponse(c, data.([]byte), err) } } @@ -107,6 +108,13 @@ func putJsonAgentGroupConfig(cfg *config.ControllerConfig) gin.HandlerFunc { } } +func getYAMLAgentGroupConfigs(cfg *config.ControllerConfig) gin.HandlerFunc { + return func(c *gin.Context) { + data, err := service.NewAgentGroupConfig(common.GetUserInfo(c), cfg).GetAgentGroupConfigs(service.DataTypeYAML) + routercommon.JsonResponse(c, data, err) + } +} + func getYAMLAgentGroupConfig(cfg *config.ControllerConfig) gin.HandlerFunc { return func(c *gin.Context) { groupLcuuid := c.Param("group-lcuuid") diff --git a/server/controller/http/service/agent_group_config.go b/server/controller/http/service/agent_group_config.go index 3d7c06f9199..315aa7a6dda 100644 --- a/server/controller/http/service/agent_group_config.go +++ b/server/controller/http/service/agent_group_config.go @@ -193,7 +193,7 @@ func (a *AgentGroupConfig) strToBytes(data string, returnType int) ([]byte, erro } } -func (a *AgentGroupConfig) GetAgentGroupConfigs() ([]byte, error) { +func (a *AgentGroupConfig) GetAgentGroupConfigs(dataType int) (interface{}, error) { dbInfo, err := mysql.GetDB(a.resourceAccess.UserInfo.ORGID) if err != nil { return nil, err @@ -202,21 +202,25 @@ func (a *AgentGroupConfig) GetAgentGroupConfigs() ([]byte, error) { if err := dbInfo.Find(&data).Error; err != nil { return nil, err } - var jsonArray bytes.Buffer - jsonArray.WriteByte('{') - for i, d := range data { - if i > 0 { - jsonArray.WriteByte(',') - } - bs, err := a.strToBytes(d.Yaml, DataTypeJSON) - if err != nil { - return nil, err + if dataType == DataTypeJSON { + var jsonArray bytes.Buffer + jsonArray.WriteByte('{') + for i, d := range data { + if i > 0 { + jsonArray.WriteByte(',') + } + bs, err := a.strToBytes(d.Yaml, dataType) + if err != nil { + return nil, err + } + jsonArray.WriteString(`"` + d.AgentGroupLcuuid + `":`) + jsonArray.Write(bs) } - jsonArray.WriteString(`"` + d.AgentGroupLcuuid + `":`) - jsonArray.Write(bs) + jsonArray.WriteByte('}') + return jsonArray.Bytes(), nil + } else { + return data, nil } - jsonArray.WriteByte('}') - return jsonArray.Bytes(), nil } func (a *AgentGroupConfig) getStringYaml(data interface{}, dataType int) (string, error) { @@ -270,8 +274,9 @@ func (a *AgentGroupConfig) CreateAgentGroupConfig(groupLcuuid string, data inter return nil, err } - refresh.RefreshCache(dbInfo.GetORGID(), []common.DataChanged{common.DATA_CHANGED_VTAP}) a.compatibleWithOldVersion(dbInfo, groupLcuuid, strYaml) + + refresh.RefreshCache(dbInfo.GetORGID(), []common.DataChanged{common.DATA_CHANGED_VTAP}) return a.GetAgentGroupConfig(groupLcuuid, dataType) } @@ -337,8 +342,9 @@ func (a *AgentGroupConfig) UpdateAgentGroupConfig(groupLcuuid string, data inter return nil, err } - refresh.RefreshCache(dbInfo.GetORGID(), []common.DataChanged{common.DATA_CHANGED_VTAP}) a.compatibleWithOldVersion(dbInfo, groupLcuuid, strYaml) + + refresh.RefreshCache(dbInfo.GetORGID(), []common.DataChanged{common.DATA_CHANGED_VTAP}) return a.GetAgentGroupConfig(groupLcuuid, dataType) } @@ -348,7 +354,17 @@ func (a *AgentGroupConfig) DeleteAgentGroupConfig(groupLcuuid string) error { return err } - if err := dbInfo.Where("agent_group_lcuuid = ?", groupLcuuid).Delete(&agentconf.MySQLAgentGroupConfiguration{}).Error; err != nil { + db := dbInfo.GetGORMDB() + err = db.Transaction(func(tx *gorm.DB) error { + if err := tx.Where("agent_group_lcuuid = ?", groupLcuuid).Delete(&agentconf.MySQLAgentGroupConfiguration{}).Error; err != nil { + return fmt.Errorf("failed to delete agent_group_configuration (agent group lcuuid %s): %v", groupLcuuid, err) + } + if err := tx.Where("vtap_group_lcuuid = ?", groupLcuuid).Delete(&agentconf.AgentGroupConfigModel{}).Error; err != nil { + return fmt.Errorf("failed to delete vtap_group_configuration (agent group lcuuid %s): %v", groupLcuuid, err) + } + return nil + }) + if err != nil { return err } refresh.RefreshCache(dbInfo.GetORGID(), []common.DataChanged{common.DATA_CHANGED_VTAP})