Exctacted a method for parsing checks

This commit is contained in:
Diptanu Choudhury
2015-11-17 14:21:14 -08:00
parent acc8ca73f7
commit 3a250a179b
3 changed files with 37 additions and 26 deletions

View File

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

View File

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

View File

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