Merge pull request #1120 from influxdata/fix/onboarding_change

Fix/onboarding change
pull/10616/head
kelwang 2018-10-19 10:59:07 -04:00 committed by GitHub
commit 022bdc86d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 32 deletions

View File

@ -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

View File

@ -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"`

View File

@ -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 {

View File

@ -4638,6 +4638,13 @@ components:
type: string
bucket:
type: string
retentionPeriodHrs:
type: integer
required:
- username
- password
- org
- bucket
OnboardingResponse:
type: object
properties:

View File

@ -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.

View File

@ -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),