mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 21:35:52 +00:00
watch: extract makeWatchPlan to facilitate testing
There is a bug in here now that slices in opaque config are unsliced. But to test that bug fix we need a function that can be easily tested.
This commit is contained in:
parent
2f721504a7
commit
653c938edc
@ -1336,44 +1336,10 @@ func (a *Agent) reloadWatches(cfg *config.RuntimeConfig) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse the watches, excluding 'handler' and 'args'
|
wp, err := makeWatchPlan(a.logger, params)
|
||||||
wp, err := watch.ParseExempt(params, []string{"handler", "args"})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Failed to parse watch (%#v): %v", params, err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the handler and subprocess arguments
|
|
||||||
handler, hasHandler := wp.Exempt["handler"]
|
|
||||||
args, hasArgs := wp.Exempt["args"]
|
|
||||||
if hasHandler {
|
|
||||||
a.logger.Warn("The 'handler' field in watches has been deprecated " +
|
|
||||||
"and replaced with the 'args' field. See https://www.consul.io/docs/agent/watches.html")
|
|
||||||
}
|
|
||||||
if _, ok := handler.(string); hasHandler && !ok {
|
|
||||||
return fmt.Errorf("Watch handler must be a string")
|
|
||||||
}
|
|
||||||
if raw, ok := args.([]interface{}); hasArgs && ok {
|
|
||||||
var parsed []string
|
|
||||||
for _, arg := range raw {
|
|
||||||
v, ok := arg.(string)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("Watch args must be a list of strings")
|
|
||||||
}
|
|
||||||
|
|
||||||
parsed = append(parsed, v)
|
|
||||||
}
|
|
||||||
wp.Exempt["args"] = parsed
|
|
||||||
} else if hasArgs && !ok {
|
|
||||||
return fmt.Errorf("Watch args must be a list of strings")
|
|
||||||
}
|
|
||||||
if hasHandler && hasArgs || hasHandler && wp.HandlerType == "http" || hasArgs && wp.HandlerType == "http" {
|
|
||||||
return fmt.Errorf("Only one watch handler allowed")
|
|
||||||
}
|
|
||||||
if !hasHandler && !hasArgs && wp.HandlerType != "http" {
|
|
||||||
return fmt.Errorf("Must define a watch handler")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store the watch plan
|
|
||||||
watchPlans = append(watchPlans, wp)
|
watchPlans = append(watchPlans, wp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -184,3 +184,44 @@ func makeHTTPWatchHandler(logger hclog.Logger, config *watch.HttpHandlerConfig)
|
|||||||
}
|
}
|
||||||
return fn
|
return fn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: return a fully constructed watch.Plan with a Plan.Handler, so that Except
|
||||||
|
// can be ignored by the caller.
|
||||||
|
func makeWatchPlan(logger hclog.Logger, params map[string]interface{}) (*watch.Plan, error) {
|
||||||
|
wp, err := watch.ParseExempt(params, []string{"handler", "args"})
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Failed to parse watch (%#v): %v", params, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the handler and subprocess arguments
|
||||||
|
handler, hasHandler := wp.Exempt["handler"]
|
||||||
|
args, hasArgs := wp.Exempt["args"]
|
||||||
|
if hasHandler {
|
||||||
|
logger.Warn("The 'handler' field in watches has been deprecated " +
|
||||||
|
"and replaced with the 'args' field. See https://www.consul.io/docs/agent/watches.html")
|
||||||
|
}
|
||||||
|
if _, ok := handler.(string); hasHandler && !ok {
|
||||||
|
return nil, fmt.Errorf("Watch handler must be a string")
|
||||||
|
}
|
||||||
|
if raw, ok := args.([]interface{}); hasArgs && ok {
|
||||||
|
var parsed []string
|
||||||
|
for _, arg := range raw {
|
||||||
|
v, ok := arg.(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("Watch args must be a list of strings")
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed = append(parsed, v)
|
||||||
|
}
|
||||||
|
wp.Exempt["args"] = parsed
|
||||||
|
} else if hasArgs && !ok {
|
||||||
|
return nil, fmt.Errorf("Watch args must be a list of strings")
|
||||||
|
}
|
||||||
|
if hasHandler && hasArgs || hasHandler && wp.HandlerType == "http" || hasArgs && wp.HandlerType == "http" {
|
||||||
|
return nil, fmt.Errorf("Only one watch handler allowed")
|
||||||
|
}
|
||||||
|
if !hasHandler && !hasArgs && wp.HandlerType != "http" {
|
||||||
|
return nil, fmt.Errorf("Must define a watch handler")
|
||||||
|
}
|
||||||
|
return wp, nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user