Azure Consumption Management Client for accessing consumption resources, budgets, and usage analytics for Azure Enterprise Subscriptions
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Access aggregated cost data for management groups across current and historical billing periods for comprehensive cost oversight and management group cost analysis.
Get the aggregate cost of a management group and all child management groups for the current billing period.
def get_by_management_group(
management_group_id: str,
filter: str = None,
**kwargs
) -> ManagementGroupAggregatedCostResult:
"""
Provides the aggregate cost of a management group and all child management groups by current billing period.
Parameters:
- management_group_id: Azure Management Group ID (str)
- filter: May be used to filter aggregated cost by properties/usageDate (Utc time),
properties/chargeType or properties/publisherType. Supported operators are 'eq', 'lt', 'gt', 'le', 'ge' (str, optional)
Returns:
ManagementGroupAggregatedCostResult: Aggregated cost data with usage dates, costs, and charge types
"""Get the aggregate cost of a management group and all child management groups for a specific historical billing period.
def get_for_billing_period_by_management_group(
management_group_id: str,
billing_period_name: str,
**kwargs
) -> ManagementGroupAggregatedCostResult:
"""
Provides the aggregate cost of a management group and all child management groups by specified billing period.
Parameters:
- management_group_id: Azure Management Group ID (str)
- billing_period_name: Billing Period Name (str)
Returns:
ManagementGroupAggregatedCostResult: Historical aggregated cost data for the specified billing period
"""from azure.identity import DefaultAzureCredential
from azure.mgmt.consumption import ConsumptionManagementClient
# Authenticate and create client
credential = DefaultAzureCredential()
subscription_id = "your-subscription-id"
client = ConsumptionManagementClient(credential, subscription_id)
# Get current billing period aggregated costs for a management group
management_group_id = "your-management-group-id"
current_costs = client.aggregated_cost.get_by_management_group(
management_group_id=management_group_id
)
print(f"Current period aggregated cost: {current_costs.properties.billing_period_id}")
print(f"Total cost: {current_costs.properties.charge_summary.value} {current_costs.properties.currency}")
# Get aggregated costs with date filter
from datetime import datetime, timedelta
end_date = datetime.now()
start_date = end_date - timedelta(days=30)
date_filter = f"properties/usageDate ge '{start_date.strftime('%Y-%m-%d')}' and properties/usageDate le '{end_date.strftime('%Y-%m-%d')}'"
filtered_costs = client.aggregated_cost.get_by_management_group(
management_group_id=management_group_id,
filter=date_filter
)
# Get historical billing period costs
billing_period = "202401" # January 2024
historical_costs = client.aggregated_cost.get_for_billing_period_by_management_group(
management_group_id=management_group_id,
billing_period_name=billing_period
)
print(f"Historical period {billing_period} cost: {historical_costs.properties.charge_summary.value}")class ManagementGroupAggregatedCostResult:
id: str
name: str
type: str
etag: str
properties: ManagementGroupAggregatedCostProperties
class ManagementGroupAggregatedCostProperties:
billing_period_id: str
usage_start: datetime
usage_end: datetime
azure_charges: float
marketplace_charges: float
charge_summary: ChargeSummary
children: List[ManagementGroupAggregatedCostResult]
included_subscriptions: List[str]
excluded_subscriptions: List[str]
currency: str
class ChargeSummary:
value: float
currency: strInstall with Tessl CLI
npx tessl i tessl/pypi-azure-mgmt-consumption