Skip to content

Commit

Permalink
feat(cmd/influx): add delete bucket by name
Browse files Browse the repository at this point in the history
  • Loading branch information
kelwang committed Mar 24, 2020
1 parent 40d3dd8 commit 6efe0eb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
1. [17273](https://github.com/influxdata/influxdb/pull/17273): Add shell completions command for the influx cli
1. [17353](https://github.com/influxdata/influxdb/pull/17353): Make all pkg resources unique by metadata.name field
1. [17363](https://github.com/influxdata/influxdb/pull/17363): Telegraf config tokens can no longer be retrieved after creation, but new tokens can be created after a telegraf has been setup
1. [17400](https://github.com/influxdata/influxdb/pull/17400): Be able to delete bucket by name via cli

### Bug Fixes

Expand Down
30 changes: 24 additions & 6 deletions cmd/influx/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ func (b *cmdBucketBuilder) cmdDelete() *cobra.Command {
cmd := b.newCmd("delete", b.cmdDeleteRunEFn)
cmd.Short = "Delete bucket"

cmd.Flags().StringVarP(&b.id, "id", "i", "", "The bucket ID (required)")
cmd.MarkFlagRequired("id")
cmd.Flags().StringVarP(&b.id, "id", "i", "", "The bucket ID, required if name isn't provided")
cmd.Flags().StringVarP(&b.name, "name", "n", "", "The bucket name, org or org-id will be required by choosing this")
b.org.register(cmd, false)

return cmd
}
Expand All @@ -131,17 +132,34 @@ func (b *cmdBucketBuilder) cmdDeleteRunEFn(cmd *cobra.Command, args []string) er
}

var id influxdb.ID
if err := id.DecodeFromString(b.id); err != nil {
var filter influxdb.BucketFilter
if b.id == "" && b.name != "" {
if err = b.org.validOrgFlags(&flags); err != nil {
return err
}
filter.Name = &b.name
if b.org.id != "" {
if filter.OrganizationID, err = influxdb.IDFromString(b.org.id); err != nil {
return err
}
} else if b.org.name != "" {
filter.Org = &b.org.name
}

} else if err := id.DecodeFromString(b.id); err != nil {
return fmt.Errorf("failed to decode bucket id %q: %v", b.id, err)
}

if id.Valid() {
filter.ID = &id
}

ctx := context.Background()
bkt, err := bktSVC.FindBucketByID(ctx, id)
bkt, err := bktSVC.FindBucket(ctx, filter)
if err != nil {
return fmt.Errorf("failed to find bucket with id %q: %v", id, err)
}

if err := bktSVC.DeleteBucket(ctx, id); err != nil {
if err := bktSVC.DeleteBucket(ctx, bkt.ID); err != nil {
return fmt.Errorf("failed to delete bucket with id %q: %v", id, err)
}

Expand Down
24 changes: 24 additions & 0 deletions cmd/influx/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,37 @@ func TestCmdBucket(t *testing.T) {
expectedID: influxdb.ID(1),
flag: "-i=",
},
{
name: "with name and org name",
expectedID: influxdb.ID(1),
flag: "--name=n1 --org=org1",
},
{
name: "with name and org name short",
expectedID: influxdb.ID(1),
flag: "-n n1 -o org1",
},
{
name: "with name and org id",
expectedID: influxdb.ID(1),
flag: "--name=n1 --org-id=" + influxdb.ID(3).String(),
},
}

cmdFn := func(expectedID influxdb.ID) func(*globalFlags, genericCLIOpts) *cobra.Command {
svc := mock.NewBucketService()
svc.FindBucketByIDFn = func(ctx context.Context, id influxdb.ID) (*influxdb.Bucket, error) {
return &influxdb.Bucket{ID: id}, nil
}
svc.FindBucketFn = func(ctx context.Context, filter influxdb.BucketFilter) (*influxdb.Bucket, error) {
if filter.ID != nil {
return &influxdb.Bucket{ID: *filter.ID}, nil
}
if filter.Name != nil {
return &influxdb.Bucket{ID: expectedID}, nil
}
return nil, nil
}
svc.DeleteBucketFn = func(ctx context.Context, id influxdb.ID) error {
if expectedID != id {
return fmt.Errorf("unexpected id:\n\twant= %s\n\tgot= %s", expectedID, id)
Expand Down

0 comments on commit 6efe0eb

Please sign in to comment.