102 lines
2.3 KiB
Go
102 lines
2.3 KiB
Go
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 grantDeleteParams struct {
|
|
srtaAccountId uint32
|
|
grantIndex string
|
|
ds uint32
|
|
saasHttp *saashttp.SaasClient
|
|
}
|
|
|
|
func RunGrantDelete(args ...string) error {
|
|
fs := flag.NewFlagSet("delete", flag.ExitOnError)
|
|
cfgFile := paramConfig(fs)
|
|
dsid := paramRawDataSpaceId(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 *dsid == 0 {
|
|
fmt.Fprintln(os.Stderr, "Error: data space ID is required")
|
|
fs.PrintDefaults()
|
|
return fmt.Errorf("data space ID is required")
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
grantDeleteParams := grantDeleteParams{
|
|
srtaAccountId: uint32(*srtaAccountId),
|
|
grantIndex: *grantIndex,
|
|
ds: uint32(*dsid),
|
|
saasHttp: &saashttp.SaasClient{
|
|
Client: &http.Client{},
|
|
ApiUrls: saashttp.InitAPIUrl(&cfg.ApiUrls),
|
|
Auth: &cfg.Auth,
|
|
},
|
|
}
|
|
|
|
return doGrantDelete(grantDeleteParams)
|
|
}
|
|
|
|
func doGrantDelete(params grantDeleteParams) error {
|
|
saasReq := &saasapi.SaasReq{
|
|
Cmd: &saasapi.SaasReq_GrantDelete{
|
|
GrantDelete: &saasapi.Grant{
|
|
TargetAccountId: params.srtaAccountId,
|
|
GrantIndex: params.grantIndex,
|
|
DataspaceId: params.ds,
|
|
},
|
|
},
|
|
}
|
|
|
|
res, err := params.saasHttp.GrantDelete(saasReq)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "submit Grant Delete error", "err", err)
|
|
return err
|
|
}
|
|
|
|
if res.Code != saasapi.ErrorCode_SUCC {
|
|
fmt.Fprintln(os.Stderr, "Grant delete failed", "code", res.Code, "status", res.Status)
|
|
return nil
|
|
}
|
|
|
|
grantRes := res.GetGrantDeleteRes()
|
|
fmt.Printf("grant delete res: %v\n", protojson.Format(grantRes))
|
|
return nil
|
|
}
|