你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

适用于 .NET 的 Microsoft Azure 计算管理客户端库

Microsoft Azure 计算提供用于托管应用的基础结构。 点击以在云中计算容量并按需缩放。 容器化应用程序, (VM) 部署 Windows 和 Linux 虚拟机,并利用灵活的选项将 VM 迁移到 Azure。 借助对混合环境的全面支持,可以采用所需方式和位置进行部署。 Azure 计算还包括一个全面的标识解决方案,因此你可以获得托管终结点保护和 Active Directory 支持,以帮助保护对本地和云应用的访问。 使用即用即付定价和Azure 混合权益部署出色的应用并节省费用。

此库支持管理 Microsoft Azure 计算资源。

此库遵循 新的 Azure SDK 准则,并提供许多核心功能:

- Support MSAL.NET, Azure.Identity is out of box for supporting MSAL.NET.
- Support [OpenTelemetry](https://opentelemetry.io/) for distributed tracing.
- HTTP pipeline with custom policies.
- Better error-handling.
- Support uniform telemetry across all languages.

入门

安装包

使用 NuGet 安装适用于 .NET 的 Microsoft Azure 计算管理库:

dotnet add package Azure.ResourceManager.Compute

先决条件

对客户端进行身份验证

创建已经过身份验证的客户端的默认选项是使用 DefaultAzureCredential。 由于所有管理 API 都通过同一终结点,因此,若要与资源交互,只需创建一个顶级 ArmClient

若要向 Azure 进行身份验证并创建 ArmClient,请执行以下代码:

using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
ArmClient armClient = new ArmClient(new DefaultAzureCredential());

可在此文档中找到有关 Azure.Identity.DefaultAzureCredential 类的更多文档。

关键概念

在此处找到 Azure .NET SDK 的关键概念

示例

创建可用性集

在创建可用性集之前,我们需要有一个资源组。

ArmClient armClient = new ArmClient(new DefaultAzureCredential());
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
ResourceGroupCollection rgCollection = subscription.GetResourceGroups();
// With the collection, we can create a new resource group with an specific name
string rgName = "myRgName";
AzureLocation location = AzureLocation.WestUS2;
ArmOperation<ResourceGroupResource> lro = await rgCollection.CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(location));
ResourceGroupResource resourceGroup = lro.Value;
AvailabilitySetCollection availabilitySetCollection = resourceGroup.GetAvailabilitySets();
string availabilitySetName = "myAvailabilitySet";
AvailabilitySetData input = new AvailabilitySetData(location);
ArmOperation<AvailabilitySetResource> lro = await availabilitySetCollection.CreateOrUpdateAsync(WaitUntil.Completed, availabilitySetName, input);
AvailabilitySetResource availabilitySet = lro.Value;

获取资源组中的所有可用性集

// First, initialize the ArmClient and get the default subscription
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Now we get a ResourceGroupResource collection for that subscription
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
ResourceGroupCollection rgCollection = subscription.GetResourceGroups();

string rgName = "myRgName";
ResourceGroupResource resourceGroup = await rgCollection.GetAsync(rgName);
// First, we get the availability set collection from the resource group
AvailabilitySetCollection availabilitySetCollection = resourceGroup.GetAvailabilitySets();
// With GetAllAsync(), we can get a list of the availability sets in the collection
AsyncPageable<AvailabilitySetResource> response = availabilitySetCollection.GetAllAsync();
await foreach (AvailabilitySetResource availabilitySet in response)
{
    Console.WriteLine(availabilitySet.Data.Name);
}

更新可用性集

// First, initialize the ArmClient and get the default subscription
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Now we get a ResourceGroupResource collection for that subscription
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
ResourceGroupCollection rgCollection = subscription.GetResourceGroups();

// With the collection, we can create a new resource group with an specific name
string rgName = "myRgName";
ResourceGroupResource resourceGroup = await rgCollection.GetAsync(rgName);
AvailabilitySetCollection availabilitySetCollection = resourceGroup.GetAvailabilitySets();
string availabilitySetName = "myAvailabilitySet";
AvailabilitySetResource availabilitySet = await availabilitySetCollection.GetAsync(availabilitySetName);
// availabilitySet is an AvailabilitySetResource instance created above
AvailabilitySetPatch update = new AvailabilitySetPatch()
{
    PlatformFaultDomainCount = 3
};
AvailabilitySetResource updatedAvailabilitySet = await availabilitySet.UpdateAsync(update);

删除可用性集

// First, initialize the ArmClient and get the default subscription
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Now we get a ResourceGroupResource collection for that subscription
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
ResourceGroupCollection rgCollection = subscription.GetResourceGroups();

// With the collection, we can create a new resource group with an specific name
string rgName = "myRgName";
ResourceGroupResource resourceGroup = await rgCollection.GetAsync(rgName);
AvailabilitySetCollection availabilitySetCollection = resourceGroup.GetAvailabilitySets();
string availabilitySetName = "myAvailabilitySet";
AvailabilitySetResource availabilitySet = await availabilitySetCollection.GetAsync(availabilitySetName);
// delete the availability set
await availabilitySet.DeleteAsync(WaitUntil.Completed);

检查可用性集是否存在

如果只想验证可用性集是否存在,可以使用函数 CheckIfExists

ArmClient armClient = new ArmClient(new DefaultAzureCredential());
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
ResourceGroupCollection rgCollection = subscription.GetResourceGroups();

string rgName = "myRgName";
ResourceGroupResource resourceGroup = await rgCollection.GetAsync(rgName);
string availabilitySetName = "myAvailabilitySet";
bool exists = await resourceGroup.GetAvailabilitySets().ExistsAsync(availabilitySetName);

if (exists)
{
    Console.WriteLine($"Availability Set {availabilitySetName} exists.");
}
else
{
    Console.WriteLine($"Availability Set {availabilitySetName} does not exist.");
}

将标记添加到可用性集

// First, initialize the ArmClient and get the default subscription
ArmClient armClient = new ArmClient(new DefaultAzureCredential());
// Now we get a ResourceGroupResource collection for that subscription
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
ResourceGroupCollection rgCollection = subscription.GetResourceGroups();

string rgName = "myRgName";
ResourceGroupResource resourceGroup = await rgCollection.GetAsync(rgName);
AvailabilitySetCollection availabilitySetCollection = resourceGroup.GetAvailabilitySets();
string availabilitySetName = "myAvailabilitySet";
AvailabilitySetResource availabilitySet = await availabilitySetCollection.GetAsync(availabilitySetName);
// add a tag on this availabilitySet
AvailabilitySetResource updatedAvailabilitySet = await availabilitySet.AddTagAsync("key", "value");

有关更详细的示例,请查看我们提供的示例

疑难解答

后续步骤

更多示例代码

更多文档

有关 Azure SDK 的详细信息,请参阅 此网站

贡献

有关参与此存储库的详细信息,请参阅 参与指南

本项目欢迎贡献和建议。 大多数贡献要求你同意贡献者许可协议 (CLA),并声明你有权(并且确实有权)授予我们使用你的贡献的权利。 有关详细信息,请访问 https://cla.microsoft.com

提交拉取请求时,CLA 机器人会自动确定是否需要提供 CLA 并适当修饰 PR, (例如标签、注释) 。 按照机器人提供的说明操作。 只需使用 CLA 在所有存储库中执行此操作一次。

此项目采用了 Microsoft 开放源代码行为准则。 有关详细信息,请参阅行为准则常见问题解答,如有任何其他问题或评论,请联系 opencode@microsoft.com