From bfd11c9a10a8064ec6b81297c2b6ebedbf129494 Mon Sep 17 00:00:00 2001 From: Cristian Magherusan-Stanciu Date: Thu, 16 Jul 2026 21:36:45 +0300 Subject: [PATCH] refactor(aws): share resolveTag helper across service clients (closes #1390) Move the 4-way duplicate resolveTag into purchasecfg.ResolveTag -- all four service packages (elasticache, memorydb, opensearch, redshift) already import purchasecfg so no new import is required. No behavior change; existing tests unchanged and green. --- providers/aws/internal/purchasecfg/config.go | 10 ++++++++++ providers/aws/services/elasticache/client.go | 13 +------------ providers/aws/services/memorydb/client.go | 13 +------------ providers/aws/services/opensearch/client.go | 13 +------------ providers/aws/services/redshift/client.go | 13 +------------ 5 files changed, 14 insertions(+), 48 deletions(-) diff --git a/providers/aws/internal/purchasecfg/config.go b/providers/aws/internal/purchasecfg/config.go index 3b623c726..19fb5773c 100644 --- a/providers/aws/internal/purchasecfg/config.go +++ b/providers/aws/internal/purchasecfg/config.go @@ -32,6 +32,16 @@ const ( HTTPTimeout = 15 * time.Second ) +// ResolveTag returns execID when non-empty, or the sentinel string "no-exec" +// for log correlation when a call occurs outside of a purchase flow (e.g. +// ValidateOffering, GetOfferingDetails). +func ResolveTag(execID string) string { + if execID == "" { + return "no-exec" + } + return execID +} + // NewConfig returns a copy of base with purchase-path-appropriate retry and // HTTP timeout settings applied. The original base config is not modified. // diff --git a/providers/aws/services/elasticache/client.go b/providers/aws/services/elasticache/client.go index dc135c201..c6d8cd16d 100644 --- a/providers/aws/services/elasticache/client.go +++ b/providers/aws/services/elasticache/client.go @@ -290,7 +290,7 @@ func (c *Client) paginateElastiCacheOfferings(ctx context.Context, rec common.Re if err != nil { return "", err } - tag := resolveTag(execID) + tag := purchasecfg.ResolveTag(execID) t0 := time.Now() log.Printf("purchase[%s]: ElastiCache findOfferingID starting (nodeType=%s engine=%s duration=%s payment=%s)", tag, rec.ResourceType, details.Engine, duration, offeringType) @@ -356,17 +356,6 @@ func scanElastiCacheOfferingPage(offerings []types.ReservedCacheNodesOffering, r return "", nil } -// resolveTag returns execID when non-empty, or a sentinel "no-exec" string for -// log correlation when called outside of a purchase flow (e.g. ValidateOffering, -// GetOfferingDetails). Extracted from paginateElastiCacheOfferings to keep -// cyclomatic complexity within the pre-commit gocyclo limit (issue #1388). -func resolveTag(execID string) string { - if execID == "" { - return "no-exec" - } - return execID -} - // ValidateOffering checks if an offering exists without purchasing func (c *Client) ValidateOffering(ctx context.Context, rec common.Recommendation) error { _, err := c.findOfferingID(ctx, rec, "") diff --git a/providers/aws/services/memorydb/client.go b/providers/aws/services/memorydb/client.go index 7f1dabd13..567d76030 100644 --- a/providers/aws/services/memorydb/client.go +++ b/providers/aws/services/memorydb/client.go @@ -292,7 +292,7 @@ func (c *Client) findOfferingID(ctx context.Context, rec common.Recommendation, if err != nil { return "", err } - tag := resolveTag(execID) + tag := purchasecfg.ResolveTag(execID) t0 := time.Now() log.Printf("purchase[%s]: MemoryDB findOfferingID starting (nodeType=%s term=%s payment=%s)", tag, rec.ResourceType, rec.Term, rec.PaymentOption) @@ -377,17 +377,6 @@ func isLastMemoryDBPage(nextToken *string) bool { return nextToken == nil || aws.ToString(nextToken) == "" } -// resolveTag returns execID when non-empty, or a sentinel "no-exec" string for -// log correlation when called outside of a purchase flow (e.g. ValidateOffering, -// GetOfferingDetails). Extracted from findOfferingID to keep cyclomatic complexity -// within the pre-commit gocyclo limit (issue #1388). -func resolveTag(execID string) string { - if execID == "" { - return "no-exec" - } - return execID -} - // getDurationStringForAPI converts the term string to a duration value accepted // by DescribeReservedNodesOfferings (seconds as a string or "1yr"/"3yr"). // The MemoryDB API accepts both numeric-seconds strings and year strings. diff --git a/providers/aws/services/opensearch/client.go b/providers/aws/services/opensearch/client.go index e96c01b20..f72cb948f 100644 --- a/providers/aws/services/opensearch/client.go +++ b/providers/aws/services/opensearch/client.go @@ -371,7 +371,7 @@ func (c *Client) findOfferingID(ctx context.Context, rec common.Recommendation, if err != nil { return "", err } - tag := resolveTag(execID) + tag := purchasecfg.ResolveTag(execID) t0 := time.Now() log.Printf("purchase[%s]: OpenSearch findOfferingID starting (instanceType=%s term=%s payment=%s)", tag, rec.ResourceType, rec.Term, rec.PaymentOption) @@ -605,14 +605,3 @@ func getTermMonthsFromDuration(duration int32) int { } return 12 } - -// resolveTag returns execID when non-empty, or a sentinel "no-exec" string for -// log correlation when called outside of a purchase flow (e.g. ValidateOffering, -// GetOfferingDetails). Extracted from findOfferingID to keep cyclomatic complexity -// within the pre-commit gocyclo limit (issue #1388). -func resolveTag(execID string) string { - if execID == "" { - return "no-exec" - } - return execID -} diff --git a/providers/aws/services/redshift/client.go b/providers/aws/services/redshift/client.go index 492e48bf6..a9d51dcc7 100644 --- a/providers/aws/services/redshift/client.go +++ b/providers/aws/services/redshift/client.go @@ -416,7 +416,7 @@ func (c *Client) findOfferingID(ctx context.Context, rec common.Recommendation, if err != nil { return "", err } - tag := resolveTag(execID) + tag := purchasecfg.ResolveTag(execID) t0 := time.Now() log.Printf("purchase[%s]: Redshift findOfferingID starting (nodeType=%s term=%s payment=%s)", tag, rec.ResourceType, rec.Term, rec.PaymentOption) @@ -688,14 +688,3 @@ func getTermMonthsFromDuration(duration int32) int { } return 12 } - -// resolveTag returns execID when non-empty, or a sentinel "no-exec" string for -// log correlation when called outside of a purchase flow (e.g. ValidateOffering, -// GetOfferingDetails). Extracted from findOfferingID to keep cyclomatic complexity -// within the pre-commit gocyclo limit (issue #1388). -func resolveTag(execID string) string { - if execID == "" { - return "no-exec" - } - return execID -}