/
libstorage_utils.go
98 lines (78 loc) · 1.96 KB
/
libstorage_utils.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
package libstorage
import (
"strings"
"github.com/codedellemc/rexray/libstorage/api/context"
"github.com/codedellemc/rexray/libstorage/api/types"
)
func (c *client) requireCtx(ctx types.Context) types.Context {
if ctx == nil {
ctx = c.ctx
} else {
ctx = ctx.Join(c.ctx)
}
return context.RequireTX(ctx)
}
func (c *driver) requireCtx(ctx types.Context) types.Context {
if ctx == nil {
ctx = c.ctx
} else {
ctx = ctx.Join(c.ctx)
}
return context.RequireTX(ctx)
}
func (c *client) withAllInstanceIDs(ctx types.Context) types.Context {
if c.isController() {
return ctx
}
iidm := types.InstanceIDMap{}
for _, k := range c.instanceIDCache.Keys() {
iidm[k] = c.instanceIDCache.GetInstanceID(k)
}
if len(iidm) == 0 {
return ctx
}
return ctx.WithValue(context.AllInstanceIDsKey, iidm)
}
func (c *client) withInstanceID(
ctx types.Context, service string) types.Context {
ctx = ctx.WithValue(context.ServiceKey, service)
if c.isController() {
return ctx
}
si := c.serviceCache.GetServiceInfo(service)
if si == nil {
return ctx
}
if !c.instanceIDCache.IsSet(si.Name) {
return ctx
}
iid := c.instanceIDCache.GetInstanceID(si.Name)
return ctx.WithValue(context.InstanceIDKey, iid)
}
func (c *client) withAllLocalDevices(ctx types.Context) (types.Context, error) {
if c.isController() {
return ctx, nil
}
ldm := types.LocalDevicesMap{}
hit := map[string]bool{}
for _, service := range c.serviceCache.Keys() {
si := c.serviceCache.GetServiceInfo(service)
dn := strings.ToLower(si.Driver.Name)
if _, ok := hit[dn]; ok {
continue
}
ctx := ctx.WithValue(context.ServiceKey, service)
ld, err := c.LocalDevices(ctx, &types.LocalDevicesOpts{})
if err != nil {
if err == errExecutorNotSupported ||
err == types.ErrNotImplemented {
ctx.WithError(err).Warn("cannot get local deviecs")
continue
}
return nil, err
}
hit[dn] = true
ldm[dn] = ld
}
return ctx.WithValue(context.AllLocalDevicesKey, ldm), nil
}