Kjensen
Kjensen

Reputation: 12374

How to get public IP of Azure VM using Azure SDK

For a specific VM, I want to be able to retrieve the public IP address.

I know how to get all public IP addresses for a resource group, I also know how to get a nic-id for a specific VM - but I can't figure out how to connect the two.

This is what I have:

var resourceGroupName = "My-Resource-Group";
var vmName = "MyVM";
var subscriptionId = "bzz-bzz-bzz-bzz-bzz-bzz";

var tenantId = "bar-bar-bar-bar-bar-bar";

string clientId = "foo-foo-foo-foo-foo-foo";
string clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        
var token = GetAccessTokenAsync(tenantId, clientId, clientSecret);
var credential = new TokenCredentials(token.Result.AccessToken);

var computeManagementClient = new ComputeManagementClient(credential) { SubscriptionId = subscriptionId };

var vmResult = await computeManagementClient.VirtualMachines.GetAsync(resourceGroupName, vmName, InstanceViewTypes.InstanceView);

//Get the NIC ID for the VM: 

foreach (NetworkInterfaceReference nic in vmResult.NetworkProfile.NetworkInterfaces)
        {
            Console.WriteLine("  networkInterface id: " + nic.Id);
        }    

this gives me something like this:

/subscriptions/[guid]/resourceGroups/My-Resource-Group/providers/Microsoft.Network/networkInterfaces/myvm123

To get all public IPs for the resource group, I can do this:

using (var client = new NetworkManagementClient(credential))
        {
            client.SubscriptionId = subscriptionId;
            foreach (var publicIpAddress in client.PublicIPAddresses.ListAll())
            {
                Console.WriteLine(publicIpAddress.IpAddress);
            }
}

...But inspecting the properties of the nic-id and the public ip object, there are no obvious ways to get from one to the other.

Question:

How do I get from the nic-id string, to the actual public IP address for that VM/nic?

Helper function:

private static async Task<AuthenticationResult> GetAccessTokenAsync(string tenantId, string clientId, string clientSecret)
    {
        var cc = new ClientCredential(clientId, clientSecret);
        var context = new AuthenticationContext($"https://login.windows.net/{tenantId}");
        var token = context.AcquireToken("https://management.azure.com/", cc);

        if (token == null)
        {
            throw new InvalidOperationException("Could not get the token");
        }
        return token;
    }

Upvotes: 5

Views: 2699

Answers (2)

Alex Rodriguez
Alex Rodriguez

Reputation: 11

For those using Azure.ResourceManager.Compute, calling Get on a resource populates its data property:

var client = new ArmClient(new DefaultAzureCredential());
var subscriptionIdentifier = SubscriptionResource.CreateResourceIdentifier(_vmSettings.SubscriptionId);
var subscription = client.GetSubscriptionResource(subscriptionIdentifier);
var resourceGroup = subscription.GetResourceGroup(_vmSettings.ResourceGroupName);
var vms = resourceGroup.Value.GetVirtualMachines();
foreach (var vm in vms)
{
    if (vm == null) { continue; }

    var vmRes = vm;
    if (!vmRes.HasData)
    {
        vmRes = vm.Get().Value;
        continue;
    }

    var networkProfile = vmRes.Data.NetworkProfile.NetworkInterfaces.FirstOrDefault();
    if(networkProfile == null) { continue; }

    var networkInterfaceResource = client.GetNetworkInterfaceResource(new ResourceIdentifier(networkProfile.Id));
    var ipAddressRes = networkInterfaceResource.GetNetworkInterfaceIPConfigurations().FirstOrDefault();
    if(ipAddressRes == null) { continue; }

    if (!ipAddressRes.HasData)
    {
        ipAddressRes = ipAddressRes.Get().Value;
    }

    var publicIpAddressRes = client.GetPublicIPAddressResource(new ResourceIdentifier(ipAddressRes.Data.PublicIPAddress.Id));
    if(publicIpAddressRes == null) { continue; }

    if(!publicIpAddressRes.HasData)
    {
        publicIpAddressRes = publicIpAddressRes.Get().Value;
    }

    _vms.Add(new VirtualMachine(vmRes.Data.Name, vmRes.Data.Id, publicIpAddressRes.Data.IPAddress));
}

Upvotes: 0

Kjensen
Kjensen

Reputation: 12374

I found a workaround. Not pretty, but it works.

It assumes you already have a Microsoft.Azure.Management.Compute.Models.VirtualMachine object from something like this:

VirtualMachine vmResult = await computeManagementClient.VirtualMachines.GetAsync(resourceGroupName, vmName, InstanceViewTypes.InstanceView);

Then you can take the first NIC, get the last part of that as an ID:

        var firstNic = vmResult.NetworkProfile.NetworkInterfaces.First();

        var nicNameParts = firstNic.Id.Split('/');
        string networkIntefaceName = nicNameParts.Last();

        using (var client = new NetworkManagementClient(credential))
        {
            client.SubscriptionId = subscriptionId;

            string publicNicId = string.Empty;

            //Query ALL Networkinterfaces in the client, and find the one with the matching NIC-name

            var nic = client.NetworkInterfaces.ListAll().FirstOrDefault(x => x.Name == networkIntefaceName);

            if (nic != null)
            {
                //If we find that, we can now use that to find the ID of the PublicIPAddress for said NIC

                publicNicId = nic.IpConfigurations[0].PublicIPAddress.Id;
                //...And when we have that, we can now query all public IP addresses for that specific public Nic ID
                var publicIp = client.PublicIPAddresses.ListAll().FirstOrDefault(x => x.Id == publicNicId);
                if (publicIp != null)
                {
                    vmInfo.PublicIP = publicIp.IpAddress;
                    Console.WriteLine("  public ip: " + publicIp.IpAddress);
                }
                else
                {
                    Console.WriteLine("  public ip: unknown");
                }
            }
        }

Yes, it is not super elegant, it can be optimized etc - but it works, so that's a start. :)

Upvotes: 5

Related Questions