/
search.go
162 lines (154 loc) · 4.79 KB
/
search.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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package v4
import (
"net/http"
"strconv"
"sync/atomic"
"github.com/juju/utils/parallel"
"gopkg.in/errgo.v1"
"gopkg.in/juju/charmstore.v4/internal/charmstore"
"gopkg.in/juju/charmstore.v4/internal/router"
"gopkg.in/juju/charmstore.v4/params"
)
const maxConcurrency = 20
// GET search[?text=text][&autocomplete=1][&filter=value…][&limit=limit][&include=meta][&skip=count][&sort=field[+dir]]
// https://github.com/juju/charmstore/blob/v4/docs/API.md#get-search
func (h *Handler) serveSearch(_ http.Header, req *http.Request) (interface{}, error) {
sp, err := parseSearchParams(req)
if err != nil {
return "", err
}
auth, err := h.checkRequest(req, nil)
if err != nil {
logger.Infof("authorization failed on search request, granting no privileges: %v", err)
}
sp.Admin = auth.Admin
if auth.Username != "" {
sp.Groups = append(sp.Groups, auth.Username)
groups, err := h.groupsForUser(auth.Username)
if err != nil {
logger.Infof("cannot get groups for user %q, assuming no groups: %v", auth.Username, err)
}
sp.Groups = append(sp.Groups, groups...)
}
// perform query
store := h.pool.Store()
defer store.Close()
results, err := store.Search(sp)
if err != nil {
return nil, errgo.Notef(err, "error performing search")
}
response := params.SearchResponse{
SearchTime: results.SearchTime,
Total: results.Total,
Results: make([]params.SearchResult, len(results.Results)),
}
run := parallel.NewRun(maxConcurrency)
var missing int32
for i, ref := range results.Results {
i, ref := i, ref
run.Do(func() error {
meta, err := h.Router.GetMetadata(ref, sp.Include, req)
if err != nil {
// Unfortunately it is possible to get errors here due to
// internal inconsistency, so rather than throwing away
// all the search results, we just log the error and move on.
logger.Errorf("cannot retrieve metadata for %v: %v", ref, err)
atomic.AddInt32(&missing, 1)
return nil
}
response.Results[i] = params.SearchResult{
Id: ref.PreferredURL(),
Meta: meta,
}
return nil
})
}
// We never return an error from the Do function above, so no need to
// check the error here.
run.Wait()
if missing == 0 {
return response, nil
}
// We're missing some results - shuffle all the results down to
// fill the gaps.
j := 0
for _, result := range response.Results {
if result.Id != nil {
response.Results[j] = result
j++
}
}
response.Results = response.Results[0:j]
return response, nil
}
// GET search/interesting[?limit=limit][&include=meta]
// https://github.com/juju/charmstore/blob/v4/docs/API.md#get-searchinteresting
func (h *Handler) serveSearchInteresting(w http.ResponseWriter, req *http.Request) {
router.WriteError(w, errNotImplemented)
}
// parseSearchParms extracts the search paramaters from the request
func parseSearchParams(req *http.Request) (charmstore.SearchParams, error) {
sp := charmstore.SearchParams{}
var err error
for k, v := range req.Form {
switch k {
case "text":
sp.Text = v[0]
case "autocomplete":
sp.AutoComplete, err = router.ParseBool(v[0])
if err != nil {
return charmstore.SearchParams{}, badRequestf(err, "invalid autocomplete parameter")
}
case "limit":
sp.Limit, err = strconv.Atoi(v[0])
if err != nil {
return charmstore.SearchParams{}, badRequestf(err, "invalid limit parameter: could not parse integer")
}
if sp.Limit < 1 {
return charmstore.SearchParams{}, badRequestf(nil, "invalid limit parameter: expected integer greater than zero")
}
case "include":
for _, s := range v {
if s != "" {
sp.Include = append(sp.Include, s)
}
}
case "description", "name", "owner", "provides", "requires", "series", "summary", "tags", "type":
if sp.Filters == nil {
sp.Filters = make(map[string][]string)
}
sp.Filters[k] = v
case "promulgated":
promulgated, err := router.ParseBool(v[0])
if err != nil {
return charmstore.SearchParams{}, badRequestf(err, "invalid promulgated filter parameter")
}
if sp.Filters == nil {
sp.Filters = make(map[string][]string)
}
if promulgated {
sp.Filters[k] = []string{"1"}
} else {
sp.Filters[k] = []string{"0"}
}
case "skip":
sp.Skip, err = strconv.Atoi(v[0])
if err != nil {
return charmstore.SearchParams{}, badRequestf(err, "invalid skip parameter: could not parse integer")
}
if sp.Skip < 0 {
return charmstore.SearchParams{}, badRequestf(nil, "invalid skip parameter: expected non-negative integer")
}
case "sort":
err = sp.ParseSortFields(v...)
if err != nil {
return charmstore.SearchParams{}, badRequestf(err, "invalid sort field")
}
default:
return charmstore.SearchParams{}, badRequestf(nil, "invalid parameter: %s", k)
}
}
return sp, nil
}