question

ArigalaNagaSwetha-9236 avatar image
0 Votes"
ArigalaNagaSwetha-9236 asked shivapatpi-MSFT answered

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

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 Docs. 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://docs.microsoft.com/en-us/azure/virtual-machines/sizes#rest-api

azure-virtual-machines-images
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

shivapatpi-MSFT avatar image
0 Votes"
shivapatpi-MSFT answered

Hello @ArigalaNagaSwetha-9236 ,
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"





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.