【问题标题】:Programmatically getting the list of azure virtual machine sizes以编程方式获取 azure 虚拟机大小列表
【发布时间】:2016-07-22 15:55:34
【问题描述】:

我是 .net 的 Azure 管理库的新手。我们如何枚举与订阅相关的可用 VM 实例大小,或者通常使用 .Net 或 Rest API 的 Azure 管理库? 请提出建议。

【问题讨论】:

    标签: azure azure-management-api


    【解决方案1】:

    您可以通过调用获取区域的 VM 大小列表

    https://management.azure.com/subscriptions/{subscription-id}/providers/Microsoft.Compute/locations/{location}/vmSizes?api-version={api-version}
    

    如此处所述 - List all available virtual machine sizes in a region

    同样还有一个 .Net 类,但我没有找到任何使用它的例子 - 记录在这里 - VirtualMachineSizeOperationsExtensions.List

    【讨论】:

    【解决方案2】:

    您可以按区域填充器获取 VM 大小列表

    AuthenticationContext authenticationContext = new AuthenticationContext("https://login.windows.net/tenantdomainname.onmicrosoft.com"]);
    UserCredential uc = new UserCredential(authusername,authpassword);
    token = authenticationContext.AcquireToken("https://management.core.windows.net/", nativetenantid, uc);
    
    var credentials = new TokenCredentials(token);
    var computeClient = new ComputeManagementClient(credentials) { SubscriptionId = subscriptionid};
    var virtualMachineSize = computeClient.VirtualMachineSizes.List(region_name).ToList();
    

    您必须在 Azure Active Directory 上创建一个本机客户端 API 以进行基于令牌的身份验证,否则您也可以使用基于证书的身份验证进行客户端授权。

    我正在使用 Microsoft.Azure.Management.Compute.dll, v10.0.0.0 作为计算资源。

    你可以在这里下载:https://www.nuget.org/packages/Microsoft.Azure.Management.Compute/13.0.4-prerelease

    【讨论】:

    • 您能否介绍一下基于证书的身份验证?
    • 这是我基于证书示例的另一个答案
    • 谢谢,似乎需要先上传 AD 应用的证书。是通过powershell完成的还是还有其他方式?
    • 任何类型的SubscriptionCloudCredentials 对象都可以使用。 -- 即你也可以使用用户名/密码,使用TokenCloudCredentials
    【解决方案3】:

    您可以使用 Certificate Base Authentication 获取 VM 大小列表

    获取证书方法

    private static X509Certificate2 GetStoreCertificate(string subscriptionId, string thumbprint)
        {
            List<StoreLocation> locations = new List<StoreLocation>
            { 
                StoreLocation.CurrentUser, 
                StoreLocation.LocalMachine
            };
    
            foreach (var location in locations)
            {
                X509Store store = new X509Store(StoreName.My, location);
                try
                {
                    store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
                    X509Certificate2Collection certificates = store.Certificates.Find(
                    X509FindType.FindByThumbprint, thumbprint, false);
                    if (certificates.Count == 1)
                    {
                        return certificates[0];
                    }
                }
                finally
                {
                    store.Close();
                }
            }
    
            throw new ArgumentException(string.Format("A Certificate with Thumbprint '{0}' could not be located.",thumbprint));
        }
    

    这里我描述了获取 VM 大小的相同方法

    private static X509Certificate2 Certificate = GetStoreCertificate(Your-subscriptionid,Your-thumbprint);
    Microsoft.Azure.CertificateCloudCredentials credentials = new Microsoft.Azure.CertificateCloudCredentials(Your-subscriptionid, Certificate);
    
     var computeClient = new ComputeManagementClient(credentials) { SubscriptionId = Your-subscriptionid};
     var virtualMachineSize = computeClient.VirtualMachineSizes.List(Your-region_name).ToList();
    

    【讨论】:

      猜你喜欢
      • 2014-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 2020-10-24
      相关资源
      最近更新 更多