CloudMonitor: Correctly encode default project response (#49510)

* Correctly encode default project response

* Make getGCEDefaultProject a method of Service and add test

* Handle error appropriately

* Update test and function definition
This commit is contained in:
Andreas Christou 2022-05-25 14:05:12 +01:00 committed by GitHub
parent 8a1b723007
commit ccb8888055
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View File

@ -14,7 +14,6 @@ import (
"strings"
"github.com/andybalholm/brotli"
"github.com/grafana/grafana-google-sdk-go/pkg/utils"
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
)
@ -27,7 +26,7 @@ type processResponse func(body []byte) ([]json.RawMessage, string, error)
func (s *Service) newResourceMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/gceDefaultProject", getGCEDefaultProject)
mux.HandleFunc("/gceDefaultProject", s.getGCEDefaultProject)
mux.HandleFunc("/metricDescriptors/", s.handleResourceReq(cloudMonitor, processMetricDescriptors))
mux.HandleFunc("/services/", s.handleResourceReq(cloudMonitor, processServices))
mux.HandleFunc("/slo-services/", s.handleResourceReq(cloudMonitor, processSLOs))
@ -35,13 +34,19 @@ func (s *Service) newResourceMux() *http.ServeMux {
return mux
}
func getGCEDefaultProject(rw http.ResponseWriter, req *http.Request) {
project, err := utils.GCEDefaultProject(req.Context())
func (s *Service) getGCEDefaultProject(rw http.ResponseWriter, req *http.Request) {
project, err := s.gceDefaultProjectGetter(req.Context())
if err != nil {
writeResponse(rw, http.StatusBadRequest, fmt.Sprintf("unexpected error %v", err))
return
}
writeResponse(rw, http.StatusOK, project)
encoded, err := json.Marshal(project)
if err != nil {
writeResponse(rw, http.StatusBadRequest, fmt.Sprintf("error retrieving default project %v", err))
return
}
writeResponseBytes(rw, http.StatusOK, encoded)
}
func (s *Service) handleResourceReq(subDataSource string, responseFn processResponse) func(rw http.ResponseWriter, req *http.Request) {

View File

@ -1,7 +1,9 @@
package cloudmonitoring
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
@ -9,6 +11,7 @@ import (
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
@ -285,3 +288,23 @@ func Test_processData_functions(t *testing.T) {
})
}
}
func Test_getGCEDefaultProject(t *testing.T) {
project := "test-project"
s := Service{
im: &fakeInstance{
services: map[string]datasourceService{
cloudMonitor: {
url: routes[cloudMonitor].url,
client: &http.Client{},
},
},
},
gceDefaultProjectGetter: func(ctx context.Context) (string, error) {
return project, nil
},
}
assert.HTTPSuccess(t, s.getGCEDefaultProject, "GET", "/gceDefaultProject", nil)
assert.HTTPBodyContains(t, s.getGCEDefaultProject, "GET", "/gceDefaultProject", nil, fmt.Sprintf("\"%v\"", project))
}