api: Ensure the internal/ui/gateway-service-nodes endpoint responds with an array (#9593)

In some circumstances this endpoint will have no results in it (dues to
ACLs, Namespaces, filtering or missing configuration).

This ensures that the response is at least an empty array (`[]`) rather
than `null`
This commit is contained in:
John Cowen 2021-01-20 16:59:02 +00:00 committed by GitHub
parent d871a6f260
commit 82a5e6d6fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -261,7 +261,12 @@ RPC:
}
summaries, _ := summarizeServices(out.Dump, s.agent.config, args.Datacenter)
return prepSummaryOutput(summaries, false), nil
prepped := prepSummaryOutput(summaries, false)
if prepped == nil {
prepped = make([]*ServiceSummary, 0)
}
return prepped, nil
}
// UIServiceTopology returns the list of upstreams and downstreams for a Connect enabled service.
@ -449,6 +454,8 @@ func summarizeServices(dump structs.ServiceDump, cfg *config.RuntimeConfig, dc s
func prepSummaryOutput(summaries map[structs.ServiceName]*ServiceSummary, excludeSidecars bool) []*ServiceSummary {
var resp []*ServiceSummary
// Ensure at least a zero length slice
resp = make([]*ServiceSummary, 0)
// Collect and sort resp for display
for _, sum := range summaries {

View File

@ -629,7 +629,18 @@ func TestUIGatewayServiceNodes_Terminating(t *testing.T) {
},
}
require.NoError(t, a.RPC("Catalog.Register", &arg, &regOutput))
}
{
// Request without having registered the config-entry, shouldn't respond with null
req, _ := http.NewRequest("GET", "/v1/internal/ui/gateway-services-nodes/terminating-gateway", nil)
resp := httptest.NewRecorder()
obj, err := a.srv.UIGatewayServicesNodes(resp, req)
require.Nil(t, err)
require.NotNil(t, obj)
}
{
// Register terminating-gateway config entry, linking it to db and redis (does not exist)
args := &structs.TerminatingGatewayConfigEntry{
Name: "terminating-gateway",