Skip to content

Commit

Permalink
feature: modifies to new agent-group config api in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhengYa-0110 authored and SongZhen0704 committed Nov 26, 2024
1 parent 5092d7b commit 083fd8a
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 77 deletions.
154 changes: 96 additions & 58 deletions cli/ctl/agent_group_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -51,7 +48,7 @@ func RegisterAgentGroupConfigCommand() *cobra.Command {

var createFilename string
create := &cobra.Command{
Use: "create -f <filename>",
Use: "create <agent-group ID> -f <filename>",
Short: "create config",
Example: "deepflow-ctl agent-group-config create -f deepflow-config.yaml",
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -63,7 +60,7 @@ func RegisterAgentGroupConfigCommand() *cobra.Command {

var updateFilename string
update := &cobra.Command{
Use: "update -f <filename>",
Use: "update <agent-group ID> -f <filename>",
Short: "update agent-group config",
Example: "deepflow-ctl agent-group-config update -f deepflow-config.yaml",
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -74,7 +71,7 @@ func RegisterAgentGroupConfigCommand() *cobra.Command {
update.MarkFlagRequired("filename")

delete := &cobra.Command{
Use: "delete [agent-group ID]",
Use: "delete <agent-group ID>",
Short: "delete agent-group config",
Example: "deepflow-ctl agent-group-config delete g-xxxxxx",
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -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 {
Expand All @@ -109,19 +106,48 @@ 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 {
agentGroupShortUUID = args[0]
}

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, "",
Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions server/controller/http/router/agent_group_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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")
Expand Down
50 changes: 33 additions & 17 deletions server/controller/http/service/agent_group_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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})
Expand Down

0 comments on commit 083fd8a

Please sign in to comment.