2019-02-19 23:47:19 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2019-03-05 20:54:32 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2019-02-19 23:47:19 +00:00
|
|
|
"github.com/influxdata/influxdb"
|
|
|
|
"github.com/influxdata/influxdb/rand"
|
|
|
|
"github.com/influxdata/influxdb/snowflake"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ influxdb.UserService = (*Service)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// OpPrefix is the prefix for kv errors.
|
|
|
|
const OpPrefix = "kv/"
|
|
|
|
|
|
|
|
// Service is the struct that influxdb services are implemented on.
|
|
|
|
type Service struct {
|
|
|
|
kv Store
|
|
|
|
Logger *zap.Logger
|
|
|
|
|
|
|
|
IDGenerator influxdb.IDGenerator
|
|
|
|
TokenGenerator influxdb.TokenGenerator
|
|
|
|
Hash Crypt
|
|
|
|
|
|
|
|
time func() time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns an instance of a Service.
|
|
|
|
func NewService(kv Store) *Service {
|
|
|
|
return &Service{
|
|
|
|
Logger: zap.NewNop(),
|
|
|
|
IDGenerator: snowflake.NewIDGenerator(),
|
|
|
|
TokenGenerator: rand.NewTokenGenerator(64),
|
|
|
|
Hash: &Bcrypt{},
|
|
|
|
kv: kv,
|
|
|
|
time: time.Now,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize creates Buckets needed.
|
|
|
|
func (s *Service) Initialize(ctx context.Context) error {
|
2019-03-05 00:38:10 +00:00
|
|
|
return s.kv.Update(ctx, func(tx Tx) error {
|
2019-02-19 23:47:19 +00:00
|
|
|
if err := s.initializeAuths(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-04 17:41:24 +00:00
|
|
|
if err := s.initializeDocuments(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:47:19 +00:00
|
|
|
if err := s.initializeBuckets(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeDashboards(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeKVLog(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeLabels(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeOnboarding(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeOrgs(ctx, tx); err != nil {
|
|
|
|
return err
|
2019-04-09 22:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeTasks(ctx, tx); err != nil {
|
|
|
|
return err
|
2019-02-19 23:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializePasswords(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeScraperTargets(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeSecrets(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeSessions(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeSources(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeTelegraf(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeURMs(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeVariables(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.initializeUsers(ctx, tx)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithTime sets the function for computing the current time. Used for updating meta data
|
|
|
|
// about objects stored. Should only be used in tests for mocking.
|
|
|
|
func (s *Service) WithTime(fn func() time.Time) {
|
|
|
|
s.time = fn
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithStore sets kv store for the service.
|
|
|
|
// Should only be used in tests for mocking.
|
|
|
|
func (s *Service) WithStore(store Store) {
|
|
|
|
s.kv = store
|
|
|
|
}
|