支持数据授权管理功能
This commit is contained in:
91
cmd/saastool/grant_add.go
Normal file
91
cmd/saastool/grant_add.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"git.algo.com.cn/public/saasapi"
|
||||
"git.algo.com.cn/public/saasapi/pkg/saashttp"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
type grantAddParams struct {
|
||||
srtaAccountId uint32
|
||||
grantIndex string
|
||||
saasHttp *saashttp.SaasClient
|
||||
}
|
||||
|
||||
func RunGrantAdd(args ...string) error {
|
||||
fs := flag.NewFlagSet("add", flag.ExitOnError)
|
||||
cfgFile := paramConfig(fs)
|
||||
srtaAccountId := paramSrtaAccountId(fs)
|
||||
grantIndex := paramGrantIndex(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
|
||||
}
|
||||
|
||||
if *srtaAccountId == 0 {
|
||||
fmt.Fprintln(os.Stderr, "Error: sRTA account ID is required")
|
||||
fs.PrintDefaults()
|
||||
return fmt.Errorf("sRTA account ID is required")
|
||||
}
|
||||
|
||||
if *grantIndex == "" {
|
||||
fmt.Fprintln(os.Stderr, "Error: grant index is required")
|
||||
fs.PrintDefaults()
|
||||
return fmt.Errorf("grant index is required")
|
||||
}
|
||||
|
||||
cfg, err := LoadConfigFile(*cfgFile)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "LoadConfigFile error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
grantAddParams := grantAddParams{
|
||||
srtaAccountId: uint32(*srtaAccountId),
|
||||
grantIndex: *grantIndex,
|
||||
saasHttp: &saashttp.SaasClient{
|
||||
Client: &http.Client{},
|
||||
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
||||
Auth: &cfg.Auth,
|
||||
},
|
||||
}
|
||||
|
||||
return doGrantAdd(grantAddParams)
|
||||
}
|
||||
|
||||
func doGrantAdd(params grantAddParams) error {
|
||||
saasReq := &saasapi.SaasReq{
|
||||
Cmd: &saasapi.SaasReq_GrantAdd{
|
||||
GrantAdd: &saasapi.Grant{
|
||||
SrtaAccountId: params.srtaAccountId,
|
||||
GrantIndex: params.grantIndex,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
res, err := params.saasHttp.GrantAdd(saasReq)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "submit Grant Add error", "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if res.Code != saasapi.ErrorCode_SUCC {
|
||||
fmt.Fprintln(os.Stderr, "Grant add failed", "code", res.Code, "status", res.Status)
|
||||
return nil
|
||||
}
|
||||
|
||||
grantRes := res.GetGrantAddRes()
|
||||
fmt.Printf("grant add res: %v\n", protojson.Format(grantRes))
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user