Microsoft Azure Cost Management Client Library for Python providing comprehensive access to cost analysis, billing data, budgets, alerts, exports, and recommendations.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Manage and organize cost analysis views for consistent reporting and sharing across teams. Create, update, and delete custom views with specific filters, groupings, and configurations for standardized cost analysis workflows.
Create, retrieve, update, and delete cost management views with custom configurations for team sharing and consistent analysis.
def list() -> ViewListResult:
"""
List all shared views accessible to the current user.
Returns:
ViewListResult: Collection of shared views
"""
def get(view_name: str) -> View:
"""
Get a specific shared view by name.
Args:
view_name (str): Name of the view to retrieve
Returns:
View: View configuration and properties
"""
def create_or_update(view_name: str, parameters: View) -> View:
"""
Create or update a shared view.
Args:
view_name (str): Name for the view
parameters (View): View configuration details
Returns:
View: Created or updated view
"""
def delete(view_name: str) -> None:
"""
Delete a shared view.
Args:
view_name (str): Name of the view to delete
"""Manage views within specific scopes like subscriptions or resource groups for targeted cost analysis.
def list_by_scope(scope: str) -> ViewListResult:
"""
List views for a specific scope.
Args:
scope (str): The scope to list views for
Returns:
ViewListResult: Collection of scoped views
"""
def get_by_scope(scope: str, view_name: str) -> View:
"""
Get a specific view within a scope.
Args:
scope (str): The scope containing the view
view_name (str): Name of the view
Returns:
View: Scoped view configuration
"""
def create_or_update_by_scope(scope: str, view_name: str, parameters: View) -> View:
"""
Create or update a view within a specific scope.
Args:
scope (str): The scope for the view
view_name (str): Name for the view
parameters (View): View configuration
Returns:
View: Created or updated scoped view
"""
def delete_by_scope(scope: str, view_name: str) -> None:
"""
Delete a view within a specific scope.
Args:
scope (str): The scope containing the view
view_name (str): Name of the view to delete
"""from azure.mgmt.costmanagement.models import (
View,
ReportConfigDefinition,
ReportConfigDataset,
ReportConfigAggregation,
ReportConfigGrouping,
ReportConfigTimePeriod,
ChartType,
TimeframeType,
GranularityType
)
# Create view for monthly costs by resource group
view_config = View()
view_config.display_name = "Monthly Costs by Resource Group"
view_config.chart = ChartType.STACKED_COLUMN
view_config.accumulated = "true"
view_config.metric = "ActualCost"
view_config.kpis = [
{
"type": "Forecast",
"enabled": True
},
{
"type": "Budget",
"enabled": True,
"id": "/subscriptions/{sub-id}/providers/Microsoft.Consumption/budgets/monthly-budget"
}
]
# Configure dataset
dataset = ReportConfigDataset(
granularity=GranularityType.MONTHLY,
aggregation={
"totalCost": ReportConfigAggregation(name="Cost", function="Sum")
},
grouping=[
ReportConfigGrouping(type="Dimension", name="ResourceGroup")
]
)
view_config.query = ReportConfigDefinition(
type="ActualCost",
timeframe=TimeframeType.THE_LAST_MONTH,
dataset=dataset
)
# Create the view
created_view = client.views.create_or_update(
view_name="monthly-rg-costs",
parameters=view_config
)
print(f"Created view: {created_view.display_name}")# List all shared views
shared_views = client.views.list()
print(f"Shared views ({len(shared_views.value)}):")
for view in shared_views.value:
print(f" Name: {view.name}")
print(f" Display Name: {view.display_name}")
print(f" Chart Type: {view.chart}")
print(f" Metric: {view.metric}")
print(f" Timeframe: {view.query.timeframe}")
print(" ---")
# List views for specific subscription
scope = "/subscriptions/{subscription-id}"
scoped_views = client.views.list_by_scope(scope)
print(f"\nSubscription views ({len(scoped_views.value)}):")
for view in scoped_views.value:
print(f" {view.name}: {view.display_name}")# Get existing view
existing_view = client.views.get("monthly-rg-costs")
# Update display name and chart type
existing_view.display_name = "Updated Monthly Resource Group Costs"
existing_view.chart = ChartType.LINE
# Add additional grouping
if existing_view.query.dataset.grouping:
existing_view.query.dataset.grouping.append(
ReportConfigGrouping(type="Dimension", name="ServiceName")
)
# Update the view
updated_view = client.views.create_or_update(
view_name="monthly-rg-costs",
parameters=existing_view
)
print(f"Updated view: {updated_view.display_name}")from azure.mgmt.costmanagement.models import ReportConfigFilter, ReportConfigComparisonExpression
# Create view filtered for specific services
service_filter = ReportConfigFilter(
and_=[
ReportConfigComparisonExpression(
name="ServiceName",
operator="In",
values=["Virtual Machines", "Storage", "Azure SQL Database"]
)
]
)
service_dataset = ReportConfigDataset(
granularity=GranularityType.DAILY,
aggregation={
"totalCost": ReportConfigAggregation(name="Cost", function="Sum")
},
grouping=[
ReportConfigGrouping(type="Dimension", name="ServiceName")
],
filter=service_filter
)
service_view = View()
service_view.display_name = "Core Services Daily Costs"
service_view.chart = ChartType.AREA
service_view.accumulated = "false"
service_view.metric = "ActualCost"
service_view.query = ReportConfigDefinition(
type="ActualCost",
timeframe=TimeframeType.THE_LAST_WEEK,
dataset=service_dataset
)
created_service_view = client.views.create_or_update(
view_name="core-services-daily",
parameters=service_view
)class View:
id: str
name: str
type: str
display_name: str
scope: str
created_on: str
modified_on: str
chart: ChartType
accumulated: str
metric: str
kpis: List[KpiProperties]
pivots: List[PivotProperties]
query: ReportConfigDefinition
class ViewListResult:
value: List[View]
next_link: str
class ReportConfigDefinition:
type: str
timeframe: TimeframeType
time_period: ReportConfigTimePeriod
dataset: ReportConfigDataset
class ReportConfigDataset:
granularity: GranularityType
configuration: ReportConfigDatasetConfiguration
aggregation: Dict[str, ReportConfigAggregation]
grouping: List[ReportConfigGrouping]
sorting: List[ReportConfigSorting]
filter: ReportConfigFilter
class KpiProperties:
type: str
enabled: bool
id: str
class PivotProperties:
type: str
name: strclass ChartType(str, Enum):
AREA = "Area"
LINE = "Line"
STACKED_COLUMN = "StackedColumn"
GROUPED_COLUMN = "GroupedColumn"
TABLE = "Table"
class KpiType(str, Enum):
FORECAST = "Forecast"
BUDGET = "Budget"
class PivotType(str, Enum):
DIMENSION = "Dimension"This module provides comprehensive view management capabilities for creating standardized, shareable cost analysis configurations that ensure consistent reporting across teams and projects.
Install with Tessl CLI
npx tessl i tessl/pypi-azure-mgmt-costmanagement