Skip to content

Commit f6d0190

Browse files
committed
Reject a single partial network name match
Require the requested name to match the sole keyword candidate before returning its ID. #164 (comment) Signed-off-by: 1fanwang <1fannnw@gmail.com>
1 parent ad15f21 commit f6d0190

4 files changed

Lines changed: 28 additions & 3 deletions

File tree

ci/ci_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ func TestCloudstackAPI(t *testing.T) {
123123
}
124124
})
125125
}
126+
t.Run("PartialName", func(t *testing.T) {
127+
name := strings.TrimPrefix(resources.networkName, "ci-")
128+
id, count, err := client.Network.GetNetworkID(name)
129+
t.Logf("GetNetworkID(%q): id=%q count=%d error=%v", name, id, count, err)
130+
if id != "" || err == nil || count < 0 || count > 1 {
131+
t.Error("partial name must not resolve to a network")
132+
}
133+
network, count, err := client.Network.GetNetworkByName(name)
134+
id = ""
135+
if network != nil {
136+
id = network.Id
137+
}
138+
t.Logf("GetNetworkByName(%q): id=%q count=%d error=%v", name, id, count, err)
139+
if network != nil || err == nil || count < 0 || count > 1 {
140+
t.Error("partial name must not resolve to a network")
141+
}
142+
})
126143
})
127144

128145
// Run the actual tests

cloudstack/NetworkService.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4872,7 +4872,7 @@ func (s *NetworkService) GetNetworkID(name string, opts ...OptionFunc) (string,
48724872
return "", l.Count, fmt.Errorf("No match found for %s: %+v", name, l)
48734873
}
48744874

4875-
if l.Count == 1 {
4875+
if l.Count == 1 && l.Networks[0].Name == name {
48764876
return l.Networks[0].Id, l.Count, nil
48774877
}
48784878

generate/generate.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,11 @@ func (s *service) generateHelperFuncs(a *API) {
17031703
pn(" return \"\", l.Count, fmt.Errorf(\"No match found for %%s: %%+v\", %s, l)", v)
17041704
pn(" }")
17051705
pn("")
1706-
pn(" if l.Count == 1 {")
1706+
if a.Name == "listNetworks" {
1707+
pn(" if l.Count == 1 && l.%s[0].Name == %s {", ln, v)
1708+
} else {
1709+
pn(" if l.Count == 1 {")
1710+
}
17071711
pn(" return l.%s[0].Id, l.Count, nil", ln)
17081712
pn(" }")
17091713
pn("")

test/GetNetworkByNameRegression_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func TestNetworkLookupCompatibility(t *testing.T) {
4646
wantCount int
4747
}{
4848
{"single match", "existing", []*cloudstack.Network{existing}, networkID, 1},
49+
{"single partial match", "exist", []*cloudstack.Network{existing}, "", 1},
4950
{"missing with one network", "missing", []*cloudstack.Network{existing}, "", 0},
5051
{"exact match among partial matches", "existing", multiple, networkID, 2},
5152
{"missing with multiple networks", "missing", multiple, "", 0},
@@ -63,6 +64,9 @@ func TestNetworkLookupCompatibility(t *testing.T) {
6364
if supportsName && wantCount > 1 {
6465
wantCount = 1
6566
}
67+
if supportsName && tc.wantID == "" {
68+
wantCount = 0
69+
}
6670
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
6771
q := r.URL.Query()
6872
if q.Get("command") != "listNetworks" || q.Get("zoneid") != zoneID {
@@ -102,7 +106,7 @@ func TestNetworkLookupCompatibility(t *testing.T) {
102106

103107
network, count, err := client.Network.GetNetworkByName(tc.lookup, cloudstack.WithZone(zoneID))
104108
if tc.wantID == "" {
105-
if network != nil || count != 0 || err == nil {
109+
if network != nil || count != wantCount || err == nil {
106110
t.Errorf("GetNetworkByName(%q) = %+v, %d, %v; want no match",
107111
tc.lookup, network, count, err)
108112
}

0 commit comments

Comments
 (0)