Skip to content

Commit

Permalink
[Server] add org id to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
roryye authored and SongZhen0704 committed May 8, 2024
1 parent 75e1645 commit 75578ac
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 51 deletions.
39 changes: 26 additions & 13 deletions cli/ctl/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ func listAgentUpgrade(cmd *cobra.Command, args []string) {
url := fmt.Sprintf("http://%s:%d/v1/vtaps/", server.IP, server.Port)

// 调用采集器API,并输出返回结果
response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
response, err := common.CURLPerform("GET", url, nil, "",
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
fmt.Println(err)
return
Expand Down Expand Up @@ -208,7 +209,8 @@ func listAgent(cmd *cobra.Command, args []string, output string) {
}

// 调用采集器API,并输出返回结果
response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
response, err := common.CURLPerform("GET", url, nil, "",
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
fmt.Println(err)
return
Expand Down Expand Up @@ -264,7 +266,8 @@ func deleteAgent(cmd *cobra.Command, args []string) {
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/vtaps/?name=%s", server.IP, server.Port, args[0])
// curl vtap API,get lcuuid
response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
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
Expand All @@ -274,7 +277,8 @@ func deleteAgent(cmd *cobra.Command, args []string) {
lcuuid := response.Get("DATA").GetIndex(0).Get("LCUUID").MustString()
url := fmt.Sprintf("http://%s:%d/v1/vtaps/%s/", server.IP, server.Port, lcuuid)
// call vtap delete api
_, err := common.CURLPerform("DELETE", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
_, 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
Expand Down Expand Up @@ -305,7 +309,8 @@ func updateAgent(cmd *cobra.Command, args []string, updateFilename string) {
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/vtaps/?name=%s", server.IP, server.Port, vtapName)
// call vtap api, get lcuuid
response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
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
Expand All @@ -325,7 +330,8 @@ func updateAgent(cmd *cobra.Command, args []string, updateFilename string) {
updateBody["TAP_MODE"] = common.GetVtapTapModeByName(tapMode.(string))

updateJson, _ := json.Marshal(updateBody)
_, err := common.CURLPerform("PATCH", url, nil, string(updateJson), []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
_, err := common.CURLPerform("PATCH", url, nil, string(updateJson),
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
Expand All @@ -350,7 +356,8 @@ func updateAgent(cmd *cobra.Command, args []string, updateFilename string) {
if agentGroupID != nil {
url := fmt.Sprintf("http://%s:%d/v1/vtap-groups/?short_uuid=%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))}...)
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
Expand All @@ -374,7 +381,8 @@ func updateAgent(cmd *cobra.Command, args []string, updateFilename string) {
// call vtap update api
updateJson, _ := json.Marshal(updateMap)
url = fmt.Sprintf("http://%s:%d/v1/vtaps/%s/", server.IP, server.Port, lcuuid)
_, err = common.CURLPerform("PATCH", url, nil, string(updateJson), []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
_, err = common.CURLPerform("PATCH", url, nil, string(updateJson),
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
Expand Down Expand Up @@ -402,7 +410,8 @@ func upgadeAgent(cmd *cobra.Command, args []string) {

server := common.GetServerInfo(cmd)
serverURL := fmt.Sprintf("http://%s:%d/v1/controllers/", server.IP, server.Port)
response, err := common.CURLPerform("GET", serverURL, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
response, err := common.CURLPerform("GET", serverURL, nil, "",
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
Expand All @@ -425,7 +434,8 @@ func upgadeAgent(cmd *cobra.Command, args []string) {
}

vtapURL := fmt.Sprintf("http://%s:%d/v1/vtaps/?name=%s", server.IP, server.Port, vtapName)
response, err = common.CURLPerform("GET", vtapURL, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
response, err = common.CURLPerform("GET", vtapURL, nil, "",
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
Expand Down Expand Up @@ -455,7 +465,8 @@ func upgadeAgent(cmd *cobra.Command, args []string) {
}
for host, _ := range hosts {
url := fmt.Sprintf(url_format, host, server.Port, vtapLcuuid)
response, err := common.CURLPerform("PATCH", url, body, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
response, err := common.CURLPerform("PATCH", url, body, "",
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
fmt.Printf("upgrade agent %s server %s failed, response: %s\n", vtapName, host, response)
Expand Down Expand Up @@ -493,7 +504,8 @@ func rebalance(cmd *cobra.Command, rebalanceType RebalanceType, typeVal string)
func ifNeedRebalance(cmd *cobra.Command, typeStr string) (bool, error) {
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/rebalance-vtap/?check=false&type=%s", server.IP, server.Port, typeStr)
resp, err := common.CURLPerform("POST", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
resp, err := common.CURLPerform("POST", url, nil, "",
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
return false, err
}
Expand All @@ -506,7 +518,8 @@ func ifNeedRebalance(cmd *cobra.Command, typeStr string) (bool, error) {
func execRebalance(cmd *cobra.Command, typeStr string) (*simplejson.Json, error) {
server := common.GetServerInfo(cmd)
url := fmt.Sprintf("http://%s:%d/v1/rebalance-vtap/?check=true&type=%s", server.IP, server.Port, typeStr)
resp, err := common.CURLPerform("POST", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
resp, err := common.CURLPerform("POST", url, nil, "",
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
return nil, err
}
Expand Down
9 changes: 6 additions & 3 deletions cli/ctl/agent_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func listAgentGroup(cmd *cobra.Command, args []string, output string) {
url += fmt.Sprintf("?name=%s", name)
}

response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
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
Expand Down Expand Up @@ -122,7 +123,8 @@ func createAgentGroup(cmd *cobra.Command, args []string, groupID string) {

// 调用采集器组API,并输出返回结果
body := map[string]interface{}{"name": args[0], "group_id": groupID}
_, err := common.CURLPerform("POST", url, body, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
_, 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
Expand All @@ -149,7 +151,8 @@ func deleteAgentGroup(cmd *cobra.Command, args []string) {
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))}...)
_, 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
Expand Down
21 changes: 14 additions & 7 deletions cli/ctl/agent_group_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ 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)
response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
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
Expand All @@ -123,7 +124,8 @@ func listAgentGroupConfig(cmd *cobra.Command, args []string, output string) {
url += "advanced/"
}

response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
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
Expand All @@ -137,7 +139,8 @@ func listAgentGroupConfig(cmd *cobra.Command, args []string, output string) {
}
}
} else {
response, err := common.CURLPerform("GET", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
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
Expand Down Expand Up @@ -168,7 +171,8 @@ func createAgentGroupConfig(cmd *cobra.Command, args []string, createFilename st
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
_, err = common.CURLPerform("POST", url, nil, string(yamlFile), []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
_, err = common.CURLPerform("POST", url, nil, string(yamlFile),
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
Expand Down Expand Up @@ -205,7 +209,8 @@ func updateAgentGroupConfig(cmd *cobra.Command, args []string, updateFilename st
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))}...)
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
Expand All @@ -219,7 +224,8 @@ func updateAgentGroupConfig(cmd *cobra.Command, args []string, updateFilename st

// 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), []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
_, err = common.CURLPerform("PATCH", url, nil, string(yamlFile),
[]common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd)), common.WithORGID(common.GetORGID(cmd))}...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
Expand All @@ -237,7 +243,8 @@ func deleteAgentGroupConfig(cmd *cobra.Command, args []string) {
"http://%s:%d/v1/vtap-group-configuration/filter/?vtap_group_id=%s",
server.IP, server.Port, args[0],
)
_, err := common.CURLPerform("DELETE", url, nil, "", []common.HTTPOption{common.WithTimeout(common.GetTimeout(cmd))}...)
_, 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
Expand Down
2 changes: 2 additions & 0 deletions cli/ctl/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/spf13/cobra"

"github.com/deepflowio/deepflow/cli/ctl/common"
ctrlcommon "github.com/deepflowio/deepflow/server/controller/common"
"github.com/deepflowio/deepflow/server/ingester/ingesterctl/cmd"
)

Expand All @@ -42,6 +43,7 @@ func Execute(version string) {
root.PersistentFlags().Uint32P("api-port", "", 30417, "deepflow-server service node port")
root.PersistentFlags().Uint32P("rpc-port", "", 30035, "deepflow-server service grpc port")
root.PersistentFlags().Uint32P("svc-port", "", 20417, "deepflow-server service http port")
root.PersistentFlags().Uint32P("org-id", "", ctrlcommon.DEFAULT_ORG_ID, fmt.Sprintf("organization id (default %d)", ctrlcommon.DEFAULT_ORG_ID))
root.PersistentFlags().DurationP("timeout", "", time.Second*30, "deepflow-ctl timeout")
root.ParseFlags(os.Args[1:])

Expand Down
18 changes: 18 additions & 0 deletions cli/ctl/common/http_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ import (
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"

ctrlcommon "github.com/deepflowio/deepflow/server/controller/common"

simplejson "github.com/bitly/go-simplejson"
"github.com/spf13/cobra"
_ "github.com/vishvananda/netlink"
Expand All @@ -39,6 +42,7 @@ type Filter map[string]interface{}

type HTTPConf struct {
Timeout time.Duration
ORGID int
}

type HTTPOption func(*HTTPConf)
Expand All @@ -49,6 +53,12 @@ func WithTimeout(t time.Duration) HTTPOption {
}
}

func WithORGID(orgID int) HTTPOption {
return func(h *HTTPConf) {
h.ORGID = orgID
}
}

// 功能:调用其他模块API并获取返回结果
func CURLPerform(method string, url string, body map[string]interface{}, strBody string, opts ...HTTPOption) (*simplejson.Json, error) {
cfg := &HTTPConf{}
Expand All @@ -73,6 +83,9 @@ func CURLPerform(method string, url string, body map[string]interface{}, strBody
if err != nil {
return nil, err
}
if cfg.ORGID != 0 {
req.Header.Set(ctrlcommon.HEADER_KEY_X_ORG_ID, strconv.Itoa(cfg.ORGID))
}
req.Header.Set("Content-Type", contentType)
req.Header.Set("Accept", "application/json, text/plain")
req.Header.Set("X-User-Id", "1")
Expand Down Expand Up @@ -196,6 +209,11 @@ func GetTimeout(cmd *cobra.Command) time.Duration {
return t
}

func GetORGID(cmd *cobra.Command) int {
orgID, _ := cmd.Flags().GetUint32("org-id")
return int(orgID)
}

func PrettyPrint(data interface{}) {
val, err := json.MarshalIndent(data, "", " ")
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.19.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand All @@ -49,6 +50,8 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/influxdata/influxdb v1.9.7 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
Expand All @@ -73,6 +76,7 @@ require (
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/common v0.35.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/satori/go.uuid v1.2.1-0.20181028125025-b2ce2384e17b // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/shirou/gopsutil/v3 v3.22.5 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
Expand All @@ -99,6 +103,8 @@ require (
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.3.4 // indirect
gorm.io/gorm v1.23.5 // indirect
k8s.io/api v0.24.0 // indirect
k8s.io/apimachinery v0.24.0 // indirect
k8s.io/client-go v0.24.0 // indirect
Expand Down
Loading

0 comments on commit 75578ac

Please sign in to comment.