open-consul/command/agent/structs.go
2015-05-11 16:48:10 -07:00

78 lines
1.6 KiB
Go

package agent
import (
"github.com/hashicorp/consul/consul/structs"
)
// ServiceDefinition is used to JSON decode the Service definitions
type ServiceDefinition struct {
ID string
Name string
Tags []string
Address string
Port int
Check CheckType
Checks CheckTypes
Token string
}
func (s *ServiceDefinition) NodeService() *structs.NodeService {
ns := &structs.NodeService{
ID: s.ID,
Service: s.Name,
Tags: s.Tags,
Address: s.Address,
Port: s.Port,
}
if ns.ID == "" && ns.Service != "" {
ns.ID = ns.Service
}
return ns
}
func (s *ServiceDefinition) CheckTypes() (checks CheckTypes) {
s.Checks = append(s.Checks, &s.Check)
for _, check := range s.Checks {
if check.Valid() {
checks = append(checks, check)
}
}
return
}
// ChecKDefinition is used to JSON decode the Check definitions
type CheckDefinition struct {
ID string
Name string
Notes string
ServiceID string
Token string
Status string
CheckType `mapstructure:",squash"`
}
func (c *CheckDefinition) HealthCheck(node string) *structs.HealthCheck {
health := &structs.HealthCheck{
Node: node,
CheckID: c.ID,
Name: c.Name,
Status: structs.HealthCritical,
Notes: c.Notes,
ServiceID: c.ServiceID,
}
if c.Status != "" {
health.Status = c.Status
}
if health.CheckID == "" && health.Name != "" {
health.CheckID = health.Name
}
return health
}
// persistedService is used to wrap a service definition and bundle it
// with an ACL token so we can restore both at a later agent start.
type persistedService struct {
Token string
Service *structs.NodeService
}