如何使用Azure SDK获取Azure VM的公共IP

时间:2017-04-18 13:52:05

标签: c# azure azure-sdk

对于特定的VM,我希望能够检索公共IP地址。

我知道如何获取资源组的所有公共IP地址,我也知道如何获取特定VM的nic-id - 但我无法弄清楚如何连接这两个。

这就是我所拥有的:

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);
        }    

这给了我这样的东西:

  

/订阅/ [GUID] /resourceGroups/My-Resource-Group/providers/Microsoft.Network/networkInterfaces/myvm123

要获取资源组的所有公共IP,我可以这样做:

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

...但是检查nic-id和public ip对象的属性,没有明显的方法可以从一个到另一个。

问题:

如何从nic-id字符串获取该VM / nic的实际公共IP地址?

辅助功能:

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;
    }

1 个答案:

答案 0 :(得分:1)

我找到了解决方法。不漂亮,但它确实有效。

它假设您已经有类似这样的Microsoft.Azure.Management.Compute.Models.VirtualMachine对象:

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

然后你可以拿第一个NIC,把它的最后一部分作为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");
                }
            }
        }

是的,它不是超级优雅,它可以优化等 - 但它的工作原理,所以这是一个开始。 :)