Hi,
I need to get the current user’s teams, unfortunately the User struct from GO SDK does not contain the field. I would like to do it by calling api/user
in Grafana API from backend Datasource plugin.
Grafana version: 10.4.0
grafana-plugin-sdk-go v0.240.0
I am able to call the Grafana API from frontend:
getBackendSrv().fetch({
url: `/api/user/teams`,
method: 'GET',
});
This request contains cookies: grafana_session
and grafana_session_expiry
.
However, I am facing issues by trying to do it using backend part. Please refer to the draft code:
func (d *Datasource) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
settings := req.PluginContext.DataSourceInstanceSettings
apiKey := settings.DecryptedSecureJSONData["apiKey"] // apiKey := "api-key"
idToken := req.GetHTTPHeader(backend.OAuthIdentityIDTokenHeaderName) // idToken := ""
token := strings.Fields(req.GetHTTPHeader(backend.OAuthIdentityTokenHeaderName)) // token := nil
cookies := req.GetHTTPHeader(backend.CookiesHeaderName) // cookies := ""
ctxLogger := log.DefaultLogger.FromContext(ctx)
role := req.PluginContext.User.Role // role := "admin"
appURL, err := req.PluginContext.GrafanaConfig.AppURL()
if err != nil {
return newHealthCheckErrorf("cannot get app URL %s", err.Error()), nil
}
idRequest, err := http.NewRequestWithContext(ctx, http.MethodGet, appURL+"api/user", nil)
if err != nil {
return newHealthCheckErrorf("cannot make request %s", err.Error()), nil
}
cfg := backend.GrafanaConfigFromContext(ctx)
secret, err := cfg.PluginAppClientSecret()
if err != nil {
return newHealthCheckErrorf("cannot make secret %s", err.Error()), nil // it fails here
}
clientOptions, err := req.PluginContext.AppInstanceSettings.HTTPClientOptions(ctx)
if err != nil {
return newHealthCheckErrorf("cannot make clientOptions %s", err.Error()), nil
}
idResp, err := d.httpClient.Do(idRequest)
if err != nil {
return newHealthCheckErrorf("cannot make request %s", err.Error()), nil // it returns 401
}
return &backend.CheckHealthResult{
Status: backend.HealthStatusOk,
Message: "Data source is working",
}, nil
}