diff --git a/command/agent/agent_endpoint.go b/command/agent/agent_endpoint.go index 71e2298c5..2552d4646 100644 --- a/command/agent/agent_endpoint.go +++ b/command/agent/agent_endpoint.go @@ -1,6 +1,7 @@ package agent import ( + "fmt" "github.com/hashicorp/consul/consul/structs" "net/http" "strings" @@ -53,7 +54,44 @@ func (s *HTTPServer) AgentForceLeave(resp http.ResponseWriter, req *http.Request } func (s *HTTPServer) AgentRegisterCheck(resp http.ResponseWriter, req *http.Request) (interface{}, error) { - return nil, nil + var args CheckDefinition + if err := decodeBody(req, &args); err != nil { + resp.WriteHeader(400) + resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + return nil, nil + } + + // Verify the check has a name + if args.Name == "" { + resp.WriteHeader(400) + resp.Write([]byte("Missing check name")) + return nil, nil + } + + // Construct the health check + health := structs.HealthCheck{ + Node: s.agent.config.NodeName, + CheckID: args.ID, + Name: args.Name, + Status: structs.HealthUnknown, + Notes: args.Notes, + } + + // Fixup the ID if not given + if health.CheckID == "" && health.Name != "" { + health.CheckID = health.Name + } + + // Verify the check type + chkType := &args.CheckType + if !chkType.Valid() { + resp.WriteHeader(400) + resp.Write([]byte("Must provide TTL or Script and Interval!")) + return nil, nil + } + + // Add the check + return s.agent.AddCheck(&health, chkType), nil } func (s *HTTPServer) AgentDeregisterCheck(resp http.ResponseWriter, req *http.Request) (interface{}, error) { @@ -80,7 +118,43 @@ func (s *HTTPServer) AgentCheckFail(resp http.ResponseWriter, req *http.Request) } func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Request) (interface{}, error) { - return nil, nil + var args ServiceDefinition + if err := decodeBody(req, &args); err != nil { + resp.WriteHeader(400) + resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err))) + return nil, nil + } + + // Verify the service has a name + if args.Name == "" { + resp.WriteHeader(400) + resp.Write([]byte("Missing service name")) + return nil, nil + } + + // Construct the health check + ns := structs.NodeService{ + ID: args.ID, + Service: args.Name, + Tag: args.Tag, + Port: args.Port, + } + + // Fixup the ID if not given + if ns.ID == "" && ns.Service != "" { + ns.ID = ns.Service + } + + // Verify the check type + chkType := args.Check + if chkType != nil && !chkType.Valid() { + resp.WriteHeader(400) + resp.Write([]byte("Must provide TTL or Script and Interval!")) + return nil, nil + } + + // Add the check + return s.agent.AddService(&ns, chkType), nil } func (s *HTTPServer) AgentDeregisterService(resp http.ResponseWriter, req *http.Request) (interface{}, error) { diff --git a/command/agent/structs.go b/command/agent/structs.go new file mode 100644 index 000000000..606e1cae3 --- /dev/null +++ b/command/agent/structs.go @@ -0,0 +1,25 @@ +package agent + +// ServiceDefinition is used to JSON decode the Service definitions +type ServiceDefinition struct { + ID string + Name string + Tag string + Port int + Check *CheckType +} + +// ChecKDefinition is used to JSON decode the Check definitions +type CheckDefinition struct { + ID string + Name string + Notes string + CheckType +} + +// UnionDefinition is used to decode when we don't know if +// we are being given a ServiceDefinition or a CheckDefinition +type UnionDefinition struct { + Service *ServiceDefinition + Check *CheckDefinition +}