Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 28 additions & 5 deletions cmd/influx/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()},
},
}

Expand All @@ -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)
Expand All @@ -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())
}
Expand Down