This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 435
/
app_block_volume.go
214 lines (179 loc) · 5.25 KB
/
app_block_volume.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
//
// Copyright (c) 2017 The heketi Authors
//
// This file is licensed to you under your choice of the GNU Lesser
// General Public License, version 3 or any later version (LGPLv3 or
// later), or the GNU General Public License, version 2 (GPLv2), in all
// cases as published by the Free Software Foundation.
//
package glusterfs
import (
"encoding/json"
"net/http"
"github.com/boltdb/bolt"
"github.com/gorilla/mux"
"github.com/heketi/heketi/pkg/glusterfs/api"
"github.com/heketi/heketi/pkg/utils"
)
func (a *App) BlockVolumeCreate(w http.ResponseWriter, r *http.Request) {
var msg api.BlockVolumeCreateRequest
err := utils.GetJsonFromRequest(r, &msg)
if err != nil {
http.Error(w, "request unable to be parsed", 422)
return
}
err = msg.Validate()
if err != nil {
http.Error(w, "validation failed: "+err.Error(), http.StatusBadRequest)
logger.LogError("validation failed: " + err.Error())
return
}
if msg.Size < 1 {
http.Error(w, "Invalid volume size", http.StatusBadRequest)
logger.LogError("Invalid volume size")
return
}
// TODO: factor this into a function (it's also in VolumeCreate)
// Check that the clusters requested are available
err = a.db.View(func(tx *bolt.Tx) error {
// :TODO: All we need to do is check for one instead of gathering all keys
clusters, err := ClusterList(tx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return err
}
if len(clusters) == 0 {
err := logger.LogError("No clusters configured")
http.Error(w, err.Error(), http.StatusBadRequest)
return ErrNotFound
}
// Check the clusters requested are correct
for _, clusterid := range msg.Clusters {
_, err := NewClusterEntryFromId(tx, clusterid)
if err != nil {
err := logger.LogError("Cluster id %v not found", clusterid)
http.Error(w, err.Error(), http.StatusBadRequest)
return err
}
}
return nil
})
if err != nil {
return
}
blockVolume := NewBlockVolumeEntryFromRequest(&msg)
bvc := NewBlockVolumeCreateOperation(blockVolume, a.db)
if err := AsyncHttpOperation(a, w, r, bvc); err != nil {
OperationHttpErrorf(w, err, "Failed to allocate new block volume: %v", err)
return
}
}
func (a *App) BlockVolumeList(w http.ResponseWriter, r *http.Request) {
var list api.BlockVolumeListResponse
err := a.db.View(func(tx *bolt.Tx) error {
var err error
list.BlockVolumes, err = ListCompleteBlockVolumes(tx)
if err != nil {
return err
}
return nil
})
if err != nil {
logger.Err(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Send list back
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(list); err != nil {
panic(err)
}
}
func (a *App) BlockVolumeInfo(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
// Get volume information
var info *api.BlockVolumeInfoResponse
err := a.db.View(func(tx *bolt.Tx) error {
entry, err := NewBlockVolumeEntryFromId(tx, id)
if err == ErrNotFound || !entry.Visible() {
http.Error(w, "Id not found", http.StatusNotFound)
return ErrNotFound
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return err
}
info, err = entry.NewInfoResponse(tx)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return err
}
return nil
})
if err != nil {
return
}
// Write msg
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(info); err != nil {
panic(err)
}
}
func (a *App) BlockVolumeDelete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
var blockVolume *BlockVolumeEntry
err := a.db.View(func(tx *bolt.Tx) error {
var err error
blockVolume, err = NewBlockVolumeEntryFromId(tx, id)
if err == ErrNotFound {
http.Error(w, err.Error(), http.StatusNotFound)
return err
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return err
}
return nil
})
if err != nil {
return
}
vdel := NewBlockVolumeDeleteOperation(blockVolume, a.db)
if err := AsyncHttpOperation(a, w, r, vdel); err != nil {
OperationHttpErrorf(w, err, "Failed to set up block volume delete: %v", err)
return
}
}
func (a *App) BlockVolumeExpand(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["id"]
var msg api.BlockVolumeExpandRequest
err := utils.GetJsonFromRequest(r, &msg)
if err != nil {
http.Error(w, "request unable to be parsed", 422)
return
}
err = msg.Validate()
if err != nil {
http.Error(w, "validation failed: "+err.Error(), http.StatusBadRequest)
logger.LogError("validation failed: " + err.Error())
return
}
if msg.Size < 1 {
http.Error(w, "Invalid block volume size", http.StatusBadRequest)
logger.LogError("Invalid block volume size")
return
}
bve, err := NewBlockVolumeExpandOperation(id, a.db, msg.Size)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
logger.LogError("failed creating a new BlockVolumeExpandOperation" + err.Error())
return
}
if err := AsyncHttpOperation(a, w, r, bve); err != nil {
OperationHttpErrorf(w, err, "Failed to allocate block volume expansion: %v", err)
return
}
}