Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions providers/azure/internal/recommendations/consumption.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,9 @@ func WithSkipLog(ctx context.Context) (context.Context, *SkipLog) {
return context.WithValue(ctx, skipLogKey{}, l), l
}

func recordSkip(ctx context.Context, service string, err error) {
// RecordSkip logs a dropped recommendation and records it in the context's
// SkipLog, if any.
func RecordSkip(ctx context.Context, service string, err error) {
logging.Errorf("azure %s: skipping recommendation: %v", service, err)
if l, ok := ctx.Value(skipLogKey{}).(*SkipLog); ok {
l.mu.Lock()
Expand Down Expand Up @@ -340,7 +342,7 @@ func (e *PartialResultError) Error() string {
func ExtractConsumptionOrSkip(ctx context.Context, service string, rec armconsumption.ReservationRecommendationClassification) *ExtractedFields {
f, err := ExtractConsumption(rec)
if err != nil {
recordSkip(ctx, service, err)
RecordSkip(ctx, service, err)
return nil
}
return f
Expand All @@ -358,7 +360,7 @@ func AppendConsumptionVariants(ctx context.Context, service string, recs []commo
if IsCollectionFailure(err) {
return recs, fmt.Errorf("azure %s: %w", service, err)
}
recordSkip(ctx, service, err)
RecordSkip(ctx, service, err)
return recs, nil
}
return append(recs, variants...), nil
Expand Down
16 changes: 10 additions & 6 deletions providers/azure/pagination_cap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,21 @@ func TestGetRegions_PageCap(t *testing.T) {
}

func advisorPage(n int) armadvisor.RecommendationsClientListResponse {
field := "Microsoft.Compute/virtualMachines"
id := "/subscriptions/123/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/vm" + string(rune('a'+n))
field := "Microsoft.Subscriptions/subscriptions"
id := "/subscriptions/123/providers/Microsoft.Advisor/recommendations/rec" + string(rune('a'+n))
savings, currency := "1000.00", "USD"
return armadvisor.RecommendationsClientListResponse{ResourceRecommendationBaseListResult: armadvisor.ResourceRecommendationBaseListResult{
Value: []*armadvisor.ResourceRecommendationBase{{
ID: &id,
Properties: &armadvisor.RecommendationProperties{
ImpactedField: &field,
ExtendedProperties: map[string]*string{
"annualSavingsAmount": &savings,
"savingsCurrency": &currency,
"annualSavingsAmount": &savings,
"savingsCurrency": &currency,
"reservedResourceType": strPtr("VirtualMachines"),
"term": strPtr("P1Y"),
"sku": strPtr("Standard_D2s_v3"),
"qty": strPtr("1"),
},
},
}},
Expand All @@ -173,10 +177,10 @@ func TestCollectAdvisorRecommendations_PageCap(t *testing.T) {
adapter := &RecommendationsClientAdapter{subscriptionID: "test-subscription"}
params := common.RecommendationParams{}

t.Run("endless pager stops at the cap keeping partial results", func(t *testing.T) {
t.Run("endless pager stops at the cap with a truncation error and the partial results", func(t *testing.T) {
pager := &fakePager[armadvisor.RecommendationsClientListResponse]{limit: 10000, page: advisorPage}
recs, err := adapter.collectAdvisorRecommendations(context.Background(), params, pager)
require.NoError(t, err)
require.ErrorIs(t, err, errAdvisorTruncated)
assert.Equal(t, maxAdvisorPages, pager.calls)
assert.NotEmpty(t, recs)
})
Expand Down
Loading