forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.go
75 lines (66 loc) · 1.91 KB
/
list.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
package api
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"github.com/puppetlabs/wash/activity"
apitypes "github.com/puppetlabs/wash/api/types"
"github.com/puppetlabs/wash/plugin"
)
// swagger:response
//nolint:deadcode,unused
type entryList struct {
// in: body
Entries []apitypes.Entry
}
// swagger:route GET /fs/list list listEntries
//
// Lists children of a path
//
// Returns a list of Entry objects describing children of the given path.
// The "metadata" key is set to the partial metadata.
//
// Produces:
// - application/json
//
// Schemes: http
//
// Responses:
// 200: entryList
// 400: errorResp
// 404: errorResp
// 500: errorResp
var listHandler = 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.ListAction().IsSupportedOn(entry) {
return unsupportedActionResponse(path, plugin.ListAction())
}
parent := entry.(plugin.Parent)
entries, err := plugin.ListWithAnalytics(ctx, parent)
if err != nil {
if cnameErr, ok := err.(plugin.DuplicateCNameErr); ok {
return duplicateCNameResponse(cnameErr)
}
return erroredActionResponse(path, plugin.ListAction(), err.Error())
}
result := make([]apitypes.Entry, 0, entries.Len())
entries.Range(func(_ string, entry plugin.Entry) bool {
apiEntry := apitypes.NewEntry(entry)
apiEntry.Path = path + "/" + apiEntry.CName
result = append(result, apiEntry)
return true
})
// Sort entries so they have a deterministic order.
sort.Slice(result, func(i, j int) bool { return result[i].Name < result[j].Name })
activity.Record(ctx, "API: List %v %v items", path, len(result))
jsonEncoder := json.NewEncoder(w)
if err = jsonEncoder.Encode(result); err != nil {
return unknownErrorResponse(fmt.Errorf("Could not marshal list results for %v: %v", path, err))
}
return nil
}}