Skip to content

Commit 70a5633

Browse files
Fix Azure Monitor API timespan format to use 'Z' for UTC (#1005)
Azure Monitor API requires timespan to use 'Z' suffix for UTC timezone instead of '+00:00' offset. Changed from datetime.isoformat() to strftime('%Y-%m-%dT%H:%M:%SZ') to match Azure's expected ISO 8601 format. This fixes the BadRequest error: "Detected invalid time interval input" that was occurring in instance_idle and unused_nat_gateway policies. Files modified: - cloud_governance/common/clouds/azure/monitor/monitor_management_operations.py - cloud_governance/policy/helpers/azure/azure_policy_operations.py Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 0c99ac2 commit 70a5633

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

cloud_governance/common/clouds/azure/monitor/monitor_management_operations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def get_resource_metrics(self, resource_id: str, metricnames: str, aggregation:
8181
if not timespan:
8282
end_date = datetime.now(tz=timezone.utc)
8383
start_date = end_date - timedelta(days=UNUSED_DAYS)
84-
timespan = f'{start_date.isoformat()}/{end_date.isoformat()}'
84+
timespan = f'{start_date.strftime("%Y-%m-%dT%H:%M:%SZ")}/{end_date.strftime("%Y-%m-%dT%H:%M:%SZ")}'
8585
response = self.__monitor_client.metrics.list(resource_uri=resource_id, timespan=timespan,
8686
metricnames=metricnames, aggregation=aggregation,
8787
result_type='Data', interval=interval,

cloud_governance/policy/helpers/azure/azure_policy_operations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def get_cpu_utilization_percentage_metric(self, resource_id: str, days: int = IN
199199
:rtype:
200200
"""
201201
start_date, end_date = Utils.get_start_and_end_datetime(days=days)
202-
timespan = f'{start_date.isoformat()}/{end_date.isoformat()}'
202+
timespan = f'{start_date.strftime("%Y-%m-%dT%H:%M:%SZ")}/{end_date.strftime("%Y-%m-%dT%H:%M:%SZ")}'
203203
cpu_metrics = self.monitor_operations.get_resource_metrics(resource_id=resource_id,
204204
metricnames='Percentage CPU',
205205
aggregation='Average',
@@ -218,7 +218,7 @@ def get_network_in_kib_metric(self, resource_id: str, days: int = INSTANCE_IDLE_
218218
:rtype:
219219
"""
220220
start_date, end_date = Utils.get_start_and_end_datetime(days=days)
221-
timespan = f'{start_date.isoformat()}/{end_date.isoformat()}'
221+
timespan = f'{start_date.strftime("%Y-%m-%dT%H:%M:%SZ")}/{end_date.strftime("%Y-%m-%dT%H:%M:%SZ")}'
222222
network_in_metrics = self.monitor_operations.get_resource_metrics(resource_id=resource_id,
223223
metricnames='Network In Total',
224224
aggregation='Average',
@@ -238,7 +238,7 @@ def get_network_out_kib_metric(self, resource_id: str, days: int = INSTANCE_IDLE
238238
:rtype:
239239
"""
240240
start_date, end_date = Utils.get_start_and_end_datetime(days=days)
241-
timespan = f'{start_date.isoformat()}/{end_date.isoformat()}'
241+
timespan = f'{start_date.strftime("%Y-%m-%dT%H:%M:%SZ")}/{end_date.strftime("%Y-%m-%dT%H:%M:%SZ")}'
242242
network_out_metrics = self.monitor_operations.get_resource_metrics(resource_id=resource_id,
243243
metricnames='Network Out Total',
244244
aggregation='Average',

0 commit comments

Comments
 (0)