Skip to content

Commit

Permalink
fix(cloud-report): fix Azure's instances state
Browse files Browse the repository at this point in the history
Cloud resources report shows wrong state of Azure instances when are
stopped.

Fix by showing real status instead of hardcoded one.
  • Loading branch information
soyacz committed Feb 7, 2024
1 parent c263c16 commit 114bcc8
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions sdcm/utils/cloud_monitor/resources/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __init__(self, instance: VirtualMachine, resource_group: str):
name=instance.name,
instance_id=resource_group,
region_az=instance.location,
state="running",
state=self._get_vm_status(instance),
lifecycle=InstanceLifecycle.SPOT if instance.priority == "Spot" else InstanceLifecycle.ON_DEMAND,
instance_type=instance.hardware_profile.vm_size,
owner=tags.get("RunByUser", NA),
Expand All @@ -122,6 +122,11 @@ def __init__(self, instance: VirtualMachine, resource_group: str):
project=resource_group
)

@staticmethod
def _get_vm_status(instance) -> str:
statuses = {s.code: s.display_status for s in instance.instance_view.statuses}
return 'running' if statuses.get("PowerState/running") else 'stopped'

@property
def region(self):
return self.region_az
Expand All @@ -148,7 +153,7 @@ def get_azure_instances(self):
res = AzureService().resource_graph_query(query=' | '.join(query_bits))
get_virtual_machine = AzureService().compute.virtual_machines.get
instances = [(get_virtual_machine(resource_group_name=vm["resourceGroup"],
vm_name=vm["name"]), vm["resourceGroup"]) for vm in res]
vm_name=vm["name"], expand='instanceView'), vm["resourceGroup"]) for vm in res]
self["azure"] = [AzureInstance(instance, resource_group) for instance, resource_group in instances]
self.all.extend(self["azure"])

Expand Down

0 comments on commit 114bcc8

Please sign in to comment.