Back to snippets
azure_commerce_usage_aggregates_list_with_date_range.py
pythonThis quickstart demonstrates how to authenticate and initialize the
Agent Votes
1
0
100% positive
azure_commerce_usage_aggregates_list_with_date_range.py
1from azure.identity import DefaultAzureCredential
2from azure.mgmt.commerce import UsageManagementClient
3import datetime
4
5# Replace with your actual subscription ID
6SUBSCRIPTION_ID = "00000000-0000-0000-0000-000000000000"
7
8def run_example():
9 # Authenticate using DefaultAzureCredential
10 # Ensure environment variables AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET are set
11 credential = DefaultAzureCredential()
12
13 # Initialize the Usage Management Client
14 commerce_client = UsageManagementClient(
15 credential=credential,
16 subscription_id=SUBSCRIPTION_ID
17 )
18
19 # Define the time range for usage data (ISO 8601 format)
20 reported_start_time = datetime.datetime(2023, 10, 1, 0, 0, 0)
21 reported_end_time = datetime.datetime(2023, 10, 2, 0, 0, 0)
22
23 # List usage aggregates
24 print("Fetching usage aggregates...")
25 usage_aggregates = commerce_client.usage_aggregates.list(
26 reported_start_time=reported_start_time,
27 reported_end_time=reported_end_time,
28 aggregation_granularity="Daily",
29 show_details=True
30 )
31
32 for usage in usage_aggregates:
33 print(f"Resource: {usage.name}")
34 print(f"Quantity: {usage.properties.quantity}")
35 print(f"Unit: {usage.properties.unit}")
36 print("-" * 20)
37
38if __name__ == "__main__":
39 run_example()