2022-11-03 13:44:37 -05:00
|
|
|
package client
|
2022-06-23 07:48:16 -05:00
|
|
|
|
|
|
|
import (
|
2023-10-16 09:40:04 -05:00
|
|
|
"context"
|
2022-06-23 07:48:16 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
2023-01-30 02:38:51 -06:00
|
|
|
|
2023-10-20 08:54:27 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
|
2022-08-11 09:12:57 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2022-11-03 13:44:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/azureauth"
|
2022-06-23 07:48:16 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/middleware"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb/prometheus/utils"
|
|
|
|
"github.com/grafana/grafana/pkg/util/maputil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateTransportOptions creates options for the http client. Probably should be shared and should not live in the
|
|
|
|
// buffered package.
|
2023-10-16 09:40:04 -05:00
|
|
|
func CreateTransportOptions(ctx context.Context, settings backend.DataSourceInstanceSettings, cfg *setting.Cfg, logger log.Logger) (*sdkhttpclient.Options, error) {
|
|
|
|
opts, err := settings.HTTPClientOptions(ctx)
|
2022-06-23 07:48:16 -05:00
|
|
|
if err != nil {
|
2023-02-02 10:58:10 -06:00
|
|
|
return nil, fmt.Errorf("error getting HTTP options: %w", err)
|
2022-06-23 07:48:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
jsonData, err := utils.GetJsonData(settings)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error reading settings: %w", err)
|
|
|
|
}
|
|
|
|
httpMethod, _ := maputil.GetStringOptional(jsonData, "httpMethod")
|
|
|
|
|
|
|
|
opts.Middlewares = middlewares(logger, httpMethod)
|
|
|
|
|
|
|
|
// Set SigV4 service namespace
|
|
|
|
if opts.SigV4 != nil {
|
|
|
|
opts.SigV4.Service = "aps"
|
|
|
|
}
|
|
|
|
|
2022-08-11 09:12:57 -05:00
|
|
|
// Set Azure authentication
|
|
|
|
if cfg.AzureAuthEnabled {
|
|
|
|
err = azureauth.ConfigureAzureAuthentication(settings, cfg.Azure, &opts)
|
2022-06-23 07:48:16 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error configuring Azure auth: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &opts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func middlewares(logger log.Logger, httpMethod string) []sdkhttpclient.Middleware {
|
|
|
|
middlewares := []sdkhttpclient.Middleware{
|
|
|
|
// TODO: probably isn't needed anymore and should by done by http infra code
|
|
|
|
middleware.CustomQueryParameters(logger),
|
|
|
|
sdkhttpclient.CustomHeadersMiddleware(),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needed to control GET vs POST method of the requests
|
|
|
|
if strings.ToLower(httpMethod) == "get" {
|
|
|
|
middlewares = append(middlewares, middleware.ForceHttpGet(logger))
|
|
|
|
}
|
|
|
|
|
|
|
|
return middlewares
|
|
|
|
}
|