Fix nil pointer with revamped chosen host ID logic

This commit is contained in:
Jeff Mitchell committed 2020-09-21 10:21:40 -04:00
1 parent fcdaf129a3
commit 5747ea603e
1 file changed
+8 -2
@@ -310,11 +310,11 @@ HostSetIterationLoop:
}
for _, host := range hosts {
compoundId := compoundHost{hostSetId: hsId, hostId: host.PublicId}
hostIds = append(hostIds, compoundId)
if host.PublicId == requestedId {
chosenId = &compoundId
break HostSetIterationLoop
}
hostIds = append(hostIds, compoundId)
}
}
}
@@ -326,7 +326,13 @@ HostSetIterationLoop:
"host_id": "The requested host id is not available.",
})
}
chosenId = &hostIds[rand.Intn(len(hostIds))]
if chosenId == nil {
if len(hostIds) == 0 {
// No hosts were found, error
return nil, handlers.NotFoundErrorf("No hosts found from available target host sets.")
}
chosenId = &hostIds[rand.Intn(len(hostIds))]
}
// Generate the endpoint URL
endpointUrl := &url.URL{