-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathroutes.go
399 lines (369 loc) · 10.8 KB
/
routes.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Copyright 2024-2025 Raamsri Kumar <[email protected]>
* Copyright 2024-2025 The StrataSTOR Authors and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package api
import (
"github.com/gin-gonic/gin"
"github.com/stratastor/rodent/pkg/zfs/common"
)
// Unlike Pool operations, Dataset API maynot be RESTFUL.
// Having dataset values with "/" in the URI params is inconvenient
// and may lead to confusion. Hence, we will pass information in the body
// to keep the URI clean and simple.
//
// Dataset Operations:
//
// GET /dataset List datasets
// DELETE /dataset Destroy dataset
// Request: {"name": "tank/ds1", "recursive_destroy_dependents": false}
// Response: 204 No Content
//
// POST /dataset/rename Rename dataset
// Request: {"name": "tank/ds1", "new_name": "tank/ds2", "force": true}
// Response: 200 OK
//
// Property Operations:
//
// GET /dataset/properties List all properties
// Request: {"name": "tank/ds1"}
// Response: {"result": {"tank/ds1": {"properties": {...}}}}
//
// GET /dataset/property Get specific property
// Request: {"name": "tank/ds1", "property": "compression"}
// Response: {"result": {"tank/ds1": {"properties": {"compression": {...}}}}}
//
// PUT /dataset/property Set property
// Request: {"name": "tank/ds1", "property": "compression", "value": "on"}
// Response: 201 Created
//
// Filesystem Operations:
//
// GET /dataset/filesystems List filesystems
// Request: {"recursive": false}
// Response: {"result": {"datasets": [...]}}
//
// POST /dataset/filesystem Create filesystem
// Request: {"name": "tank/fs1", "properties": {"compression": "on"}}
// Response: 201 Created
//
// Volume Operations:
//
// GET /dataset/volumes List volumes
// Request: {"recursive": false}
// Response: {"result": {"datasets": [...]}}
//
// POST /dataset/volume Create volume
// Request: {"name": "tank/vol1", "size": "10G", "properties": {...}}
// Response: 201 Created
//
// Snapshot Operations:
//
// GET /dataset/snapshots List snapshots
// Request: {"name": "tank/fs1"}
// Response: {"result": {"datasets": [...]}}
//
// POST /dataset/snapshot Create snapshot
// Request: {"name": "tank/fs1", "snap_name": "snap1"}
// Response: 201 Created
//
// POST /dataset/snapshot/rollback Rollback to snapshot
// Request: {"name": "tank/fs1@snap1", "destroy_recent": true}
// Response: 200 OK
//
// Clone Operations:
//
// POST /dataset/clone Create clone
// Request: {"name": "tank/fs1@snap1", "clone_name": "tank/clone1"}
// Response: 201 Created
//
// POST /dataset/clone/promote Promote clone
// Request: {"name": "tank/clone1"}
// Response: 200 OK
//
// Bookmark Operations:
//
// GET /dataset/bookmarks List bookmarks
// Request: {"name": "tank/fs1"}
// Response: {"result": {"datasets": [...]}}
//
// POST /dataset/bookmark Create bookmark
// Request: {"name": "tank/fs1@snap1", "bookmark_name": "mark1"}
// Response: 201 Created
//
// Mount Operations:
//
// POST /dataset/mount Mount dataset
// Request: {"name": "tank/fs1", "force": true}
// Response: 200 OK
//
// POST /dataset/unmount Unmount dataset
// Request: {"name": "tank/fs1", "force": true}
// Response: 204 No Content
//
// Data Transfer:
//
// POST /dataset/transfer/send Send dataset
// Response: 200 OK
//
// GET /dataset/transfer/resume-token Get resume token
// Request: {"name": "tank/backup"}
// Response: {"result": "token-string"}
func (h *DatasetHandler) RegisterRoutes(router *gin.RouterGroup) {
dataset := router.Group("/dataset")
{
// TODO: Add appropriate validation middlewares
// Dataset operations
dataset.POST("/list", h.listDatasets)
dataset.POST("/delete",
ValidateZFSEntityName(common.TypeZFSEntityMask),
h.destroyDataset)
dataset.POST("/rename",
// TODO: Validate NewName?
ValidateZFSEntityName(common.TypeDatasetMask),
h.renameDataset)
dataset.POST("/diff",
ValidateDiffConfig(),
h.diffDataset)
// Property operations
properties := dataset.Group("/properties",
ValidateZFSEntityName(common.TypeZFSEntityMask))
{
properties.POST("/list", h.listProperties)
}
property := dataset.Group("/property",
ValidateZFSEntityName(common.TypeZFSEntityMask))
{
property.POST("/fetch",
ValidatePropertyName(),
h.getProperty)
property.PUT("",
ValidateZFSProperties(),
h.setProperty)
property.PUT("/inherit",
ValidateZFSProperties(),
h.inheritProperty)
}
// Filesystem operations
filesystems := dataset.Group("/filesystems")
{
filesystems.POST("/list", h.listFilesystems)
}
filesystem := dataset.Group("/filesystem")
{
filesystem.POST("",
ValidateMountPoint(),
ValidateZFSProperties(),
h.createFilesystem)
// Mount operations
filesystem.POST("/mount",
ValidateZFSEntityName(common.TypeFilesystem),
ValidateMountPoint(),
h.mountDataset)
filesystem.POST("/unmount",
ValidateZFSEntityName(common.TypeFilesystem),
h.unmountDataset)
}
// Volume operations
volumes := dataset.Group("/volumes")
{
volumes.POST("/list", h.listVolumes)
}
volume := dataset.Group("/volume")
{
volume.POST("",
ValidateVolumeSize(),
ValidateBlockSize(),
ValidateZFSProperties(),
h.createVolume)
}
// Snapshot operations
snapshots := dataset.Group("/snapshots")
{
snapshots.POST("/list", h.listSnapshots)
}
snapshot := dataset.Group("/snapshot")
{
snapshot.POST("",
ValidateZFSEntityName(common.TypeFilesystem|common.TypeVolume),
ValidateZFSProperties(),
h.createSnapshot)
snapshot.POST("/rollback",
ValidateZFSEntityName(common.TypeSnapshot),
h.rollbackSnapshot)
}
// Clone operations
clone := dataset.Group("/clone")
{
clone.POST("",
ValidateZFSEntityName(common.TypeSnapshot),
ValidateCloneConfig(),
ValidateZFSProperties(),
h.createClone)
clone.POST("/promote",
ValidateZFSEntityName(common.TypeFilesystem),
h.promoteClone)
}
// Bookmark operations
bookmarks := dataset.Group("/bookmarks")
{
bookmarks.POST("/list", h.listBookmarks)
}
bookmark := dataset.Group("/bookmark")
{
bookmark.POST("",
ValidateZFSEntityName(common.TypeSnapshot|common.TypeBookmark),
h.createBookmark)
}
// Permission operations
permissions := dataset.Group("/permissions",
ValidateZFSEntityName(common.TypeDatasetMask))
{
permissions.POST("/list", h.listPermissions)
permissions.POST("",
ValidatePermissionConfig(),
h.allowPermissions)
permissions.DELETE("",
ValidateUnallowConfig(),
h.unallowPermissions)
}
// Share operations
share := dataset.Group("/share")
{
share.POST("", h.shareDataset)
share.DELETE("", h.unshareDataset)
}
// Data transfer operations
transfer := dataset.Group("/transfer")
{
transfer.POST("/send",
h.sendDataset)
transfer.POST("/resume-token/fetch",
ValidateZFSEntityName(common.TypeFilesystem),
h.getResumeToken)
}
}
}
// TODO: Perhaps Pool APIs can also be refactored to send param values in the body to maintain consistency?
// Pool APIs do not have the same issue as Dataset APIs, but it would be pragmatic to have a consistent approach.
// API Routes
//
// Pool Operations:
//
// POST /api/v1/pools
// Request: {"name": "mypool", "vdev_spec": [{"type": "mirror", "devices": ["/dev/sda", "/dev/sdb"]}]}
// Response: 201 Created
//
// GET /api/v1/pools
// Response: {"pools": [{"name": "mypool", "state": "ONLINE", ...}]}
//
// DELETE /api/v1/pools/:name
// Response: 204 No Content
//
// Import/Export:
//
// POST /api/v1/pools/import
// Request: {"name": "mypool", "force": false}
// Response: 200 OK
//
// POST /api/v1/pools/:name/export
// Response: 200 OK
//
// Status and Properties:
//
// GET /api/v1/pools/:name/status
// Response: {"name": "mypool", "state": "ONLINE", "vdevs": [...]}
//
// GET /api/v1/pools/:name/properties/:property
// Response: {"value": "on", "source": {"type": "local"}}
//
// PUT /api/v1/pools/:name/properties/:property
// Request: {"value": "off"}
// Response: 200 OK
//
// Maintenance:
//
// POST /api/v1/pools/:name/scrub
// Request: {"stop": false}
// Response: 200 OK
//
// POST /api/v1/pools/:name/resilver
// Response: 200 OK
//
// Device Operations:
//
// POST /api/v1/pools/:name/devices/attach
// Request: {"device": "/dev/sdc", "new_device": "/dev/sdd"}
// Response: 200 OK
//
// POST /api/v1/pools/:name/devices/detach
// Request: {"device": "/dev/sdc"}
// Response: 200 OK
//
// POST /api/v1/pools/:name/devices/replace
// Request: {"old_device": "/dev/sdc", "new_device": "/dev/sdd"}
// Response: 200 OK
//
// Error Responses:
//
// 400 Bad Request: Invalid input (name, device path, property)
// 403 Forbidden: Permission denied
// 404 Not Found: Pool not found
// 500 Internal Error: Command execution failed
func (h *PoolHandler) RegisterRoutes(router *gin.RouterGroup) {
pools := router.Group("/pools")
{
// Create/List/Destroy
pools.POST("",
ValidatePoolName(),
ValidateNameLength(),
EnhancedValidateDevicePaths(),
ValidatePoolProperties(common.CreatePoolPropContext),
h.createPool)
pools.GET("", h.listPools)
pools.DELETE("/:name", ValidatePoolName(), h.destroyPool)
// Import/Export
pools.POST("/import",
ValidatePoolProperties(common.ImportPoolPropContext),
h.importPool)
pools.POST("/:name/export", ValidatePoolName(), h.exportPool)
// Status and properties
pools.GET("/:name/status", ValidatePoolName(), h.getPoolStatus)
pools.GET("/:name/properties",
ValidatePoolName(),
h.getProperties)
pools.GET("/:name/properties/:property",
ValidatePoolName(),
ValidatePoolProperty(common.ValidPoolGetPropContext),
h.getProperty)
pools.PUT("/:name/properties/:property",
ValidatePoolName(),
ValidatePoolProperty(common.AnytimePoolPropContext),
ValidatePropertyValue(),
h.setProperty)
// Maintenance
pools.POST("/:name/scrub", ValidatePoolName(), h.scrubPool)
pools.POST("/:name/resilver", ValidatePoolName(), h.resilverPool)
// Device operations
devices := pools.Group("/:name/devices", ValidatePoolName())
{
// TODO: Validate device paths
devices.POST("/attach", h.attachDevice)
devices.POST("/detach", h.detachDevice)
devices.POST("/replace",
h.replaceDevice)
}
}
}