Skip to content

Commit 3424606

Browse files
committed
feat: Paginate over prices sheets
1 parent 3ad5f9f commit 3424606

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

plugins/source/azure/resources/services/consumption/subscription_price_sheets.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package consumption
22

33
import (
44
"context"
5+
"net/url"
56

67
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
78
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/consumption/armconsumption"
@@ -26,10 +27,37 @@ func fetchSubscriptionPriceSheets(ctx context.Context, meta schema.ClientMeta, p
2627
if err != nil {
2728
return err
2829
}
29-
resp, err := svc.Get(ctx, &armconsumption.PriceSheetClientGetOptions{Expand: to.Ptr("properties/meterDetails")})
30+
opts := &armconsumption.PriceSheetClientGetOptions{Expand: to.Ptr("properties/meterDetails")}
31+
resp, err := svc.Get(ctx, opts)
3032
if err != nil {
3133
return err
3234
}
35+
36+
// This is a workaround to get all price sheets
37+
// Somehow related to https://github.com/Azure/azure-sdk-for-go/issues/4142
38+
allPricesSheets := resp.PriceSheetResult.Properties.Pricesheets
39+
nextLink := resp.PriceSheetResult.Properties.NextLink
40+
for nextLink != nil {
41+
parsedNextLink, err := url.Parse(*nextLink)
42+
if err != nil {
43+
cl.Logger().Warn().Err(err).Msgf("failed to parse next link: %q", *nextLink)
44+
break
45+
}
46+
token := parsedNextLink.Query().Get("skiptoken")
47+
if token == "" {
48+
cl.Logger().Warn().Msgf("failed to get skiptoken from next link: %q", *nextLink)
49+
break
50+
}
51+
opts.Skiptoken = to.Ptr(token)
52+
paginatedResponse, err := svc.Get(ctx, opts)
53+
if err != nil {
54+
cl.Logger().Warn().Err(err).Msgf("failed to get paginated response for next link: %q", *nextLink)
55+
break
56+
}
57+
allPricesSheets = append(allPricesSheets, paginatedResponse.PriceSheetResult.Properties.Pricesheets...)
58+
}
59+
60+
resp.PriceSheetResult.Properties.Pricesheets = allPricesSheets
3361
res <- resp.PriceSheetResult
3462
return nil
3563
}

plugins/source/azure/resources/services/consumption/subscription_price_sheets_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func createSubscriptionPriceSheets(router *mux.Router) error {
1717
if err := faker.FakeObject(&resp); err != nil {
1818
return err
1919
}
20+
resp.PriceSheetResult.Properties.NextLink = nil
2021

2122
router.HandleFunc("/subscriptions/{subscriptionId}/providers/Microsoft.Consumption/pricesheets/default", func(w http.ResponseWriter, r *http.Request) {
2223
b, err := json.Marshal(&resp)

0 commit comments

Comments
 (0)