From 67a9ba39c4c9bd968f5be1bdc723e3b2e5dce29b Mon Sep 17 00:00:00 2001 From: Deary Hudson Date: Tue, 10 Dec 2019 13:04:55 -0600 Subject: [PATCH 1/3] feat(cmd): bucket create accepts org name as flag adds: - `--org` or -`o` shorthand for the organization name enables the user to specify an organization name when creating a bucket. --- CHANGELOG.md | 1 + cmd/influx/bucket.go | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 373cc5dfb4b..e47773f1d78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ 1. [15836](https://github.com/influxdata/influxdb/pull/16077): Add stacked line layer option to graphs 1. [16094](https://github.com/influxdata/influxdb/pull/16094): Annotate log messages with trace ID, if available +1. [16187](https://github.com/influxdata/influxdb/pull/16187): Bucket create to accept an org name flag ### Bug Fixes diff --git a/cmd/influx/bucket.go b/cmd/influx/bucket.go index 0e1a66021e1..fe88af2e864 100644 --- a/cmd/influx/bucket.go +++ b/cmd/influx/bucket.go @@ -27,6 +27,7 @@ func bucketF(cmd *cobra.Command, args []string) { type BucketCreateFlags struct { name string orgID string + org string retention time.Duration } @@ -42,6 +43,7 @@ func init() { bucketCreateCmd.Flags().StringVarP(&bucketCreateFlags.name, "name", "n", "", "Name of bucket that will be created") bucketCreateCmd.Flags().DurationVarP(&bucketCreateFlags.retention, "retention", "r", 0, "Duration in nanoseconds data will live in bucket") bucketCreateCmd.Flags().StringVarP(&bucketCreateFlags.orgID, "org-id", "", "", "The ID of the organization that owns the bucket") + bucketCreateCmd.Flags().StringVarP(&bucketCreateFlags.org, "org", "o", "", "The org name") bucketCreateCmd.MarkFlagRequired("name") bucketCmd.AddCommand(bucketCreateCmd) @@ -63,8 +65,10 @@ func newBucketService(f Flags) (platform.BucketService, error) { } func bucketCreateF(cmd *cobra.Command, args []string) error { - if bucketCreateFlags.orgID == "" { - return fmt.Errorf("must specify org-id") + if bucketCreateFlags.orgID == "" && bucketCreateFlags.org == "" { + return fmt.Errorf("must specify org-id, or org name") + } else if bucketCreateFlags.orgID != "" && bucketCreateFlags.org != "" { + return fmt.Errorf("must specify org-id, or org name not both") } s, err := newBucketService(flags) @@ -83,6 +87,19 @@ func bucketCreateF(cmd *cobra.Command, args []string) error { return fmt.Errorf("failed to decode org id %q: %v", bucketCreateFlags.orgID, err) } b.OrgID = *id + } else if bucketCreateFlags.org != "" { + orgSvc, err := newOrganizationService(flags) + if err != nil { + return fmt.Errorf("failed to initialize organization service client: %v", err) + } + + filter := platform.OrganizationFilter{Name: &bucketCreateFlags.org} + org, err := orgSvc.FindOrganization(context.Background(), filter) + if err != nil { + return fmt.Errorf("%v", err) + } + + b.OrgID = org.ID } if err := s.CreateBucket(context.Background(), b); err != nil { From afa03e7bdcdfb90f3931a2ed827c49884f0e28f2 Mon Sep 17 00:00:00 2001 From: Deary Hudson Date: Tue, 10 Dec 2019 16:42:30 -0600 Subject: [PATCH 2/3] fix(cmd): removes shorthand from --org-id - to ensure consistency across the cli the shorthand is being given to the shorthand was used for both flags inconsistently, but now it only works for --- cmd/influx/bucket.go | 4 ++-- cmd/influx/inspect.go | 1 + cmd/influx/pkg.go | 32 +++++++++++++++++++++++++++----- cmd/influx/task.go | 9 +++++++-- cmd/influx/user.go | 30 ++++++++++++++++++++++++++++-- 5 files changed, 65 insertions(+), 11 deletions(-) diff --git a/cmd/influx/bucket.go b/cmd/influx/bucket.go index fe88af2e864..96b27db7a49 100644 --- a/cmd/influx/bucket.go +++ b/cmd/influx/bucket.go @@ -88,7 +88,7 @@ func bucketCreateF(cmd *cobra.Command, args []string) error { } b.OrgID = *id } else if bucketCreateFlags.org != "" { - orgSvc, err := newOrganizationService(flags) + orgSvc, err := newOrganizationService() if err != nil { return fmt.Errorf("failed to initialize organization service client: %v", err) } @@ -96,7 +96,7 @@ func bucketCreateF(cmd *cobra.Command, args []string) error { filter := platform.OrganizationFilter{Name: &bucketCreateFlags.org} org, err := orgSvc.FindOrganization(context.Background(), filter) if err != nil { - return fmt.Errorf("%v", err) + return err } b.OrgID = org.ID diff --git a/cmd/influx/inspect.go b/cmd/influx/inspect.go index fd2607a3360..173ac981f04 100644 --- a/cmd/influx/inspect.go +++ b/cmd/influx/inspect.go @@ -63,6 +63,7 @@ in the following ways: inspectReportTSMCommand.Flags().BoolVarP(&inspectReportTSMFlags.detailed, "detailed", "", false, "emit series cardinality segmented by measurements, tag keys and fields. Warning, may take a while.") inspectReportTSMCommand.Flags().StringVarP(&inspectReportTSMFlags.orgID, "org-id", "", "", "process only data belonging to organization ID.") + inspectReportTSMCommand.Flags().StringVarP(&inspectReportTSMFlags.orgID, "org", "o", "", "process only data belonging to organization ID.") inspectReportTSMCommand.Flags().StringVarP(&inspectReportTSMFlags.bucketID, "bucket-id", "", "", "process only data belonging to bucket ID. Requires org flag to be set.") dir, err := fs.InfluxDir() diff --git a/cmd/influx/pkg.go b/cmd/influx/pkg.go index d23638a0dea..129457aebae 100644 --- a/cmd/influx/pkg.go +++ b/cmd/influx/pkg.go @@ -44,6 +44,7 @@ type cmdPkgBuilder struct { hasTableBorders bool meta pkger.Metadata orgID string + org string quiet bool applyOpts struct { @@ -95,8 +96,8 @@ func (b *cmdPkgBuilder) cmdPkgApply() *cobra.Command { cmd.Flags().IntVarP(&b.applyReqLimit, "req-limit", "r", 0, "Request limit for applying a pkg, defaults to 5(recommended for OSS).") cmd.Flags().StringVar(&b.applyOpts.force, "force", "", `TTY input, if package will have destructive changes, proceed if set "true".`) - cmd.Flags().StringVarP(&b.orgID, "org-id", "o", "", "The ID of the organization that owns the bucket") - cmd.MarkFlagRequired("org-id") + cmd.Flags().StringVarP(&b.orgID, "org-id", "", "", "The ID of the organization that owns the bucket") + cmd.Flags().StringVarP(&b.org, "org", "o", "", "The name of the organization that owns the bucket") cmd.Flags().BoolVarP(&b.hasColor, "color", "c", true, "Enable color in output, defaults true") cmd.Flags().BoolVar(&b.hasTableBorders, "table-borders", true, "Enable table borders, defaults true") @@ -108,11 +109,32 @@ func (b *cmdPkgBuilder) cmdPkgApply() *cobra.Command { func (b *cmdPkgBuilder) pkgApplyRunEFn() func(*cobra.Command, []string) error { return func(cmd *cobra.Command, args []string) (e error) { + if b.orgID == "" && b.org == "" { + return fmt.Errorf("must specify org-id, or org name") + } else if b.orgID != "" && b.org != "" { + return fmt.Errorf("must specify org-id, or org name not both") + } color.NoColor = !b.hasColor + var influxOrgID *influxdb.ID - influxOrgID, err := influxdb.IDFromString(b.orgID) - if err != nil { - return fmt.Errorf("invalid org ID provided: %s", err.Error()) + if b.orgID != "" { + var err error + influxOrgID, err = influxdb.IDFromString(b.orgID) + if err != nil { + return fmt.Errorf("invalid org ID provided: %s", err.Error()) + } + } else if b.org != "" { + orgSvc, err := newOrganizationService() + if err != nil { + return fmt.Errorf("failed to initialize organization service client: %v", err) + } + + filter := influxdb.OrganizationFilter{Name: &b.org} + org, err := orgSvc.FindOrganization(context.Background(), filter) + if err != nil { + return fmt.Errorf("%v", err) + } + influxOrgID = &org.ID } svc, err := b.svcFn(flags.httpClientOpts(), pkger.WithApplyReqLimit(b.applyReqLimit)) diff --git a/cmd/influx/task.go b/cmd/influx/task.go index cfbd64560ae..3fe86eadf29 100644 --- a/cmd/influx/task.go +++ b/cmd/influx/task.go @@ -70,7 +70,7 @@ func init() { RunE: wrapCheckSetup(taskCreateF), } - taskCreateCmd.Flags().StringVarP(&taskCreateFlags.org, "org", "", "", "organization name") + taskCreateCmd.Flags().StringVarP(&taskCreateFlags.org, "org", "o", "", "organization name") taskCreateCmd.Flags().StringVarP(&taskCreateFlags.orgID, "org-id", "", "", "id of the organization that owns the task") taskCreateCmd.MarkFlagRequired("flux") @@ -155,7 +155,7 @@ func init() { taskFindCmd.Flags().StringVarP(&taskFindFlags.id, "id", "i", "", "task ID") taskFindCmd.Flags().StringVarP(&taskFindFlags.user, "user-id", "n", "", "task owner ID") - taskFindCmd.Flags().StringVarP(&taskFindFlags.org, "org", "", "", "task organization name") + taskFindCmd.Flags().StringVarP(&taskFindFlags.org, "org", "o", "", "task organization name") taskFindCmd.Flags().StringVarP(&taskFindFlags.orgID, "org-id", "", "", "task organization ID") taskFindCmd.Flags().IntVarP(&taskFindFlags.limit, "limit", "", platform.TaskDefaultPageSize, "the number of tasks to find") @@ -163,6 +163,11 @@ func init() { } func taskFindF(cmd *cobra.Command, args []string) error { + if taskFindFlags.orgID == "" && taskFindFlags.org == "" { + return fmt.Errorf("must specify org-id, or org name") + } else if taskFindFlags.orgID != "" && taskFindFlags.org != "" { + return fmt.Errorf("must specify org-id, or org name not both") + } s := &http.TaskService{ Addr: flags.host, Token: flags.token, diff --git a/cmd/influx/user.go b/cmd/influx/user.go index 8ecfef76c5a..ed46b0f964a 100644 --- a/cmd/influx/user.go +++ b/cmd/influx/user.go @@ -3,6 +3,7 @@ package main import ( "context" "errors" + "fmt" "os" platform "github.com/influxdata/influxdb" @@ -113,6 +114,7 @@ var userCreateFlags struct { name string password string orgID string + org string } func userCreateCmd() *cobra.Command { @@ -125,12 +127,19 @@ func userCreateCmd() *cobra.Command { cmd.Flags().StringVarP(&userCreateFlags.name, "name", "n", "", "The user name (required)") cmd.MarkFlagRequired("name") cmd.Flags().StringVarP(&userCreateFlags.password, "password", "p", "", "The user password") - cmd.Flags().StringVarP(&userCreateFlags.orgID, "org-id", "o", "", "The organization id the user belongs too. Is required if password provided.") + cmd.Flags().StringVarP(&userCreateFlags.orgID, "org-id", "", "", "The organization id the user belongs to. Is required if password provided.") + cmd.Flags().StringVarP(&userCreateFlags.org, "org", "o", "", "The organization name the user belongs to. Is required if password provided.") return cmd } func userCreateF(cmd *cobra.Command, args []string) error { + if userCreateFlags.orgID == "" && userCreateFlags.org == "" { + return errors.New("must specify org-id, or org name") + } else if userCreateFlags.orgID != "" && userCreateFlags.org != "" { + return errors.New("must specify org-id, or org name not both") + } + s, err := newUserService() if err != nil { return err @@ -161,7 +170,24 @@ func userCreateF(cmd *cobra.Command, args []string) error { return nil } - orgIDStr := userCreateFlags.orgID + var orgIDStr string + + if userCreateFlags.orgID != "" { + orgIDStr = userCreateFlags.orgID + } else if userCreateFlags.org != "" { + orgSvc, err := newOrganizationService() + if err != nil { + return fmt.Errorf("failed to initialize organization service client: %v", err) + } + + filter := platform.OrganizationFilter{Name: &bucketCreateFlags.org} + org, err := orgSvc.FindOrganization(context.Background(), filter) + if err != nil { + return fmt.Errorf("%v", err) + } + + orgIDStr = org.ID.GoString() + } pass := userCreateFlags.password if orgIDStr == "" && pass == "" { return writeOutput([]string{"ID", "Name"}, user.ID.String(), user.Name) From 09697c331aef937733a70ae22394edcddf08c577 Mon Sep 17 00:00:00 2001 From: Deary Hudson Date: Tue, 10 Dec 2019 17:14:39 -0600 Subject: [PATCH 3/3] fix(cmd): fixes bug in --org flag in inspect.go --- cmd/influx/inspect.go | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/cmd/influx/inspect.go b/cmd/influx/inspect.go index 173ac981f04..a04d1105f22 100644 --- a/cmd/influx/inspect.go +++ b/cmd/influx/inspect.go @@ -1,6 +1,7 @@ package main import ( + "context" "errors" "fmt" "os" @@ -18,8 +19,8 @@ type InspectReportTSMFlags struct { exact bool detailed bool - orgID, bucketID string - dataDir string + orgID, org, bucketID string + dataDir string } var inspectReportTSMFlags InspectReportTSMFlags @@ -63,7 +64,7 @@ in the following ways: inspectReportTSMCommand.Flags().BoolVarP(&inspectReportTSMFlags.detailed, "detailed", "", false, "emit series cardinality segmented by measurements, tag keys and fields. Warning, may take a while.") inspectReportTSMCommand.Flags().StringVarP(&inspectReportTSMFlags.orgID, "org-id", "", "", "process only data belonging to organization ID.") - inspectReportTSMCommand.Flags().StringVarP(&inspectReportTSMFlags.orgID, "org", "o", "", "process only data belonging to organization ID.") + inspectReportTSMCommand.Flags().StringVarP(&inspectReportTSMFlags.org, "org", "o", "", "process only data belonging to organization name.") inspectReportTSMCommand.Flags().StringVarP(&inspectReportTSMFlags.bucketID, "bucket-id", "", "", "process only data belonging to bucket ID. Requires org flag to be set.") dir, err := fs.InfluxDir() @@ -76,6 +77,11 @@ in the following ways: // inspectReportTSMF runs the report-tsm tool. func inspectReportTSMF(cmd *cobra.Command, args []string) error { + if inspectReportTSMFlags.orgID == "" && inspectReportTSMFlags.org == "" { + return fmt.Errorf("must specify org-id, or org name") + } else if inspectReportTSMFlags.orgID != "" && inspectReportTSMFlags.org != "" { + return fmt.Errorf("must specify org-id, or org name not both") + } report := &tsm1.Report{ Stderr: os.Stderr, Stdout: os.Stdout, @@ -85,16 +91,28 @@ func inspectReportTSMF(cmd *cobra.Command, args []string) error { Exact: inspectReportTSMFlags.exact, } - if inspectReportTSMFlags.orgID == "" && inspectReportTSMFlags.bucketID != "" { + if (inspectReportTSMFlags.org == "" || inspectReportTSMFlags.orgID == "") && inspectReportTSMFlags.bucketID != "" { return errors.New("org-id must be set for non-empty bucket-id") } if inspectReportTSMFlags.orgID != "" { - orgID, err := influxdb.IDFromString(inspectReportTSMFlags.orgID) + var err error + report.OrgID, err = influxdb.IDFromString(inspectReportTSMFlags.orgID) if err != nil { - return err + return fmt.Errorf("invalid org ID provided: %s", err.Error()) + } + } else if inspectReportTSMFlags.org != "" { + orgSvc, err := newOrganizationService() + if err != nil { + return fmt.Errorf("failed to initialize organization service client: %v", err) + } + + filter := influxdb.OrganizationFilter{Name: &inspectReportTSMFlags.org} + org, err := orgSvc.FindOrganization(context.Background(), filter) + if err != nil { + return fmt.Errorf("%v", err) } - report.OrgID = orgID + report.OrgID = &org.ID } if inspectReportTSMFlags.bucketID != "" {