agent: support adding multiple checks during service registration from the API

This commit is contained in:
Ryan Uber 2015-01-13 19:08:30 -08:00
parent 27de0adf3b
commit 684474af1d
2 changed files with 29 additions and 12 deletions

View File

@ -132,17 +132,25 @@ func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Re
return nil return nil
} }
var check interface{}
for k, v := range rawMap { for k, v := range rawMap {
if strings.ToLower(k) == "check" { switch strings.ToLower(k) {
check = v case "check":
if err := FixupCheckType(v); err != nil {
return err
}
case "checks":
chkTypes, ok := v.([]interface{})
if !ok {
return nil
}
for _, chkType := range chkTypes {
if err := FixupCheckType(chkType); err != nil {
return err
}
}
} }
} }
if check == nil { return nil
return nil
}
return FixupCheckType(check)
} }
if err := decodeBody(req, &args, decodeCB); err != nil { if err := decodeBody(req, &args, decodeCB); err != nil {
resp.WriteHeader(400) resp.WriteHeader(400)

View File

@ -430,6 +430,14 @@ func TestHTTPAgentRegisterService(t *testing.T) {
Check: CheckType{ Check: CheckType{
TTL: 15 * time.Second, TTL: 15 * time.Second,
}, },
Checks: CheckTypes{
&CheckType{
TTL: 20 * time.Second,
},
&CheckType{
TTL: 30 * time.Second,
},
},
} }
req.Body = encodeReq(args) req.Body = encodeReq(args)
@ -447,12 +455,13 @@ func TestHTTPAgentRegisterService(t *testing.T) {
} }
// Ensure we have a check mapping // Ensure we have a check mapping
if _, ok := srv.agent.state.Checks()["service:test"]; !ok { checks := srv.agent.state.Checks()
t.Fatalf("missing test check") if len(checks) != 3 {
t.Fatalf("bad: %v", checks)
} }
if _, ok := srv.agent.checkTTLs["service:test"]; !ok { if len(srv.agent.checkTTLs) != 3 {
t.Fatalf("missing test check ttl") t.Fatalf("missing test check ttls: %v", srv.agent.checkTTLs)
} }
} }