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: How can I only query the VMs' cost within a subscription? #20741

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/constants.go#L507

Can I only query the VMs's cost within a subscription through the armcostmanagement pkg? Or can the API support something like below?

{
    Dimensions: &armcostmanagement.QueryComparisonExpression{
        Name:     to.Ptr("ResourceId"),
        Operator: to.Ptr(armcostmanagement.QueryOperatorTypeContains),
        Values: []*string{
            to.Ptr("microsoft.compute/virtualmachines")},
    },
},
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. We are looking into this issue and we will provide an update.

navba-MSFT commented 1 year ago

@xqi-aviatrix Could you please check if the below snippet helps ?

Dimensions: &armcostmanagement.QueryComparisonExpression{
                            Name:     to.Ptr("ResourceType"),
                            Operator: to.Ptr(armcostmanagement.QueryOperatorTypeContains),
                            Values: []*string{
                                to.Ptr("Microsoft.Compute/virtualMachines"),
                        },
                    },
                },
            },
        },

Reference.

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.

xqi-aviatrix commented 1 year ago

Hi @navba-MSFT, Thanks for the response.

I get the following compilation error Screenshot 2023-05-08 at 9 47 14 AM

But this one works for

                         Filter: &armcostmanagement.QueryFilter{
                Dimensions: &armcostmanagement.QueryComparisonExpression{
                    Name:     to.Ptr("ResourceType"),
                    Operator: to.Ptr(armcostmanagement.QueryOperatorTypeIn),
                    Values: []*string{
                        to.Ptr("Microsoft.Compute/virtualMachines"),
                    },
                },
            },
navba-MSFT commented 1 year ago

@xqi-aviatrix Here is the complete sample code. Please check if this helps.

package main

import (
    "context"
    "fmt"

    "github.com/Azure/azure-sdk-for-go/services/costmanagement/mgmt/2020-06-01/costmanagement"
    "github.com/Azure/go-autorest/autorest/azure/auth"
    "github.com/Azure/go-autorest/autorest/to"
)

func main() {

    subscriptionID := "XXXXXXXXXXXXXXXX"
    clientID := "XXXXXXX"
    clientSecret := "XXXXXXXXXXXXXXXXXXX"
    tenantID := "XXXXXXXXXXXXXXX"

    scope := "/subscriptions/XXXXXXXXXXXXXXX/resourceGroups/RGName"

    // Define AAD service principal credentials
    spConfig := auth.NewClientCredentialsConfig(clientID, clientSecret, tenantID)
    authorizer, err := spConfig.Authorizer()

    if err != nil {
        // handle error
    }

    costQueryClient := costmanagement.NewQueryClient(subscriptionID)
    costQueryClient.Authorizer = authorizer
    queryAggregation := make(map[string]*costmanagement.QueryAggregation)
    queryAggregation["totalCost"] = &costmanagement.QueryAggregation{
        Name:     to.StringPtr("PreTaxCost"),
        Function: to.StringPtr("Sum"),
    }
    queryAggregation["totalCostUSD"] = &costmanagement.QueryAggregation{
        Name:     to.StringPtr("PreTaxCostUSD"),
        Function: to.StringPtr("Sum"),
    }

    queryDefinition := costmanagement.QueryDefinition{
        Type:      costmanagement.ExportTypeActualCost,
        Timeframe: costmanagement.TimeframeTypeMonthToDate,
        Dataset: &costmanagement.QueryDataset{
            Granularity: costmanagement.Daily,
            Aggregation: queryAggregation,
            Filter: &costmanagement.QueryFilter{
                And: &[]costmanagement.QueryFilter{
                    {
                        Dimension: &costmanagement.QueryComparisonExpression{
                            Name:     to.StringPtr("ResourceType"),
                            Operator: to.StringPtr("In"),
                            Values:   to.StringSlicePtr([]string{"Microsoft.Compute/virtualMachines"}),
                        },
                    },
                    {
                        Dimension: &costmanagement.QueryComparisonExpression{
                            Name:     to.StringPtr("ResourceId"),
                            Operator: to.StringPtr("In"),
                            Values:   to.StringSlicePtr([]string{"/subscriptions/XXX/resourceGroups/RGName/providers/Microsoft.Compute/virtualMachines/VMName"}),
                        },
                    },
                },
            },
        },
    }

    // result, err := costQueryClient.Usage() Usage() Usage() context.Background(),  , queryDefinition)
    result, err := costQueryClient.Usage(context.Background(), scope, queryDefinition)

    // print the result
    if err != nil {
        // handle error
    }

    fmt.Printf("result: %v\n", result)

}
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.

xqi-aviatrix commented 1 year ago

Thank you for answering the question @navba-MSFT I am going to close this issue