【问题标题】:Azure resource management API not showing virtual machine state?Azure 资源管理 API 未显示虚拟机状态?
【发布时间】:2016-05-30 23:31:10
【问题描述】:

因此,我一直在使用资源管理 api 对 azure 进行只读 api 访问。现在我专注于虚拟机。我一直在使用这个带有 TokenCredentials 的预发布包:

https://www.nuget.org/packages/Microsoft.Azure.Management.Compute/13.0.1-prerelease

我得到了一堆关于我的虚拟机的丰富信息,但我错过了一个非常关键的数据,那就是虚拟机是打开还是关闭。我发现一些元数据属性(如 InstanceView 和 Plan)在我期望它们被填充时为空。这可能是因为我如何启动我的虚拟机,它可能是一个未完成或有问题的新包,我无法判断。我在想 InstanceViews 雕像会告诉我 vm 处于什么状态。

https://msdn.microsoft.com/en-us/library/microsoft.azure.management.compute.models.virtualmachineinstanceview.aspx

所以我想我必须去别处看看。我确实发现了这个旧的 stackoverflow 问题,这可能是我正在寻找的:

azure management libraries virtual machine state

但是我不确定这个 GetAzureDeyployment 是哪个 dll 的一部分,或者它是否与 TokenCredential 兼容。有谁知道怎么回事?

【问题讨论】:

  • GetAzureDeyployment 适用于 ASM,而您使用的是 ARM。因此,堆栈溢出问题不适合您的情况。

标签: c# api azure


【解决方案1】:

您可以使用以下 c# 代码来获取 VM 的电源状态。

using System;
using System.Security;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest;

namespace GetVmARM
{
    class Program
    {

        private static String tenantID = "<your tenant id>";
        private static String loginEndpoint = "https://login.windows.net/";
        private static Uri redirectURI = new Uri("urn:ietf:wg:oauth:2.0:oob");
        private static String clientID = "1950a258-227b-4e31-a9cf-717495945fc2";
        private static String subscriptionID = "<your subscription id>";
        private static String resource = "https://management.core.windows.net/";



        static void Main(string[] args)
        {
            var token = GetTokenCloudCredentials();
            var credential = new TokenCredentials(token);
            var computeManagementClient = new ComputeManagementClient(credential);
            computeManagementClient.SubscriptionId = subscriptionID;

            InstanceViewTypes expand = new InstanceViewTypes();

            var vm = computeManagementClient.VirtualMachines.Get("<the resource group name>", "<the VM>", expand);

            System.Console.WriteLine(vm.InstanceView.Statuses[1].Code);
            System.Console.WriteLine("Press ENTER to continue");
            System.Console.ReadLine();
        }

        public static String GetTokenCloudCredentials(string username = null, SecureString password = null)
        {
            String authString = loginEndpoint + tenantID;

            AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);

            var promptBehaviour = PromptBehavior.Auto;

            var userIdentifierType = UserIdentifierType.RequiredDisplayableId;

            var userIdentifier = new UserIdentifier("<your azure account>", userIdentifierType);

            var authenticationResult = authenticationContext.AcquireToken(resource, clientID, redirectURI, promptBehaviour, userIdentifier);

            return authenticationResult.AccessToken;
        }

    }
}

正如您在这段代码中看到的,我使用的是文档中没有的 InstanceViewTypes。这是 13.0.1 预发布版本中的新功能。但是,是的,如果您将其添加到您的 computeManagementClient.VirtualMachines.Get 方法中,您将能够为您的 VM 获取额外信息。

此外,我使用 vm.InstanceView.Statuses[1] 因为 vm.InstanceView.Statuses[0] 是 ProvisioningState。而且,我不确定顺序是否总是这样,所以您可能需要遍历整个状态列表。

【讨论】:

  • 就是这样!非常感谢。这对微软来说是非常奇怪的用法。为什么 Get 填充状态而不填充列表?它在两种情况下都使用相同的类,所以你会假设它是相同的。嗯嗯嗯。
猜你喜欢
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-03
相关资源
最近更新 更多