Skip to content

Commit

Permalink
feat(cmd): bucket create to accept org name as flag (#16187)
Browse files Browse the repository at this point in the history
* enables the user to specify an organization name when creating a bucket.
  • Loading branch information
dearyhud authored and alexpaxton committed Jan 9, 2020
1 parent 75917b4 commit 13146ce
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 19 additions & 2 deletions cmd/influx/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func bucketF(cmd *cobra.Command, args []string) {
type BucketCreateFlags struct {
name string
orgID string
org string
retention time.Duration
}

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

b.OrgID = org.ID
}

if err := s.CreateBucket(context.Background(), b); err != nil {
Expand Down
31 changes: 25 additions & 6 deletions cmd/influx/inspect.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"fmt"
"os"
Expand All @@ -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
Expand Down Expand Up @@ -63,6 +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.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()
Expand All @@ -75,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,
Expand All @@ -84,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 != "" {
Expand Down
32 changes: 27 additions & 5 deletions cmd/influx/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type cmdPkgBuilder struct {
hasTableBorders bool
meta pkger.Metadata
orgID string
org string
quiet bool

applyOpts struct {
Expand Down Expand Up @@ -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")
Expand All @@ -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))
Expand Down
9 changes: 7 additions & 2 deletions cmd/influx/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -155,14 +155,19 @@ 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")

taskCmd.AddCommand(taskFindCmd)
}

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,
Expand Down
30 changes: 28 additions & 2 deletions cmd/influx/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"errors"
"fmt"
"os"

platform "github.com/influxdata/influxdb"
Expand Down Expand Up @@ -113,6 +114,7 @@ var userCreateFlags struct {
name string
password string
orgID string
org string
}

func userCreateCmd() *cobra.Command {
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 13146ce

Please sign in to comment.