Skip to content

Commit 336961c

Browse files
authored
feat(cmd): bucket create to accept org name as flag (#16187)
* enables the user to specify an organization name when creating a bucket.
1 parent f64c631 commit 336961c

File tree

6 files changed

+107
-17
lines changed

6 files changed

+107
-17
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
1. [15836](https://github.com/influxdata/influxdb/pull/16077): Add stacked line layer option to graphs
66
1. [16094](https://github.com/influxdata/influxdb/pull/16094): Annotate log messages with trace ID, if available
7+
1. [16187](https://github.com/influxdata/influxdb/pull/16187): Bucket create to accept an org name flag
78

89
### Bug Fixes
910

cmd/influx/bucket.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func bucketF(cmd *cobra.Command, args []string) {
2727
type BucketCreateFlags struct {
2828
name string
2929
orgID string
30+
org string
3031
retention time.Duration
3132
}
3233

@@ -42,6 +43,7 @@ func init() {
4243
bucketCreateCmd.Flags().StringVarP(&bucketCreateFlags.name, "name", "n", "", "Name of bucket that will be created")
4344
bucketCreateCmd.Flags().DurationVarP(&bucketCreateFlags.retention, "retention", "r", 0, "Duration in nanoseconds data will live in bucket")
4445
bucketCreateCmd.Flags().StringVarP(&bucketCreateFlags.orgID, "org-id", "", "", "The ID of the organization that owns the bucket")
46+
bucketCreateCmd.Flags().StringVarP(&bucketCreateFlags.org, "org", "o", "", "The org name")
4547
bucketCreateCmd.MarkFlagRequired("name")
4648

4749
bucketCmd.AddCommand(bucketCreateCmd)
@@ -63,8 +65,10 @@ func newBucketService(f Flags) (platform.BucketService, error) {
6365
}
6466

6567
func bucketCreateF(cmd *cobra.Command, args []string) error {
66-
if bucketCreateFlags.orgID == "" {
67-
return fmt.Errorf("must specify org-id")
68+
if bucketCreateFlags.orgID == "" && bucketCreateFlags.org == "" {
69+
return fmt.Errorf("must specify org-id, or org name")
70+
} else if bucketCreateFlags.orgID != "" && bucketCreateFlags.org != "" {
71+
return fmt.Errorf("must specify org-id, or org name not both")
6872
}
6973

7074
s, err := newBucketService(flags)
@@ -83,6 +87,19 @@ func bucketCreateF(cmd *cobra.Command, args []string) error {
8387
return fmt.Errorf("failed to decode org id %q: %v", bucketCreateFlags.orgID, err)
8488
}
8589
b.OrgID = *id
90+
} else if bucketCreateFlags.org != "" {
91+
orgSvc, err := newOrganizationService()
92+
if err != nil {
93+
return fmt.Errorf("failed to initialize organization service client: %v", err)
94+
}
95+
96+
filter := platform.OrganizationFilter{Name: &bucketCreateFlags.org}
97+
org, err := orgSvc.FindOrganization(context.Background(), filter)
98+
if err != nil {
99+
return err
100+
}
101+
102+
b.OrgID = org.ID
86103
}
87104

88105
if err := s.CreateBucket(context.Background(), b); err != nil {

cmd/influx/inspect.go

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"os"
@@ -18,8 +19,8 @@ type InspectReportTSMFlags struct {
1819
exact bool
1920
detailed bool
2021

21-
orgID, bucketID string
22-
dataDir string
22+
orgID, org, bucketID string
23+
dataDir string
2324
}
2425

2526
var inspectReportTSMFlags InspectReportTSMFlags
@@ -63,6 +64,7 @@ in the following ways:
6364
inspectReportTSMCommand.Flags().BoolVarP(&inspectReportTSMFlags.detailed, "detailed", "", false, "emit series cardinality segmented by measurements, tag keys and fields. Warning, may take a while.")
6465

6566
inspectReportTSMCommand.Flags().StringVarP(&inspectReportTSMFlags.orgID, "org-id", "", "", "process only data belonging to organization ID.")
67+
inspectReportTSMCommand.Flags().StringVarP(&inspectReportTSMFlags.org, "org", "o", "", "process only data belonging to organization name.")
6668
inspectReportTSMCommand.Flags().StringVarP(&inspectReportTSMFlags.bucketID, "bucket-id", "", "", "process only data belonging to bucket ID. Requires org flag to be set.")
6769

6870
dir, err := fs.InfluxDir()
@@ -75,6 +77,11 @@ in the following ways:
7577

7678
// inspectReportTSMF runs the report-tsm tool.
7779
func inspectReportTSMF(cmd *cobra.Command, args []string) error {
80+
if inspectReportTSMFlags.orgID == "" && inspectReportTSMFlags.org == "" {
81+
return fmt.Errorf("must specify org-id, or org name")
82+
} else if inspectReportTSMFlags.orgID != "" && inspectReportTSMFlags.org != "" {
83+
return fmt.Errorf("must specify org-id, or org name not both")
84+
}
7885
report := &tsm1.Report{
7986
Stderr: os.Stderr,
8087
Stdout: os.Stdout,
@@ -84,16 +91,28 @@ func inspectReportTSMF(cmd *cobra.Command, args []string) error {
8491
Exact: inspectReportTSMFlags.exact,
8592
}
8693

87-
if inspectReportTSMFlags.orgID == "" && inspectReportTSMFlags.bucketID != "" {
94+
if (inspectReportTSMFlags.org == "" || inspectReportTSMFlags.orgID == "") && inspectReportTSMFlags.bucketID != "" {
8895
return errors.New("org-id must be set for non-empty bucket-id")
8996
}
9097

9198
if inspectReportTSMFlags.orgID != "" {
92-
orgID, err := influxdb.IDFromString(inspectReportTSMFlags.orgID)
99+
var err error
100+
report.OrgID, err = influxdb.IDFromString(inspectReportTSMFlags.orgID)
93101
if err != nil {
94-
return err
102+
return fmt.Errorf("invalid org ID provided: %s", err.Error())
103+
}
104+
} else if inspectReportTSMFlags.org != "" {
105+
orgSvc, err := newOrganizationService()
106+
if err != nil {
107+
return fmt.Errorf("failed to initialize organization service client: %v", err)
108+
}
109+
110+
filter := influxdb.OrganizationFilter{Name: &inspectReportTSMFlags.org}
111+
org, err := orgSvc.FindOrganization(context.Background(), filter)
112+
if err != nil {
113+
return fmt.Errorf("%v", err)
95114
}
96-
report.OrgID = orgID
115+
report.OrgID = &org.ID
97116
}
98117

99118
if inspectReportTSMFlags.bucketID != "" {

cmd/influx/pkg.go

+27-5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type cmdPkgBuilder struct {
4444
hasTableBorders bool
4545
meta pkger.Metadata
4646
orgID string
47+
org string
4748
quiet bool
4849

4950
applyOpts struct {
@@ -95,8 +96,8 @@ func (b *cmdPkgBuilder) cmdPkgApply() *cobra.Command {
9596
cmd.Flags().IntVarP(&b.applyReqLimit, "req-limit", "r", 0, "Request limit for applying a pkg, defaults to 5(recommended for OSS).")
9697
cmd.Flags().StringVar(&b.applyOpts.force, "force", "", `TTY input, if package will have destructive changes, proceed if set "true".`)
9798

98-
cmd.Flags().StringVarP(&b.orgID, "org-id", "o", "", "The ID of the organization that owns the bucket")
99-
cmd.MarkFlagRequired("org-id")
99+
cmd.Flags().StringVarP(&b.orgID, "org-id", "", "", "The ID of the organization that owns the bucket")
100+
cmd.Flags().StringVarP(&b.org, "org", "o", "", "The name of the organization that owns the bucket")
100101

101102
cmd.Flags().BoolVarP(&b.hasColor, "color", "c", true, "Enable color in output, defaults true")
102103
cmd.Flags().BoolVar(&b.hasTableBorders, "table-borders", true, "Enable table borders, defaults true")
@@ -108,11 +109,32 @@ func (b *cmdPkgBuilder) cmdPkgApply() *cobra.Command {
108109

109110
func (b *cmdPkgBuilder) pkgApplyRunEFn() func(*cobra.Command, []string) error {
110111
return func(cmd *cobra.Command, args []string) (e error) {
112+
if b.orgID == "" && b.org == "" {
113+
return fmt.Errorf("must specify org-id, or org name")
114+
} else if b.orgID != "" && b.org != "" {
115+
return fmt.Errorf("must specify org-id, or org name not both")
116+
}
111117
color.NoColor = !b.hasColor
118+
var influxOrgID *influxdb.ID
112119

113-
influxOrgID, err := influxdb.IDFromString(b.orgID)
114-
if err != nil {
115-
return fmt.Errorf("invalid org ID provided: %s", err.Error())
120+
if b.orgID != "" {
121+
var err error
122+
influxOrgID, err = influxdb.IDFromString(b.orgID)
123+
if err != nil {
124+
return fmt.Errorf("invalid org ID provided: %s", err.Error())
125+
}
126+
} else if b.org != "" {
127+
orgSvc, err := newOrganizationService()
128+
if err != nil {
129+
return fmt.Errorf("failed to initialize organization service client: %v", err)
130+
}
131+
132+
filter := influxdb.OrganizationFilter{Name: &b.org}
133+
org, err := orgSvc.FindOrganization(context.Background(), filter)
134+
if err != nil {
135+
return fmt.Errorf("%v", err)
136+
}
137+
influxOrgID = &org.ID
116138
}
117139

118140
svc, err := b.svcFn(flags.httpClientOpts(), pkger.WithApplyReqLimit(b.applyReqLimit))

cmd/influx/task.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func init() {
7070
RunE: wrapCheckSetup(taskCreateF),
7171
}
7272

73-
taskCreateCmd.Flags().StringVarP(&taskCreateFlags.org, "org", "", "", "organization name")
73+
taskCreateCmd.Flags().StringVarP(&taskCreateFlags.org, "org", "o", "", "organization name")
7474
taskCreateCmd.Flags().StringVarP(&taskCreateFlags.orgID, "org-id", "", "", "id of the organization that owns the task")
7575
taskCreateCmd.MarkFlagRequired("flux")
7676

@@ -155,14 +155,19 @@ func init() {
155155

156156
taskFindCmd.Flags().StringVarP(&taskFindFlags.id, "id", "i", "", "task ID")
157157
taskFindCmd.Flags().StringVarP(&taskFindFlags.user, "user-id", "n", "", "task owner ID")
158-
taskFindCmd.Flags().StringVarP(&taskFindFlags.org, "org", "", "", "task organization name")
158+
taskFindCmd.Flags().StringVarP(&taskFindFlags.org, "org", "o", "", "task organization name")
159159
taskFindCmd.Flags().StringVarP(&taskFindFlags.orgID, "org-id", "", "", "task organization ID")
160160
taskFindCmd.Flags().IntVarP(&taskFindFlags.limit, "limit", "", platform.TaskDefaultPageSize, "the number of tasks to find")
161161

162162
taskCmd.AddCommand(taskFindCmd)
163163
}
164164

165165
func taskFindF(cmd *cobra.Command, args []string) error {
166+
if taskFindFlags.orgID == "" && taskFindFlags.org == "" {
167+
return fmt.Errorf("must specify org-id, or org name")
168+
} else if taskFindFlags.orgID != "" && taskFindFlags.org != "" {
169+
return fmt.Errorf("must specify org-id, or org name not both")
170+
}
166171
s := &http.TaskService{
167172
Addr: flags.host,
168173
Token: flags.token,

cmd/influx/user.go

+28-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"errors"
6+
"fmt"
67
"os"
78

89
platform "github.com/influxdata/influxdb"
@@ -113,6 +114,7 @@ var userCreateFlags struct {
113114
name string
114115
password string
115116
orgID string
117+
org string
116118
}
117119

118120
func userCreateCmd() *cobra.Command {
@@ -125,12 +127,19 @@ func userCreateCmd() *cobra.Command {
125127
cmd.Flags().StringVarP(&userCreateFlags.name, "name", "n", "", "The user name (required)")
126128
cmd.MarkFlagRequired("name")
127129
cmd.Flags().StringVarP(&userCreateFlags.password, "password", "p", "", "The user password")
128-
cmd.Flags().StringVarP(&userCreateFlags.orgID, "org-id", "o", "", "The organization id the user belongs too. Is required if password provided.")
130+
cmd.Flags().StringVarP(&userCreateFlags.orgID, "org-id", "", "", "The organization id the user belongs to. Is required if password provided.")
131+
cmd.Flags().StringVarP(&userCreateFlags.org, "org", "o", "", "The organization name the user belongs to. Is required if password provided.")
129132

130133
return cmd
131134
}
132135

133136
func userCreateF(cmd *cobra.Command, args []string) error {
137+
if userCreateFlags.orgID == "" && userCreateFlags.org == "" {
138+
return errors.New("must specify org-id, or org name")
139+
} else if userCreateFlags.orgID != "" && userCreateFlags.org != "" {
140+
return errors.New("must specify org-id, or org name not both")
141+
}
142+
134143
s, err := newUserService()
135144
if err != nil {
136145
return err
@@ -161,7 +170,24 @@ func userCreateF(cmd *cobra.Command, args []string) error {
161170
return nil
162171
}
163172

164-
orgIDStr := userCreateFlags.orgID
173+
var orgIDStr string
174+
175+
if userCreateFlags.orgID != "" {
176+
orgIDStr = userCreateFlags.orgID
177+
} else if userCreateFlags.org != "" {
178+
orgSvc, err := newOrganizationService()
179+
if err != nil {
180+
return fmt.Errorf("failed to initialize organization service client: %v", err)
181+
}
182+
183+
filter := platform.OrganizationFilter{Name: &bucketCreateFlags.org}
184+
org, err := orgSvc.FindOrganization(context.Background(), filter)
185+
if err != nil {
186+
return fmt.Errorf("%v", err)
187+
}
188+
189+
orgIDStr = org.ID.GoString()
190+
}
165191
pass := userCreateFlags.password
166192
if orgIDStr == "" && pass == "" {
167193
return writeOutput([]string{"ID", "Name"}, user.ID.String(), user.Name)

0 commit comments

Comments
 (0)