Azure / azure-sdk-for-go

This repository is for active development of the Azure SDK for Go. For consumers of the SDK we recommend visiting our public developer docs at:
https://docs.microsoft.com/azure/developer/go/
MIT License
1.64k stars 842 forks source link

Question: Why the Azure Cost Usage Func does NOT support Pagination? #20851

Closed xqi-aviatrix closed 1 year ago

xqi-aviatrix commented 1 year ago

https://github.com/Azure/azure-sdk-for-go/blob/f252cd9f178d89af5a3b0f595e72c8b1479904c7/sdk/resourcemanager/costmanagement/armcostmanagement/query_client.go#L60

In case, I query the vms' costs within a subscription with the following query and there were thousands of vms in the subscription, without pagination will there be potential problem?

        scope := fmt.Sprint("subscriptions/", subscriptionId)
    now := time.Now()
    start := now.Add(-168 * time.Hour)
    end := now
    metricName := armcostmanagement.ExportTypeActualCost
    res, err := client.Usage(ctx, scope, armcostmanagement.QueryDefinition{
        Type:      to.Ptr(metricName),
        Timeframe: to.Ptr(armcostmanagement.TimeframeTypeCustom),
        TimePeriod: &armcostmanagement.QueryTimePeriod{
            From: &start,
            To:   &end,
        },
        Dataset: &armcostmanagement.QueryDataset{
            Aggregation: map[string]*armcostmanagement.QueryAggregation{
                "totalCost": {
                    Name:     to.Ptr("PreTaxCost"),
                    Function: to.Ptr(armcostmanagement.FunctionTypeSum),
                },
            },
            Granularity: to.Ptr(armcostmanagement.GranularityTypeDaily), //GranularityType("None")
            Grouping: []*armcostmanagement.QueryGrouping{
                {
                    Name: to.Ptr("ResourceId"), //ResourceGroup, SubscriptionId
                    Type: to.Ptr(armcostmanagement.QueryColumnTypeDimension),
                }},
            Filter: &armcostmanagement.QueryFilter{
                Dimensions: &armcostmanagement.QueryComparisonExpression{
                    Name:     to.Ptr("ResourceType"),
                    Operator: to.Ptr(armcostmanagement.QueryOperatorTypeIn),
                    Values: []*string{
                        to.Ptr("microsoft.compute/virtualmachines"),
                    },
                },
            },
        },
    }, nil)
github-actions[bot] commented 1 year ago

Thank you for your feedback. This has been routed to the support team for assistance.

navba-MSFT commented 1 year ago

@xqi-aviatrix Apologies for the late reply. Thanks for reaching out to us and reporting this issue. Most ARM APIs deal with Creating/Updating/Getting/Deleting an azure resource, so a get response is usually in the form of a “value” array of objects that represent the resource(s) information. Example:

image

In the Cost Management query case, we do not do the above operations on a resource, we simply fetch the aggregated cost of a resource based o what the customer specifies (and I am sure, as customers of the API, you already know that).

Hence, our response in unconventional:

image

So due to the above we cannot add the x-ms-pageable field in the REST API specs swagger. And since this is not available in the swagger, the SDK cannot provide this functionality.

So in short, the ONLY workaround here is to invoke the Cost management query API, Get the NextLink marker and enumerate through it until the provided nextLink is null. Samples are here and here. Hope this helps.

github-actions[bot] commented 1 year ago

Hi @xqi-aviatrix. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

navba-MSFT commented 1 year ago

@xqi-aviatrix I wanted to do quick follow-up to check if you had a chance to look at my above comment. Please let us know if you had any updates on this. Awaiting your reply.

navba-MSFT commented 1 year ago

@xqi-aviatrix I hope you had a chance to look at my above comment. We will now proceed with closure of this GitHub issue. If you need any further assistance on this issue in future, please feel free to reopen this thread. We would be happy to help.