增加策略列表、绑定、解绑
This commit is contained in:
110
cmd/saastool/bind_setad.go
Normal file
110
cmd/saastool/bind_setad.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"e.coding.net/rta/public/saasapi"
|
||||
"e.coding.net/rta/public/saasapi/pkg/saashttp"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
type bindSetAdParams struct {
|
||||
target string
|
||||
account int64
|
||||
ads []int64
|
||||
saasHttp *saashttp.SaasClient
|
||||
}
|
||||
|
||||
func RunBindSetAd(args ...string) error {
|
||||
fs := flag.NewFlagSet("setad", flag.ExitOnError)
|
||||
cfgFile := paramConfig(fs)
|
||||
target := paramTarget(fs)
|
||||
account := paramAccount(fs)
|
||||
ads := paramAds(fs)
|
||||
|
||||
if err := fs.Parse(args); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "command line parse error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
// 切割字符串
|
||||
idsSlice := strings.Split(*ads, ",")
|
||||
if len(idsSlice) == 1 && idsSlice[0] == "" {
|
||||
idsSlice = []string{}
|
||||
}
|
||||
|
||||
if fs.NArg() > 0 || len(idsSlice) == 0 || (len(idsSlice) == 1 && idsSlice[0] == "") {
|
||||
fs.PrintDefaults()
|
||||
return nil
|
||||
}
|
||||
|
||||
numAds := []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
|
||||
}
|
||||
numAds = append(numAds, n)
|
||||
}
|
||||
|
||||
cfg, err := LoadConfigFile(*cfgFile)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
bindSetAdParams := bindSetAdParams{
|
||||
target: *target,
|
||||
account: *account,
|
||||
ads: numAds,
|
||||
saasHttp: &saashttp.SaasClient{
|
||||
Client: &http.Client{},
|
||||
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
||||
Auth: &cfg.Auth,
|
||||
},
|
||||
}
|
||||
|
||||
return doBindSetAd(bindSetAdParams)
|
||||
}
|
||||
|
||||
func doBindSetAd(p bindSetAdParams) error {
|
||||
bindSet := &saasapi.BindSet{}
|
||||
|
||||
saasReq := &saasapi.SaasReq{
|
||||
Cmd: &saasapi.SaasReq_BindSet{
|
||||
BindSet: bindSet,
|
||||
},
|
||||
}
|
||||
|
||||
for _, v := range p.ads {
|
||||
bindSet.Binds = append(bindSet.Binds, &saasapi.Bind{
|
||||
BindId: v,
|
||||
BindType: saasapi.BindType_AdgroupId,
|
||||
TargetId: p.target,
|
||||
AccountId: p.account,
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
bindRes := res.GetBindSetRes()
|
||||
|
||||
fmt.Printf("bind res: %v\n", protojson.Format(bindRes))
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user