2013-12-19 17:14:46 -08:00
|
|
|
package agent
|
|
|
|
|
2013-12-20 15:33:13 -08:00
|
|
|
import (
|
2017-06-07 01:11:56 +02:00
|
|
|
"errors"
|
2013-12-20 16:39:32 -08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2014-01-15 12:17:40 -10:00
|
|
|
"time"
|
2017-09-25 20:40:42 +02:00
|
|
|
)
|
2014-02-03 15:15:35 -08:00
|
|
|
|
2017-06-07 01:11:56 +02:00
|
|
|
var errInvalidHeaderFormat = errors.New("agent: invalid format of 'header' field")
|
2015-01-13 17:52:17 -08:00
|
|
|
|
2017-06-07 01:11:56 +02:00
|
|
|
func FixupCheckType(raw interface{}) error {
|
2014-02-03 15:15:35 -08:00
|
|
|
rawMap, ok := raw.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-24 19:44:27 -07:00
|
|
|
|
2017-06-07 01:11:56 +02:00
|
|
|
parseDuration := func(v interface{}) (time.Duration, error) {
|
|
|
|
if v == nil {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2017-06-08 22:34:08 +02: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-07 01:11:56 +02:00
|
|
|
return 0, fmt.Errorf("invalid format")
|
|
|
|
}
|
2014-04-24 19:44:27 -07:00
|
|
|
}
|
|
|
|
|
2017-06-07 01:11:56 +02: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 15:15:35 -08:00
|
|
|
}
|
|
|
|
}
|
2017-06-07 01:11:56 +02:00
|
|
|
return m, nil
|
2014-02-03 15:15:35 -08:00
|
|
|
}
|
2014-04-24 19:44:27 -07:00
|
|
|
|
2017-06-07 01:11:56 +02:00
|
|
|
replace := func(oldKey, newKey string, val interface{}) {
|
|
|
|
rawMap[newKey] = val
|
|
|
|
if oldKey != newKey {
|
|
|
|
delete(rawMap, oldKey)
|
2014-02-03 15:15:35 -08:00
|
|
|
}
|
|
|
|
}
|
2015-02-02 15:29:42 +07:00
|
|
|
|
2017-06-07 01:11:56 +02:00
|
|
|
for k, v := range rawMap {
|
|
|
|
switch strings.ToLower(k) {
|
|
|
|
case "header":
|
|
|
|
h, err := parseHeaderMap(v)
|
2017-04-20 18:59:42 -07:00
|
|
|
if err != nil {
|
2017-06-07 01:11:56 +02:00
|
|
|
return fmt.Errorf("invalid %q: %s", k, err)
|
2015-02-02 15:29:42 +07:00
|
|
|
}
|
2017-06-07 01:11:56 +02:00
|
|
|
rawMap[k] = h
|
2015-02-02 15:29:42 +07:00
|
|
|
|
2017-06-07 01:11:56 +02: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-20 18:59:42 -07:00
|
|
|
if err != nil {
|
2017-06-07 01:11:56 +02:00
|
|
|
return fmt.Errorf("invalid %q: %v", k, err)
|
2016-08-16 00:05:55 -07:00
|
|
|
}
|
2017-06-07 01:11:56 +02: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 00:05:55 -07:00
|
|
|
}
|
|
|
|
}
|
2014-02-03 15:15:35 -08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-02 12:04:04 +02:00
|
|
|
func ParseMetaPair(raw string) (string, string) {
|
|
|
|
pair := strings.SplitN(raw, ":", 2)
|
|
|
|
if len(pair) == 2 {
|
|
|
|
return pair[0], pair[1]
|
|
|
|
}
|
|
|
|
return pair[0], ""
|
|
|
|
}
|