feat(cmd/influx): add delete bucket by name

pull/17400/head
Kelvin Wang 2020-03-24 14:43:10 -04:00
parent 40d3dd8cc4
commit 1e9099c45f
3 changed files with 53 additions and 11 deletions

View File

@ -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

View File

@ -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
}
@ -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)
}

View File

@ -128,17 +128,32 @@ func TestCmdBucket(t *testing.T) {
tests := []struct {
name string
expectedID influxdb.ID
flag string
flags []string
}{
{
name: "with description and retention period",
expectedID: influxdb.ID(1),
flag: "--id=",
flags: []string{"--id=" + influxdb.ID(1).String()},
},
{
name: "shorts",
expectedID: influxdb.ID(1),
flag: "-i=",
flags: []string{"-i=" + influxdb.ID(1).String()},
},
{
name: "with name and org name",
expectedID: influxdb.ID(1),
flags: []string{"--name=n1", "--org=org1"},
},
{
name: "with name and org name short",
expectedID: influxdb.ID(1),
flags: []string{"-n=n1", "-o=org1"},
},
{
name: "with name and org id",
expectedID: influxdb.ID(1),
flags: []string{"--name=n1", "--org-id=" + influxdb.ID(3).String()},
},
}
@ -147,6 +162,15 @@ func TestCmdBucket(t *testing.T) {
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)
@ -167,8 +191,7 @@ func TestCmdBucket(t *testing.T) {
)
cmd := builder.cmd(cmdFn(tt.expectedID))
idFlag := tt.flag + tt.expectedID.String()
cmd.SetArgs([]string{"bucket", "delete", idFlag})
cmd.SetArgs(append([]string{"bucket", "delete"}, tt.flags...))
require.NoError(t, cmd.Execute())
}