mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
K8s: Match status codes from single tenant (#90153)
Co-authored-by: Jean-Philippe Quémémer <jeanphilippe.quemener@grafana.com>
This commit is contained in:
parent
936b3a7a5d
commit
bb187ce4b1
@ -1945,4 +1945,4 @@ delete_access_policy_timeout = 5s
|
|||||||
# The domain name used to access cms
|
# The domain name used to access cms
|
||||||
domain = grafana-dev.net
|
domain = grafana-dev.net
|
||||||
# Folder used to store snapshot files. Defaults to the home dir
|
# Folder used to store snapshot files. Defaults to the home dir
|
||||||
snapshot_folder = ""
|
snapshot_folder = ""
|
||||||
|
@ -27,14 +27,14 @@ type QueryDataResponse struct {
|
|||||||
backend.QueryDataResponse `json:",inline"`
|
backend.QueryDataResponse `json:",inline"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// If errors exist, return multi-status
|
// GetResponseCode return the right status code for the response by checking the responses.
|
||||||
func GetResponseCode(rsp *backend.QueryDataResponse) int {
|
func GetResponseCode(rsp *backend.QueryDataResponse) int {
|
||||||
if rsp == nil {
|
if rsp == nil {
|
||||||
return http.StatusInternalServerError
|
return http.StatusBadRequest
|
||||||
}
|
}
|
||||||
for _, v := range rsp.Responses {
|
for _, res := range rsp.Responses {
|
||||||
if v.Error != nil {
|
if res.Error != nil {
|
||||||
return http.StatusMultiStatus
|
return http.StatusBadRequest
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return http.StatusOK
|
return http.StatusOK
|
||||||
|
@ -2,9 +2,12 @@ package v0alpha1_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||||
data "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
|
data "github.com/grafana/grafana-plugin-sdk-go/experimental/apis/data/v0alpha1"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
|
query "github.com/grafana/grafana/pkg/apis/query/v0alpha1"
|
||||||
@ -76,3 +79,39 @@ func TestParseQueriesIntoQueryDataRequest(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}`, string(out))
|
}`, string(out))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetResponseCode(t *testing.T) {
|
||||||
|
t.Run("return 200 if no errors in responses", func(t *testing.T) {
|
||||||
|
assert.Equal(t, 200, query.GetResponseCode(&backend.QueryDataResponse{
|
||||||
|
Responses: map[string]backend.DataResponse{
|
||||||
|
"A": {
|
||||||
|
Error: nil,
|
||||||
|
},
|
||||||
|
"B": {
|
||||||
|
Error: nil,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
t.Run("return 400 if there is an error in the responses", func(t *testing.T) {
|
||||||
|
assert.Equal(t, 400, query.GetResponseCode(&backend.QueryDataResponse{
|
||||||
|
Responses: map[string]backend.DataResponse{
|
||||||
|
"A": {
|
||||||
|
Error: fmt.Errorf("some wild error"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
t.Run("return 400 if there is a partial error", func(t *testing.T) {
|
||||||
|
assert.Equal(t, 400, query.GetResponseCode(&backend.QueryDataResponse{
|
||||||
|
Responses: map[string]backend.DataResponse{
|
||||||
|
"A": {
|
||||||
|
Error: nil,
|
||||||
|
},
|
||||||
|
"B": {
|
||||||
|
Error: fmt.Errorf("some partial error"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -80,19 +80,11 @@ func (d *pluginClient) QueryData(ctx context.Context, req data.QueryDataRequest)
|
|||||||
return http.StatusBadRequest, nil, err
|
return http.StatusBadRequest, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
code := http.StatusOK
|
|
||||||
rsp, err := d.pluginClient.QueryData(ctx, qdr)
|
rsp, err := d.pluginClient.QueryData(ctx, qdr)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
for _, v := range rsp.Responses {
|
return http.StatusInternalServerError, rsp, err
|
||||||
if v.Error != nil {
|
|
||||||
code = http.StatusMultiStatus
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
code = http.StatusInternalServerError
|
|
||||||
}
|
}
|
||||||
return code, rsp, err
|
return query.GetResponseCode(rsp), rsp, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDatasourceAPI implements DataSourceRegistry.
|
// GetDatasourceAPI implements DataSourceRegistry.
|
||||||
|
Loading…
Reference in New Issue
Block a user