[#372] cli/sg: Support bearer token in storagegroup command

Add `--bearer` flag to storagegroup command similar to object command.
Attach parsed bearer token to all API requests of storagegroup command.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-02-10 00:55:38 +03:00 committed by Alex Vanin
parent 687af30c05
commit c6411e4f87

View file

@ -52,6 +52,7 @@ var sgDelCmd = &cobra.Command{
const (
sgMembersFlag = "members"
sgIDFlag = "id"
sgBearerFlag = "bearer"
)
var (
@ -62,6 +63,9 @@ var (
func init() {
rootCmd.AddCommand(storagegroupCmd)
storagegroupCmd.PersistentFlags().String(sgBearerFlag, "",
"File with signed JSON or binary encoded bearer token")
storagegroupCmd.AddCommand(sgPutCmd)
sgPutCmd.Flags().String("cid", "", "Container ID")
_ = sgPutCmd.MarkFlagRequired("cid")
@ -115,6 +119,10 @@ func (c *sgHeadReceiver) Head(addr *objectSDK.Address) (interface{}, error) {
}
}
func sgBearerToken(cmd *cobra.Command) (*token.BearerToken, error) {
return getBearerToken(cmd, sgBearerFlag)
}
func putSG(cmd *cobra.Command, _ []string) error {
ownerID, err := getOwnerID()
if err != nil {
@ -137,6 +145,11 @@ func putSG(cmd *cobra.Command, _ []string) error {
members = append(members, id)
}
bearerToken, err := sgBearerToken(cmd)
if err != nil {
return err
}
ctx := context.Background()
cli, tok, err := initSession(ctx)
@ -169,6 +182,7 @@ func putSG(cmd *cobra.Command, _ []string) error {
WithObject(obj.Object()),
append(globalCallOptions(),
client.WithSession(tok),
client.WithBearer(bearerToken),
)...,
)
if err != nil {
@ -199,6 +213,11 @@ func getSG(cmd *cobra.Command, _ []string) error {
return err
}
bearerToken, err := sgBearerToken(cmd)
if err != nil {
return err
}
addr := objectSDK.NewAddress()
addr.SetContainerID(cid)
addr.SetObjectID(id)
@ -215,6 +234,7 @@ func getSG(cmd *cobra.Command, _ []string) error {
WithAddress(addr),
append(globalCallOptions(),
client.WithSession(tok),
client.WithBearer(bearerToken),
)...,
)
if err != nil {
@ -247,6 +267,11 @@ func listSG(cmd *cobra.Command, _ []string) error {
return err
}
bearerToken, err := sgBearerToken(cmd)
if err != nil {
return err
}
ctx := context.Background()
cli, tok, err := initSession(ctx)
@ -260,6 +285,7 @@ func listSG(cmd *cobra.Command, _ []string) error {
WithSearchFilters(storagegroup.SearchQuery()),
append(globalCallOptions(),
client.WithSession(tok),
client.WithBearer(bearerToken),
)...,
)
if err != nil {
@ -286,6 +312,11 @@ func delSG(cmd *cobra.Command, _ []string) error {
return err
}
bearerToken, err := sgBearerToken(cmd)
if err != nil {
return err
}
ctx := context.Background()
cli, tok, err := initSession(ctx)
@ -302,6 +333,7 @@ func delSG(cmd *cobra.Command, _ []string) error {
WithAddress(addr),
append(globalCallOptions(),
client.WithSession(tok),
client.WithBearer(bearerToken),
)...,
)
if err != nil {