-
Notifications
You must be signed in to change notification settings - Fork 5
/
mgmtcomponent.go
234 lines (198 loc) · 7.49 KB
/
mgmtcomponent.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package gocbcorex
import (
"context"
"net/http"
"time"
"github.com/couchbase/gocbcorex/cbhttpx"
"github.com/couchbase/gocbcorex/cbmgmtx"
"github.com/couchbase/gocbcorex/contrib/cbconfig"
"go.uber.org/zap"
)
type MgmtComponent struct {
baseHttpComponent
logger *zap.Logger
}
type MgmtComponentConfig struct {
HttpRoundTripper http.RoundTripper
Endpoints map[string]string
Authenticator Authenticator
}
type MgmtComponentOptions struct {
Logger *zap.Logger
UserAgent string
}
func OrchestrateMgmtEndpoint[RespT any](
ctx context.Context,
w *MgmtComponent,
fn func(roundTripper http.RoundTripper, endpoint, username, password string) (RespT, error),
) (RespT, error) {
roundTripper, _, endpoint, username, password, err := w.SelectEndpoint(nil)
if err != nil {
var emptyResp RespT
return emptyResp, err
}
if endpoint == "" {
var emptyResp RespT
return emptyResp, serviceNotAvailableError{Service: ServiceTypeMgmt}
}
return fn(roundTripper, endpoint, username, password)
}
func NewMgmtComponent(retries RetryManager, config *MgmtComponentConfig, opts *MgmtComponentOptions) *MgmtComponent {
return &MgmtComponent{
baseHttpComponent: baseHttpComponent{
serviceType: ServiceTypeMgmt,
userAgent: opts.UserAgent,
state: &baseHttpComponentState{
httpRoundTripper: config.HttpRoundTripper,
endpoints: config.Endpoints,
authenticator: config.Authenticator,
},
},
logger: opts.Logger,
}
}
func (w *MgmtComponent) Reconfigure(config *MgmtComponentConfig) error {
w.updateState(baseHttpComponentState{
httpRoundTripper: config.HttpRoundTripper,
endpoints: config.Endpoints,
authenticator: config.Authenticator,
})
return nil
}
func OrchestrateSimpleMgmtCall[OptsT any, RespT any](
ctx context.Context,
w *MgmtComponent,
execFn func(o cbmgmtx.Management, ctx context.Context, req OptsT) (RespT, error),
opts OptsT,
) (RespT, error) {
return OrchestrateMgmtEndpoint(ctx, w,
func(roundTripper http.RoundTripper, endpoint, username, password string) (RespT, error) {
return execFn(cbmgmtx.Management{
UserAgent: w.userAgent,
Transport: roundTripper,
Endpoint: endpoint,
Username: username,
Password: password,
}, ctx, opts)
})
}
func OrchestrateNoResMgmtCall[OptsT any](
ctx context.Context,
w *MgmtComponent,
execFn func(o cbmgmtx.Management, ctx context.Context, req OptsT) error,
opts OptsT,
) error {
_, err := OrchestrateMgmtEndpoint(ctx, w,
func(roundTripper http.RoundTripper, endpoint, username, password string) (interface{}, error) {
return nil, execFn(cbmgmtx.Management{
UserAgent: w.userAgent,
Transport: roundTripper,
Endpoint: endpoint,
Username: username,
Password: password,
}, ctx, opts)
})
return err
}
type GetMgmtEndpointResult struct {
RoundTripper http.RoundTripper
Endpoint string
Username string
Password string
}
func (w *MgmtComponent) GetEndpoint(ctx context.Context) (*GetMgmtEndpointResult, error) {
return OrchestrateMgmtEndpoint(ctx, w,
func(roundTripper http.RoundTripper, endpoint, username, password string) (*GetMgmtEndpointResult, error) {
return &GetMgmtEndpointResult{
RoundTripper: roundTripper,
Endpoint: endpoint,
Username: username,
Password: password,
}, nil
})
}
func (w *MgmtComponent) GetCollectionManifest(ctx context.Context, opts *cbmgmtx.GetCollectionManifestOptions) (*cbconfig.CollectionManifestJson, error) {
return OrchestrateSimpleMgmtCall(ctx, w, cbmgmtx.Management.GetCollectionManifest, opts)
}
func (w *MgmtComponent) CreateScope(ctx context.Context, opts *cbmgmtx.CreateScopeOptions) (*cbmgmtx.CreateScopeResponse, error) {
return OrchestrateSimpleMgmtCall(ctx, w, cbmgmtx.Management.CreateScope, opts)
}
func (w *MgmtComponent) DeleteScope(ctx context.Context, opts *cbmgmtx.DeleteScopeOptions) (*cbmgmtx.DeleteScopeResponse, error) {
return OrchestrateSimpleMgmtCall(ctx, w, cbmgmtx.Management.DeleteScope, opts)
}
func (w *MgmtComponent) CreateCollection(ctx context.Context, opts *cbmgmtx.CreateCollectionOptions) (*cbmgmtx.CreateCollectionResponse, error) {
return OrchestrateSimpleMgmtCall(ctx, w, cbmgmtx.Management.CreateCollection, opts)
}
func (w *MgmtComponent) DeleteCollection(ctx context.Context, opts *cbmgmtx.DeleteCollectionOptions) (*cbmgmtx.DeleteCollectionResponse, error) {
return OrchestrateSimpleMgmtCall(ctx, w, cbmgmtx.Management.DeleteCollection, opts)
}
func (w *MgmtComponent) UpdateCollection(ctx context.Context, opts *cbmgmtx.UpdateCollectionOptions) (*cbmgmtx.UpdateCollectionResponse, error) {
return OrchestrateSimpleMgmtCall(ctx, w, cbmgmtx.Management.UpdateCollection, opts)
}
func (w *MgmtComponent) GetAllBuckets(ctx context.Context, opts *cbmgmtx.GetAllBucketsOptions) ([]*cbmgmtx.BucketDef, error) {
return OrchestrateSimpleMgmtCall(ctx, w, cbmgmtx.Management.GetAllBuckets, opts)
}
func (w *MgmtComponent) GetBucket(ctx context.Context, opts *cbmgmtx.GetBucketOptions) (*cbmgmtx.BucketDef, error) {
return OrchestrateSimpleMgmtCall(ctx, w, cbmgmtx.Management.GetBucket, opts)
}
func (w *MgmtComponent) CreateBucket(ctx context.Context, opts *cbmgmtx.CreateBucketOptions) error {
return OrchestrateNoResMgmtCall(ctx, w, cbmgmtx.Management.CreateBucket, opts)
}
func (w *MgmtComponent) UpdateBucket(ctx context.Context, opts *cbmgmtx.UpdateBucketOptions) error {
return OrchestrateNoResMgmtCall(ctx, w, cbmgmtx.Management.UpdateBucket, opts)
}
func (w *MgmtComponent) FlushBucket(ctx context.Context, opts *cbmgmtx.FlushBucketOptions) error {
return OrchestrateNoResMgmtCall(ctx, w, cbmgmtx.Management.FlushBucket, opts)
}
func (w *MgmtComponent) DeleteBucket(ctx context.Context, opts *cbmgmtx.DeleteBucketOptions) error {
return OrchestrateNoResMgmtCall(ctx, w, cbmgmtx.Management.DeleteBucket, opts)
}
func (w *MgmtComponent) CheckBucketExists(ctx context.Context, opts *cbmgmtx.CheckBucketExistsOptions) (bool, error) {
return OrchestrateSimpleMgmtCall(ctx, w, cbmgmtx.Management.CheckBucketExists, opts)
}
type EnsureBucketOptions struct {
BucketName string
BucketUUID string
WantMissing bool
OnBehalfOf *cbhttpx.OnBehalfOfInfo
}
func (w *MgmtComponent) EnsureBucket(ctx context.Context, opts *EnsureBucketOptions) error {
hlpr := cbmgmtx.EnsureBucketHelper{
Logger: w.logger.Named("ensure-bucket"),
UserAgent: w.userAgent,
OnBehalfOf: opts.OnBehalfOf,
BucketName: opts.BucketName,
BucketUUID: opts.BucketUUID,
WantMissing: opts.WantMissing,
}
b := ExponentialBackoff(100*time.Millisecond, 1*time.Second, 1.5)
return w.ensureResource(ctx, b, func(ctx context.Context, roundTripper http.RoundTripper,
ensureTargets baseHttpTargets) (bool, error) {
return hlpr.Poll(ctx, &cbmgmtx.EnsureBucketPollOptions{
Transport: roundTripper,
Targets: ensureTargets.ToMgmtx(),
})
})
}
type EnsureManifestOptions struct {
BucketName string
ManifestUid uint64
OnBehalfOf *cbhttpx.OnBehalfOfInfo
}
func (w *MgmtComponent) EnsureManifest(ctx context.Context, opts *EnsureManifestOptions) error {
hlpr := cbmgmtx.EnsureManifestHelper{
Logger: w.logger.Named("ensure-manifest"),
UserAgent: w.userAgent,
OnBehalfOf: opts.OnBehalfOf,
BucketName: opts.BucketName,
ManifestUid: opts.ManifestUid,
}
b := ExponentialBackoff(100*time.Millisecond, 1*time.Second, 1.5)
return w.ensureResource(ctx, b, func(ctx context.Context, roundTripper http.RoundTripper,
ensureTargets baseHttpTargets) (bool, error) {
return hlpr.Poll(ctx, &cbmgmtx.EnsureManifestPollOptions{
Transport: roundTripper,
Targets: ensureTargets.ToMgmtx(),
})
})
}