diff --git a/cns/restserver/api.go b/cns/restserver/api.go index c5f820c95c..ecbc3efa7e 100644 --- a/cns/restserver/api.go +++ b/cns/restserver/api.go @@ -9,7 +9,7 @@ import ( "io" "net" "net/http" - "regexp" + "net/url" "runtime" "strings" @@ -1082,14 +1082,12 @@ func getAuthTokenFromCreateNetworkContainerURL( return strings.Split(strings.Split(createNetworkContainerURL, "authenticationToken/")[1], "/")[0] } -var rgx = regexp.MustCompile("^http[s]?://(.*?)/joinedVirtualNetworks.*?$") - -func extractHostFromJoinNetworkURL(url string) string { - submatches := rgx.FindStringSubmatch(url) - if len(submatches) != 2 { +func extractHostFromJoinNetworkURL(urlToParse string) string { + parsedURL, err := url.Parse(urlToParse) + if err != nil { return "" } - return submatches[1] + return parsedURL.Host } // Publish Network Container by calling nmagent diff --git a/cns/restserver/api_test.go b/cns/restserver/api_test.go index 3c61b1fe53..34fc663f89 100644 --- a/cns/restserver/api_test.go +++ b/cns/restserver/api_test.go @@ -562,13 +562,29 @@ func publishNCViaCNS(t *testing.T, } func TestExtractHost(t *testing.T) { - joinURL := "http://127.0.0.1:9001/joinedVirtualNetworks/c9b8e695-2de1-11eb-bf54-000d3af666c8/api-version/1" + joinURL := "http://127.0.0.1:9001/machine/plugins/?comp=nmagent&type=NetworkManagement/joinedVirtualNetworks/c9b8e695-2de1-11eb-bf54-000d3af666c8/api-version/1" host := extractHostFromJoinNetworkURL(joinURL) expected := "127.0.0.1:9001" if host != expected { t.Fatalf("expected host %q, got %q", expected, host) } + + joinURL = "http://168.63.129.16/machine/plugins/?comp=nmagent&type=NetworkManagement/joinedVirtualNetworks/4941a21f-1a8d-4d0f-8256-cc6e73a8cd22/api-version/1" + + host = extractHostFromJoinNetworkURL(joinURL) + expected = "168.63.129.16" + if host != expected { + t.Fatalf("expected host %q, got %q", expected, host) + } + + joinURL = "http://168.63.129.16/joinedVirtualNetworks/4941a21f-1a8d-4d0f-8256-cc6e73a8cd22/api-version/1" + + host = extractHostFromJoinNetworkURL(joinURL) + expected = "168.63.129.16" + if host != expected { + t.Fatalf("expected host %q, got %q", expected, host) + } } func TestUnpublishNCViaCNS(t *testing.T) {