agent: Support event filtering on name

This commit is contained in:
Armon Dadgar 2014-08-28 15:25:53 -07:00
parent 602828472d
commit c3bb7de75d
2 changed files with 65 additions and 2 deletions

View File

@ -73,6 +73,12 @@ func (s *HTTPServer) EventList(resp http.ResponseWriter, req *http.Request) (int
return nil, nil
}
// Look for a name filter
var nameFilter string
if filt := req.URL.Query().Get("name"); filt != "" {
nameFilter = filt
}
// Lots of this logic is borrowed from consul/rpc.go:blockingRPC
// However we cannot use that directly since this code has some
// slight semantics differences...
@ -109,6 +115,22 @@ SETUP_NOTIFY:
RUN_QUERY:
// Get the recent events
events := s.agent.UserEvents()
// Filter the events if necessary
if nameFilter != "" {
n := len(events)
for i := 0; i < n; i++ {
if events[i].Name == nameFilter {
continue
}
events[i], events[n-1] = events[n-1], nil
i--
n--
}
events = events[:n]
}
// Determine the index
var index uint64
if len(events) == 0 {
index = 0

View File

@ -77,7 +77,48 @@ func TestEventList(t *testing.T) {
return false, fmt.Errorf("bad: %#v", list)
}
header := resp.Header().Get("X-Consul-Index")
if header == "" {
if header == "" || header == "0" {
return false, fmt.Errorf("bad: %#v", header)
}
return true, nil
}, func(err error) {
t.Fatalf("err: %v", err)
})
})
}
func TestEventList_Filter(t *testing.T) {
httpTest(t, func(srv *HTTPServer) {
p := &UserEvent{Name: "test"}
if err := srv.agent.UserEvent("", p); err != nil {
t.Fatalf("err: %v", err)
}
p = &UserEvent{Name: "foo"}
if err := srv.agent.UserEvent("", p); err != nil {
t.Fatalf("err: %v", err)
}
testutil.WaitForResult(func() (bool, error) {
req, err := http.NewRequest("GET", "/v1/event/list?name=foo", nil)
if err != nil {
return false, err
}
resp := httptest.NewRecorder()
obj, err := srv.EventList(resp, req)
if err != nil {
return false, err
}
list, ok := obj.([]*UserEvent)
if !ok {
return false, fmt.Errorf("bad: %#v", obj)
}
if len(list) != 1 || list[0].Name != "foo" {
return false, fmt.Errorf("bad: %#v", list)
}
header := resp.Header().Get("X-Consul-Index")
if header == "" || header == "0" {
return false, fmt.Errorf("bad: %#v", header)
}
return true, nil
@ -106,7 +147,7 @@ func TestEventList_Blocking(t *testing.T) {
return false, err
}
header := resp.Header().Get("X-Consul-Index")
if header == "" {
if header == "" || header == "0" {
return false, fmt.Errorf("bad: %#v", header)
}
index = header