agent: First pass at register service and check locally

This commit is contained in:
Armon Dadgar 2014-01-30 15:35:38 -08:00
parent 98464b4ec0
commit bfc5c21aa5
2 changed files with 101 additions and 2 deletions

View File

@ -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) {

25
command/agent/structs.go Normal file
View File

@ -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
}