mirror of
https://github.com/kemko/nomad.git
synced 2026-01-04 17:35:43 +03:00
Exctacted a method for parsing checks
This commit is contained in:
@@ -26,6 +26,9 @@ type ServiceCheck struct {
|
||||
Id string
|
||||
Name string
|
||||
Type string
|
||||
Script string
|
||||
Http string
|
||||
Protocol string
|
||||
Interval time.Duration
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
@@ -490,31 +490,8 @@ func parseServices(task *structs.Task, serviceObjs *ast.ObjectList) error {
|
||||
}
|
||||
|
||||
if co := checkList.Filter("check"); len(co.Items) > 0 {
|
||||
service.Checks = make([]structs.ServiceCheck, len(co.Items))
|
||||
for idx, co := range co.Items {
|
||||
var check structs.ServiceCheck
|
||||
label := co.Keys[0].Token.Value().(string)
|
||||
var cm map[string]interface{}
|
||||
if err := hcl.DecodeObject(&cm, co.Val); err != nil {
|
||||
return err
|
||||
}
|
||||
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
||||
WeaklyTypedInput: true,
|
||||
Result: &check,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := dec.Decode(cm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
check.Id = label
|
||||
if check.Name == "" {
|
||||
check.Name = label
|
||||
}
|
||||
service.Checks[idx] = check
|
||||
if err := parseChecks(&service, co); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -524,6 +501,37 @@ func parseServices(task *structs.Task, serviceObjs *ast.ObjectList) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseChecks(service *structs.Service, checkObjs *ast.ObjectList) error {
|
||||
service.Checks = make([]structs.ServiceCheck, len(checkObjs.Items))
|
||||
for idx, co := range checkObjs.Items {
|
||||
var check structs.ServiceCheck
|
||||
label := co.Keys[0].Token.Value().(string)
|
||||
var cm map[string]interface{}
|
||||
if err := hcl.DecodeObject(&cm, co.Val); err != nil {
|
||||
return err
|
||||
}
|
||||
dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
||||
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
||||
WeaklyTypedInput: true,
|
||||
Result: &check,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := dec.Decode(cm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
check.Id = label
|
||||
if check.Name == "" {
|
||||
check.Name = label
|
||||
}
|
||||
service.Checks[idx] = check
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func parseResources(result *structs.Resources, list *ast.ObjectList) error {
|
||||
list = list.Elem()
|
||||
if len(list.Items) == 0 {
|
||||
|
||||
@@ -1025,7 +1025,7 @@ func (sc *ServiceCheck) Validate() error {
|
||||
|
||||
// The Service model represents a Consul service defintion
|
||||
type Service struct {
|
||||
Id string // Id of the service
|
||||
Id string // Id of the service, this needs to be unique on a local machine
|
||||
Name string // Name of the service, defaults to id
|
||||
Tags []string // List of tags for the service
|
||||
PortLabel string `mapstructure:"port"` // port for the service
|
||||
|
||||
Reference in New Issue
Block a user