以编程方式获取 azure 虚拟机大小列表
Programmatically getting the list of azure virtual machine sizes
我是 .net 的 Azure 管理库的新手。我们如何枚举与订阅有关的可用 VM 实例大小,或者通常使用 .Net 或 Rest API 的 Azure 管理库?
请提出建议。
您可以通过调用
获取区域的 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 Class,但我没有找到任何使用它的示例 - 记录在此处 - VirtualMachineSizeOperationsExtensions.List
您可以按区域筛选器获取 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
您可以使用证书库身份验证
获取 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();
我是 .net 的 Azure 管理库的新手。我们如何枚举与订阅有关的可用 VM 实例大小,或者通常使用 .Net 或 Rest API 的 Azure 管理库? 请提出建议。
您可以通过调用
获取区域的 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 Class,但我没有找到任何使用它的示例 - 记录在此处 - VirtualMachineSizeOperationsExtensions.List
您可以按区域筛选器获取 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
您可以使用证书库身份验证
获取 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();