2013-12-20 01:14:46 +00:00
|
|
|
package agent
|
|
|
|
|
2013-12-20 23:33:13 +00:00
|
|
|
import (
|
2017-06-06 23:11:56 +00:00
|
|
|
"errors"
|
2013-12-21 00:39:32 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-01-15 22:17:40 +00:00
|
|
|
"time"
|
2017-09-25 18:40:42 +00:00
|
|
|
)
|
2014-02-03 23:15:35 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
var errInvalidHeaderFormat = errors.New("agent: invalid format of 'header' field")
|
2015-01-14 01:52:17 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
func FixupCheckType(raw interface{}) error {
|
2014-02-03 23:15:35 +00:00
|
|
|
rawMap, ok := raw.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-25 02:44:27 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
parseDuration := func(v interface{}) (time.Duration, error) {
|
|
|
|
if v == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2017-06-08 20:34:08 +00:00
|
|
|
switch x := v.(type) {
|
|
|
|
case time.Duration:
|
|
|
|
return x, nil
|
|
|
|
case float64:
|
|
|
|
return time.Duration(x), nil
|
|
|
|
case string:
|
|
|
|
return time.ParseDuration(x)
|
|
|
|
default:
|
2017-06-06 23:11:56 +00:00
|
|
|
return 0, fmt.Errorf("invalid format")
|
|
|
|
}
|
2014-04-25 02:44:27 +00:00
|
|
|
}
|
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
parseHeaderMap := func(v interface{}) (map[string][]string, error) {
|
|
|
|
if v == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
vm, ok := v.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, errInvalidHeaderFormat
|
|
|
|
}
|
|
|
|
m := map[string][]string{}
|
|
|
|
for k, vv := range vm {
|
|
|
|
vs, ok := vv.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil, errInvalidHeaderFormat
|
|
|
|
}
|
|
|
|
for _, vs := range vs {
|
|
|
|
s, ok := vs.(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errInvalidHeaderFormat
|
|
|
|
}
|
|
|
|
m[k] = append(m[k], s)
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-06 23:11:56 +00:00
|
|
|
return m, nil
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
2014-04-25 02:44:27 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
replace := func(oldKey, newKey string, val interface{}) {
|
|
|
|
rawMap[newKey] = val
|
|
|
|
if oldKey != newKey {
|
|
|
|
delete(rawMap, oldKey)
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-02 08:29:42 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
for k, v := range rawMap {
|
|
|
|
switch strings.ToLower(k) {
|
|
|
|
case "header":
|
|
|
|
h, err := parseHeaderMap(v)
|
2017-04-21 01:59:42 +00:00
|
|
|
if err != nil {
|
2017-06-06 23:11:56 +00:00
|
|
|
return fmt.Errorf("invalid %q: %s", k, err)
|
2015-02-02 08:29:42 +00:00
|
|
|
}
|
2017-06-06 23:11:56 +00:00
|
|
|
rawMap[k] = h
|
2015-02-02 08:29:42 +00:00
|
|
|
|
2017-06-06 23:11:56 +00:00
|
|
|
case "ttl", "interval", "timeout":
|
|
|
|
d, err := parseDuration(v)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid %q: %v", k, err)
|
|
|
|
}
|
|
|
|
rawMap[k] = d
|
|
|
|
|
|
|
|
case "deregister_critical_service_after", "deregistercriticalserviceafter":
|
|
|
|
d, err := parseDuration(v)
|
2017-04-21 01:59:42 +00:00
|
|
|
if err != nil {
|
2017-06-06 23:11:56 +00:00
|
|
|
return fmt.Errorf("invalid %q: %v", k, err)
|
2016-08-16 07:05:55 +00:00
|
|
|
}
|
2017-06-06 23:11:56 +00:00
|
|
|
replace(k, "DeregisterCriticalServiceAfter", d)
|
|
|
|
|
|
|
|
case "docker_container_id":
|
|
|
|
replace(k, "DockerContainerID", v)
|
|
|
|
|
|
|
|
case "service_id":
|
|
|
|
replace(k, "ServiceID", v)
|
|
|
|
|
|
|
|
case "tls_skip_verify":
|
|
|
|
replace(k, "TLSSkipVerify", v)
|
2016-08-16 07:05:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-03 23:15:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:04:04 +00:00
|
|
|
func ParseMetaPair(raw string) (string, string) {
|
|
|
|
pair := strings.SplitN(raw, ":", 2)
|
|
|
|
if len(pair) == 2 {
|
|
|
|
return pair[0], pair[1]
|
|
|
|
}
|
|
|
|
return pair[0], ""
|
|
|
|
}
|