forked from thecodeteam/libstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux_utils.go
117 lines (104 loc) · 2.5 KB
/
linux_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package linux
import (
"fmt"
"path"
"strings"
"github.com/akutz/goof"
"github.com/codedellemc/libstorage/api/context"
"github.com/codedellemc/libstorage/api/types"
"github.com/codedellemc/libstorage/api/utils"
)
func (d *driver) getVolumeMountPath(volumeName string) (string, error) {
if volumeName == "" {
return "", goof.New("missing volume name")
}
return path.Join(d.mountDirPath(), volumeName), nil
}
func (d *driver) volumeInspectByID(
ctx types.Context,
volumeID string,
attachments types.VolumeAttachmentsTypes,
opts types.Store) (*types.Volume, error) {
client := context.MustClient(ctx)
vol, err := client.Storage().VolumeInspect(ctx, volumeID,
&types.VolumeInspectOpts{
Attachments: attachments})
if err != nil {
return nil, err
}
return vol, nil
}
func (d *driver) volumeInspectByName(
ctx types.Context,
volumeName string,
attachments types.VolumeAttachmentsTypes,
opts types.Store) (*types.Volume, error) {
client := context.MustClient(ctx)
if sd, ok := client.Storage().(types.StorageDriverVolInspectByName); ok {
vol, err := sd.VolumeInspectByName(
ctx,
volumeName,
&types.VolumeInspectOpts{
Attachments: attachments})
if err != nil {
return nil, err
}
return vol, nil
} else {
vols, err := client.Storage().Volumes(
ctx, &types.VolumesOpts{Attachments: 0})
if err != nil {
return nil, err
}
for _, v := range vols {
if strings.EqualFold(volumeName, v.Name) {
vol, err := d.volumeInspectByID(
ctx, v.ID, attachments, opts)
if err != nil {
return nil, err
}
return vol, nil
}
}
}
return nil, nil
}
func (d *driver) volumeInspectByIDOrName(
ctx types.Context,
volumeID, volumeName string,
attachments types.VolumeAttachmentsTypes,
opts types.Store) (*types.Volume, error) {
if volumeID != "" && volumeName != "" {
return nil, goof.New("specify either volumeID or volumeName")
}
var (
obj *types.Volume
err error
)
if volumeID != "" {
obj, err = d.volumeInspectByID(
ctx, volumeID, attachments, opts)
} else {
obj, err = d.volumeInspectByName(
ctx, volumeName, attachments, opts)
}
if err != nil {
return nil, err
}
if obj == nil {
return nil, utils.NewNotFoundError(
fmt.Sprintf("volumeID=%s,volumeName=%s", volumeID, volumeName))
}
return obj, nil
}
func isErrNotFound(err error) bool {
switch err.(type) {
case *types.ErrNotFound:
return true
default:
return false
}
}
func (d *driver) volumeMountPath(target string) string {
return path.Join(target, d.volumeRootPath())
}