/
api_volumes.go
141 lines (122 loc) · 3.88 KB
/
api_volumes.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
package docker
import (
"context"
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/azukaar/cosmos-server/src/utils"
volumeTypes"github.com/docker/docker/api/types/volume"
)
func ListVolumeRoute(w http.ResponseWriter, req *http.Request) {
if utils.AdminOnly(w, req) != nil {
return
}
if req.Method == "GET" {
errD := Connect()
if errD != nil {
utils.Error("ManageContainer", errD)
utils.HTTPError(w, "Internal server error: " + errD.Error(), http.StatusInternalServerError, "LV001")
return
}
// List Docker volumes
volumes, err := DockerClient.VolumeList(context.Background(), volumeTypes.ListOptions{})
if err != nil {
utils.Error("ListVolumeRoute: Error while getting volumes", err)
utils.HTTPError(w, "Volumes Get Error", http.StatusInternalServerError, "LV002")
return
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
"data": volumes,
})
} else {
utils.Error("ListVolumeRoute: Method not allowed " + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}
func DeleteVolumeRoute(w http.ResponseWriter, req *http.Request) {
if utils.AdminOnly(w, req) != nil {
return
}
if req.Method == "DELETE" {
// Get the volume name from URL
vars := mux.Vars(req)
volumeName := vars["volumeName"]
errD := Connect()
if errD != nil {
utils.Error("DeleteVolumeRoute", errD)
utils.HTTPError(w, "Internal server error: "+errD.Error(), http.StatusInternalServerError, "DV001")
return
}
// Delete the specified Docker volume
err := DockerClient.VolumeRemove(context.Background(), volumeName, true)
if err != nil {
utils.Error("DeleteVolumeRoute: Error while deleting volume", err)
utils.HTTPError(w, "Volume Deletion Error " + err.Error(), http.StatusInternalServerError, "DV002")
return
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
"message": "Volume deleted successfully",
})
} else {
utils.Error("DeleteVolumeRoute: Method not allowed " + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}
type VolumeCreateRequest struct {
Name string `json:"name"`
Driver string `json:"driver"`
}
func CreateVolumeRoute(w http.ResponseWriter, req *http.Request) {
if utils.AdminOnly(w, req) != nil {
return
}
if req.Method == "POST" {
errD := Connect()
if errD != nil {
utils.Error("CreateVolumeRoute", errD)
utils.HTTPError(w, "Internal server error: "+errD.Error(), http.StatusInternalServerError, "CV001")
return
}
var payload VolumeCreateRequest
err := json.NewDecoder(req.Body).Decode(&payload)
if err != nil {
utils.Error("CreateNetworkRoute: Error reading request body", err)
utils.HTTPError(w, "Error reading request body: "+err.Error(), http.StatusBadRequest, "CN002")
return
}
// Create Docker volume with the provided options
volumeOptions := volumeTypes.CreateOptions {
Name: payload.Name,
Driver: payload.Driver,
}
volume, err := DockerClient.VolumeCreate(context.Background(), volumeOptions)
if err != nil {
utils.Error("CreateVolumeRoute: Error while creating volume", err)
utils.HTTPError(w, "Volume creation error: "+err.Error(), http.StatusInternalServerError, "CV004")
return
}
json.NewEncoder(w).Encode(map[string]interface{}{
"status": "OK",
"data": volume,
})
} else {
utils.Error("CreateVolumeRoute: Method not allowed " + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}
func VolumesRoute(w http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
ListVolumeRoute(w, req)
} else if req.Method == "POST" {
CreateVolumeRoute(w, req)
} else {
utils.Error("VolumesRoute: Method not allowed " + req.Method, nil)
utils.HTTPError(w, "Method not allowed", http.StatusMethodNotAllowed, "HTTP001")
return
}
}