From 881833232e91f9840f2164f89f89dd30ee247ca6 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Tue, 21 Jun 2016 14:27:04 -0700 Subject: [PATCH] Unify swarm init and update options Add api side validation and defaults for init and join requests. Signed-off-by: Tonis Tiigi --- opts/hosts.go | 11 +++++++---- opts/hosts_test.go | 4 ++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/opts/hosts.go b/opts/hosts.go index ad16759236..957b24ce7d 100644 --- a/opts/hosts.go +++ b/opts/hosts.go @@ -70,7 +70,7 @@ func parseDockerDaemonHost(addr string) (string, error) { switch addrParts[0] { case "tcp": - return parseTCPAddr(addrParts[1], DefaultTCPHost) + return ParseTCPAddr(addrParts[1], DefaultTCPHost) case "unix": return parseSimpleProtoAddr("unix", addrParts[1], DefaultUnixSocket) case "npipe": @@ -97,12 +97,12 @@ func parseSimpleProtoAddr(proto, addr, defaultAddr string) (string, error) { return fmt.Sprintf("%s://%s", proto, addr), nil } -// parseTCPAddr parses and validates that the specified address is a valid TCP +// ParseTCPAddr parses and validates that the specified address is a valid TCP // address. It returns a formatted TCP address, either using the address parsed // from tryAddr, or the contents of defaultAddr if tryAddr is a blank string. // tryAddr is expected to have already been Trim()'d // defaultAddr must be in the full `tcp://host:port` form -func parseTCPAddr(tryAddr string, defaultAddr string) (string, error) { +func ParseTCPAddr(tryAddr string, defaultAddr string) (string, error) { if tryAddr == "" || tryAddr == "tcp://" { return defaultAddr, nil } @@ -127,8 +127,11 @@ func parseTCPAddr(tryAddr string, defaultAddr string) (string, error) { if err != nil { return "", err } - host, port, err := net.SplitHostPort(u.Host) + if err != nil { + // try port addition once + host, port, err = net.SplitHostPort(net.JoinHostPort(u.Host, defaultPort)) + } if err != nil { return "", fmt.Errorf("Invalid bind address format: %s", tryAddr) } diff --git a/opts/hosts_test.go b/opts/hosts_test.go index dc527e6388..57161eaf46 100644 --- a/opts/hosts_test.go +++ b/opts/hosts_test.go @@ -130,12 +130,12 @@ func TestParseTCP(t *testing.T) { "localhost:5555/path": "tcp://localhost:5555/path", } for invalidAddr, expectedError := range invalids { - if addr, err := parseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError { + if addr, err := ParseTCPAddr(invalidAddr, defaultHTTPHost); err == nil || err.Error() != expectedError { t.Errorf("tcp %v address expected error %v return, got %s and addr %v", invalidAddr, expectedError, err, addr) } } for validAddr, expectedAddr := range valids { - if addr, err := parseTCPAddr(validAddr, defaultHTTPHost); err != nil || addr != expectedAddr { + if addr, err := ParseTCPAddr(validAddr, defaultHTTPHost); err != nil || addr != expectedAddr { t.Errorf("%v -> expected %v, got %v and addr %v", validAddr, expectedAddr, err, addr) } }