106 lines
2.2 KiB
Go
106 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.algo.com.cn/public/saasapi"
|
|
"git.algo.com.cn/public/saasapi/pkg/saashttp"
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
)
|
|
|
|
type bindSetAccountParams struct {
|
|
target string
|
|
accounts []int64
|
|
saasHttp *saashttp.SaasClient
|
|
}
|
|
|
|
func RunBindSetAccount(args ...string) error {
|
|
fs := flag.NewFlagSet("setaccount", flag.ExitOnError)
|
|
cfgFile := paramConfig(fs)
|
|
target := paramTarget(fs)
|
|
accounts := paramAccounts(fs)
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
fmt.Fprintln(os.Stderr, "command line parse error", "err", err)
|
|
return err
|
|
}
|
|
|
|
// 切割字符串
|
|
idsSlice := strings.Split(*accounts, ",")
|
|
if len(idsSlice) == 1 && idsSlice[0] == "" {
|
|
idsSlice = []string{}
|
|
}
|
|
|
|
if fs.NArg() > 0 || len(idsSlice) == 0 || (len(idsSlice) == 1 && idsSlice[0] == "") {
|
|
fs.PrintDefaults()
|
|
return nil
|
|
}
|
|
|
|
numAccounts := []int64{}
|
|
for _, v := range idsSlice {
|
|
n, err := strconv.ParseInt(v, 10, 64)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "account parse error", "value", v, "err", err)
|
|
return err
|
|
}
|
|
numAccounts = append(numAccounts, n)
|
|
}
|
|
|
|
cfg, err := LoadConfigFile(*cfgFile)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
|
|
return err
|
|
}
|
|
|
|
bindSetAccountParams := bindSetAccountParams{
|
|
target: *target,
|
|
accounts: numAccounts,
|
|
saasHttp: &saashttp.SaasClient{
|
|
Client: &http.Client{},
|
|
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
|
Auth: &cfg.Auth,
|
|
},
|
|
}
|
|
|
|
return doBindSetAccount(bindSetAccountParams)
|
|
}
|
|
|
|
func doBindSetAccount(p bindSetAccountParams) error {
|
|
bindSet := &saasapi.BindSet{}
|
|
|
|
saasReq := &saasapi.SaasReq{
|
|
Cmd: &saasapi.SaasReq_BindSet{
|
|
BindSet: bindSet,
|
|
},
|
|
}
|
|
|
|
for _, v := range p.accounts {
|
|
bindSet.Binds = append(bindSet.Binds, &saasapi.Bind{
|
|
BindId: v,
|
|
BindType: saasapi.BindType_AccountId,
|
|
TargetId: p.target,
|
|
AccountId: v,
|
|
})
|
|
}
|
|
|
|
res, err := p.saasHttp.BindSet(saasReq)
|
|
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "submit Bind Set error", "err", err)
|
|
return err
|
|
}
|
|
|
|
if res.Code != saasapi.ErrorCode_SUCC {
|
|
fmt.Fprintln(os.Stderr, "Bind Set failed", "code", res.Code, "status", res.Status)
|
|
return nil
|
|
}
|
|
|
|
fmt.Printf("bind res: %v\n", protojson.Format(res.GetBindSetRes()))
|
|
return nil
|
|
}
|