ovsutils: add NormalizeDbHost

This commit is contained in:
Yousong Zhou
2020-02-28 11:43:12 +08:00
parent e8dc05084a
commit df94352e00
+23
View File
@@ -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
}