grafana/pkg/tsdb/cloudwatch/routes/regions.go
Shabeeb Khalid 9dc6cac1f2
CloudWatch: Use context in aws DescribeRegionsWithContext (#76922)
* Use context in aws DescribeRegionsWithContext

In the current way, DescribeRegions is used which doesn't allow
cancelling the request if the context changes. Using
DescribeRegionsWithContext is the preferred way.

* Fix context variable

* Revert GetRegionsWithContext to GetRegions

GetRegions is not an AWS SDK method. Hence, GetRegions should be enough
as the name change is not needed for context implementation.
2023-10-23 16:39:07 +02:00

49 lines
1.6 KiB
Go

package routes
import (
"context"
"encoding/json"
"errors"
"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"
)
const (
defaultRegion = "default"
)
func RegionsHandler(ctx context.Context, pluginCtx backend.PluginContext, reqCtxFactory models.RequestContextFactoryFunc, parameters url.Values) ([]byte, *models.HttpError) {
service, err := newRegionsService(ctx, pluginCtx, reqCtxFactory, defaultRegion)
if err != nil {
if errors.Is(err, models.ErrMissingRegion) {
return nil, models.NewHttpError("Error in Regions Handler when connecting to aws without a default region selection", http.StatusBadRequest, err)
}
return nil, models.NewHttpError("Error in Regions Handler when connecting to aws", http.StatusInternalServerError, err)
}
regions, err := service.GetRegions(ctx)
if err != nil {
return nil, models.NewHttpError("Error in Regions Handler while fetching regions", http.StatusInternalServerError, err)
}
regionsResponse, err := json.Marshal(regions)
if err != nil {
return nil, models.NewHttpError("Error in Regions Handler while parsing regions", http.StatusInternalServerError, err)
}
return regionsResponse, nil
}
var newRegionsService = func(ctx context.Context, pluginCtx backend.PluginContext, reqCtxFactory models.RequestContextFactoryFunc, region string) (models.RegionsAPIProvider, error) {
reqCtx, err := reqCtxFactory(ctx, pluginCtx, region)
if err != nil {
return nil, err
}
return services.NewRegionsService(reqCtx.EC2APIProvider, reqCtx.Logger), nil
}