Azure JAVA SDK API to get the list of VM sizes based on VM image

Arigala, NagaSwetha 1 Reputation point
2021-04-08T00:48:11.633+00:00

I am looking for Azure SDK or REST API (code sample) to get the list of available VM sizes based on VM image.
I see below are only 3 options as per Microsoft Learn. but in Azure portal while creating VM after choosing VM we can see the list of available VM sizes.
I need to know the details of API that's been used in Azure portal

https://learn.microsoft.com/en-us/azure/virtual-machines/sizes#rest-api

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,102 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. shiva patpi 13,131 Reputation points Microsoft Employee
    2021-04-10T05:24:14.18+00:00

    Hello @Arigala, NagaSwetha ,
    Thanks for your query ! To start with can you try out the below sample C#.net code APIs

    Reference:- https://github.com/Azure-Samples/compute-dotnet-list-vm-images

    Detailed Steps:-

    Step1 : Authenticate using Management libraries

    Step2: Try using the program.cs file as a sample to start

    Step3: Try to resolve all the references

    Below is the sample piece of C#.net code:

    using iTextSharp.text;
    using Microsoft.Azure.Management.Fluent;
    using Microsoft.Azure.Management.ResourceManager.Fluent;
    using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
    using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace AzureCompute

    {

    class Program  
    
    {  
    
        static void Main(string[] args)  
        {  
    
            IAzure azure = Azure.Authenticate("my.azureauth").WithDefaultSubscription();  
            ListAllVMImages(azure);  
            ListVMSizes(azure);  
        }  
    
        public static void ListAllVMImages(IAzure azure)  
        {  
            var publishers = azure  
                    .VirtualMachineImages  
                    .Publishers  
                    .ListByRegion(Region.USEast);  
    
            Console.WriteLine("\n");  
    
            foreach (var publisher in publishers)  
            {  
                Console.WriteLine("Publisher - " + publisher.Name);  
    
                if (StringComparer.OrdinalIgnoreCase.Equals(publisher.Name, "Canonical") ||  
                    StringComparer.OrdinalIgnoreCase.Equals(publisher.Name, "Suse") ||  
                    StringComparer.OrdinalIgnoreCase.Equals(publisher.Name, "RedHat"))  
                {  
                    Console.WriteLine("\n\n");  
                    Console.WriteLine("=======================================================");  
                    Console.WriteLine("Located " + publisher.Name);  
                    Console.WriteLine("=======================================================");  
                    Console.WriteLine("Printing entries as publisher/offer/sku/image/version");  
    
                    foreach (var offer in publisher.Offers.List())  
                    {  
                        foreach (var sku in offer.Skus.List())  
                        {  
                            foreach (var image in sku.Images.List())  
                            {  
                                Console.WriteLine($"Image - {publisher.Name}/{offer.Name}/{sku.Name}/{image.Version}");  
                            }  
                        }  
                    }  
    
                    Console.WriteLine("\n\n");  
                }  
            }  
        }  
    
        public static void ListVMSizes(IAzure azure)  
        {  
            var VMsizes = azure  
                    .VirtualMachines  
                    .Sizes  
                    .ListByRegion(Region.USEast);  
    
            Console.WriteLine("List All VM Sizes");  
            Console.WriteLine("\n");  
    
            foreach (var vmsize in VMsizes)  
            {  
                Console.WriteLine(vmsize.Name);  
            }  
        }  
    }  
    

    }

    Let us know if the above instructions and sample code helps out. Please make sure to "Upvote and Accept the Answer"

    0 comments No comments