-
Notifications
You must be signed in to change notification settings - Fork 0
/
policies_index.go
65 lines (54 loc) · 1.67 KB
/
policies_index.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
package handlers
import (
"net/http"
"policy-server/models"
"policy-server/uaa_client"
"code.cloudfoundry.org/go-db-helpers/marshal"
"code.cloudfoundry.org/lager"
)
//go:generate counterfeiter -o fakes/policy_filter.go --fake-name PolicyFilter . policyFilter
type policyFilter interface {
FilterPolicies(policies []models.Policy, userToken uaa_client.CheckTokenResponse) ([]models.Policy, error)
}
type PoliciesIndex struct {
Logger lager.Logger
Store store
Marshaler marshal.Marshaler
PolicyFilter policyFilter
ErrorResponse errorResponse
}
func (h *PoliciesIndex) ServeHTTP(w http.ResponseWriter, req *http.Request, userToken uaa_client.CheckTokenResponse) {
queryValues := req.URL.Query()
ids := parseIds(queryValues)
var policies []models.Policy
var err error
if len(ids) == 0 {
policies, err = h.Store.All()
} else {
policies, err = h.Store.ByGuids(ids, ids)
}
if err != nil {
h.ErrorResponse.InternalServerError(w, err, "policies-index", "database read failed")
return
}
policies, err = h.PolicyFilter.FilterPolicies(policies, userToken)
if err != nil {
h.ErrorResponse.InternalServerError(w, err, "policies-index", "filter policies failed")
return
}
for i, _ := range policies {
policies[i].Source.Tag = ""
policies[i].Destination.Tag = ""
}
policyResponse := struct {
TotalPolicies int `json:"total_policies"`
Policies []models.Policy `json:"policies"`
}{len(policies), policies}
bytes, err := h.Marshaler.Marshal(policyResponse)
if err != nil {
h.ErrorResponse.InternalServerError(w, err, "policies-index", "database marshaling failed")
return
}
w.WriteHeader(http.StatusOK)
w.Write(bytes)
}