-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
onboarding.go
178 lines (156 loc) · 4.28 KB
/
onboarding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package kv
import (
"context"
"errors"
"fmt"
"time"
"github.com/influxdata/influxdb/v2"
)
var (
onboardingBucket = []byte("onboardingv1")
onboardingKey = []byte("onboarding_key")
)
var _ influxdb.OnboardingService = (*Service)(nil)
func (s *Service) initializeOnboarding(ctx context.Context, tx Tx) error {
_, err := tx.Bucket(onboardingBucket)
return err
}
// IsOnboarding means if the initial setup of influxdb has happened.
// true means that the onboarding setup has not yet happened.
// false means that the onboarding has been completed.
func (s *Service) IsOnboarding(ctx context.Context) (bool, error) {
notSetup := true
err := s.kv.View(ctx, func(tx Tx) error {
bucket, err := tx.Bucket(onboardingBucket)
if err != nil {
return err
}
v, err := bucket.Get(onboardingKey)
// If the sentinel onboarding key is not found, then, setup
// has not been performed.
if IsNotFound(err) {
notSetup = true
return nil
}
if err != nil {
return err
}
// If the sentinel key has any bytes whatsoever, then,
if len(v) > 0 {
notSetup = false // this means that it is setup. I hate bools.
}
return nil
})
return notSetup, err
}
// PutOnboardingStatus will update the flag,
// so future onboarding request will be denied.
// true means that onboarding is NOT needed.
// false means that onboarding is needed.
func (s *Service) PutOnboardingStatus(ctx context.Context, hasBeenOnboarded bool) error {
return s.kv.Update(ctx, func(tx Tx) error {
return s.putOnboardingStatus(ctx, tx, hasBeenOnboarded)
})
}
func (s *Service) putOnboardingStatus(ctx context.Context, tx Tx, hasBeenOnboarded bool) error {
if hasBeenOnboarded {
return s.setOnboarded(ctx, tx)
}
return s.setOffboarded(ctx, tx)
}
func (s *Service) setOffboarded(ctx context.Context, tx Tx) error {
bucket, err := tx.Bucket(onboardingBucket)
if err != nil {
// TODO(goller): check err
return err
}
err = bucket.Delete(onboardingKey)
if err != nil {
// TODO(goller): check err
return err
}
return nil
}
func (s *Service) setOnboarded(ctx context.Context, tx Tx) error {
bucket, err := tx.Bucket(onboardingBucket)
if err != nil {
// TODO(goller): check err
return err
}
err = bucket.Put(onboardingKey, []byte{0x1})
if err != nil {
// TODO(goller): check err
return err
}
return nil
}
// OnboardInitialUser OnboardingResults from onboarding request,
// update db so this request will be disabled for the second run.
func (s *Service) OnboardInitialUser(ctx context.Context, req *influxdb.OnboardingRequest) (*influxdb.OnboardingResults, error) {
isOnboarding, err := s.IsOnboarding(ctx)
if err != nil {
return nil, err
}
if !isOnboarding {
return nil, &influxdb.Error{
Code: influxdb.EConflict,
Msg: "onboarding has already been completed",
}
}
if err := req.Valid(); err != nil {
return nil, err
}
u := &influxdb.User{Name: req.User}
o := &influxdb.Organization{Name: req.Org}
bucket := &influxdb.Bucket{
Name: req.Bucket,
RetentionPeriod: time.Duration(req.RetentionPeriod) * time.Hour,
}
mapping := &influxdb.UserResourceMapping{
ResourceType: influxdb.OrgsResourceType,
UserType: influxdb.Owner,
}
auth := &influxdb.Authorization{
Description: fmt.Sprintf("%s's Token", u.Name),
Permissions: influxdb.OperPermissions(),
Token: req.Token,
}
err = s.kv.Update(ctx, func(tx Tx) error {
if err := s.createUser(ctx, tx, u); err != nil {
return err
}
if err := s.setPassword(ctx, tx, u.ID, req.Password); err != nil {
return err
}
if err := s.createOrganization(ctx, tx, o); err != nil {
return err
}
bucket.OrgID = o.ID
if err := s.createBucket(ctx, tx, bucket); err != nil {
return err
}
mapping.ResourceID = o.ID
mapping.UserID = u.ID
if err := s.createUserResourceMapping(ctx, tx, mapping); err != nil {
return err
}
auth.UserID = u.ID
auth.OrgID = o.ID
if err := s.createAuthorization(ctx, tx, auth); err != nil {
return err
}
return s.putOnboardingStatus(ctx, tx, true)
})
if err != nil {
return nil, err
}
return &influxdb.OnboardingResults{
User: u,
Org: o,
Bucket: bucket,
Auth: auth,
}, nil
}
func (s *Service) OnboardUser(ctx context.Context, req *influxdb.OnboardingRequest) (*influxdb.OnboardingResults, error) {
return nil, errors.New("not yet implemented")
}