Skip to content

Commit fd000af

Browse files
committed
MGDAPI-5690 CRO Redis snapshots tagging - review
1 parent 668a6c7 commit fd000af

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

pkg/providers/aws/credentials.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ var (
9999
"rds:RemoveTagsFromResource",
100100
"rds:ApplyPendingMaintenanceAction",
101101
//"sts:GetCallerIdentity",
102-
//"iam:CreateServiceLinkedRole", // Only needed for CloudWatch alarms (not used)
103-
//"cloudwatch:ListMetrics", // Only needed for metric discovery (not used)
104-
"cloudwatch:GetMetricData", // ACTUALLY USED for metrics collection
102+
//"iam:CreateServiceLinkedRole", // Required for CloudWatch alarms, but we are not using it.
103+
"cloudwatch:ListMetrics",
104+
"cloudwatch:GetMetricData",
105105
},
106106
Resource: "*",
107107
},

pkg/providers/aws/provider_redis.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -458,14 +458,14 @@ func (p *RedisProvider) TagElasticacheNode(ctx context.Context, elasticacheClien
458458
// Get default tags
459459
cacheTags, clusterID, err := p.getDefaultElasticacheTags(ctx, r)
460460
if err != nil {
461-
msg := "Failed to build default tags"
461+
msg := "failed to build default tags"
462462
return croType.StatusMessage(msg), errorUtil.Wrapf(err, msg)
463463
}
464464

465465
// Filter out already applied tags
466-
filteredTags, err := filterAlreadyAppliedTags(ctx, elasticacheClient, arn, cacheTags)
466+
filteredTags, err := filterAlreadyAppliedTags(ctx, elasticacheClient, arn, *cache.CacheClusterId, cacheTags)
467467
if err != nil {
468-
msg := "Failed to filter already applied tags"
468+
msg := "failed to filter already applied tags"
469469
return croType.StatusMessage(msg), errorUtil.Wrapf(err, msg)
470470
}
471471

@@ -479,9 +479,9 @@ func (p *RedisProvider) TagElasticacheNode(ctx context.Context, elasticacheClien
479479
msg := "failed to add tags to aws elasticache :"
480480
return croType.StatusMessage(msg), err
481481
}
482-
logrus.Infof("Successfully applied %d new/updated tags to cluster %s", len(filteredTags), arn)
482+
logrus.Infof("successfully applied %d new/updated tags to cluster %s", len(filteredTags), *cache.CacheClusterId)
483483
} else {
484-
logrus.Infof("Redis cluster %s: no tag changes required", arn)
484+
logrus.Infof("redis cluster %s: no tag changes required", *cache.CacheClusterId)
485485
}
486486

