Query - Usage
Query the usage data for scope defined.
In this article
POST https://management.azure.com/{scope}/providers/Microsoft.CostManagement/query?api-version=2022-10-01
URI Parameters
Name
In
Required
Type
Description
scope
path
True
string
The scope associated with query and export operations. This includes '/subscriptions/{subscriptionId}/' for subscription scope, '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}' for resourceGroup scope, '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}' for Billing Account scope and '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/departments/{departmentId}' for Department scope, '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/enrollmentAccounts/{enrollmentAccountId}' for EnrollmentAccount scope, '/providers/Microsoft.Management/managementGroups/{managementGroupId} for Management Group scope, '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/billingProfiles/{billingProfileId}' for billingProfile scope, '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/billingProfiles/{billingProfileId}/invoiceSections/{invoiceSectionId}' for invoiceSection scope, and '/providers/Microsoft.Billing/billingAccounts/{billingAccountId}/customers/{customerId}' specific for partners.
api-version
query
True
string
The API version to use for this operation.
Request Body
Name
Required
Type
Description
dataset
True
QueryDataset
Has definition for data in this query.
timeframe
True
TimeframeType
The time frame for pulling data for the query. If custom, then a specific time period must be provided.
type
True
ExportType
The type of the query.
timePeriod
QueryTimePeriod
Has time period for pulling data for the query.
Responses
Name
Type
Description
200 OK
QueryResult
OK. The request has succeeded.
204 No Content
No Content. Resource is not available.
Other Status Codes
ErrorResponse
Error response describing why the operation failed.
Security
azure_auth
Azure Active Directory OAuth2 Flow.
Type:
oauth2
Flow:
implicit
Authorization URL:
https://login.microsoftonline.com/common/oauth2/authorize
Scopes
Name
Description
user_impersonation
impersonate your user account
Examples
BillingAccountQuery-Legacy
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/70664866/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": [
"East US",
"West Europe"
]
}
},
{
"tags": {
"name": "Environment",
"operator": "In",
"values": [
"UAT",
"Prod"
]
}
}
]
},
{
"dimensions": {
"name": "ResourceGroup",
"operator": "In",
"values": [
"API"
]
}
}
]
}
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryOperatorType;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/BillingAccountQuery.json
*/
/**
* Sample code: BillingAccountQuery-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void billingAccountQueryLegacy(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/70664866",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.MONTH_TO_DATE)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withFilter(
new QueryFilter()
.withAnd(
Arrays
.asList(
new QueryFilter()
.withOr(
Arrays
.asList(
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceLocation")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new QueryFilter()
.withTags(
new QueryComparisonExpression()
.withName("Environment")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceGroup")
.withOperator(QueryOperatorType.IN)
.withValues(Arrays.asList("API"))))))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python billing_account_query_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/70664866",
parameters={
"dataset": {
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": ["East US", "West Europe"],
}
},
{"tags": {"name": "Environment", "operator": "In", "values": ["UAT", "Prod"]}},
]
},
{"dimensions": {"name": "ResourceGroup", "operator": "In", "values": ["API"]}},
]
},
"granularity": "Daily",
},
"timeframe": "MonthToDate",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/BillingAccountQuery.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/70664866/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/70664866/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
20180331,
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
20180331,
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
20180331,
"USD"
],
[
0.16677720329728665,
"gs-stms-dev",
20180331,
"USD"
]
]
}
}
BillingAccountQuery-MCA
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": [
"East US",
"West Europe"
]
}
},
{
"tags": {
"name": "Environment",
"operator": "In",
"values": [
"UAT",
"Prod"
]
}
}
]
},
{
"dimensions": {
"name": "ResourceGroup",
"operator": "In",
"values": [
"API"
]
}
}
]
}
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryOperatorType;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCABillingAccountQuery.json
*/
/**
* Sample code: BillingAccountQuery-MCA.
*
* @param manager Entry point to CostManagementManager.
*/
public static void billingAccountQueryMCA(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.MONTH_TO_DATE)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withFilter(
new QueryFilter()
.withAnd(
Arrays
.asList(
new QueryFilter()
.withOr(
Arrays
.asList(
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceLocation")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new QueryFilter()
.withTags(
new QueryComparisonExpression()
.withName("Environment")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceGroup")
.withOperator(QueryOperatorType.IN)
.withValues(Arrays.asList("API"))))))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python billing_account_query_mca.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789",
parameters={
"dataset": {
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": ["East US", "West Europe"],
}
},
{"tags": {"name": "Environment", "operator": "In", "values": ["UAT", "Prod"]}},
]
},
{"dimensions": {"name": "ResourceGroup", "operator": "In", "values": ["API"]}},
]
},
"granularity": "Daily",
},
"timeframe": "MonthToDate",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCABillingAccountQuery.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/12345:6789/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
20180331,
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
20180331,
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
20180331,
"USD"
],
[
0.16677720329728665,
"gs-stms-dev",
20180331,
"USD"
]
]
}
}
BillingAccountQueryGrouping-Legacy
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/70664866/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/BillingAccountQueryGrouping.json
*/
/**
* Sample code: BillingAccountQueryGrouping-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void billingAccountQueryGroupingLegacy(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/70664866",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.THE_LAST_MONTH)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.fromString("None"))
.withAggregation(
mapOf(
"totalCost",
new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
.withGrouping(
Arrays
.asList(
new QueryGrouping()
.withType(QueryColumnType.DIMENSION)
.withName("ResourceGroup")))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python billing_account_query_grouping_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/70664866",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "PreTaxCost"}},
"granularity": "None",
"grouping": [{"name": "ResourceGroup", "type": "Dimension"}],
},
"timeframe": "TheLastMonth",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/BillingAccountQueryGrouping.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/70664866/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/70664866/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
"USD"
]
]
}
}
BillingAccountQueryGrouping-MCA
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCABillingAccountQueryGrouping.json
*/
/**
* Sample code: BillingAccountQueryGrouping-MCA.
*
* @param manager Entry point to CostManagementManager.
*/
public static void billingAccountQueryGroupingMCA(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.THE_LAST_MONTH)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.fromString("None"))
.withAggregation(
mapOf(
"totalCost",
new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
.withGrouping(
Arrays
.asList(
new QueryGrouping()
.withType(QueryColumnType.DIMENSION)
.withName("ResourceGroup")))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python billing_account_query_grouping_mca.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "PreTaxCost"}},
"granularity": "None",
"grouping": [{"name": "ResourceGroup", "type": "Dimension"}],
},
"timeframe": "TheLastMonth",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCABillingAccountQueryGrouping.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/12345:6789/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
"USD"
]
]
}
}
BillingProfileQuery-MCA
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": [
"East US",
"West Europe"
]
}
},
{
"tags": {
"name": "Environment",
"operator": "In",
"values": [
"UAT",
"Prod"
]
}
}
]
},
{
"dimensions": {
"name": "ResourceGroup",
"operator": "In",
"values": [
"API"
]
}
}
]
}
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryOperatorType;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCABillingProfileQuery.json
*/
/**
* Sample code: BillingProfileQuery-MCA.
*
* @param manager Entry point to CostManagementManager.
*/
public static void billingProfileQueryMCA(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.MONTH_TO_DATE)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withFilter(
new QueryFilter()
.withAnd(
Arrays
.asList(
new QueryFilter()
.withOr(
Arrays
.asList(
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceLocation")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new QueryFilter()
.withTags(
new QueryComparisonExpression()
.withName("Environment")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceGroup")
.withOperator(QueryOperatorType.IN)
.withValues(Arrays.asList("API"))))))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python billing_profile_query_mca.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579",
parameters={
"dataset": {
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": ["East US", "West Europe"],
}
},
{"tags": {"name": "Environment", "operator": "In", "values": ["UAT", "Prod"]}},
]
},
{"dimensions": {"name": "ResourceGroup", "operator": "In", "values": ["API"]}},
]
},
"granularity": "Daily",
},
"timeframe": "MonthToDate",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCABillingProfileQuery.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
20180331,
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
20180331,
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
20180331,
"USD"
],
[
0.16677720329728665,
"gs-stms-dev",
20180331,
"USD"
]
]
}
}
BillingProfileQueryGrouping-MCA
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCABillingProfileQueryGrouping.json
*/
/**
* Sample code: BillingProfileQueryGrouping-MCA.
*
* @param manager Entry point to CostManagementManager.
*/
public static void billingProfileQueryGroupingMCA(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.THE_LAST_MONTH)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.fromString("None"))
.withAggregation(
mapOf(
"totalCost",
new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
.withGrouping(
Arrays
.asList(
new QueryGrouping()
.withType(QueryColumnType.DIMENSION)
.withName("ResourceGroup")))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python billing_profile_query_grouping_mca.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "PreTaxCost"}},
"granularity": "None",
"grouping": [{"name": "ResourceGroup", "type": "Dimension"}],
},
"timeframe": "TheLastMonth",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCABillingProfileQueryGrouping.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
"USD"
]
]
}
}
CustomerQuery-MCA
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/customers/5678/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": [
"East US",
"West Europe"
]
}
},
{
"tags": {
"name": "Environment",
"operator": "In",
"values": [
"UAT",
"Prod"
]
}
}
]
},
{
"dimensions": {
"name": "ResourceGroup",
"operator": "In",
"values": [
"API"
]
}
}
]
}
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryOperatorType;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCACustomerQuery.json
*/
/**
* Sample code: CustomerQuery-MCA.
*
* @param manager Entry point to CostManagementManager.
*/
public static void customerQueryMCA(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789/customers/5678",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.MONTH_TO_DATE)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withFilter(
new QueryFilter()
.withAnd(
Arrays
.asList(
new QueryFilter()
.withOr(
Arrays
.asList(
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceLocation")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new QueryFilter()
.withTags(
new QueryComparisonExpression()
.withName("Environment")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceGroup")
.withOperator(QueryOperatorType.IN)
.withValues(Arrays.asList("API"))))))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python customer_query_mca.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789/customers/5678",
parameters={
"dataset": {
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": ["East US", "West Europe"],
}
},
{"tags": {"name": "Environment", "operator": "In", "values": ["UAT", "Prod"]}},
]
},
{"dimensions": {"name": "ResourceGroup", "operator": "In", "values": ["API"]}},
]
},
"granularity": "Daily",
},
"timeframe": "MonthToDate",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCACustomerQuery.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/12345:6789/customers/5678/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/customers/5678/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
20180331,
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
20180331,
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
20180331,
"USD"
],
[
0.16677720329728665,
"gs-stms-dev",
20180331,
"USD"
]
]
}
}
CustomerQueryGrouping-MCA
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/customers/5678/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCACustomerQueryGrouping.json
*/
/**
* Sample code: CustomerQueryGrouping-MCA.
*
* @param manager Entry point to CostManagementManager.
*/
public static void customerQueryGroupingMCA(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789/customers/5678",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.THE_LAST_MONTH)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.fromString("None"))
.withAggregation(
mapOf(
"totalCost",
new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
.withGrouping(
Arrays
.asList(
new QueryGrouping()
.withType(QueryColumnType.DIMENSION)
.withName("ResourceGroup")))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python customer_query_grouping_mca.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789/customers/5678",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "PreTaxCost"}},
"granularity": "None",
"grouping": [{"name": "ResourceGroup", "type": "Dimension"}],
},
"timeframe": "TheLastMonth",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCACustomerQueryGrouping.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/12345:6789/customers/5678/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/customers/5678/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
"USD"
]
]
}
}
DepartmentQuery-Legacy
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/100/departments/123/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": [
"East US",
"West Europe"
]
}
},
{
"tags": {
"name": "Environment",
"operator": "In",
"values": [
"UAT",
"Prod"
]
}
}
]
},
{
"dimensions": {
"name": "ResourceGroup",
"operator": "In",
"values": [
"API"
]
}
}
]
}
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryOperatorType;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/DepartmentQuery.json
*/
/**
* Sample code: DepartmentQuery-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void departmentQueryLegacy(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/100/departments/123",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.MONTH_TO_DATE)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withFilter(
new QueryFilter()
.withAnd(
Arrays
.asList(
new QueryFilter()
.withOr(
Arrays
.asList(
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceLocation")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new QueryFilter()
.withTags(
new QueryComparisonExpression()
.withName("Environment")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceGroup")
.withOperator(QueryOperatorType.IN)
.withValues(Arrays.asList("API"))))))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python department_query_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/100/departments/123",
parameters={
"dataset": {
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": ["East US", "West Europe"],
}
},
{"tags": {"name": "Environment", "operator": "In", "values": ["UAT", "Prod"]}},
]
},
{"dimensions": {"name": "ResourceGroup", "operator": "In", "values": ["API"]}},
]
},
"granularity": "Daily",
},
"timeframe": "MonthToDate",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/DepartmentQuery.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/70664866/departments/123/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/70664866/departments/123/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
20180331,
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
20180331,
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
20180331,
"USD"
],
[
0.16677720329728665,
"gs-stms-dev",
20180331,
"USD"
]
]
}
}
DepartmentQueryGrouping-Legacy
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/100/departments/123/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/DepartmentQueryGrouping.json
*/
/**
* Sample code: DepartmentQueryGrouping-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void departmentQueryGroupingLegacy(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/100/departments/123",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.THE_LAST_MONTH)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.fromString("None"))
.withAggregation(
mapOf(
"totalCost",
new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
.withGrouping(
Arrays
.asList(
new QueryGrouping()
.withType(QueryColumnType.DIMENSION)
.withName("ResourceGroup")))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python department_query_grouping_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/100/departments/123",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "PreTaxCost"}},
"granularity": "None",
"grouping": [{"name": "ResourceGroup", "type": "Dimension"}],
},
"timeframe": "TheLastMonth",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/DepartmentQueryGrouping.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/70664866/departments/123/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/70664866/departments/123/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
"USD"
]
]
}
}
EnrollmentAccountQuery-Legacy
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/100/enrollmentAccounts/456/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": [
"East US",
"West Europe"
]
}
},
{
"tags": {
"name": "Environment",
"operator": "In",
"values": [
"UAT",
"Prod"
]
}
}
]
},
{
"dimensions": {
"name": "ResourceGroup",
"operator": "In",
"values": [
"API"
]
}
}
]
}
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryOperatorType;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/EnrollmentAccountQuery.json
*/
/**
* Sample code: EnrollmentAccountQuery-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void enrollmentAccountQueryLegacy(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/100/enrollmentAccounts/456",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.MONTH_TO_DATE)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withFilter(
new QueryFilter()
.withAnd(
Arrays
.asList(
new QueryFilter()
.withOr(
Arrays
.asList(
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceLocation")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new QueryFilter()
.withTags(
new QueryComparisonExpression()
.withName("Environment")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceGroup")
.withOperator(QueryOperatorType.IN)
.withValues(Arrays.asList("API"))))))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python enrollment_account_query_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/100/enrollmentAccounts/456",
parameters={
"dataset": {
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": ["East US", "West Europe"],
}
},
{"tags": {"name": "Environment", "operator": "In", "values": ["UAT", "Prod"]}},
]
},
{"dimensions": {"name": "ResourceGroup", "operator": "In", "values": ["API"]}},
]
},
"granularity": "Daily",
},
"timeframe": "MonthToDate",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/EnrollmentAccountQuery.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/70664866/enrollmentAccounts/456/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/70664866/enrollmentAccounts/456/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
20180331,
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
20180331,
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
20180331,
"USD"
],
[
0.16677720329728665,
"gs-stms-dev",
20180331,
"USD"
]
]
}
}
EnrollmentAccountQueryGrouping-Legacy
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/100/enrollmentAccounts/456/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/EnrollmentAccountQueryGrouping.json
*/
/**
* Sample code: EnrollmentAccountQueryGrouping-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void enrollmentAccountQueryGroupingLegacy(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/100/enrollmentAccounts/456",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.THE_LAST_MONTH)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withAggregation(
mapOf(
"totalCost",
new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
.withGrouping(
Arrays
.asList(
new QueryGrouping()
.withType(QueryColumnType.DIMENSION)
.withName("ResourceGroup")))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python enrollment_account_query_grouping_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/100/enrollmentAccounts/456",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "PreTaxCost"}},
"granularity": "Daily",
"grouping": [{"name": "ResourceGroup", "type": "Dimension"}],
},
"timeframe": "TheLastMonth",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/EnrollmentAccountQueryGrouping.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/70664866/enrollmentAccounts/456/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/70664866/enrollmentAccounts/456/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
20180331,
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
20180331,
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
20180331,
"USD"
]
]
}
}
InvoiceSectionQuery-MCA
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": [
"East US",
"West Europe"
]
}
},
{
"tags": {
"name": "Environment",
"operator": "In",
"values": [
"UAT",
"Prod"
]
}
}
]
},
{
"dimensions": {
"name": "ResourceGroup",
"operator": "In",
"values": [
"API"
]
}
}
]
}
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryOperatorType;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCAInvoiceSectionQuery.json
*/
/**
* Sample code: InvoiceSectionQuery-MCA.
*
* @param manager Entry point to CostManagementManager.
*/
public static void invoiceSectionQueryMCA(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.MONTH_TO_DATE)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withFilter(
new QueryFilter()
.withAnd(
Arrays
.asList(
new QueryFilter()
.withOr(
Arrays
.asList(
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceLocation")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new QueryFilter()
.withTags(
new QueryComparisonExpression()
.withName("Environment")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceGroup")
.withOperator(QueryOperatorType.IN)
.withValues(Arrays.asList("API"))))))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python invoice_section_query_mca.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876",
parameters={
"dataset": {
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": ["East US", "West Europe"],
}
},
{"tags": {"name": "Environment", "operator": "In", "values": ["UAT", "Prod"]}},
]
},
{"dimensions": {"name": "ResourceGroup", "operator": "In", "values": ["API"]}},
]
},
"granularity": "Daily",
},
"timeframe": "MonthToDate",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCAInvoiceSectionQuery.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
20180331,
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
20180331,
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
20180331,
"USD"
],
[
0.16677720329728665,
"gs-stms-dev",
20180331,
"USD"
]
]
}
}
InvoiceSectionQueryGrouping-MCA
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCAInvoiceSectionQueryGrouping.json
*/
/**
* Sample code: InvoiceSectionQueryGrouping-MCA.
*
* @param manager Entry point to CostManagementManager.
*/
public static void invoiceSectionQueryGroupingMCA(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.THE_LAST_MONTH)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.fromString("None"))
.withAggregation(
mapOf(
"totalCost",
new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
.withGrouping(
Arrays
.asList(
new QueryGrouping()
.withType(QueryColumnType.DIMENSION)
.withName("ResourceGroup")))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python invoice_section_query_grouping_mca.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "PreTaxCost"}},
"granularity": "None",
"grouping": [{"name": "ResourceGroup", "type": "Dimension"}],
},
"timeframe": "TheLastMonth",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/MCAInvoiceSectionQueryGrouping.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
"USD"
]
]
}
}
ManagementGroupQuery-Legacy
Sample Request
POST https://management.azure.com/providers/Microsoft.Management/managementGroups/MyMgId/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": [
"East US",
"West Europe"
]
}
},
{
"tags": {
"name": "Environment",
"operator": "In",
"values": [
"UAT",
"Prod"
]
}
}
]
},
{
"dimensions": {
"name": "ResourceGroup",
"operator": "In",
"values": [
"API"
]
}
}
]
}
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryOperatorType;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/ManagementGroupQuery.json
*/
/**
* Sample code: ManagementGroupQuery-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void managementGroupQueryLegacy(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Management/managementGroups/MyMgId",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.MONTH_TO_DATE)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withFilter(
new QueryFilter()
.withAnd(
Arrays
.asList(
new QueryFilter()
.withOr(
Arrays
.asList(
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceLocation")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new QueryFilter()
.withTags(
new QueryComparisonExpression()
.withName("Environment")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceGroup")
.withOperator(QueryOperatorType.IN)
.withValues(Arrays.asList("API"))))))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python management_group_query_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Management/managementGroups/MyMgId",
parameters={
"dataset": {
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": ["East US", "West Europe"],
}
},
{"tags": {"name": "Environment", "operator": "In", "values": ["UAT", "Prod"]}},
]
},
{"dimensions": {"name": "ResourceGroup", "operator": "In", "values": ["API"]}},
]
},
"granularity": "Daily",
},
"timeframe": "MonthToDate",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/ManagementGroupQuery.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Management/managementGroups/MyMgId/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Management/managementGroups/MyMgId/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
19.545363672276512,
"JapanUnifia-Trial",
20180331,
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
20180331,
"USD"
],
[
20.35941656262545,
"VSTSHOL-1595322048000",
20180331,
"USD"
],
[
0.16677720329728665,
"gs-stms-dev",
20180331,
"USD"
]
]
}
}
ManagementGroupQueryGrouping-Legacy
Sample Request
POST https://management.azure.com/providers/Microsoft.Management/managementGroups/MyMgId/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/ManagementGroupQueryGrouping.json
*/
/**
* Sample code: ManagementGroupQueryGrouping-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void managementGroupQueryGroupingLegacy(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"providers/Microsoft.Management/managementGroups/MyMgId",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.THE_LAST_MONTH)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.fromString("None"))
.withAggregation(
mapOf(
"totalCost",
new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
.withGrouping(
Arrays
.asList(
new QueryGrouping()
.withType(QueryColumnType.DIMENSION)
.withName("ResourceGroup")))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python management_group_query_grouping_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="providers/Microsoft.Management/managementGroups/MyMgId",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "PreTaxCost"}},
"granularity": "None",
"grouping": [{"name": "ResourceGroup", "type": "Dimension"}],
},
"timeframe": "TheLastMonth",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/ManagementGroupQueryGrouping.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "providers/Microsoft.Management/managementGroups/MyMgId/providers/Microsoft.CostManagement/Query/ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"name": "ad67fd91-c131-4bda-9ba9-7187ecb1cebd",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": "https://management.azure.com/providers/Microsoft.Management/managementGroups/MyMgId/providers/Microsoft.CostManagement/Query?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
20.35941656262545,
"VSTSHOL-1595322048000",
20180331,
"USD"
],
[
173.41979241290323,
"RVIIOT-TRIAL",
20180331,
"USD"
],
[
19.545363672276512,
"JapanUnifia-Trial",
20180331,
"USD"
]
]
}
}
ResourceGroupQuery-Legacy
Sample Request
POST https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/ScreenSharingTest-peer/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": [
"East US",
"West Europe"
]
}
},
{
"tags": {
"name": "Environment",
"operator": "In",
"values": [
"UAT",
"Prod"
]
}
}
]
},
{
"dimensions": {
"name": "ResourceGroup",
"operator": "In",
"values": [
"API"
]
}
}
]
}
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryOperatorType;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/ResourceGroupQuery.json
*/
/**
* Sample code: ResourceGroupQuery-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void resourceGroupQueryLegacy(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/ScreenSharingTest-peer",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.MONTH_TO_DATE)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withFilter(
new QueryFilter()
.withAnd(
Arrays
.asList(
new QueryFilter()
.withOr(
Arrays
.asList(
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceLocation")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new QueryFilter()
.withTags(
new QueryComparisonExpression()
.withName("Environment")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceGroup")
.withOperator(QueryOperatorType.IN)
.withValues(Arrays.asList("API"))))))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python resource_group_query_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/ScreenSharingTest-peer",
parameters={
"dataset": {
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": ["East US", "West Europe"],
}
},
{"tags": {"name": "Environment", "operator": "In", "values": ["UAT", "Prod"]}},
]
},
{"dimensions": {"name": "ResourceGroup", "operator": "In", "values": ["API"]}},
]
},
"granularity": "Daily",
},
"timeframe": "MonthToDate",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/ResourceGroupQuery.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "subscriptions/55312978-ba1b-415c-9304-c4b9c43c0481/resourcegroups/ScreenSharingTest-peer/providers/Microsoft.CostManagement/Query/9af9459d-441d-4055-9ed0-83d4c4a363fb",
"name": "9af9459d-441d-4055-9ed0-83d4c4a363fb",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": null,
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
2.10333307059661,
"ScreenSharingTest-peer",
20180417,
"USD"
],
[
20.10333307059661,
"ScreenSharingTest-peer",
20180418,
"USD"
]
]
}
}
ResourceGroupQueryGrouping-Legacy
Sample Request
POST https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/ScreenSharingTest-peer/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceType"
}
]
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/ResourceGroupQueryGrouping.json
*/
/**
* Sample code: ResourceGroupQueryGrouping-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void resourceGroupQueryGroupingLegacy(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/ScreenSharingTest-peer",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.THE_LAST_MONTH)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withAggregation(
mapOf(
"totalCost",
new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
.withGrouping(
Arrays
.asList(
new QueryGrouping()
.withType(QueryColumnType.DIMENSION)
.withName("ResourceType")))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python resource_group_query_grouping_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/ScreenSharingTest-peer",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "PreTaxCost"}},
"granularity": "Daily",
"grouping": [{"name": "ResourceType", "type": "Dimension"}],
},
"timeframe": "TheLastMonth",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/ResourceGroupQueryGrouping.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/ScreenSharingTest-peer/providers/Microsoft.CostManagement/Query/9af9459d-441d-4055-9ed0-83d4c4a363fb",
"name": "9af9459d-441d-4055-9ed0-83d4c4a363fb",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": null,
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceType",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
2.10333307059661,
"Microsoft.SqlServer",
20180417,
"USD"
],
[
20.10333307059661,
"Microsoft.Compute",
20180418,
"USD"
]
]
}
}
SubscriptionQuery-Legacy
Sample Request
POST https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "MonthToDate",
"dataset": {
"granularity": "Daily",
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": [
"East US",
"West Europe"
]
}
},
{
"tags": {
"name": "Environment",
"operator": "In",
"values": [
"UAT",
"Prod"
]
}
}
]
},
{
"dimensions": {
"name": "ResourceGroup",
"operator": "In",
"values": [
"API"
]
}
}
]
}
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryFilter;
import com.azure.resourcemanager.costmanagement.models.QueryOperatorType;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/SubscriptionQuery.json
*/
/**
* Sample code: SubscriptionQuery-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void subscriptionQueryLegacy(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"subscriptions/00000000-0000-0000-0000-000000000000",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.MONTH_TO_DATE)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.DAILY)
.withFilter(
new QueryFilter()
.withAnd(
Arrays
.asList(
new QueryFilter()
.withOr(
Arrays
.asList(
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceLocation")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new QueryFilter()
.withTags(
new QueryComparisonExpression()
.withName("Environment")
.withOperator(QueryOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new QueryFilter()
.withDimensions(
new QueryComparisonExpression()
.withName("ResourceGroup")
.withOperator(QueryOperatorType.IN)
.withValues(Arrays.asList("API"))))))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python subscription_query_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="subscriptions/00000000-0000-0000-0000-000000000000",
parameters={
"dataset": {
"filter": {
"and": [
{
"or": [
{
"dimensions": {
"name": "ResourceLocation",
"operator": "In",
"values": ["East US", "West Europe"],
}
},
{"tags": {"name": "Environment", "operator": "In", "values": ["UAT", "Prod"]}},
]
},
{"dimensions": {"name": "ResourceGroup", "operator": "In", "values": ["API"]}},
]
},
"granularity": "Daily",
},
"timeframe": "MonthToDate",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/SubscriptionQuery.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/Query/00000000-0000-0000-0000-000000000000",
"name": "55312978-ba1b-415c-9304-cfd9c43c0481",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": null,
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
2.10333307059661,
"ScreenSharingTest-peer",
20180331,
"USD"
],
[
218.68795741935486,
"Ict_StratAndPlan_GoldSprova_Prod",
20180331,
"USD"
],
[
0.14384913581657052,
"ssbciotelement01",
20180401,
"USD"
],
[
0.009865586851323632,
"ict_stratandplan_goldsprova_prod",
20180429,
"USD"
]
]
}
}
SubscriptionQueryGrouping-Legacy
Sample Request
POST https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/query?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
import com.azure.resourcemanager.costmanagement.models.ExportType;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import com.azure.resourcemanager.costmanagement.models.QueryAggregation;
import com.azure.resourcemanager.costmanagement.models.QueryColumnType;
import com.azure.resourcemanager.costmanagement.models.QueryDataset;
import com.azure.resourcemanager.costmanagement.models.QueryDefinition;
import com.azure.resourcemanager.costmanagement.models.QueryGrouping;
import com.azure.resourcemanager.costmanagement.models.TimeframeType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Query Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/SubscriptionQueryGrouping.json
*/
/**
* Sample code: SubscriptionQueryGrouping-Legacy.
*
* @param manager Entry point to CostManagementManager.
*/
public static void subscriptionQueryGroupingLegacy(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.queries()
.usageWithResponse(
"subscriptions/00000000-0000-0000-0000-000000000000",
new QueryDefinition()
.withType(ExportType.USAGE)
.withTimeframe(TimeframeType.THE_LAST_MONTH)
.withDataset(
new QueryDataset()
.withGranularity(GranularityType.fromString("None"))
.withAggregation(
mapOf(
"totalCost",
new QueryAggregation().withName("PreTaxCost").withFunction(FunctionType.SUM)))
.withGrouping(
Arrays
.asList(
new QueryGrouping()
.withType(QueryColumnType.DIMENSION)
.withName("ResourceGroup")))),
com.azure.core.util.Context.NONE);
}
@SuppressWarnings("unchecked")
private static <T> Map<String, T> mapOf(Object... inputs) {
Map<String, T> map = new HashMap<>();
for (int i = 0; i < inputs.length; i += 2) {
String key = (String) inputs[i];
T value = (T) inputs[i + 1];
map.put(key, value);
}
return map;
}
}
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
from azure.identity import DefaultAzureCredential
from azure.mgmt.costmanagement import CostManagementClient
"""
# PREREQUISITES
pip install azure-identity
pip install azure-mgmt-costmanagement
# USAGE
python subscription_query_grouping_legacy.py
Before run the sample, please set the values of the client ID, tenant ID and client secret
of the AAD application as environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID,
AZURE_CLIENT_SECRET. For more info about how to get the value, please see:
https://docs.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal
"""
def main():
client = CostManagementClient(
credential=DefaultAzureCredential(),
)
response = client.query.usage(
scope="subscriptions/00000000-0000-0000-0000-000000000000",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "PreTaxCost"}},
"granularity": "None",
"grouping": [{"name": "ResourceGroup", "type": "Dimension"}],
},
"timeframe": "TheLastMonth",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/SubscriptionQueryGrouping.json
if __name__ == "__main__":
main()
To use the Azure SDK library in your project, see this documentation . To provide feedback on this code sample, open a GitHub issue
Sample Response
{
"id": "subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/Query/00000000-0000-0000-0000-000000000000",
"name": "55312978-ba1b-415c-9304-cfd9c43c0481",
"type": "microsoft.costmanagement/Query",
"properties": {
"nextLink": null,
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "ResourceGroup",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
0.009865586851323632,
"Ict_StratAndPlan_GoldSprova_Prod_0",
"USD"
],
[
218.68795741935486,
"Ict_StratAndPlan_GoldSprova_Prod_1",
"USD"
],
[
2.10333307059661,
"ScreenSharingTest-peer1",
"USD"
],
[
0.14384913581657052,
"Ssbciotelement01",
"USD"
]
]
}
}
Definitions
ErrorDetails
The details of the error.
ErrorResponse
Error response indicates that the service is not able to process the incoming request. The reason is provided in the error message.
Some Error responses:
429 TooManyRequests - Request is throttled. Retry after waiting for the time specified in the "x-ms-ratelimit-microsoft.consumption-retry-after" header.
503 ServiceUnavailable - Service is temporarily unavailable. Retry after waiting for the time specified in the "Retry-After" header.
ExportType
The type of the query.
FunctionType
The name of the aggregation function to use.
GranularityType
The granularity of rows in the forecast.
QueryAggregation
The aggregation expression to be used in the query.
QueryColumn
QueryColumn properties
QueryColumnType
The type of the column in the export.
QueryComparisonExpression
The comparison expression to be used in the query.
QueryDataset
The definition of data present in the query.
QueryDatasetConfiguration
The configuration of dataset in the query.
QueryDefinition
The definition of a query.
QueryFilter
The filter expression to be used in the export.
QueryGrouping
The group by expression to be used in the query.
QueryOperatorType
The operator to use for comparison.
QueryResult
Result of query. It contains all columns listed under groupings and aggregation.
QueryTimePeriod
The start and end date for pulling data for the query.
TimeframeType
The time frame for pulling data for the query. If custom, then a specific time period must be provided.
ErrorDetails
The details of the error.
Name
Type
Description
code
string
Error code.
message
string
Error message indicating why the operation failed.
ErrorResponse
Error response indicates that the service is not able to process the incoming request. The reason is provided in the error message.
Some Error responses:
429 TooManyRequests - Request is throttled. Retry after waiting for the time specified in the "x-ms-ratelimit-microsoft.consumption-retry-after" header.
503 ServiceUnavailable - Service is temporarily unavailable. Retry after waiting for the time specified in the "Retry-After" header.
Name
Type
Description
error
ErrorDetails
The details of the error.
ExportType
The type of the query.
Name
Type
Description
ActualCost
string
AmortizedCost
string
Usage
string
FunctionType
The name of the aggregation function to use.
Name
Type
Description
Sum
string
GranularityType
The granularity of rows in the forecast.
Name
Type
Description
Daily
string
QueryAggregation
The aggregation expression to be used in the query.
Name
Type
Description
function
FunctionType
The name of the aggregation function to use.
name
string
The name of the column to aggregate.
QueryColumn
QueryColumn properties
Name
Type
Description
name
string
The name of column.
type
string
The type of column.
QueryColumnType
The type of the column in the export.
Name
Type
Description
Dimension
string
The dimension of cost data.
TagKey
string
The tag associated with the cost data.
QueryComparisonExpression
The comparison expression to be used in the query.
Name
Type
Description
name
string
The name of the column to use in comparison.
operator
QueryOperatorType
The operator to use for comparison.
values
string[]
Array of values to use for comparison
QueryDataset
The definition of data present in the query.
Name
Type
Description
aggregation
<string,
QueryAggregation >
Dictionary of aggregation expression to use in the query. The key of each item in the dictionary is the alias for the aggregated column. Query can have up to 2 aggregation clauses.
configuration
QueryDatasetConfiguration
Has configuration information for the data in the export. The configuration will be ignored if aggregation and grouping are provided.
filter
QueryFilter
The filter expression to use in the query. Please reference our Query API REST documentation for how to properly format the filter.
granularity
GranularityType
The granularity of rows in the query.
grouping
QueryGrouping []
Array of group by expression to use in the query. Query can have up to 2 group by clauses.
QueryDatasetConfiguration
The configuration of dataset in the query.
Name
Type
Description
columns
string[]
Array of column names to be included in the query. Any valid query column name is allowed. If not provided, then query includes all columns.
QueryDefinition
The definition of a query.
Name
Type
Description
dataset
QueryDataset
Has definition for data in this query.
timePeriod
QueryTimePeriod
Has time period for pulling data for the query.
timeframe
TimeframeType
The time frame for pulling data for the query. If custom, then a specific time period must be provided.
type
ExportType
The type of the query.
QueryFilter
The filter expression to be used in the export.
QueryGrouping
The group by expression to be used in the query.
Name
Type
Description
name
string
The name of the column to group.
type
QueryColumnType
Has type of the column to group.
QueryOperatorType
The operator to use for comparison.
Name
Type
Description
In
string
QueryResult
Result of query. It contains all columns listed under groupings and aggregation.
Name
Type
Description
eTag
string
ETag of the resource.
id
string
Resource Id.
location
string
Location of the resource.
name
string
Resource name.
properties.columns
QueryColumn []
Array of columns
properties.nextLink
string
The link (url) to the next page of results.
properties.rows
array[]
Array of rows
sku
string
SKU of the resource.
tags
object
Resource tags.
type
string
Resource type.
QueryTimePeriod
The start and end date for pulling data for the query.
Name
Type
Description
from
string
The start date to pull data from.
to
string
The end date to pull data to.
TimeframeType
The time frame for pulling data for the query. If custom, then a specific time period must be provided.
Name
Type
Description
BillingMonthToDate
string
Custom
string
MonthToDate
string
TheLastBillingMonth
string
TheLastMonth
string
WeekToDate
string