-
Notifications
You must be signed in to change notification settings - Fork 270
/
agent_group.go
161 lines (141 loc) · 4.75 KB
/
agent_group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (c) 2024 Yunshan Networks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ctl
import (
"fmt"
"os"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
"github.com/deepflowio/deepflow/cli/ctl/common"
"github.com/deepflowio/deepflow/cli/ctl/common/table"
)
func RegisterAgentGroupCommand() *cobra.Command {
agentGroup := &cobra.Command{
Use: "agent-group",
Short: "agent-group operation commands",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("please run with 'list | create | delete'.\n")
},
}
var listOutput string
list := &cobra.Command{
Use: "list [name]",
Short: "list agent-group info",
Example: "deepflow-ctl agent-group list deepflow-agent-group",
Run: func(cmd *cobra.Command, args []string) {
listAgentGroup(cmd, args, listOutput)
},
}
list.Flags().StringVarP(&listOutput, "output", "o", "", "output format")
var groupID string
create := &cobra.Command{
Use: "create <name>",
Short: "create agent-group",
Example: "deepflow-ctl agent-group create deepflow-agent-group",
Run: func(cmd *cobra.Command, args []string) {
createAgentGroup(cmd, args, groupID)
},
}
create.Flags().StringVar(&groupID, "id", "", "id must start with 'g-' prefix and have a length of 10 numbers and letters, such as g-1yhIguXABC")
delete := &cobra.Command{
Use: "delete [name]",
Short: "delete agent-group",
Example: "deepflow-ctl agent-group delete deepflow-agent-group",
Run: func(cmd *cobra.Command, args []string) {
deleteAgentGroup(cmd, args)
},
}
agentGroup.AddCommand(list)
agentGroup.AddCommand(create)
agentGroup.AddCommand(delete)
return agentGroup
}
func listAgentGroup(cmd *cobra.Command, args []string, output string) {
name := ""
if len(args) > 0 {
name = args[0]
}
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/vtap-groups/", server.IP, server.Port)
if name != "" {
url += fmt.Sprintf("?name=%s", name)
}
response, err := common.CURLPerform("GET", url, nil, "",
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
if output == "yaml" {
dataJson, _ := response.Get("DATA").MarshalJSON()
dataYaml, _ := yaml.JSONToYAML(dataJson)
fmt.Printf(string(dataYaml))
} else {
t := table.New()
t.SetHeader([]string{"NAME", "ID"})
tableItems := [][]string{}
for i := range response.Get("DATA").MustArray() {
group := response.Get("DATA").GetIndex(i)
tableItems = append(tableItems, []string{
group.Get("NAME").MustString(),
group.Get("SHORT_UUID").MustString(),
})
}
t.AppendBulk(tableItems)
t.Render()
}
}
func createAgentGroup(cmd *cobra.Command, args []string, groupID string) {
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "must specify name.\nExample: %s", cmd.Example)
return
}
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/vtap-groups/", server.IP, server.Port)
// 调用采集器组API,并输出返回结果
body := map[string]interface{}{"name": args[0], "group_id": groupID}
_, err := common.CURLPerform("POST", url, body, "",
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
}
func deleteAgentGroup(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Fprintln(os.Stderr, "must specify name.\nExample: %s", cmd.Example)
return
}
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/vtap-groups/?name=%s", server.IP, server.Port, args[0])
// 调用采集器组API,获取lcuuid
response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
if len(response.Get("DATA").MustArray()) > 0 {
group := response.Get("DATA").GetIndex(0)
lcuuid := group.Get("LCUUID").MustString()
url := fmt.Sprintf("http://%s:%d/v1/vtap-groups/%s/", server.IP, server.Port, lcuuid)
_, 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)
return
}
}
}