487487
// if snapshots exist add tags to them
@@ -497,7 +497,7 @@ func (p *RedisProvider) TagElasticacheNode(ctx context.Context, elasticacheClien
497497
resources.ResetMetric(metricName)
498498
for _, snapshot := range snapshotList.Snapshots {
499499
snapshotArn := fmt.Sprintf("arn:aws:elasticache:%s:%s:snapshot:%s", region, *id.Account, *snapshot.SnapshotName)
500-
logrus.Infof("Adding operator tags to snapshot : %s", *snapshot.SnapshotName)
500+
logrus.Infof("adding operator tags to snapshot : %s", *snapshot.SnapshotName)
501501
snapshotInput := &elasticache.AddTagsToResourceInput{
502502
ResourceName: aws.String(snapshotArn),
503503
Tags: cacheTags,
@@ -1260,6 +1260,7 @@ func (p *RedisProvider) applySpecifiedSecurityUpdates(ctx context.Context, elast
12601260
}
12611261
return nil
12621262
}
1263+
12631264
func (p *RedisProvider) applyServiceUpdate(ctx context.Context, elasticacheClient ElastiCacheAPI, replicationgroupid, serviceupdateName *string) error {
12641265
logger := p.Logger.WithField("action", "applyServiceUpdate")
12651266

@@ -1292,14 +1293,14 @@ func validServiceUpdateStates(status string) bool {
12921293
}
12931294

12941295
// filterAlreadyAppliedTags removes tags from `desired` that already exist (same key and value) on the resource.
1295-
func filterAlreadyAppliedTags(ctx context.Context, client ElastiCacheAPI, resourceARN string, desired []elasticachetypes.Tag) ([]elasticachetypes.Tag, error) {
1296+
func filterAlreadyAppliedTags(ctx context.Context, client ElastiCacheAPI, resourceARN string, cacheClusterID string, desired []elasticachetypes.Tag) ([]elasticachetypes.Tag, error) {
12961297
// List current tags on the resource
12971298
resp, err := client.ListTagsForResource(ctx, &elasticache.ListTagsForResourceInput{
12981299
ResourceName: aws.String(resourceARN),
12991300
})
13001301
if err != nil {
13011302
// If we can't list tags (permission issue), fall back to applying all tags
1302-
logrus.Warnf("Could not list existing tags for %s: %v. Will attempt to apply all tags (may result in unnecessary API calls for already-applied tags).", resourceARN, err)
1303+
logrus.Warnf("could not list existing tags for %s: %v. Will attempt to apply all tags (may result in unnecessary API calls for already-applied tags).", cacheClusterID, err)
13031304
return desired, nil
13041305
}
13051306

pkg/providers/aws/provider_redis_test.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,10 +3583,11 @@ func TestRedisProvider_getElasticacheConfig(t *testing.T) {
35833583
// Test_filterAlreadyAppliedTags was created with the help of Cursor AI IDE.
35843584
func Test_filterAlreadyAppliedTags(t *testing.T) {
35853585
type args struct {
3586-
ctx context.Context
3587-
client ElastiCacheAPI
3588-
resourceARN string
3589-
desired []elasticachetypes.Tag
3586+
ctx context.Context
3587+
client ElastiCacheAPI
3588+
resourceARN string
3589+
cacheClusterID string
3590+
desired []elasticachetypes.Tag
35903591
}
35913592
tests := []struct {
35923593
name string
@@ -3606,7 +3607,8 @@ func Test_filterAlreadyAppliedTags(t *testing.T) {
36063607
)
36073608
return mockClient
36083609
}(),
3609-
resourceARN: "arn:aws:elasticache:us-east-1:123456789012:cluster:test-cluster",
3610+
resourceARN: "arn:aws:elasticache:us-east-1:123456789012:cluster:test-cluster",
3611+
cacheClusterID: "test-cluster-id",
36103612
desired: []elasticachetypes.Tag{
36113613
{Key: aws.String("Environment"), Value: aws.String("test")},
36123614
},
@@ -3630,7 +3632,8 @@ func Test_filterAlreadyAppliedTags(t *testing.T) {
36303632
)
36313633
return mockClient
36323634
}(),
3633-
resourceARN: "arn:aws:elasticache:us-east-1:123456789012:cluster:test-cluster",
3635+
resourceARN: "arn:aws:elasticache:us-east-1:123456789012:cluster:test-cluster",
3636+
cacheClusterID: "test-cluster-id",
36343637
desired: []elasticachetypes.Tag{
36353638
{Key: aws.String("Environment"), Value: aws.String("test")},
36363639
{Key: aws.String("Team"), Value: aws.String("backend")},
@@ -3659,7 +3662,8 @@ func Test_filterAlreadyAppliedTags(t *testing.T) {
36593662
)
36603663
return mockClient
36613664
}(),
3662-
resourceARN: "arn:aws:elasticache:us-east-1:123456789012:cluster:test-cluster",
3665+
resourceARN: "arn:aws:elasticache:us-east-1:123456789012:cluster:test-cluster",
3666+
cacheClusterID: "test-cluster-id",
36633667
desired: []elasticachetypes.Tag{
36643668
{Key: aws.String("Environment"), Value: aws.String("test")}, // Same value - should be filtered
36653669
{Key: aws.String("Team"), Value: aws.String("backend")}, // New tag - should be kept
@@ -3689,7 +3693,8 @@ func Test_filterAlreadyAppliedTags(t *testing.T) {
36893693
)
36903694
return mockClient
36913695
}(),
3692-
resourceARN: "arn:aws:elasticache:us-east-1:123456789012:cluster:test-cluster",
3696+
resourceARN: "arn:aws:elasticache:us-east-1:123456789012:cluster:test-cluster",
3697+
cacheClusterID: "test-cluster-id",
36933698
desired: []elasticachetypes.Tag{
36943699
{Key: aws.String("Environment"), Value: aws.String("test")},
36953700
{Key: aws.String("Team"), Value: aws.String("backend")},
@@ -3717,7 +3722,8 @@ func Test_filterAlreadyAppliedTags(t *testing.T) {
37173722
)
37183723
return mockClient
37193724
}(),
3720-
resourceARN: "arn:aws:elasticache:us-east-1:123456789012:cluster:test-cluster",
3725+
resourceARN: "arn:aws:elasticache:us-east-1:123456789012:cluster:test-cluster",
3726+
cacheClusterID: "test-cluster-id",
37213727
desired: []elasticachetypes.Tag{
37223728
{Key: aws.String("Environment"), Value: aws.String("test")}, // Different value - should be kept
37233729
},
@@ -3730,7 +3736,7 @@ func Test_filterAlreadyAppliedTags(t *testing.T) {
37303736
}
37313737
for _, tt := range tests {
37323738
t.Run(tt.name, func(t *testing.T) {
3733-
got, err := filterAlreadyAppliedTags(tt.args.ctx, tt.args.client, tt.args.resourceARN, tt.args.desired)
3739+
got, err := filterAlreadyAppliedTags(tt.args.ctx, tt.args.client, tt.args.resourceARN, tt.args.cacheClusterID, tt.args.desired)
37343740
if (err != nil) != tt.wantErr {
37353741
t.Errorf("filterAlreadyAppliedTags() error = %v, wantErr %v", err, tt.wantErr)
37363742
return

0 commit comments

Comments
 (0)