From df94352e00fda2db5c8afd511935bf5237658236 Mon Sep 17 00:00:00 2001 From: Yousong Zhou Date: Thu, 27 Feb 2020 19:58:52 +0800 Subject: [PATCH] ovsutils: add NormalizeDbHost --- pkg/util/ovsutils/ovsutils.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/util/ovsutils/ovsutils.go b/pkg/util/ovsutils/ovsutils.go index 7076d2c6ee..1769f49e5f 100644 --- a/pkg/util/ovsutils/ovsutils.go +++ b/pkg/util/ovsutils/ovsutils.go @@ -15,10 +15,13 @@ package ovsutils import ( + "fmt" + "net" "regexp" "strings" "yunion.io/x/log" + "yunion.io/x/pkg/errors" "yunion.io/x/pkg/utils" "yunion.io/x/onecloud/pkg/util/procutils" @@ -103,3 +106,23 @@ func CleanAllHiddenPorts() { CleanHiddenPorts(br) } } + +func NormalizeDbHost(db string) (string, error) { + if strings.HasPrefix(db, "tcp:") { + host, port, err := net.SplitHostPort(db[4:]) + if err != nil { + return "", errors.Wrapf(err, "split host port: %s", db) + } + if ip := net.ParseIP(host); len(ip) == 0 { + addrs, err := net.LookupHost(host) + if err != nil { + return "", errors.Wrapf(err, "dns lookup (%s) failed", host) + } + if len(addrs) == 0 { + return "", fmt.Errorf("dns lookup (%s) returned empty result", host) + } + return "tcp:" + addrs[0] + ":" + port, nil + } + } + return db, nil +}