94 lines
2.0 KiB
Go
94 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"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
|
|
ds uint32
|
|
saasHttp *saashttp.SaasClient
|
|
}
|
|
|
|
func RunGrantAdd(args ...string) error {
|
|
fs := flag.NewFlagSet("add", flag.ExitOnError)
|
|
cfgFile := paramConfig(fs)
|
|
srtaAccountId := paramSrtaAccountId(fs)
|
|
dsid := paramRawDataSpaceId(fs)
|
|
grantIndex := paramGrantIndex(fs)
|
|
|
|
if err := fs.Parse(args); err != nil {
|
|
return fmt.Errorf("Command line parse error: %w", err)
|
|
}
|
|
|
|
if fs.NArg() > 0 {
|
|
fs.PrintDefaults()
|
|
return nil
|
|
}
|
|
|
|
if *srtaAccountId == 0 {
|
|
fs.PrintDefaults()
|
|
return fmt.Errorf("sRTA account ID is required.")
|
|
}
|
|
|
|
if *grantIndex == "" {
|
|
fs.PrintDefaults()
|
|
return fmt.Errorf("Grant index is required.")
|
|
}
|
|
|
|
if *dsid == 0 {
|
|
fs.PrintDefaults()
|
|
return fmt.Errorf("Data space ID is required.")
|
|
}
|
|
|
|
cfg, err := LoadConfigFile(*cfgFile)
|
|
if err != nil {
|
|
return fmt.Errorf("LoadConfigFile error: %w", err)
|
|
}
|
|
|
|
grantAddParams := grantAddParams{
|
|
srtaAccountId: uint32(*srtaAccountId),
|
|
grantIndex: *grantIndex,
|
|
ds: uint32(*dsid),
|
|
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{
|
|
TargetAccountId: params.srtaAccountId,
|
|
GrantIndex: params.grantIndex,
|
|
DataspaceId: params.ds,
|
|
},
|
|
},
|
|
}
|
|
|
|
res, err := params.saasHttp.GrantAdd(saasReq)
|
|
if err != nil {
|
|
return fmt.Errorf("Submit Command error: %w", err)
|
|
}
|
|
|
|
if res.Code != saasapi.ErrorCode_SUCC {
|
|
return fmt.Errorf("Command failed. code:%v, status:%v", res.Code, res.Status)
|
|
}
|
|
|
|
grantRes := res.GetGrantAddRes()
|
|
fmt.Printf("grant add res: %v\n", protojson.Format(grantRes))
|
|
return nil
|
|
}
|