grafana/pkg/tsdb/cloudmonitoring/httpclient.go

69 lines
1.9 KiB
Go

package cloudmonitoring
import (
"net/http"
"github.com/grafana/grafana-google-sdk-go/pkg/tokenprovider"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
infrahttp "github.com/grafana/grafana/pkg/infra/httpclient"
)
const (
cloudMonitor = "cloudmonitoring"
resourceManager = "cloudresourcemanager"
)
type routeInfo struct {
method string
url string
scopes []string
}
var routes = map[string]routeInfo{
cloudMonitor: {
method: "GET",
url: "https://monitoring.googleapis.com",
scopes: []string{"https://www.googleapis.com/auth/monitoring.read"},
},
resourceManager: {
method: "GET",
url: "https://cloudresourcemanager.googleapis.com",
scopes: []string{"https://www.googleapis.com/auth/cloudplatformprojects.readonly"},
},
}
func getMiddleware(model *datasourceInfo, routePath string) (httpclient.Middleware, error) {
providerConfig := tokenprovider.Config{
RoutePath: routePath,
RouteMethod: routes[routePath].method,
DataSourceID: model.id,
DataSourceUpdated: model.updated,
Scopes: routes[routePath].scopes,
}
var provider tokenprovider.TokenProvider
switch model.authenticationType {
case gceAuthentication:
provider = tokenprovider.NewGceAccessTokenProvider(providerConfig)
case jwtAuthentication:
providerConfig.JwtTokenConfig = &tokenprovider.JwtTokenConfig{
Email: model.clientEmail,
URI: model.tokenUri,
PrivateKey: []byte(model.decryptedSecureJSONData["privateKey"]),
}
provider = tokenprovider.NewJwtAccessTokenProvider(providerConfig)
}
return tokenprovider.AuthMiddleware(provider), nil
}
func newHTTPClient(model *datasourceInfo, opts httpclient.Options, clientProvider infrahttp.Provider, route string) (*http.Client, error) {
m, err := getMiddleware(model, route)
if err != nil {
return nil, err
}
opts.Middlewares = append(opts.Middlewares, m)
return clientProvider.New(opts)
}