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.
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sort"
|
||||
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/constants"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models"
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models/resources"
|
||||
@@ -31,12 +31,12 @@ func mergeEC2RegionsAndConstantRegions(regions map[string]struct{}, ec2Regions [
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RegionsService) GetRegions() ([]resources.ResourceResponse[resources.Region], error) {
|
||||
func (r *RegionsService) GetRegions(ctx context.Context) ([]resources.ResourceResponse[resources.Region], error) {
|
||||
regions := constants.Regions()
|
||||
|
||||
result := make([]resources.ResourceResponse[resources.Region], 0)
|
||||
|
||||
ec2Regions, err := r.DescribeRegions(&ec2.DescribeRegionsInput{})
|
||||
ec2Regions, err := r.DescribeRegionsWithContext(ctx, &ec2.DescribeRegionsInput{})
|
||||
// we ignore this error and always send default regions
|
||||
// we only fetch incase a user has enabled additional regions
|
||||
// but we still log it in case the user is expecting to fetch regions specific to their account and are unable to
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ec2"
|
||||
@@ -23,8 +24,8 @@ func TestRegions(t *testing.T) {
|
||||
},
|
||||
}
|
||||
ec2Mock := &mocks.EC2Mock{}
|
||||
ec2Mock.On("DescribeRegions").Return(mockRegions, nil)
|
||||
regions, err := NewRegionsService(ec2Mock, testLogger).GetRegions()
|
||||
ec2Mock.On("DescribeRegionsWithContext").Return(mockRegions, nil)
|
||||
regions, err := NewRegionsService(ec2Mock, testLogger).GetRegions(context.Background())
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, regions, resources.ResourceResponse[resources.Region]{
|
||||
Value: resources.Region{
|
||||
@@ -43,8 +44,8 @@ func TestRegions(t *testing.T) {
|
||||
mockRegions := &ec2.DescribeRegionsOutput{
|
||||
Regions: []*ec2.Region{},
|
||||
}
|
||||
ec2Mock.On("DescribeRegions").Return(mockRegions, assert.AnError)
|
||||
regions, err := NewRegionsService(ec2Mock, testLogger).GetRegions()
|
||||
ec2Mock.On("DescribeRegionsWithContext").Return(mockRegions, assert.AnError)
|
||||
regions, err := NewRegionsService(ec2Mock, testLogger).GetRegions(context.Background())
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, regions, resources.ResourceResponse[resources.Region]{
|
||||
Value: resources.Region{
|
||||
|
||||
Reference in New Issue
Block a user