0

I am working on a Java project that needs to retrieve a VM's public ip and private ip with Azure Java SDK.

I find two potentially useful classes com.microsoft.azure.management.compute.VirtualMachine and

com.azure.resourcemanager.compute.fluent.models.VirtualMachineInner

After hours of investigation, I cannot figure out how. The VirtualMachine class only has a function to get primary public ip. Neither of them has a function to get private ip.

Did I missing anything from the two classes?

Is there a way to list all details of VMs under a subscription with Java SDK like what we get with az vm list -d --subscription $Subscription_Id in Azure CLI?

Thanks in advance

  • 2
    Example in C# https://stackoverflow.com/a/37575946/8016720 The class names are similar in Java. https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.management.network?view=azure-java-stable – John Hanley Nov 25 '21 at 05:22

1 Answers1

0

Call (the azure object is initialized via guide https://aka.ms/azsdk/java/mgmt)

var vms = azure.virtualMachines().list();

Then for each of them, call

vm.getPrimaryNetworkInterface().primaryPrivateIP();

for private IP.

vm.getPrimaryPublicIPAddress();

for object of public IP, it could be null. If not null, then use ipAddress().

It is more complicated as either private IP or public IP is not within the VirtualMachine object, but in NetworkInterface object (or further PublicIpAddress object) and need to be done via additional REST API.

weidongxu
  • 303
  • 1
  • 7