diff --git a/bolt/onboarding.go b/bolt/onboarding.go index 77636ac703..f7ec16e0c8 100644 --- a/bolt/onboarding.go +++ b/bolt/onboarding.go @@ -2,6 +2,7 @@ package bolt import ( "context" + "time" bolt "github.com/coreos/bbolt" "github.com/influxdata/platform" @@ -99,9 +100,10 @@ func (c *Client) Generate(ctx context.Context, req *platform.OnboardingRequest) return nil, err } bucket := &platform.Bucket{ - Name: req.Bucket, - Organization: o.Name, - OrganizationID: o.ID, + Name: req.Bucket, + Organization: o.Name, + OrganizationID: o.ID, + RetentionPeriod: time.Duration(req.RetentionPeriod) * time.Hour, } if err = c.CreateBucket(ctx, bucket); err != nil { return nil, err diff --git a/bucket.go b/bucket.go index 27ee514643..c9873d3501 100644 --- a/bucket.go +++ b/bucket.go @@ -14,6 +14,9 @@ const ( BucketTypeLogs = BucketType(iota + 10) ) +// InfiniteRetention is default infinite retention period. +const InfiniteRetention = 0 + // Bucket is a bucket. 🎉 type Bucket struct { ID ID `json:"id,omitempty"` diff --git a/cmd/influx/setup.go b/cmd/influx/setup.go index acf59aa319..ebc63c315a 100644 --- a/cmd/influx/setup.go +++ b/cmd/influx/setup.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "os" + "strconv" "strings" "github.com/influxdata/platform" @@ -36,9 +37,9 @@ func setupF(cmd *cobra.Command, args []string) { os.Exit(0) } - or := getOnboardingRequest() + req := getOnboardingRequest() - result, err := s.Generate(context.Background(), or) + result, err := s.Generate(context.Background(), req) if err != nil { fmt.Println(err) os.Exit(1) @@ -61,24 +62,33 @@ func setupF(cmd *cobra.Command, args []string) { w.Flush() } -func getOnboardingRequest() (or *platform.OnboardingRequest) { +func getOnboardingRequest() (req *platform.OnboardingRequest) { ui := &input.UI{ Writer: os.Stdout, Reader: os.Stdin, } - or = new(platform.OnboardingRequest) + req = new(platform.OnboardingRequest) fmt.Println(promptWithColor(`Welcome to InfluxDB 2.0!`, colorYellow)) - or.User = getInput(ui, "Please type your primary username", "") - or.Password = getPassword(ui) - or.Org = getInput(ui, "Please type your primary organization name.\r\nOr ENTER to use \"default\"", "default") - or.Bucket = getInput(ui, "Please type your primary bucket name.\r\nOr ENTER to use \"default\"", "default") - - if confirmed := getConfirm(ui, or); !confirmed { - fmt.Println("Setup is canceled.") - os.Exit(1) + req.User = getInput(ui, "Please type your primary username", "") + req.Password = getPassword(ui) + req.Org = getInput(ui, "Please type your primary organization name.", "") + req.Bucket = getInput(ui, "Please type your primary bucket name.", "") + for { + rpStr := getInput(ui, "Please type your retention period in hours (exp 168 for 1 week).\r\nOr press ENTER for infinite.", strconv.Itoa(platform.InfiniteRetention)) + rp, err := strconv.Atoi(rpStr) + if rp >= 0 && err == nil { + req.RetentionPeriod = uint(rp) + break + } } - return or + if confirmed := getConfirm(ui, req); !confirmed { + fmt.Println("Setup is canceled.") + // user cancel + os.Exit(0) + } + + return req } // vt100EscapeCodes @@ -102,16 +112,22 @@ func promptWithColor(s string, color []byte) string { func getConfirm(ui *input.UI, or *platform.OnboardingRequest) bool { prompt := promptWithColor("Confirm? (y/n)", colorRed) for { + rp := "infinite" + if or.RetentionPeriod > 0 { + rp = fmt.Sprintf("%d hrs", or.RetentionPeriod) + } fmt.Print(promptWithColor(fmt.Sprintf(` You have entered: - Username: %s - Organization: %s - Bucket: %s -`, or.User, or.Org, or.Bucket), colorCyan)) + Username: %s + Organization: %s + Bucket: %s + Retention Period: %s +`, or.User, or.Org, or.Bucket, rp), colorCyan)) result, err := ui.Ask(prompt, &input.Options{ HideOrder: true, }) if err != nil { + // interrupt os.Exit(1) } switch result { diff --git a/http/swagger.yml b/http/swagger.yml index 7d592d5b46..c9296d5d82 100644 --- a/http/swagger.yml +++ b/http/swagger.yml @@ -4638,6 +4638,13 @@ components: type: string bucket: type: string + retentionPeriodHrs: + type: integer + required: + - username + - password + - org + - bucket OnboardingResponse: type: object properties: diff --git a/onboarding.go b/onboarding.go index 18fa74b713..a29cbfd780 100644 --- a/onboarding.go +++ b/onboarding.go @@ -13,10 +13,11 @@ type OnboardingResults struct { // OnboardingRequest is the request // to setup defaults. type OnboardingRequest struct { - User string `json:"username"` - Password string `json:"password"` - Org string `json:"org"` - Bucket string `json:"bucket"` + User string `json:"username"` + Password string `json:"password"` + Org string `json:"org"` + Bucket string `json:"bucket"` + RetentionPeriod uint `json:"retentionPeriodHrs,omitempty"` } // OnboardingService represents a service for the first run. diff --git a/testing/onboarding.go b/testing/onboarding.go index de94ca5e21..99fd618a75 100644 --- a/testing/onboarding.go +++ b/testing/onboarding.go @@ -3,6 +3,7 @@ package testing import ( "context" "testing" + "time" "github.com/google/go-cmp/cmp" "github.com/influxdata/platform" @@ -144,10 +145,11 @@ func Generate( }, args: args{ request: &platform.OnboardingRequest{ - User: "admin", - Org: "org1", - Bucket: "bucket1", - Password: "pass1", + User: "admin", + Org: "org1", + Bucket: "bucket1", + Password: "pass1", + RetentionPeriod: 24 * 7, // 1 week }, }, wants: wants{ @@ -162,10 +164,11 @@ func Generate( Name: "org1", }, Bucket: &platform.Bucket{ - ID: MustIDBase16(threeID), - Name: "bucket1", - Organization: "org1", - OrganizationID: MustIDBase16(twoID), + ID: MustIDBase16(threeID), + Name: "bucket1", + Organization: "org1", + OrganizationID: MustIDBase16(twoID), + RetentionPeriod: time.Hour * 24 * 7, }, Auth: &platform.Authorization{ ID: MustIDBase16(fourID),