Azure REST API - query parameters for getting all the virtual machine

故事扮演 提交于 2019-12-30 14:17:38

问题


I want to query all the virtual Machines in a subscription, but I want to be able to filter using a query param and not by code.

I am able to get all the virtual machines but I want to know if I can filter them and how.

I am not using the classic REST but ARM.


回答1:


If this is the API you use:

https://management.azure.com/subscriptions/{subscription-id}/resources?&api-version={api-version}

then you can use filter like this:

?$filter={filter}

Your request will look like this:

https://management.azure.com/subscriptions/{subscription-id}/resources?$filter=name eq {resourcename}&api-version={api-version}

$filter is optional and is used to filter results. Replace {filter} with one of the following values:

$filter=tagname eq {value}
$filter=tagname eq {tagname} and tagvalue eq {tagvalue}
$filter=startswith(tagname, {tagname prefix})
$filter=resourceType eq {resourceProviderNamespace/resourceType}
$filter=name eq {resourcename}
$filter=location eq {locationname}

Currently, you cannot use the tagname or tagvalue filters with other filters; such as, name, location, or resourceType.

To get all the Resources of type VM you can use a filter like this:

$filter=resourceType eq {Microsoft.Compute/virtualMachines}

For more info:

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




回答2:


I would add one change on @Aram's answer. An example for a filter would have to include single quotes, like:

https://management.azure.com/subscriptions/{subscription-id}/resources?$filter=name eq 'some_name'&api-version={api-version}

So possible filters would be:

$filter=tagname eq 'value'
$filter=tagname eq 'tagname' and tagvalue eq 'tagvalue'
$filter=resourceType eq 'resourceProviderNamespace/resourceType'


来源:https://stackoverflow.com/questions/34270735/azure-rest-api-query-parameters-for-getting-all-the-virtual-machine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!