2016-11-21 48 views
0

我已经使用Resource Manager部署模型部署了一个虚拟机。使用资源管理器部署和休息API获取Azure VM

Using rest api as described here我能够获得有关虚拟机的信息。
我期待获得电源状态,IP地址和机器大小。然而,为了获得所有的信息,我需要3个不同的电话 https://management.azure.com/subscriptions/ {} SubscriptionId/resourceGroups/{}资源组{/providers/Microsoft.Compute/virtualmachines/服务器名}

https://management.azure.com/subscriptions/ {} SubscriptionId/resourceGroups/{}资源组/供应商/Microsoft.Compute/virtualmachines/{ServerName}/InstanceView

https://management.azure.com/subscriptions/ {} SubscriptionId/resourceGroups/{}资源组{/providers/Microsoft.Network/networkInterfaces/服务器名} _NIC

有没有办法让所有这个信息在1个电话中?

回答

1

由于使用资源管理器部署VM,因此在不同提供者(计算和网络)下的状态,IP地址和大小信息。当前可能无法在通话中获取虚拟机信息和网络信息。

随着Microsoft Azure Management Client Library (Fluent),我们可以得到VM信息(电源状态,机器大小,IP地址)。实际上,它调用REST API 两次。关于Azure认证请参考how to create an authentication file

AzureCredentials credentials = AzureCredentials.FromFile("Full path of your AzureAuthFile"); 
       var azure = Azure 
        .Configure() 
        .WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC) 
        .Authenticate(credentials) 
        .WithDefaultSubscription(); 
    foreach (var virtualMachine in azure.VirtualMachines.ListByGroup("Your Resource Group Name").Where(virtualMachine => virtualMachine.ComputerName.Equals("vmName"))) 
        { 
         var state = virtualMachine.PowerState; 
         var size = virtualMachine.Size; 
         var ip = virtualMachine.GetPrimaryPublicIpAddress().IpAddress; //call Rest API again 
        } 

如果它部署在CloudService下,那么我们可以使用Windows Azure management library。很容易获得虚拟机(角色) 有关电源状态,IP地址和机器大小的信息。

var certificate = new CertificateCloudCredentials(subscriptionId, x509Certificate); 
var computeManagementClient = new ComputeManagementClient(certificate); 
var deployments = await computeManagementClient.Deployments.GetByNameAsync (hostedServiceName,"Your Deployment Name"); 
var state = deployments.RoleInstances.First().PowerState; 
var ipAddress = deployments.RoleInstances.First().IPAddress; 
var size = deployments.RoleInstances.First().InstanceSize; 
相关问题