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): bucket create to accept org name as flag #16187

Merged
merged 3 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -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
1 change: 1 addition & 0 deletions cmd/influx/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just caught this. will patch this up

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 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't going to work, breaks the pattern for the builder...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any SVC dep needs to be provided through DI not dumped in here globally

needs fixup

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching, we'll fix in the morning.

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