Filtering or removing Azure.ResourceManager VirtualMachineResource as part of query, or after getting query result

139 Views Asked by At

Using Azure.ResourceManager.

I need to filter returned VirtualMachineResources based on a list of Virtual Machine IDs that I have from a Graph query.

Ideally, I'd like to limit the results returned by GetVirtualMachines() but that doesn't seem possible. Alternately, I don't mind removing elements from the resulting VirtualMachineCollection, but it seems that class is protected so it's not possible.

Another option would be to remove (or limit on query) items from AsyncPageable<VirtualMachineResource>, but again, that doesn't seem possible.

I would very much like to be able to pass the already culled VM collection to other functions rather than manually iterating through polluted lists later.

I have tried post-filtering the results into a new <List> but that is not a viable solution as I need the methods that operate on AsyncPageable<VirtualMachineResource> which are not available to a generic List.

public static AsyncPageable<VirtualMachineResource> GetVmList(ResourceGroupResource resourceGroup, FilterBy filterBy, List<string> filterList)
{
    //need to filter vmCollection or vms by either removing that aren't included in filterList param
    //or by limiting what GetVirtualMachines() or GetAllAsync returns
    VirtualMachineCollection vmCollection = resourceGroup.GetVirtualMachines();
    AsyncPageable<VirtualMachineResource> vms = vmCollection.GetAllAsync();
    return vms;
}
1

There are 1 best solutions below

2
Rajesh  Mopati On
  1. You need to install the below NuGet packages from the NuGet Package Manager Console.

enter image description here

  1. You need to use the below namespaces in the code.
     Azure
     Azure.Identity
     Azure.ResourceManager
     Azure.ResourceManager.Compute
     Azure.ResourceManager.Resources

You can filter the returned VirtualMachineResources by using the below code.

I have tried using the below code and it worked for me.


     ArmClient clnt = new ArmClient(new InteractiveBrowserCredential(new InteractiveBrowserCredentialOptions() { TenantId = "Your TenantId" }));
     SubscriptionResource RsrcSubcrip = await clnt.GetDefaultSubscriptionAsync();

     string ResourceGrpName = "Resource Group Name";
     ResourceGroupResource rscGrp = await RsrcSubcrip.GetResourceGroups().GetAsync(ResourceGrpName);
     VirtualMachineCollection vmCollection = rscGrp.GetVirtualMachines();
     AsyncPageable<VirtualMachineResource> VMachines = vmCollection.GetAllAsync();
     List<string> myVMS = new List<string>();
     Console.WriteLine("Virtual Machines...");
     var Vms = "myWindowsVM";
         await foreach (VirtualMachineResource vm in VMachines)
            {

                Console.WriteLine(vm.Data.Name);
                myVMS.Add(vm.Data.Name);
                Console.WriteLine(vm.Get().Value.InstanceView().Value.Statuses[1].DisplayStatus);
            }

 var filteredList = myVMS.Where(p => myVMS.Contains(Vms))
                                       .ToList();
 var Vms = "myWindowsVM"; // your list of Virtual Machines.
 var filteredList = myVMS.Where(p => myVMS.Contains(Vms))
                                       .ToList();

Fetching the VMs from azure

enter image description here

Filtering the VMs from the list of VMs

enter image description here

This will give you a new collection of VirtualMachineResource objects that contains only the virtual machines with names that are in the list.

And if you want to you want to fetch the resources API, check this MS-Doc.