forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.go
45 lines (42 loc) · 1.21 KB
/
delete.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
package api
import (
"encoding/json"
"fmt"
"net/http"
"github.com/puppetlabs/wash/activity"
"github.com/puppetlabs/wash/plugin"
)
// swagger:route DELETE /fs/delete delete deleteEntry
//
// Deletes the entry at the specified path.
//
// On success, returns a boolean that describes whether the delete was applied immediately
// or is pending.
//
// Schemes: http
//
// Responses:
// 200:
// 400: errorResp
// 404: errorResp
// 500: errorResp
var deleteHandler = handler{fn: func(w http.ResponseWriter, r *http.Request) *errorResponse {
ctx := r.Context()
entry, path, errResp := getEntryFromRequest(r)
if errResp != nil {
return errResp
}
if !plugin.DeleteAction().IsSupportedOn(entry) {
return unsupportedActionResponse(path, plugin.DeleteAction())
}
deleted, err := plugin.DeleteWithAnalytics(ctx, entry.(plugin.Deletable))
if err != nil {
return erroredActionResponse(path, plugin.DeleteAction(), err.Error())
}
activity.Record(ctx, "API: Delete %v %v", path, deleted)
jsonEncoder := json.NewEncoder(w)
if err = jsonEncoder.Encode(deleted); err != nil {
return unknownErrorResponse(fmt.Errorf("Could not marshal delete's result for %v: %v", path, err))
}
return nil
}}