Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions cns/restserver/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"io"
"net"
"net/http"
"regexp"
"net/url"
"runtime"
"strings"

Expand Down Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion cns/restserver/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down