mirror of https://github.com/grafana/grafana
Prometheus: Fix Azure authentication support (#44407)
Re-adding back Azure authentication support to Prometheus datasource after the datasource query logic was rewritten from plugin.json routes to Go backend. Ref #35857pull/44866/head
parent
6d931226d8
commit
85ea1a5d64
@ -0,0 +1,32 @@ |
||||
package promclient |
||||
|
||||
import ( |
||||
"fmt" |
||||
|
||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient" |
||||
"github.com/grafana/grafana/pkg/tsdb/azuremonitor/azcredentials" |
||||
"github.com/grafana/grafana/pkg/util/maputil" |
||||
) |
||||
|
||||
func (p *Provider) configureAzureAuthentication(opts sdkhttpclient.Options) error { |
||||
credentials, err := azcredentials.FromDatasourceData(p.jsonData, p.settings.DecryptedSecureJSONData) |
||||
if err != nil { |
||||
err = fmt.Errorf("invalid Azure credentials: %s", err) |
||||
return err |
||||
} |
||||
|
||||
if credentials != nil { |
||||
opts.CustomOptions["_azureCredentials"] = credentials |
||||
|
||||
resourceId, err := maputil.GetStringOptional(p.jsonData, "azureEndpointResourceId") |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
if resourceId != "" { |
||||
opts.CustomOptions["azureEndpointResourceId"] = resourceId |
||||
} |
||||
} |
||||
|
||||
return nil |
||||
} |
||||
@ -0,0 +1,73 @@ |
||||
package maputil |
||||
|
||||
import "fmt" |
||||
|
||||
func GetMap(obj map[string]interface{}, key string) (map[string]interface{}, error) { |
||||
if untypedValue, ok := obj[key]; ok { |
||||
if value, ok := untypedValue.(map[string]interface{}); ok { |
||||
return value, nil |
||||
} else { |
||||
err := fmt.Errorf("the field '%s' should be an object", key) |
||||
return nil, err |
||||
} |
||||
} else { |
||||
err := fmt.Errorf("the field '%s' should be set", key) |
||||
return nil, err |
||||
} |
||||
} |
||||
|
||||
func GetBool(obj map[string]interface{}, key string) (bool, error) { |
||||
if untypedValue, ok := obj[key]; ok { |
||||
if value, ok := untypedValue.(bool); ok { |
||||
return value, nil |
||||
} else { |
||||
err := fmt.Errorf("the field '%s' should be a bool", key) |
||||
return false, err |
||||
} |
||||
} else { |
||||
err := fmt.Errorf("the field '%s' should be set", key) |
||||
return false, err |
||||
} |
||||
} |
||||
|
||||
func GetBoolOptional(obj map[string]interface{}, key string) (bool, error) { |
||||
if untypedValue, ok := obj[key]; ok { |
||||
if value, ok := untypedValue.(bool); ok { |
||||
return value, nil |
||||
} else { |
||||
err := fmt.Errorf("the field '%s' should be a bool", key) |
||||
return false, err |
||||
} |
||||
} else { |
||||
// Value optional, not error
|
||||
return false, nil |
||||
} |
||||
} |
||||
|
||||
func GetString(obj map[string]interface{}, key string) (string, error) { |
||||
if untypedValue, ok := obj[key]; ok { |
||||
if value, ok := untypedValue.(string); ok { |
||||
return value, nil |
||||
} else { |
||||
err := fmt.Errorf("the field '%s' should be a string", key) |
||||
return "", err |
||||
} |
||||
} else { |
||||
err := fmt.Errorf("the field '%s' should be set", key) |
||||
return "", err |
||||
} |
||||
} |
||||
|
||||
func GetStringOptional(obj map[string]interface{}, key string) (string, error) { |
||||
if untypedValue, ok := obj[key]; ok { |
||||
if value, ok := untypedValue.(string); ok { |
||||
return value, nil |
||||
} else { |
||||
err := fmt.Errorf("the field '%s' should be a string", key) |
||||
return "", err |
||||
} |
||||
} else { |
||||
// Value optional, not error
|
||||
return "", nil |
||||
} |
||||
} |
||||
Loading…
Reference in new issue