github.com/influxdata/influxdb/v2@v2.7.6/onboarding.go (about)

     1  package influxdb
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/influxdata/influxdb/v2/kit/platform/errors"
     8  )
     9  
    10  // OnboardingService represents a service for the first run.
    11  type OnboardingService interface {
    12  	// IsOnboarding determine if onboarding request is allowed.
    13  	IsOnboarding(ctx context.Context) (bool, error)
    14  
    15  	// OnboardInitialUser creates the initial org/user/bucket in the DB.
    16  	OnboardInitialUser(ctx context.Context, req *OnboardingRequest) (*OnboardingResults, error)
    17  }
    18  
    19  // OnboardingResults is a group of elements required for first run.
    20  type OnboardingResults struct {
    21  	User   *User          `json:"user"`
    22  	Org    *Organization  `json:"org"`
    23  	Bucket *Bucket        `json:"bucket"`
    24  	Auth   *Authorization `json:"auth"`
    25  }
    26  
    27  // OnboardingRequest is the request
    28  // to setup defaults.
    29  type OnboardingRequest struct {
    30  	User                      string        `json:"username"`
    31  	Password                  string        `json:"password"`
    32  	Org                       string        `json:"org"`
    33  	Bucket                    string        `json:"bucket"`
    34  	RetentionPeriodSeconds    int64         `json:"retentionPeriodSeconds,omitempty"`
    35  	RetentionPeriodDeprecated time.Duration `json:"retentionPeriodHrs,omitempty"`
    36  	Token                     string        `json:"token,omitempty"`
    37  }
    38  
    39  func (r *OnboardingRequest) Valid() error {
    40  	if r.User == "" {
    41  		return &errors.Error{
    42  			Code: errors.EEmptyValue,
    43  			Msg:  "username is empty",
    44  		}
    45  	}
    46  
    47  	if r.Org == "" {
    48  		return &errors.Error{
    49  			Code: errors.EEmptyValue,
    50  			Msg:  "org name is empty",
    51  		}
    52  	}
    53  
    54  	if r.Bucket == "" {
    55  		return &errors.Error{
    56  			Code: errors.EEmptyValue,
    57  			Msg:  "bucket name is empty",
    58  		}
    59  	}
    60  	return nil
    61  }
    62  
    63  func (r *OnboardingRequest) RetentionPeriod() time.Duration {
    64  	if r.RetentionPeriodSeconds > 0 {
    65  		return time.Duration(r.RetentionPeriodSeconds) * time.Second
    66  	}
    67  	return r.RetentionPeriodDeprecated
    68  }