增加策略列表、绑定、解绑
This commit is contained in:
88
cmd/saastool/target_list.go
Normal file
88
cmd/saastool/target_list.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"e.coding.net/rta/public/saasapi"
|
||||
"e.coding.net/rta/public/saasapi/pkg/saashttp"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
type listTargetParams struct {
|
||||
targets []string
|
||||
listBinds bool
|
||||
saasHttp *saashttp.SaasClient
|
||||
}
|
||||
|
||||
func RunTargetList(args ...string) error {
|
||||
fs := flag.NewFlagSet("list", flag.ExitOnError)
|
||||
cfgFile := paramConfig(fs)
|
||||
targets := paramTargets(fs)
|
||||
listBinds := paramListBinds(fs)
|
||||
|
||||
if err := fs.Parse(args); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "command line parse error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if fs.NArg() > 0 {
|
||||
fs.PrintDefaults()
|
||||
return nil
|
||||
}
|
||||
|
||||
// 切割字符串
|
||||
idsSlice := strings.Split(*targets, ",")
|
||||
if len(idsSlice) == 1 && idsSlice[0] == "" {
|
||||
idsSlice = []string{}
|
||||
}
|
||||
|
||||
cfg, err := LoadConfigFile(*cfgFile)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
listTargetParams := listTargetParams{
|
||||
listBinds: *listBinds,
|
||||
targets: idsSlice,
|
||||
saasHttp: &saashttp.SaasClient{
|
||||
Client: &http.Client{},
|
||||
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
||||
Auth: &cfg.Auth,
|
||||
},
|
||||
}
|
||||
|
||||
return doTargetList(listTargetParams)
|
||||
}
|
||||
|
||||
func doTargetList(listTargetParams listTargetParams) error {
|
||||
saasReq := &saasapi.SaasReq{
|
||||
Cmd: &saasapi.SaasReq_TargetList{
|
||||
TargetList: &saasapi.TargetList{
|
||||
Targets: listTargetParams.targets,
|
||||
ListBind: listTargetParams.listBinds,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := listTargetParams.saasHttp.TargetList(saasReq)
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "submit List Target error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.Code != saasapi.ErrorCode_SUCC {
|
||||
fmt.Fprintln(os.Stderr, "Target list failed", "code", res.Code, "status", res.Status)
|
||||
return nil
|
||||
}
|
||||
|
||||
targetRes := res.GetTargetListRes()
|
||||
|
||||
fmt.Printf("target res: %v\n", protojson.Format(targetRes))
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user