azure-xplat-cli icon indicating copy to clipboard operation
azure-xplat-cli copied to clipboard

In Azure ARM REST API, how do a get the state (i.e. started, stopped etc) for running virtual machines?

Open dukedougal opened this issue 9 years ago • 12 comments

I am using ARM in Azure.

Azure provides a REST API to get a list of virtual machines.

https://msdn.microsoft.com/en-us/library/azure/mt163572.aspx

Strangely the response data does not seem to provide the state of those machines i.e stopped, running etc. Am I looking in the wrong place?

I need to be able to get a list of all virtual machines and carry out actions on them depending on whether or not they are running or stopped etc.

dukedougal avatar Feb 22 '16 11:02 dukedougal

@dukedougal - I assume that you would have read my response about changing modes in the previous issue had filed https://github.com/Azure/azure-xplat-cli/issues/2566

So, if you execute the vm list in arm mode then the output has PowerState property

D:\sdk\azure-xplat-cli>azure vm list
info:    Executing command vm list
+ Getting virtual machines
data:    ResourceGroupName      Name             ProvisioningState  PowerState      Location       Size
data:    ---------------------  ---------------  -----------------  --------------  -------------  -----------
data:    TST232                  test12             Succeeded          VM running      westus         Standard_A1
data:    TST343                  test34             Failed             VM running      westus         Standard_A1
info:    vm list command OK

amarzavery avatar Feb 22 '16 19:02 amarzavery

Hello @amarzavery - I am trying to program only using the ARM REST API. As far as I can tell, there needs to be a request for every single VM to work out its running state. I have opened an issue about this here https://github.com/Azure/azure-xplat-cli/issues/2569

dukedougal avatar Feb 22 '16 20:02 dukedougal

@dukedougal - Are you using the CLI or the node sdk?

amarzavery avatar Feb 22 '16 23:02 amarzavery

@dukedougal - This the CLI project not the node sdk project. I have shown you the output of azure vm list command that has the PowerState property in its output.

amarzavery avatar Feb 22 '16 23:02 amarzavery

Hi @amarzavery - I'm trying to use only the REST API and it appears as far as I can tell that the cli does an initial request for the list of VMs then sends an additional request for every VM to get the required state data.

dukedougal avatar Feb 22 '16 23:02 dukedougal

@dukedougal - I understand what you are doing. The script that you are writing is from the (azure-arm-compute) node sdk which is in this project https://github.com/Azure/azure-sdk-for-node and you are filing an issue in xplat cli which consumes the node sdk. These are two different things. As far as ARM API for VM is not offering parity wrt V1 API, the service team would be able to comment on it.

@huangpf , @sauryadas , @singhkay - Please respond to this customer query.

amarzavery avatar Feb 23 '16 00:02 amarzavery

@dukedougal You are correct, with the ARM API you can't get the VM state in the List ALL VMs API. You need to call the instance view API on each VM to get this.

https://management.azure.com/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.Compute/virtualMachines/{vm-name}/InstanceView?api-version={api-version}

Adding @mahthi for this feedback

singhkays avatar Feb 23 '16 00:02 singhkays

On EC2 I can ask for a description of my instances and I get everything I need to know, but on Azure I need to send an individual request for every instance just to find out if they are running or stopped.

Number of requests sent = number of running VMs plus one. If you are running 1000 instances then you need 1001 requests?

Seems like a flawed design to me. What do you guys think?

http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-DescribeInstances.html

dukedougal avatar Feb 23 '16 00:02 dukedougal

This is a pretty important and easy to implement feature one would think.

danmo avatar Apr 05 '17 14:04 danmo

This is an ask about service API, not the CLI commands. For service issue, please use your subscription's support.

huangpf avatar Apr 05 '17 16:04 huangpf

Also, in order to get public IP, Private IP, VM list, VM status, you need to do a separate REST calls. (For V run status you need to do separate calls for each VM !!!) Is it really that hard to provide all these info in a single REST call?

gokhanoner avatar Dec 13 '18 23:12 gokhanoner

This document instruct you to pass parameter "statusOnly=true" to get VM status list in a single call: https://docs.microsoft.com/en-us/rest/api/compute/virtualmachines/listall#code-try-0.

When following the instruction, the response shows "Error - message": "Query param statusOnly=true is only supported when subscription is registered with Microsoft.Compute/BulkGETVMRuntimeState feature."

So I attempted to register Microsoft.Compute/BulkGETVMRuntimeState on CLI. And it says: "The feature 'BulkGETVMRuntimeState' does not support registration." ... what does "does not support registration" mean? does it mean its depreciated?

Next step, I ran "az feature list --namespace Microsoft.Compute --output table" on CLI. Confirmed that "Microsoft.Compute/BulkGETVMRuntimeState" is not on the feature list for Microsoft.Compute.

So, the official document is wrong. The API response is wrong. CLI response is vague.

And yes, when you are working in a large environment, getting all the VM names first over API to send another API request to get VM running state is not a small deal. @gokhanoner's response above tells me perhaps Azure is designed for small accounts only?

Most importantly @gokhanoner, I do not see an API where you can pass all VM names in a single request to get the running state. If you have 2000 VMs, you have to call 2000 REST API to VM to get the running state.

This is sub-optimal from the quality of (1) documentation, (2) API - response showing deprecated feature as if it is required, and no API replacing the depreciated feature. Lastly (3) CLI response being vague. Should just say "deprecated" or "does not exist".

It takes little to no effort to get this info over API from other 2 clouds - AWS and GCP.

grumblegirl avatar Mar 20 '20 23:03 grumblegirl