-
Notifications
You must be signed in to change notification settings - Fork 950
/
Copy pathvolume_bridge.go
139 lines (116 loc) · 3.32 KB
/
volume_bridge.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
package server
import (
"context"
"encoding/json"
"net/http"
"github.com/alibaba/pouch/apis/filters"
"github.com/alibaba/pouch/apis/types"
"github.com/alibaba/pouch/pkg/httputils"
"github.com/alibaba/pouch/pkg/randomid"
volumetypes "github.com/alibaba/pouch/storage/volume/types"
"github.com/go-openapi/strfmt"
"github.com/gorilla/mux"
)
func (s *Server) createVolume(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
config := &types.VolumeCreateConfig{}
// decode request body
if err := json.NewDecoder(req.Body).Decode(config); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
logCreateOptions(ctx, "volume", config)
// validate request body
if err := config.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
name := config.Name
driver := config.Driver
options := config.DriverOpts
labels := config.Labels
if name == "" {
name = randomid.Generate()
}
if driver == "" {
driver = volumetypes.DefaultBackend
}
volume, err := s.VolumeMgr.Create(ctx, name, driver, options, labels)
if err != nil {
return err
}
status := map[string]interface{}{}
for k, v := range volume.Options() {
if k != "" && v != "" {
status[k] = v
}
}
status["size"] = volume.Size()
respVolume := types.VolumeInfo{
Name: name,
Driver: driver,
Labels: config.Labels,
Mountpoint: volume.Path(),
Status: status,
CreatedAt: volume.CreationTimestamp.Format("2006-1-2 15:04:05"),
}
return EncodeResponse(rw, http.StatusCreated, respVolume)
}
func (s *Server) getVolume(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) {
name := mux.Vars(req)["name"]
volume, err := s.VolumeMgr.Get(ctx, name)
if err != nil {
return err
}
status := map[string]interface{}{}
for k, v := range volume.Options() {
if k != "" && v != "" {
status[k] = v
}
}
status["size"] = volume.Size()
respVolume := types.VolumeInfo{
Name: volume.Name,
Driver: volume.Driver(),
Mountpoint: volume.Path(),
CreatedAt: volume.CreateTime(),
Labels: volume.Labels,
Status: status,
}
return EncodeResponse(rw, http.StatusOK, respVolume)
}
func (s *Server) listVolume(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
filter, err := filters.FromParam(req.FormValue("filters"))
if err != nil {
return err
}
volumes, err := s.VolumeMgr.List(ctx, filter)
if err != nil {
return err
}
respVolumes := types.VolumeListResp{Volumes: []*types.VolumeInfo{}, Warnings: nil}
for _, volume := range volumes {
status := map[string]interface{}{}
for k, v := range volume.Options() {
if k != "" && v != "" {
status[k] = v
}
}
status["size"] = volume.Size()
respVolume := &types.VolumeInfo{
Name: volume.Name,
Driver: volume.Driver(),
Mountpoint: volume.Path(),
CreatedAt: volume.CreateTime(),
Labels: volume.Labels,
Status: status,
}
respVolumes.Volumes = append(respVolumes.Volumes, respVolume)
}
return EncodeResponse(rw, http.StatusOK, respVolumes)
}
func (s *Server) removeVolume(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
name := mux.Vars(req)["name"]
if err := s.VolumeMgr.Remove(ctx, name); err != nil {
return err
}
rw.WriteHeader(http.StatusNoContent)
return nil
}