Closed xqi-aviatrix closed 1 year ago
Thank you for your feedback. This has been routed to the support team for assistance.
@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.
@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"),
},
},
},
},
},
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.
Hi @navba-MSFT, Thanks for the response.
I get the following compilation error
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"),
},
},
},
@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)
}
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.
Thank you for answering the question @navba-MSFT I am going to close this issue
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?