mirror of
				https://github.com/grafana/grafana.git
				synced 2025-02-25 18:55:37 -06:00 
			
		
		
		
	* Update middleware * Update Grafana Azure SDK * Client options to client builder * Fix tests * maputil from SDK
		
			
				
	
	
		
			64 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			64 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package client
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"strings"
 | |
| 
 | |
| 	"github.com/grafana/grafana-plugin-sdk-go/backend"
 | |
| 	sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
 | |
| 
 | |
| 	"github.com/grafana/grafana/pkg/infra/log"
 | |
| 	"github.com/grafana/grafana/pkg/setting"
 | |
| 	"github.com/grafana/grafana/pkg/tsdb/prometheus/azureauth"
 | |
| 	"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.
 | |
| func CreateTransportOptions(settings backend.DataSourceInstanceSettings, cfg *setting.Cfg, logger log.Logger) (*sdkhttpclient.Options, error) {
 | |
| 	opts, err := settings.HTTPClientOptions()
 | |
| 	if err != nil {
 | |
| 		return nil, fmt.Errorf("error getting HTTP options: %w", err)
 | |
| 	}
 | |
| 
 | |
| 	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"
 | |
| 	}
 | |
| 
 | |
| 	// Set Azure authentication
 | |
| 	if cfg.AzureAuthEnabled {
 | |
| 		err = azureauth.ConfigureAzureAuthentication(settings, cfg.Azure, &opts)
 | |
| 		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
 | |
| }
 |