forked from rexray/rexray
/
context.go
501 lines (424 loc) · 13.2 KB
/
context.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
package context
import (
"context"
"fmt"
"net/http"
"os"
"strings"
log "github.com/sirupsen/logrus"
gcontext "github.com/gorilla/context"
"github.com/rexray/rexray/libstorage/api/types"
)
type lsc struct {
context.Context
key interface{}
val interface{}
req *http.Request
right context.Context
logger *log.Logger
pathConfig *types.PathConfig
loggerInherited bool
}
func newContext(
parent context.Context,
key interface{},
val interface{},
req *http.Request,
right context.Context) *lsc {
if parent != nil && key == nil && val == nil && req == nil && right == nil {
if ctx, ok := parent.(*lsc); ok {
return ctx
}
}
if parent == nil {
parent = context.Background()
}
// figure out who the parent logger instance is. if there is none,
// reference the log.StandardLogger as the parent.
var logger *log.Logger
if ctx, ok := parent.(*lsc); ok {
logger = ctx.logger
}
if logger == nil {
var lvl log.Level
if types.Debug {
lvl = log.DebugLevel
} else {
ll, err := log.ParseLevel(os.Getenv("LIBSTORAGE_LOGGING_LEVEL"))
if err != nil {
ll = log.WarnLevel
}
lvl = ll
}
logger = &log.Logger{
Formatter: log.StandardLogger().Formatter,
Hooks: log.StandardLogger().Hooks,
Level: lvl,
Out: types.Stderr,
}
}
// forward the pathConfig reference
var pathConfig *types.PathConfig
if key == PathConfigKey {
pathConfig = val.(*types.PathConfig)
} else if ctx, ok := parent.(*lsc); ok {
pathConfig = ctx.pathConfig
}
return &lsc{
Context: parent,
key: key,
val: val,
req: req,
right: right,
logger: logger,
pathConfig: pathConfig,
loggerInherited: true,
}
}
type hasRightSide interface {
rightSide() context.Context
}
func (ctx *lsc) rightSide() context.Context {
return ctx.right
}
// New returns a new context with the provided parent.
func New(parent context.Context) types.Context {
return newContext(parent, nil, nil, nil, nil)
}
// Background returns a new context with logging capabilities.
func Background() types.Context {
return New(nil)
}
// WithCancel returns a copy of parent with a new Done channel. The
// returned context's Done channel is closed when the returned cancel
// function is called or when the parent context's Done channel is
// closed, whichever happens first.
func WithCancel(parent context.Context) (types.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(parent)
return New(ctx), cancel
}
// WithRequestRoute returns a new context with the injected *http.Request
// and Route.
func WithRequestRoute(
parent context.Context,
req *http.Request,
route types.Route) types.Context {
return newContext(parent, RouteKey, route, req, nil)
}
// WithStorageService returns a new context with the StorageService as the
// value and attempts to assign the service's associated InstanceID and
// LocalDevices (by way of the service's StorageDriver) to the context as well.
func WithStorageService(
parent context.Context, service types.StorageService) types.Context {
var (
serviceName = strings.ToLower(service.Name())
driver = service.Driver()
driverName = strings.ToLower(driver.Name())
)
parent = newContext(parent, DriverKey, driver, nil, nil)
// set the service's InstanceID if present
if iidm, ok := parent.Value(AllInstanceIDsKey).(types.InstanceIDMap); ok {
if iid, ok := iidm[serviceName]; ok {
parent = newContext(parent, InstanceIDKey, iid, nil, nil)
}
}
// set the service's LocalDevices if present
if ldm, ok := parent.Value(AllLocalDevicesKey).(types.LocalDevicesMap); ok {
if ld, ok := ldm[driverName]; ok {
parent = newContext(parent, LocalDevicesKey, ld, nil, nil)
}
}
return newContext(parent, ServiceKey, service, nil, nil)
}
// WithStorageSession returns a context that is logged into the storage
// platform.
func WithStorageSession(parent context.Context) (types.Context, error) {
pctx := New(parent)
d, ok := MustDriver(parent).(types.StorageDriverWithLogin)
if !ok {
pctx.Debug("driver is not StorageDriverWithLogin")
return pctx, nil
}
sess, err := d.Login(pctx)
if err != nil {
pctx.WithError(err).Error("error logging into storage driver")
return nil, err
}
if sess == nil {
pctx.Warn("session is nil")
return pctx, nil
}
return newContext(pctx, SessionKey, sess, nil, nil), nil
}
// WithValue returns a copy of parent in which the value associated with
// key is val.
func WithValue(ctx context.Context, key, val interface{}) types.Context {
if customKeyID, ok := isCustomKey(key); ok {
key = customKeyID
}
return newContext(ctx, key, val, nil, nil)
}
func (ctx *lsc) WithValue(key, val interface{}) types.Context {
return WithValue(ctx, key, val)
}
// Value returns the value associated with this context for key, or nil
// if no value is associated with key. Successive calls to Value with
// the same key returns the same result.
func Value(ctx context.Context, key interface{}) interface{} {
return ctx.Value(key)
}
func (ctx *lsc) Value(key interface{}) interface{} {
if key == LoggerKey && ctx.logger != nil {
return ctx.logger
}
if key == PathConfigKey && ctx.pathConfig != nil {
return ctx.pathConfig
}
if customKeyID, ok := isCustomKey(key); ok {
key = customKeyID
}
if key == HTTPRequestKey {
return ctx.req
}
if ctx.req != nil {
if req, ok := ctx.Context.Value(HTTPRequestKey).(*http.Request); ok {
if val, ok := gcontext.GetOk(req, key); ok {
return val
}
}
}
if ctx.key == key {
return ctx.val
}
if val := ctx.Context.Value(key); val != nil {
return val
}
if ctx.right != nil {
return ctx.right.Value(key)
}
return nil
}
type hasName interface {
Name() string
}
type hasID interface {
ID() string
}
func stringValue(ctx context.Context, key interface{}) (string, bool) {
switch tv := ctx.Value(key).(type) {
case string:
return tv, true
case *string:
return *tv, true
case hasName:
return tv.Name(), true
case hasID:
return tv.ID(), true
case fmt.Stringer:
return tv.String(), true
default:
return "", false
}
}
// Join joins this context with another, such that value lookups will first
// first check the current context, and if no such value exist, a lookup
// will be performed against the right side.
func Join(left types.Context, right context.Context) types.Context {
if left == nil {
return nil
}
if right == nil {
return left
}
if left == right {
return left
}
return newContext(left, nil, nil, nil, right)
}
func (ctx *lsc) Join(right context.Context) types.Context {
return Join(ctx, right)
}
// SetLogLevel sets the context's log level.
func SetLogLevel(ctx context.Context, lvl log.Level) {
if logCtx, ok := ctx.(*lsc); ok {
if lvl == logCtx.logger.Level {
return
}
if logCtx.loggerInherited {
parentLogger := logCtx.logger
logCtx.logger = &log.Logger{
Formatter: parentLogger.Formatter,
Out: parentLogger.Out,
Hooks: parentLogger.Hooks,
Level: lvl,
}
logCtx.loggerInherited = false
}
}
}
// GetLogLevel gets the context's log level.
func GetLogLevel(ctx context.Context) (log.Level, bool) {
if logCtx, ok := ctx.(*lsc); ok {
return logCtx.logger.Level, true
}
return 0, false
}
// Session returns the context's session object. This value is valid only
// on the server.
func Session(ctx context.Context) (interface{}, bool) {
v := ctx.Value(SessionKey)
return v, v != nil
}
// MustSession returns the context's session object and panics if a session
// does not exist.
func MustSession(ctx context.Context) interface{} {
v := ctx.Value(SessionKey)
if v == nil {
panic("missing session")
}
return v
}
// PathConfig returns the context's path config. This value is valid on
// both the client and the server.
func PathConfig(ctx context.Context) (*types.PathConfig, bool) {
if v, ok := ctx.Value(PathConfigKey).(*types.PathConfig); ok {
return v, ok
}
return nil, false
}
// MustPathConfig returns the context's path config and panics if it does
// not exist and/or cannot be type cast.
func MustPathConfig(ctx context.Context) *types.PathConfig {
return ctx.Value(PathConfigKey).(*types.PathConfig)
}
// AuthToken returns the context's security token. This value is valid on
// both the client and the server.
func AuthToken(ctx context.Context) (*types.AuthToken, bool) {
if v, ok := ctx.Value(AuthTokenKey).(*types.AuthToken); ok {
return v, ok
}
return nil, false
}
// MustAuthToken returns the context's security token and panics if it does
// not exist and/or cannot be type cast.
func MustAuthToken(ctx context.Context) *types.AuthToken {
return ctx.Value(AuthTokenKey).(*types.AuthToken)
}
// InstanceID returns the context's InstanceID. This value is valid on both
// the client and the server.
func InstanceID(ctx context.Context) (*types.InstanceID, bool) {
v, ok := ctx.Value(InstanceIDKey).(*types.InstanceID)
return v, ok
}
// MustInstanceID returns the context's InstanceID and panics if it does not
// exist and/or cannot be type cast.
func MustInstanceID(ctx context.Context) *types.InstanceID {
return ctx.Value(InstanceIDKey).(*types.InstanceID)
}
// LocalDevices returns the context's local devices map. This value is valid
// on both the client and the server.
func LocalDevices(ctx context.Context) (*types.LocalDevices, bool) {
v, ok := ctx.Value(LocalDevicesKey).(*types.LocalDevices)
return v, ok
}
// Transaction returns the context's Transaction. This value is valid on both
// the client and the server.
func Transaction(ctx context.Context) (*types.Transaction, bool) {
v, ok := ctx.Value(TransactionKey).(*types.Transaction)
return v, ok
}
// MustTransaction returns the context's Transaction and will panic if the
// value is missing or not castable.
func MustTransaction(ctx context.Context) *types.Transaction {
return ctx.Value(TransactionKey).(*types.Transaction)
}
// RequireTX ensures a context has a transaction, and if it doesn't creates a
// new one.
func RequireTX(ctx context.Context) types.Context {
if _, ok := Transaction(ctx); ok {
return newContext(ctx, nil, nil, nil, nil)
}
tx, err := types.NewTransaction()
if err != nil {
panic(err)
}
return newContext(ctx, TransactionKey, tx, nil, nil)
}
// Client returns the context's Client. This value is only valid for
// contexts created on the client.
func Client(ctx context.Context) (types.Client, bool) {
v, ok := ctx.Value(ClientKey).(types.Client)
return v, ok
}
// MustClient returns the context's Client and panics if the client is not
// available and/or not castable..
func MustClient(ctx context.Context) types.Client {
return ctx.Value(ClientKey).(types.Client)
}
// Profile returns the context's profile. This value is only valid for
// contexts created on the server after the profile handler has been executred.
func Profile(ctx context.Context) (string, bool) {
return stringValue(ctx, ProfileKey)
}
// Route returns the context's route. This value is only valid for contexts
// created on the server after a mux has received an incoming HTTP request.
// Any part of the libStorage workflow after that, including the handlers,
// routers, and storage drivers, should have access to the Route.
func Route(ctx context.Context) (types.Route, bool) {
v, ok := ctx.Value(RouteKey).(types.Route)
return v, ok
}
// Server returns the context's server name. This value is valid on both the
// client and the server.
func Server(ctx context.Context) (string, bool) {
return stringValue(ctx, ServerKey)
}
// Service returns the context's storage service. This value is valid only for
// contexts created on the server. The value is only available after the
// service has been injected as part of the ServiceValidator handler or by
// one of the routes that injects it manually such as Volumes or Snapshots.
func Service(ctx context.Context) (types.StorageService, bool) {
v, ok := ctx.Value(ServiceKey).(types.StorageService)
if !ok {
v, ok = ctx.Value(StorageServiceKey).(types.StorageService)
}
return v, ok
}
// MustService returns the context's StorageService and panics if it does not
// exist and/or cannot be type cast.
func MustService(ctx context.Context) types.StorageService {
v, _ := Service(ctx)
return v.(types.StorageService)
}
// ServiceName returns the context's service name. This value is valid for
// contexts created on both the client and the server. On the server this
// value is subject to the same restrictions as listed in the Service function.
func ServiceName(ctx context.Context) (string, bool) {
v, ok := stringValue(ctx, ServiceKey)
if !ok {
v, ok = stringValue(ctx, StorageServiceKey)
}
return v, ok
}
// Driver returns the context's storage driver. This value is valid only
// on the server and subject to the same restrictions as listed in the Service
// function.
func Driver(ctx context.Context) (types.StorageDriver, bool) {
v, ok := ctx.Value(DriverKey).(types.StorageDriver)
if ok {
return v, ok
}
w, ok := ctx.Value(StorageServiceKey).(types.StorageService)
if ok {
if d := w.Driver(); d != nil {
return d, true
}
}
return nil, false
}
// MustDriver returns the context's storage driver or panics if no driver
// exists.
func MustDriver(ctx context.Context) types.StorageDriver {
v, _ := Driver(ctx)
return v.(types.StorageDriver)
}