mirror of
https://github.com/grafana/grafana.git
synced 2025-02-11 08:05:43 -06:00
* cleanup cloudwatch.go * streamline interface naming * use utility func * rename test utils file * move util function to where they are used * move dtos to models * split integration tests from the rest * Update pkg/tsdb/cloudwatch/cloudwatch.go Co-authored-by: Isabella Siu <Isabella.siu@grafana.com> * refactor error codes aggregation * move error messages to models Co-authored-by: Isabella Siu <Isabella.siu@grafana.com>
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package routes
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/services"
|
|
)
|
|
|
|
func AccountsHandler(pluginCtx backend.PluginContext, reqCtxFactory models.RequestContextFactoryFunc, parameters url.Values) ([]byte, *models.HttpError) {
|
|
region := parameters.Get("region")
|
|
if region == "" {
|
|
return nil, models.NewHttpError("error in AccountsHandler", http.StatusBadRequest, fmt.Errorf("region is required"))
|
|
}
|
|
|
|
service, err := newAccountsService(pluginCtx, reqCtxFactory, region)
|
|
if err != nil {
|
|
return nil, models.NewHttpError("error in AccountsHandler", http.StatusInternalServerError, err)
|
|
}
|
|
|
|
accounts, err := service.GetAccountsForCurrentUserOrRole()
|
|
if err != nil {
|
|
msg := "error getting accounts for current user or role"
|
|
switch {
|
|
case errors.Is(err, services.ErrAccessDeniedException):
|
|
return nil, models.NewHttpError(msg, http.StatusForbidden, err)
|
|
default:
|
|
return nil, models.NewHttpError(msg, http.StatusInternalServerError, err)
|
|
}
|
|
}
|
|
|
|
accountsResponse, err := json.Marshal(accounts)
|
|
if err != nil {
|
|
return nil, models.NewHttpError("error in AccountsHandler", http.StatusInternalServerError, err)
|
|
}
|
|
|
|
return accountsResponse, nil
|
|
}
|
|
|
|
// newAccountService is an account service factory.
|
|
//
|
|
// Stubbable by tests.
|
|
var newAccountsService = func(pluginCtx backend.PluginContext, reqCtxFactory models.RequestContextFactoryFunc, region string) (models.AccountsProvider, error) {
|
|
oamClient, err := reqCtxFactory(pluginCtx, region)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return services.NewAccountsService(oamClient.OAMAPIProvider), nil
|
|
}
|