grafana/pkg/tsdb/azuremonitor/httpclient.go
Andreas Christou dd77ff6bcd
Plugins: Externalise Azure Monitor data source (#79545)
* Set up frontend linting for Azure

- Fix final frontend import
- Fix other lint issues

* Add Azure Monitor to backend linting

* Remove featuremgmt dependency

* Add intervalv2 to list of disallowed imports

* Remove config dependency

- Replace with function from azure-sdk

* Remove util dependency

* Duplicate interval functionality from core

* Add required backend wrappers

* Update frontend

* Add testing helper

* Add missing package

* Bump minimum grafana dependency

* Fix dependency

* Regen cue

* Fix lint

* Update expected response file

* Update import and dependency
2024-01-03 19:20:22 +00:00

47 lines
1.5 KiB
Go

package azuremonitor
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"github.com/grafana/grafana-azure-sdk-go/azcredentials"
"github.com/grafana/grafana-azure-sdk-go/azhttpclient"
"github.com/grafana/grafana-azure-sdk-go/azsettings"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/types"
)
type Provider interface {
New(...httpclient.Options) (*http.Client, error)
GetTransport(...httpclient.Options) (http.RoundTripper, error)
GetTLSConfig(...httpclient.Options) (*tls.Config, error)
}
func newHTTPClient(ctx context.Context, route types.AzRoute, model types.DatasourceInfo, settings *backend.DataSourceInstanceSettings, azureSettings *azsettings.AzureSettings, clientProvider Provider) (*http.Client, error) {
clientOpts, err := settings.HTTPClientOptions(ctx)
if err != nil {
return nil, fmt.Errorf("error getting HTTP options: %w", err)
}
for header, value := range route.Headers {
clientOpts.Headers[header] = value
}
// Use Azure credentials if the route has OAuth scopes configured
if len(route.Scopes) > 0 {
if cred, ok := model.Credentials.(*azcredentials.AzureClientSecretCredentials); ok && cred.ClientSecret == "" {
return nil, fmt.Errorf("unable to initialize HTTP Client: clientSecret not found")
}
authOpts := azhttpclient.NewAuthOptions(azureSettings)
authOpts.Scopes(route.Scopes)
azhttpclient.AddAzureAuthentication(&clientOpts, authOpts, model.Credentials)
}
return clientProvider.New(clientOpts)
}