Skip to content

Commit

Permalink
feat: add tags for bucket/object/group and support SetTags tx (#105)
Browse files Browse the repository at this point in the history
* feat: add tags for bucket/object/group
  • Loading branch information
ruojunm authored Dec 12, 2023
1 parent a860429 commit cd2be3a
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 185 deletions.
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,16 @@ SHELL := /bin/bash

build:
go build -o ./build/gnfd-cmd cmd/*.go

golangci_lint_cmd=golangci-lint
golangci_version=v1.51.2

lint:
@echo "--> Running linter"
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version)
@$(golangci_lint_cmd) run --timeout=10m

lint-fix:
@echo "--> Running linter"
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version)
@$(golangci_lint_cmd) run --fix --out-format=tab --issues-exit-code=0
88 changes: 85 additions & 3 deletions cmd/cmd_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ package main

import (
"context"
"encoding/json"
"errors"
"fmt"
"time"

"cosmossdk.io/math"
sdktypes "github.com/bnb-chain/greenfield-go-sdk/types"
"github.com/bnb-chain/greenfield/sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/urfave/cli/v2"

sdktypes "github.com/bnb-chain/greenfield-go-sdk/types"
"github.com/bnb-chain/greenfield/sdk/types"
gtypes "github.com/bnb-chain/greenfield/types"
storagetypes "github.com/bnb-chain/greenfield/x/storage/types"
)

// cmdCreateBucket create a new Bucket
Expand All @@ -27,7 +31,7 @@ The command need to set the primary SP address with --primarySP.
Examples:
# Create a new bucket called gnfd-bucket, visibility is public-read
$ gnfd-cmd bucket create --visibility=public-read gnfd://gnfd-bucket`,
$ gnfd-cmd bucket create --visibility=public-read --tags='[{"key":"key1","value":"value1"},{"key":"key2","value":"value2"}]' gnfd://gnfd-bucket`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: primarySPFlag,
Expand All @@ -52,6 +56,11 @@ $ gnfd-cmd bucket create --visibility=public-read gnfd://gnfd-bucket`,
},
Usage: "set visibility of the bucket",
},
&cli.StringFlag{
Name: tagFlag,
Value: "",
Usage: "set one or more tags of the bucket. The tag value is key-value pairs in json array format. E.g. [{\"key\":\"key1\",\"value\":\"value1\"},{\"key\":\"key2\",\"value\":\"value2\"}]",
},
},
}
}
Expand Down Expand Up @@ -147,6 +156,70 @@ $ gnfd-cmd bucket mirror --destChainId 97 --bucketName yourBucketName
}
}

func cmdSetTagForBucket() *cli.Command {
return &cli.Command{
Name: "setTag",
Action: setTagForBucket,
Usage: "Set tags for the given bucket",
ArgsUsage: "BUCKET-URL",
Description: `
The command is used to set tag for a given existing bucket.
Examples:
$ gnfd-cmd bucket setTag --tags='[{"key":"key1","value":"value1"},{"key":"key2","value":"value2"}]' gnfd://gnfd-bucket`,

Flags: []cli.Flag{
&cli.StringFlag{
Name: tagFlag,
Value: "",
Usage: "set one or more tags for the given bucket. The tag value is key-value pairs in json array format. E.g. [{\"key\":\"key1\",\"value\":\"value1\"},{\"key\":\"key2\",\"value\":\"value2\"}]",
},
},
}
}

// setTag Set tag for a given existing bucket
func setTagForBucket(ctx *cli.Context) error {
bucketName, err := getBucketNameByUrl(ctx)
if err != nil {
return toCmdErr(err)
}

grn := gtypes.NewBucketGRN(bucketName)
client, err := NewClient(ctx, false)
if err != nil {
return toCmdErr(err)
}

tagsParam := ctx.String(tagFlag)
if tagsParam == "" {
err = errors.New("invalid tags parameter")
}
if err != nil {
return toCmdErr(err)
}
tags := &storagetypes.ResourceTags{}
err = json.Unmarshal([]byte(tagsParam), &tags.Tags)
if err != nil {
return toCmdErr(err)
}

c, cancelSetTag := context.WithCancel(globalContext)
defer cancelSetTag()
txnHash, err := client.SetTag(c, grn.String(), *tags, sdktypes.SetTagsOptions{})

if err != nil {
return toCmdErr(err)
}

err = waitTxnStatus(client, c, txnHash, "SetTags")
if err != nil {
return toCmdErr(err)
}

return nil
}

// createBucket send the create bucket request to storage provider
func createBucket(ctx *cli.Context) error {
bucketName, err := getBucketNameByUrl(ctx)
Expand Down Expand Up @@ -192,6 +265,15 @@ func createBucket(ctx *cli.Context) error {
opts.ChargedQuota = chargedQuota
}

tags := ctx.String(tagFlag)
if tags != "" {
opts.Tags = &storagetypes.ResourceTags{}
err = json.Unmarshal([]byte(tags), &opts.Tags.Tags)
if err != nil {
return toCmdErr(err)
}
}

opts.TxOpts = &types.TxOption{Mode: &SyncBroadcastMode}
txnHash, err := client.CreateBucket(c, bucketName, primarySpAddrStr, opts)
if err != nil {
Expand Down
100 changes: 94 additions & 6 deletions cmd/cmd_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import (
"time"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/urfave/cli/v2"

sdktypes "github.com/bnb-chain/greenfield-go-sdk/types"
"github.com/bnb-chain/greenfield/sdk/types"
gtypes "github.com/bnb-chain/greenfield/types"
storageTypes "github.com/bnb-chain/greenfield/x/storage/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/urfave/cli/v2"
)

// cmdCreateBucket create a new Bucket
Expand All @@ -28,7 +30,14 @@ func cmdCreateGroup() *cli.Command {
Create a new group
Examples:
$ gnfd-cmd group create group-name`,
$ gnfd-cmd group create --tags='[{"key":"key1","value":"value1"},{"key":"key2","value":"value2"}]' group-name`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: tagFlag,
Value: "",
Usage: "set one or more tags of the group. The tag value is key-value pairs in json array format. E.g. [{\"key\":\"key1\",\"value\":\"value1\"},{\"key\":\"key2\",\"value\":\"value2\"}]",
},
},
}
}

Expand Down Expand Up @@ -219,6 +228,76 @@ $ gnfd-cmd group mirror --destChainId 97 --groupName yourGroupName
}
}

func cmdSetTagForGroup() *cli.Command {
return &cli.Command{
Name: "setTag",
Action: setTagForGroup,
Usage: "Set tags for the given group",
ArgsUsage: "GROUP-NAME",
Description: `
The command is used to set tag for a given existing group.
Examples:
$ gnfd-cmd group setTag --tags='[{"key":"key1","value":"value1"},{"key":"key2","value":"value2"}]' group-name`,

Flags: []cli.Flag{
&cli.StringFlag{
Name: tagFlag,
Value: "",
Usage: "set one or more tags for the given group. The tag value is key-value pairs in json array format. E.g. [{\"key\":\"key1\",\"value\":\"value1\"},{\"key\":\"key2\",\"value\":\"value2\"}]",
},
},
}
}

// setTag Set tag for a given existing group
func setTagForGroup(ctx *cli.Context) error {
client, err := NewClient(ctx, false)
if err != nil {
return toCmdErr(err)
}

groupName, err := getGroupNameByUrl(ctx)
if err != nil {
return toCmdErr(err)
}

acct, err := client.GetDefaultAccount()
if err != nil {
return toCmdErr(err)
}

grn := gtypes.NewGroupGRN(acct.GetAddress(), groupName)

tagsParam := ctx.String(tagFlag)
if tagsParam == "" {
err = errors.New("invalid tags parameter")
}
if err != nil {
return toCmdErr(err)
}
tags := &storageTypes.ResourceTags{}
err = json.Unmarshal([]byte(tagsParam), &tags.Tags)
if err != nil {
return toCmdErr(err)
}

c, cancelSetTag := context.WithCancel(globalContext)
defer cancelSetTag()
txnHash, err := client.SetTag(c, grn.String(), *tags, sdktypes.SetTagsOptions{})

if err != nil {
return toCmdErr(err)
}

err = waitTxnStatus(client, c, txnHash, "SetTags")
if err != nil {
return toCmdErr(err)
}

return nil
}

// createGroup send the create bucket request to storage provider
func createGroup(ctx *cli.Context) error {
groupName, err := getGroupNameByUrl(ctx)
Expand All @@ -233,6 +312,15 @@ func createGroup(ctx *cli.Context) error {

opts := sdktypes.CreateGroupOptions{}

tags := ctx.String(tagFlag)
if tags != "" {
opts.Tags = &storageTypes.ResourceTags{}
err = json.Unmarshal([]byte(tags), &opts.Tags.Tags)
if err != nil {
return toCmdErr(err)
}
}

opts.TxOpts = &types.TxOption{Mode: &SyncBroadcastMode}

c, cancelCreateGroup := context.WithCancel(globalContext)
Expand Down Expand Up @@ -482,7 +570,7 @@ func listGroup(ctx *cli.Context) error {
}

id := groupList.Groups[memberNum-1].Group.Id
initStartKey = strconv.FormatUint(id, 10)
initStartKey = strconv.FormatUint(id.Uint64(), 10)
}

return nil
Expand Down Expand Up @@ -518,7 +606,7 @@ func listBelongGroup(ctx *cli.Context) error {
}

id := groupList.Groups[memberNum-1].Group.Id
initStartKey = strconv.FormatUint(id, 10)
initStartKey = strconv.FormatUint(id.Uint64(), 10)
}

return nil
Expand Down Expand Up @@ -556,7 +644,7 @@ func printListGroupResult(listResult *sdktypes.GroupsResult) {
location, _ := time.LoadLocation("Asia/Shanghai")
t := time.Unix(group.CreateTime, 0).In(location)

fmt.Printf(format, t.Format(iso8601DateFormat), group.Group.GroupName, strconv.FormatUint(group.Group.Id, 10))
fmt.Printf(format, t.Format(iso8601DateFormat), group.Group.GroupName, strconv.FormatUint(group.Group.Id.Uint64(), 10))
}
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/cmd_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func headObject(ctx *cli.Context) error {
if format == defaultFormat {
parseObjectInfo(objectDetail)
} else if format == jsonFormat {
parseObjectByJsonFormat(objectDetail)
parseByJsonFormat(objectDetail)
} else {
return toCmdErr(fmt.Errorf("invalid format"))
}
Expand Down Expand Up @@ -169,7 +169,7 @@ func headBucket(ctx *cli.Context) error {
if format == defaultFormat {
parseBucketInfo(bucketInfo)
} else if format == jsonFormat {
parseBucketByJsonFormat(bucketInfo)
parseByJsonFormat(bucketInfo)
} else {
return toCmdErr(fmt.Errorf("invalid format"))
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func headGroup(ctx *cli.Context) error {
fmt.Println(info)
}
} else if format == jsonFormat {
parseGroupByJson(groupInfo)
parseByJsonFormat(groupInfo)
} else {
return toCmdErr(fmt.Errorf("invalid format"))
}
Expand Down
Loading

0 comments on commit cd2be3a

Please sign in to comment.