AWS: Update deprecated aws-sdk functions from env variable versions (#89643)

This commit is contained in:
Isabella Siu
2024-06-26 11:56:39 -04:00
committed by GitHub
parent 40207c53ae
commit 3ac109e759
10 changed files with 54 additions and 33 deletions
+4 -5
View File
@@ -80,7 +80,7 @@ type CloudWatchService struct {
}
type SessionCache interface {
GetSession(c awsds.SessionConfig) (*session.Session, error)
GetSessionWithAuthSettings(c awsds.GetSessionConfig, as awsds.AuthSettings) (*session.Session, error)
}
func newExecutor(im instancemgmt.InstanceManager, logger log.Logger) *cloudWatchExecutor {
@@ -299,7 +299,7 @@ func (ds *DataSource) newSession(region string) (*session.Session, error) {
}
region = ds.Settings.Region
}
sess, err := ds.sessions.GetSession(awsds.SessionConfig{
sess, err := ds.sessions.GetSessionWithAuthSettings(awsds.GetSessionConfig{
// https://github.com/grafana/grafana/issues/46365
// HTTPClient: instance.HTTPClient,
Settings: awsds.AWSDatasourceSettings{
@@ -313,9 +313,8 @@ func (ds *DataSource) newSession(region string) (*session.Session, error) {
AccessKey: ds.Settings.AccessKey,
SecretKey: ds.Settings.SecretKey,
},
UserAgentName: aws.String("Cloudwatch"),
AuthSettings: &ds.Settings.GrafanaSettings,
})
UserAgentName: aws.String("Cloudwatch")},
ds.Settings.GrafanaSettings)
if err != nil {
return nil, err
}
+3 -4
View File
@@ -191,7 +191,7 @@ func Test_CheckHealth(t *testing.T) {
im := datasource.NewInstanceManager(func(ctx context.Context, s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
return DataSource{
Settings: models.CloudWatchSettings{AWSDatasourceSettings: awsds.AWSDatasourceSettings{Region: "us-east-1"}},
sessions: &fakeSessionCache{getSession: func(c awsds.SessionConfig) (*session.Session, error) {
sessions: &fakeSessionCache{getSessionWithAuthSettings: func(c awsds.GetSessionConfig, a awsds.AuthSettings) (*session.Session, error) {
return nil, fmt.Errorf("some sessions error")
}},
}, nil
@@ -229,9 +229,8 @@ func TestNewSession_passes_authSettings(t *testing.T) {
},
GrafanaSettings: expectedSettings,
},
sessions: &fakeSessionCache{getSession: func(c awsds.SessionConfig) (*session.Session, error) {
assert.NotNil(t, c.AuthSettings)
assert.Equal(t, expectedSettings, *c.AuthSettings)
sessions: &fakeSessionCache{getSessionWithAuthSettings: func(c awsds.GetSessionConfig, a awsds.AuthSettings) (*session.Session, error) {
assert.Equal(t, expectedSettings, a)
return &session.Session{
Config: &aws.Config{},
}, nil
+2 -1
View File
@@ -43,7 +43,8 @@ func LoadCloudWatchSettings(ctx context.Context, config backend.DataSourceInstan
instance.LogsTimeout = Duration{30 * time.Minute}
}
instance.GrafanaSettings = *awsds.ReadAuthSettings(ctx)
authSettings, _ := awsds.ReadAuthSettingsFromContext(ctx)
instance.GrafanaSettings = *authSettings
return instance, nil
}
+6 -6
View File
@@ -224,21 +224,21 @@ type mockSessionCache struct {
mock.Mock
}
func (c *mockSessionCache) GetSession(config awsds.SessionConfig) (*session.Session, error) {
func (c *mockSessionCache) GetSessionWithAuthSettings(config awsds.GetSessionConfig, auth awsds.AuthSettings) (*session.Session, error) {
args := c.Called(config)
return args.Get(0).(*session.Session), args.Error(1)
}
type fakeSessionCache struct {
getSession func(c awsds.SessionConfig) (*session.Session, error)
calledRegions []string
getSessionWithAuthSettings func(c awsds.GetSessionConfig, a awsds.AuthSettings) (*session.Session, error)
calledRegions []string
}
func (s *fakeSessionCache) GetSession(c awsds.SessionConfig) (*session.Session, error) {
func (s *fakeSessionCache) GetSessionWithAuthSettings(c awsds.GetSessionConfig, a awsds.AuthSettings) (*session.Session, error) {
s.calledRegions = append(s.calledRegions, c.Settings.Region)
if s.getSession != nil {
return s.getSession(c)
if s.getSessionWithAuthSettings != nil {
return s.getSessionWithAuthSettings(c, a)
}
return &session.Session{
Config: &aws.Config{},
+16 -10
View File
@@ -147,10 +147,12 @@ func Test_executeTimeSeriesQuery_getCWClient_is_called_once_per_region_and_GetMe
return &mockMetricClient
}
t.Run("Queries with the same region should call GetSession with that region 1 time and call GetMetricDataWithContext 1 time", func(t *testing.T) {
t.Run("Queries with the same region should call GetSessionWithAuthSettings with that region 1 time and call GetMetricDataWithContext 1 time", func(t *testing.T) {
mockSessionCache := &mockSessionCache{}
mockSessionCache.On("GetSession", mock.MatchedBy(
func(config awsds.SessionConfig) bool { return config.Settings.Region == "us-east-1" })). // region from queries is asserted here
mockSessionCache.On("GetSessionWithAuthSettings", mock.MatchedBy(
func(config awsds.GetSessionConfig) bool {
return config.Settings.Region == "us-east-1"
})). // region from queries is asserted here
Return(&session.Session{Config: &aws.Config{}}, nil).Once()
im := datasource.NewInstanceManager(func(ctx context.Context, s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
return DataSource{Settings: models.CloudWatchSettings{}, sessions: mockSessionCache}, nil
@@ -198,14 +200,18 @@ func Test_executeTimeSeriesQuery_getCWClient_is_called_once_per_region_and_GetMe
// GetMetricData is asserted to have been called 1 time for the 1 region present in the queries
})
t.Run("3 queries with 2 regions calls GetSession 2 times and calls GetMetricDataWithContext 2 times", func(t *testing.T) {
t.Run("3 queries with 2 regions calls GetSessionWithAuthSettings 2 times and calls GetMetricDataWithContext 2 times", func(t *testing.T) {
sessionCache := &mockSessionCache{}
sessionCache.On("GetSession", mock.MatchedBy(
func(config awsds.SessionConfig) bool { return config.Settings.Region == "us-east-1" })).
Return(&session.Session{Config: &aws.Config{}}, nil, nil).Once()
sessionCache.On("GetSession", mock.MatchedBy(
func(config awsds.SessionConfig) bool { return config.Settings.Region == "us-east-2" })).
Return(&session.Session{Config: &aws.Config{}}, nil, nil).Once()
sessionCache.On("GetSessionWithAuthSettings", mock.MatchedBy(
func(config awsds.GetSessionConfig) bool {
return config.Settings.Region == "us-east-1"
})).
Return(&session.Session{Config: &aws.Config{}}, nil).Once()
sessionCache.On("GetSessionWithAuthSettings", mock.MatchedBy(
func(config awsds.GetSessionConfig) bool {
return config.Settings.Region == "us-east-2"
})).
Return(&session.Session{Config: &aws.Config{}}, nil).Once()
im := datasource.NewInstanceManager(func(ctx context.Context, s backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) {
return DataSource{Settings: models.CloudWatchSettings{}, sessions: sessionCache}, nil