forked from puppetlabs-toy-chest/wash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
107 lines (96 loc) · 2.49 KB
/
find.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
package api
import (
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/puppetlabs/wash/activity"
"github.com/puppetlabs/wash/api/rql"
"github.com/puppetlabs/wash/api/rql/ast"
apitypes "github.com/puppetlabs/wash/api/types"
"github.com/puppetlabs/wash/plugin"
)
// swagger:parameters findQuery
//nolint:deadcode,unused
type findParams struct {
params
rql.Options
}
// swagger:route GET /fs/find find findQuery
//
// Find entries using RQL
//
// Recursively descends the given path, returning all children that satisfy
// the given RQL query.
//
// Consumes:
// - application/json
//
// Produces:
// - application/json
//
// Schemes: http
//
// Responses:
// 200: entryList
// 400: errorResp
// 404: errorResp
// 500: errorResp
var findHandler = 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())
}
minDepth, hasMinDepth, errResp := getIntParam(r.URL, "mindepth")
if errResp != nil {
return errResp
}
maxDepth, hasMaxDepth, errResp := getIntParam(r.URL, "maxdepth")
if errResp != nil {
return errResp
}
fullMeta, errResp := getBoolParam(r.URL, "fullmeta")
if errResp != nil {
return errResp
}
var rawQuery interface{}
if err := json.NewDecoder(r.Body).Decode(&rawQuery); err != nil {
if err != io.EOF {
return badRequestResponse(fmt.Sprintf("could not decode the RQL query: %v", err))
}
rawQuery = true
}
query := ast.Query()
if err := query.Unmarshal(rawQuery); err != nil {
return badRequestResponse(fmt.Sprintf("could not decode the RQL query: %v", err))
}
opts := rql.NewOptions()
opts.Fullmeta = fullMeta
if hasMinDepth {
opts.Mindepth = minDepth
}
if hasMaxDepth {
opts.Maxdepth = maxDepth
}
rqlEntries, err := rql.Find(ctx, entry, query, opts)
if err != nil {
return unknownErrorResponse(err)
}
result := []apitypes.Entry{}
for _, rqlEntry := range rqlEntries {
apiEntry := rqlEntry.Entry
// Make sure all paths are absolute paths
apiEntry.Path = path + "/" + apiEntry.Path
result = append(result, apiEntry)
}
activity.Record(ctx, "API: Find %v %v items", path, len(result))
jsonEncoder := json.NewEncoder(w)
if err = jsonEncoder.Encode(result); err != nil {
return unknownErrorResponse(fmt.Errorf("Could not marshal find results for %v: %v", path, err))
}
return nil
}}