Forecast - Usage
Lists the forecast charges for scope defined.
In this article
POST https://management.azure.com/{scope}/providers/Microsoft.CostManagement/forecast?api-version=2022-10-01
With optional parameters:
POST https://management.azure.com/{scope}/providers/Microsoft.CostManagement/forecast?$filter={$filter}&api-version=2022-10-01
URI Parameters
Name
In
Required
Type
Description
scope
path
True
string
The scope associated with forecast 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.
$filter
query
string
May be used to filter forecasts by properties/usageDate (Utc time), properties/chargeType or properties/grain. The filter supports 'eq', 'lt', 'gt', 'le', 'ge', and 'and'. It does not currently support 'ne', 'or', or 'not'.
Request Body
Name
Required
Type
Description
dataset
True
ForecastDataset
Has definition for data in this forecast.
timeframe
True
ForecastTimeframe
The time frame for pulling data for the forecast. If custom, then a specific time period must be provided.
type
True
ForecastType
The type of the forecast.
includeActualCost
boolean
A boolean determining if actualCost will be included.
includeFreshPartialCost
boolean
A boolean determining if FreshPartialCost will be included.
timePeriod
ForecastTimePeriod
Has time period for pulling data for the forecast.
Responses
Name
Type
Description
200 OK
ForecastResult
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
BillingAccountForecast
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/providers/Microsoft.CostManagement/forecast?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "Custom",
"timePeriod": {
"from": "2022-08-01T00:00:00+00:00",
"to": "2022-08-31T23:59:59+00:00"
},
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": {
"name": "Cost",
"function": "Sum"
}
},
"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"
]
}
}
]
}
},
"includeActualCost": false,
"includeFreshPartialCost": false
}
import com.azure.resourcemanager.costmanagement.models.ForecastAggregation;
import com.azure.resourcemanager.costmanagement.models.ForecastComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.ForecastDataset;
import com.azure.resourcemanager.costmanagement.models.ForecastDefinition;
import com.azure.resourcemanager.costmanagement.models.ForecastFilter;
import com.azure.resourcemanager.costmanagement.models.ForecastOperatorType;
import com.azure.resourcemanager.costmanagement.models.ForecastTimePeriod;
import com.azure.resourcemanager.costmanagement.models.ForecastTimeframe;
import com.azure.resourcemanager.costmanagement.models.ForecastType;
import com.azure.resourcemanager.costmanagement.models.FunctionName;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Forecast Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/BillingAccountForecast.json
*/
/**
* Sample code: BillingAccountForecast.
*
* @param manager Entry point to CostManagementManager.
*/
public static void billingAccountForecast(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.forecasts()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789",
new ForecastDefinition()
.withType(ForecastType.USAGE)
.withTimeframe(ForecastTimeframe.CUSTOM)
.withTimePeriod(
new ForecastTimePeriod()
.withFrom(OffsetDateTime.parse("2022-08-01T00:00:00+00:00"))
.withTo(OffsetDateTime.parse("2022-08-31T23:59:59+00:00")))
.withDataset(
new ForecastDataset()
.withGranularity(GranularityType.DAILY)
.withAggregation(
mapOf(
"totalCost",
new ForecastAggregation()
.withName(FunctionName.COST)
.withFunction(FunctionType.SUM)))
.withFilter(
new ForecastFilter()
.withAnd(
Arrays
.asList(
new ForecastFilter()
.withOr(
Arrays
.asList(
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceLocation")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new ForecastFilter()
.withTags(
new ForecastComparisonExpression()
.withName("Environment")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceGroup")
.withOperator(ForecastOperatorType.IN)
.withValues(Arrays.asList("API")))))))
.withIncludeActualCost(false)
.withIncludeFreshPartialCost(false),
null,
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_forecast.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.forecast.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "Cost"}},
"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",
},
"includeActualCost": False,
"includeFreshPartialCost": False,
"timePeriod": {"from": "2022-08-01T00:00:00+00:00", "to": "2022-08-31T23:59:59+00:00"},
"timeframe": "Custom",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/BillingAccountForecast.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/Forecast?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "CostStatus",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
2.10333307059661,
20180331,
"Forecast",
"USD"
],
[
218.68795741935486,
20180331,
"Forecast",
"USD"
],
[
0.14384913581657052,
20180401,
"Forecast",
"USD"
],
[
0.009865586851323632,
20180429,
"Forecast",
"USD"
]
]
}
}
BillingProfileForecast
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/providers/Microsoft.CostManagement/forecast?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "Custom",
"timePeriod": {
"from": "2022-08-01T00:00:00+00:00",
"to": "2022-08-31T23:59:59+00:00"
},
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": {
"name": "Cost",
"function": "Sum"
}
},
"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"
]
}
}
]
}
},
"includeActualCost": false,
"includeFreshPartialCost": false
}
import com.azure.resourcemanager.costmanagement.models.ForecastAggregation;
import com.azure.resourcemanager.costmanagement.models.ForecastComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.ForecastDataset;
import com.azure.resourcemanager.costmanagement.models.ForecastDefinition;
import com.azure.resourcemanager.costmanagement.models.ForecastFilter;
import com.azure.resourcemanager.costmanagement.models.ForecastOperatorType;
import com.azure.resourcemanager.costmanagement.models.ForecastTimePeriod;
import com.azure.resourcemanager.costmanagement.models.ForecastTimeframe;
import com.azure.resourcemanager.costmanagement.models.ForecastType;
import com.azure.resourcemanager.costmanagement.models.FunctionName;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Forecast Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/BillingProfileForecast.json
*/
/**
* Sample code: BillingProfileForecast.
*
* @param manager Entry point to CostManagementManager.
*/
public static void billingProfileForecast(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.forecasts()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579",
new ForecastDefinition()
.withType(ForecastType.USAGE)
.withTimeframe(ForecastTimeframe.CUSTOM)
.withTimePeriod(
new ForecastTimePeriod()
.withFrom(OffsetDateTime.parse("2022-08-01T00:00:00+00:00"))
.withTo(OffsetDateTime.parse("2022-08-31T23:59:59+00:00")))
.withDataset(
new ForecastDataset()
.withGranularity(GranularityType.DAILY)
.withAggregation(
mapOf(
"totalCost",
new ForecastAggregation()
.withName(FunctionName.COST)
.withFunction(FunctionType.SUM)))
.withFilter(
new ForecastFilter()
.withAnd(
Arrays
.asList(
new ForecastFilter()
.withOr(
Arrays
.asList(
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceLocation")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new ForecastFilter()
.withTags(
new ForecastComparisonExpression()
.withName("Environment")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceGroup")
.withOperator(ForecastOperatorType.IN)
.withValues(Arrays.asList("API")))))))
.withIncludeActualCost(false)
.withIncludeFreshPartialCost(false),
null,
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_forecast.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.forecast.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "Cost"}},
"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",
},
"includeActualCost": False,
"includeFreshPartialCost": False,
"timePeriod": {"from": "2022-08-01T00:00:00+00:00", "to": "2022-08-31T23:59:59+00:00"},
"timeframe": "Custom",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/BillingProfileForecast.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/Forecast?api-version=2019-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "CostStatus",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
2.10333307059661,
20180331,
"Forecast",
"USD"
],
[
218.68795741935486,
20180331,
"Forecast",
"USD"
],
[
0.14384913581657052,
20180401,
"Forecast",
"USD"
],
[
0.009865586851323632,
20180429,
"Forecast",
"USD"
]
]
}
}
DepartmentForecast
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/departments/123/providers/Microsoft.CostManagement/forecast?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "Custom",
"timePeriod": {
"from": "2022-08-01T00:00:00+00:00",
"to": "2022-08-31T23:59:59+00:00"
},
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": {
"name": "Cost",
"function": "Sum"
}
},
"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"
]
}
}
]
}
},
"includeActualCost": false,
"includeFreshPartialCost": false
}
import com.azure.resourcemanager.costmanagement.models.ForecastAggregation;
import com.azure.resourcemanager.costmanagement.models.ForecastComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.ForecastDataset;
import com.azure.resourcemanager.costmanagement.models.ForecastDefinition;
import com.azure.resourcemanager.costmanagement.models.ForecastFilter;
import com.azure.resourcemanager.costmanagement.models.ForecastOperatorType;
import com.azure.resourcemanager.costmanagement.models.ForecastTimePeriod;
import com.azure.resourcemanager.costmanagement.models.ForecastTimeframe;
import com.azure.resourcemanager.costmanagement.models.ForecastType;
import com.azure.resourcemanager.costmanagement.models.FunctionName;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Forecast Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/DepartmentForecast.json
*/
/**
* Sample code: DepartmentForecast.
*
* @param manager Entry point to CostManagementManager.
*/
public static void departmentForecast(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.forecasts()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789/departments/123",
new ForecastDefinition()
.withType(ForecastType.USAGE)
.withTimeframe(ForecastTimeframe.CUSTOM)
.withTimePeriod(
new ForecastTimePeriod()
.withFrom(OffsetDateTime.parse("2022-08-01T00:00:00+00:00"))
.withTo(OffsetDateTime.parse("2022-08-31T23:59:59+00:00")))
.withDataset(
new ForecastDataset()
.withGranularity(GranularityType.DAILY)
.withAggregation(
mapOf(
"totalCost",
new ForecastAggregation()
.withName(FunctionName.COST)
.withFunction(FunctionType.SUM)))
.withFilter(
new ForecastFilter()
.withAnd(
Arrays
.asList(
new ForecastFilter()
.withOr(
Arrays
.asList(
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceLocation")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new ForecastFilter()
.withTags(
new ForecastComparisonExpression()
.withName("Environment")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceGroup")
.withOperator(ForecastOperatorType.IN)
.withValues(Arrays.asList("API")))))))
.withIncludeActualCost(false)
.withIncludeFreshPartialCost(false),
null,
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_forecast.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.forecast.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789/departments/123",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "Cost"}},
"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",
},
"includeActualCost": False,
"includeFreshPartialCost": False,
"timePeriod": {"from": "2022-08-01T00:00:00+00:00", "to": "2022-08-31T23:59:59+00:00"},
"timeframe": "Custom",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/DepartmentForecast.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/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/12345:6789/departments/123/providers/Microsoft.CostManagement/Forecast?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "CostStatus",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
2.10333307059661,
20180331,
"Forecast",
"USD"
],
[
218.68795741935486,
20180331,
"Forecast",
"USD"
],
[
0.14384913581657052,
20180401,
"Forecast",
"USD"
],
[
0.009865586851323632,
20180429,
"Forecast",
"USD"
]
]
}
}
EnrollmentAccountForecast
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/enrollmentAccounts/456/providers/Microsoft.CostManagement/forecast?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "Custom",
"timePeriod": {
"from": "2022-08-01T00:00:00+00:00",
"to": "2022-08-31T23:59:59+00:00"
},
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": {
"name": "Cost",
"function": "Sum"
}
},
"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"
]
}
}
]
}
},
"includeActualCost": false,
"includeFreshPartialCost": false
}
import com.azure.resourcemanager.costmanagement.models.ForecastAggregation;
import com.azure.resourcemanager.costmanagement.models.ForecastComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.ForecastDataset;
import com.azure.resourcemanager.costmanagement.models.ForecastDefinition;
import com.azure.resourcemanager.costmanagement.models.ForecastFilter;
import com.azure.resourcemanager.costmanagement.models.ForecastOperatorType;
import com.azure.resourcemanager.costmanagement.models.ForecastTimePeriod;
import com.azure.resourcemanager.costmanagement.models.ForecastTimeframe;
import com.azure.resourcemanager.costmanagement.models.ForecastType;
import com.azure.resourcemanager.costmanagement.models.FunctionName;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Forecast Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/EnrollmentAccountForecast.json
*/
/**
* Sample code: EnrollmentAccountForecast.
*
* @param manager Entry point to CostManagementManager.
*/
public static void enrollmentAccountForecast(
com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.forecasts()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789/enrollmentAccounts/456",
new ForecastDefinition()
.withType(ForecastType.USAGE)
.withTimeframe(ForecastTimeframe.CUSTOM)
.withTimePeriod(
new ForecastTimePeriod()
.withFrom(OffsetDateTime.parse("2022-08-01T00:00:00+00:00"))
.withTo(OffsetDateTime.parse("2022-08-31T23:59:59+00:00")))
.withDataset(
new ForecastDataset()
.withGranularity(GranularityType.DAILY)
.withAggregation(
mapOf(
"totalCost",
new ForecastAggregation()
.withName(FunctionName.COST)
.withFunction(FunctionType.SUM)))
.withFilter(
new ForecastFilter()
.withAnd(
Arrays
.asList(
new ForecastFilter()
.withOr(
Arrays
.asList(
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceLocation")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new ForecastFilter()
.withTags(
new ForecastComparisonExpression()
.withName("Environment")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceGroup")
.withOperator(ForecastOperatorType.IN)
.withValues(Arrays.asList("API")))))))
.withIncludeActualCost(false)
.withIncludeFreshPartialCost(false),
null,
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_forecast.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.forecast.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789/enrollmentAccounts/456",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "Cost"}},
"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",
},
"includeActualCost": False,
"includeFreshPartialCost": False,
"timePeriod": {"from": "2022-08-01T00:00:00+00:00", "to": "2022-08-31T23:59:59+00:00"},
"timeframe": "Custom",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/EnrollmentAccountForecast.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/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/12345:6789/enrollmentAccounts/456/providers/Microsoft.CostManagement/Forecast?api-version=2021-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "CostStatus",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
2.10333307059661,
20180331,
"Forecast",
"USD"
],
[
218.68795741935486,
20180331,
"Forecast",
"USD"
],
[
0.14384913581657052,
20180401,
"Forecast",
"USD"
],
[
0.009865586851323632,
20180429,
"Forecast",
"USD"
]
]
}
}
InvoiceSectionForecast
Sample Request
POST https://management.azure.com/providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876/providers/Microsoft.CostManagement/forecast?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "Custom",
"timePeriod": {
"from": "2022-08-01T00:00:00+00:00",
"to": "2022-08-31T23:59:59+00:00"
},
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": {
"name": "Cost",
"function": "Sum"
}
},
"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"
]
}
}
]
}
},
"includeActualCost": false,
"includeFreshPartialCost": false
}
import com.azure.resourcemanager.costmanagement.models.ForecastAggregation;
import com.azure.resourcemanager.costmanagement.models.ForecastComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.ForecastDataset;
import com.azure.resourcemanager.costmanagement.models.ForecastDefinition;
import com.azure.resourcemanager.costmanagement.models.ForecastFilter;
import com.azure.resourcemanager.costmanagement.models.ForecastOperatorType;
import com.azure.resourcemanager.costmanagement.models.ForecastTimePeriod;
import com.azure.resourcemanager.costmanagement.models.ForecastTimeframe;
import com.azure.resourcemanager.costmanagement.models.ForecastType;
import com.azure.resourcemanager.costmanagement.models.FunctionName;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Forecast Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/InvoiceSectionForecast.json
*/
/**
* Sample code: InvoiceSectionForecast.
*
* @param manager Entry point to CostManagementManager.
*/
public static void invoiceSectionForecast(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.forecasts()
.usageWithResponse(
"providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876",
new ForecastDefinition()
.withType(ForecastType.USAGE)
.withTimeframe(ForecastTimeframe.CUSTOM)
.withTimePeriod(
new ForecastTimePeriod()
.withFrom(OffsetDateTime.parse("2022-08-01T00:00:00+00:00"))
.withTo(OffsetDateTime.parse("2022-08-31T23:59:59+00:00")))
.withDataset(
new ForecastDataset()
.withGranularity(GranularityType.DAILY)
.withAggregation(
mapOf(
"totalCost",
new ForecastAggregation()
.withName(FunctionName.COST)
.withFunction(FunctionType.SUM)))
.withFilter(
new ForecastFilter()
.withAnd(
Arrays
.asList(
new ForecastFilter()
.withOr(
Arrays
.asList(
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceLocation")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new ForecastFilter()
.withTags(
new ForecastComparisonExpression()
.withName("Environment")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceGroup")
.withOperator(ForecastOperatorType.IN)
.withValues(Arrays.asList("API")))))))
.withIncludeActualCost(false)
.withIncludeFreshPartialCost(false),
null,
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_forecast.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.forecast.usage(
scope="providers/Microsoft.Billing/billingAccounts/12345:6789/billingProfiles/13579/invoiceSections/9876",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "Cost"}},
"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",
},
"includeActualCost": False,
"includeFreshPartialCost": False,
"timePeriod": {"from": "2022-08-01T00:00:00+00:00", "to": "2022-08-31T23:59:59+00:00"},
"timeframe": "Custom",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/InvoiceSectionForecast.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/Forecast?api-version=2019-10-01&$skiptoken=AQAAAA%3D%3D",
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "CostStatus",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
2.10333307059661,
20180331,
"Forecast",
"USD"
],
[
218.68795741935486,
20180331,
"Forecast",
"USD"
],
[
0.14384913581657052,
20180401,
"Forecast",
"USD"
],
[
0.009865586851323632,
20180429,
"Forecast",
"USD"
]
]
}
}
ResourceGroupForecast
Sample Request
POST https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/ScreenSharingTest-peer/providers/Microsoft.CostManagement/forecast?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "Custom",
"timePeriod": {
"from": "2022-08-01T00:00:00+00:00",
"to": "2022-08-31T23:59:59+00:00"
},
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": {
"name": "Cost",
"function": "Sum"
}
},
"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"
]
}
}
]
}
},
"includeActualCost": false,
"includeFreshPartialCost": false
}
import com.azure.resourcemanager.costmanagement.models.ForecastAggregation;
import com.azure.resourcemanager.costmanagement.models.ForecastComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.ForecastDataset;
import com.azure.resourcemanager.costmanagement.models.ForecastDefinition;
import com.azure.resourcemanager.costmanagement.models.ForecastFilter;
import com.azure.resourcemanager.costmanagement.models.ForecastOperatorType;
import com.azure.resourcemanager.costmanagement.models.ForecastTimePeriod;
import com.azure.resourcemanager.costmanagement.models.ForecastTimeframe;
import com.azure.resourcemanager.costmanagement.models.ForecastType;
import com.azure.resourcemanager.costmanagement.models.FunctionName;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Forecast Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/ResourceGroupForecast.json
*/
/**
* Sample code: ResourceGroupForecast.
*
* @param manager Entry point to CostManagementManager.
*/
public static void resourceGroupForecast(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.forecasts()
.usageWithResponse(
"subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/ScreenSharingTest-peer",
new ForecastDefinition()
.withType(ForecastType.USAGE)
.withTimeframe(ForecastTimeframe.CUSTOM)
.withTimePeriod(
new ForecastTimePeriod()
.withFrom(OffsetDateTime.parse("2022-08-01T00:00:00+00:00"))
.withTo(OffsetDateTime.parse("2022-08-31T23:59:59+00:00")))
.withDataset(
new ForecastDataset()
.withGranularity(GranularityType.DAILY)
.withAggregation(
mapOf(
"totalCost",
new ForecastAggregation()
.withName(FunctionName.COST)
.withFunction(FunctionType.SUM)))
.withFilter(
new ForecastFilter()
.withAnd(
Arrays
.asList(
new ForecastFilter()
.withOr(
Arrays
.asList(
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceLocation")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new ForecastFilter()
.withTags(
new ForecastComparisonExpression()
.withName("Environment")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceGroup")
.withOperator(ForecastOperatorType.IN)
.withValues(Arrays.asList("API")))))))
.withIncludeActualCost(false)
.withIncludeFreshPartialCost(false),
null,
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_forecast.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.forecast.usage(
scope="subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/ScreenSharingTest-peer",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "Cost"}},
"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",
},
"includeActualCost": False,
"includeFreshPartialCost": False,
"timePeriod": {"from": "2022-08-01T00:00:00+00:00", "to": "2022-08-31T23:59:59+00:00"},
"timeframe": "Custom",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/ResourceGroupForecast.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/00000000-0000-0000-0000-000000000000",
"name": "55312978-ba1b-415c-9304-cfd9c43c0481",
"type": "Microsoft.CostManagement/query",
"properties": {
"nextLink": null,
"columns": [
{
"name": "PreTaxCost",
"type": "Number"
},
{
"name": "UsageDate",
"type": "Number"
},
{
"name": "CostStatus",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
2.10333307059661,
20180331,
"Forecast",
"USD"
]
]
}
}
SubscriptionForecast
Sample Request
POST https://management.azure.com/subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.CostManagement/forecast?api-version=2022-10-01
{
"type": "Usage",
"timeframe": "Custom",
"timePeriod": {
"from": "2022-08-01T00:00:00+00:00",
"to": "2022-08-31T23:59:59+00:00"
},
"dataset": {
"granularity": "Daily",
"aggregation": {
"totalCost": {
"name": "Cost",
"function": "Sum"
}
},
"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"
]
}
}
]
}
},
"includeActualCost": false,
"includeFreshPartialCost": false
}
import com.azure.resourcemanager.costmanagement.models.ForecastAggregation;
import com.azure.resourcemanager.costmanagement.models.ForecastComparisonExpression;
import com.azure.resourcemanager.costmanagement.models.ForecastDataset;
import com.azure.resourcemanager.costmanagement.models.ForecastDefinition;
import com.azure.resourcemanager.costmanagement.models.ForecastFilter;
import com.azure.resourcemanager.costmanagement.models.ForecastOperatorType;
import com.azure.resourcemanager.costmanagement.models.ForecastTimePeriod;
import com.azure.resourcemanager.costmanagement.models.ForecastTimeframe;
import com.azure.resourcemanager.costmanagement.models.ForecastType;
import com.azure.resourcemanager.costmanagement.models.FunctionName;
import com.azure.resourcemanager.costmanagement.models.FunctionType;
import com.azure.resourcemanager.costmanagement.models.GranularityType;
import java.time.OffsetDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/** Samples for Forecast Usage. */
public final class Main {
/*
* x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/SubscriptionForecast.json
*/
/**
* Sample code: SubscriptionForecast.
*
* @param manager Entry point to CostManagementManager.
*/
public static void subscriptionForecast(com.azure.resourcemanager.costmanagement.CostManagementManager manager) {
manager
.forecasts()
.usageWithResponse(
"subscriptions/00000000-0000-0000-0000-000000000000",
new ForecastDefinition()
.withType(ForecastType.USAGE)
.withTimeframe(ForecastTimeframe.CUSTOM)
.withTimePeriod(
new ForecastTimePeriod()
.withFrom(OffsetDateTime.parse("2022-08-01T00:00:00+00:00"))
.withTo(OffsetDateTime.parse("2022-08-31T23:59:59+00:00")))
.withDataset(
new ForecastDataset()
.withGranularity(GranularityType.DAILY)
.withAggregation(
mapOf(
"totalCost",
new ForecastAggregation()
.withName(FunctionName.COST)
.withFunction(FunctionType.SUM)))
.withFilter(
new ForecastFilter()
.withAnd(
Arrays
.asList(
new ForecastFilter()
.withOr(
Arrays
.asList(
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceLocation")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays
.asList("East US", "West Europe"))),
new ForecastFilter()
.withTags(
new ForecastComparisonExpression()
.withName("Environment")
.withOperator(ForecastOperatorType.IN)
.withValues(
Arrays.asList("UAT", "Prod"))))),
new ForecastFilter()
.withDimensions(
new ForecastComparisonExpression()
.withName("ResourceGroup")
.withOperator(ForecastOperatorType.IN)
.withValues(Arrays.asList("API")))))))
.withIncludeActualCost(false)
.withIncludeFreshPartialCost(false),
null,
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_forecast.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.forecast.usage(
scope="subscriptions/00000000-0000-0000-0000-000000000000",
parameters={
"dataset": {
"aggregation": {"totalCost": {"function": "Sum", "name": "Cost"}},
"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",
},
"includeActualCost": False,
"includeFreshPartialCost": False,
"timePeriod": {"from": "2022-08-01T00:00:00+00:00", "to": "2022-08-31T23:59:59+00:00"},
"timeframe": "Custom",
"type": "Usage",
},
)
print(response)
# x-ms-original-file: specification/cost-management/resource-manager/Microsoft.CostManagement/stable/2022-10-01/examples/SubscriptionForecast.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": "UsageDate",
"type": "Number"
},
{
"name": "CostStatus",
"type": "String"
},
{
"name": "Currency",
"type": "String"
}
],
"rows": [
[
2.10333307059661,
20180331,
"Forecast",
"USD"
],
[
218.68795741935486,
20180331,
"Forecast",
"USD"
],
[
0.14384913581657052,
20180401,
"Forecast",
"USD"
],
[
0.009865586851323632,
20180429,
"Forecast",
"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.
ForecastAggregation
The aggregation expression to be used in the forecast.
ForecastColumn
Forecast column properties
ForecastComparisonExpression
The comparison expression to be used in the forecast.
ForecastDataset
The definition of data present in the forecast.
ForecastDatasetConfiguration
The configuration of dataset in the forecast.
ForecastDefinition
The definition of a forecast.
ForecastFilter
The filter expression to be used in the export.
ForecastOperatorType
The operator to use for comparison.
ForecastResult
Result of forecast. It contains all columns listed under groupings and aggregation.
ForecastTimeframe
The time frame for pulling data for the forecast.
ForecastTimePeriod
Has time period for pulling data for the forecast.
ForecastType
The type of the forecast.
FunctionName
The name of the column to aggregate.
FunctionType
The name of the aggregation function to use.
GranularityType
The granularity of rows in the forecast.
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.
ForecastAggregation
The aggregation expression to be used in the forecast.
Name
Type
Description
function
FunctionType
The name of the aggregation function to use.
name
FunctionName
The name of the column to aggregate.
ForecastColumn
Forecast column properties
Name
Type
Description
name
string
The name of column.
type
string
The type of column.
ForecastComparisonExpression
The comparison expression to be used in the forecast.
Name
Type
Description
name
string
The name of the column to use in comparison.
operator
ForecastOperatorType
The operator to use for comparison.
values
string[]
Array of values to use for comparison
ForecastDataset
The definition of data present in the forecast.
Name
Type
Description
aggregation
<string,
ForecastAggregation >
Dictionary of aggregation expression to use in the forecast. The key of each item in the dictionary is the alias for the aggregated column. forecast can have up to 2 aggregation clauses.
configuration
ForecastDatasetConfiguration
Has configuration information for the data in the export. The configuration will be ignored if aggregation and grouping are provided.
filter
ForecastFilter
Has filter expression to use in the forecast.
granularity
GranularityType
The granularity of rows in the forecast.
ForecastDatasetConfiguration
The configuration of dataset in the forecast.
Name
Type
Description
columns
string[]
Array of column names to be included in the forecast. Any valid forecast column name is allowed. If not provided, then forecast includes all columns.
ForecastDefinition
The definition of a forecast.
Name
Type
Description
dataset
ForecastDataset
Has definition for data in this forecast.
includeActualCost
boolean
A boolean determining if actualCost will be included.
includeFreshPartialCost
boolean
A boolean determining if FreshPartialCost will be included.
timePeriod
ForecastTimePeriod
Has time period for pulling data for the forecast.
timeframe
ForecastTimeframe
The time frame for pulling data for the forecast. If custom, then a specific time period must be provided.
type
ForecastType
The type of the forecast.
ForecastFilter
The filter expression to be used in the export.
ForecastOperatorType
The operator to use for comparison.
Name
Type
Description
In
string
ForecastResult
Result of forecast. 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
ForecastColumn []
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.
ForecastTimeframe
The time frame for pulling data for the forecast.
Name
Type
Description
Custom
string
ForecastTimePeriod
Has time period for pulling data for the forecast.
Name
Type
Description
from
string
The start date to pull data from.
to
string
The end date to pull data to.
ForecastType
The type of the forecast.
Name
Type
Description
ActualCost
string
AmortizedCost
string
Usage
string
FunctionName
The name of the column to aggregate.
Name
Type
Description
Cost
string
CostUSD
string
PreTaxCost
string
PreTaxCostUSD
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