Matteo91
Contributor
Contributor

Is it posible to request all Virtual Machine Resources with a specific StatKey via vR Ops REST API

  Hello again,

I’m trying to request all Virtual Machine Resources like (///suite-api/api/resources?resourceKind=VirtualMachine) but each with a specific StatKey. I know I can request a specific ressource with a specific StatKey via "///suite-api/api/resources/{ID}/stats/latest?statKey=someStatKey",
but I need it for all resources in one request. In my code, I already looped through all the Ids in (///suite-api/api/resources?resourceKind=VirtualMachine) and then made a single request for each id via  "///suite-api/api/resources/{ID}/stats/latest?statKey=someStatKey".
So I can get a specific StatKey Value for each virtual machine. This actually works, but the performance is very bad, since I have a lot of single
virtual machines in my System. That's why im looking for a single request or maybe another solution.

I hope someone can help me.

Greetings

0 Kudos
3 Replies
MichaelRyom
Hot Shot
Hot Shot

This is how im doing it... Though it sounds a lot like how you are doing it.

$PageSize = "5000" #Maximum is 5000


#Get a List of resources
$resourcelist = @()
$i=0
if((($resources = Invoke-RestMethod -Method Get -Uri "https://$vRopsFQDN/suite-api/api/resources/?pageSize=$PageSize" -Credential $cred).resources.links.link | Select -Last 1).href -eq ($resources.resources.links.link | Select -First 1).href){
$resourcelist = $resources
}else{
Do{

$resourcelist += Invoke-RestMethod -Method Get -Uri "https://$vRopsFQDN/suite-api/api/resources/?page=$i&pageSize=$PageSize" -Credential $cred

$i++
}until("/suite-api/api/resources/?page=$i&pageSize=$PageSize" -eq ($resources.resources.links.link | Select -Last 1).href)
}
#^End Get a List of resources

#Resource IDs
$ClusterID = ($resourcelist.resources.resource | where {$_.resourceKey.resourceKindKey -match "ClusterComputeResource"}).identifier
$HostID = ($resourcelist.resources.resource | where {$_.resourceKey.resourceKindKey -match "HostSystem"}).identifier
$VMID = ($resourcelist.resources.resource | where {$_.resourceKey.resourceKindKey -match "VirtualMachine"}).identifier
#^End of Resource IDs

#Get properties
$ClusterProperties = @()
Foreach($ID in $ClusterID){
$ClusterProperties += Invoke-RestMethod -Method Get -Uri "https://$vRopsFQDN/suite-api/api/resources/$ID/properties" -Credential $cred
}

$HostProperties = @()
Foreach($ID in $HostID){
$HostProperties += Invoke-RestMethod -Method Get -Uri "https://$vRopsFQDN/suite-api/api/resources/$ID/properties" -Credential $cred
}

$VMProperties = @()
Foreach($ID in $VMID){
$VMProperties += Invoke-RestMethod -Method Get -Uri "https://$vRopsFQDN/suite-api/api/resources/$ID/properties" -Credential $cred
}
#^End of Get properties

#Get Stats
$VMStats = @()
Foreach($ID in $VMID){
$VMStats += Invoke-RestMethod -Method Get -Uri "https://$vRopsFQDN/suite-api/api/resources/$ID/stats" -Credential $cred
}
#^End of Get Stats

Blogging at https://MichaelRyom.dk
0 Kudos
Matteo91
Contributor
Contributor

  Thank you for your answer Michael. I'm not using Php and my Php is a bit rusty :smileygrin: but I will try to adapt your solution on to my own. Hopefully it will perform a bit better than.

 

0 Kudos
MichaelRyom
Hot Shot
Hot Shot

It PowerShell code... What are you writing in ?

Blogging at https://MichaelRyom.dk
0 Kudos