SuperAPI AI 工具集

azure-mgmt-mongodbatlas-dotnet

部署与运维

查看项目

更新于 10-16 02:58

技能介绍

用途

Azure.ResourceManager.MongoDBAtlas SDK 用于将 MongoDB Atlas 组织(Organization)作为 Azure ARM 资源进行管理,并通过 Azure Marketplace 实现统一计费。它面向需要在 Azure 资源管理体系中创建、查询、更新和删除 MongoDB Atlas 组织的 .NET 开发者。

主要能力

  • 创建 MongoDB Atlas 组织(长时间运行操作)
  • 获取已有组织(通过集合或资源标识符)
  • 在资源组内或跨订阅列出组织
  • 更新标签(添加、替换、删除单个标签)
  • 更新组织属性(如用户信息)
  • 删除组织(长时间运行操作)
  • 查询预配状态(ProvisioningState)与 Marketplace 订阅状态

安装

dotnet add package Azure.ResourceManager.MongoDBAtlas
dotnet add package Azure.Identity
dotnet add package Azure.ResourceManager

认证

使用 Azure.Identity 的 DefaultAzureCredential 创建 ArmClient:

using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.MongoDBAtlas;
using Azure.ResourceManager.MongoDBAtlas.Models;

var credential = new DefaultAzureCredential();
var armClient = new ArmClient(credential);

核心类型

  • MongoDBAtlasOrganizationResource:表示 Atlas 组织的 ARM 资源
  • MongoDBAtlasOrganizationCollection:资源组中的组织集合
  • MongoDBAtlasOrganizationData:组织资源的数据模型
  • MongoDBAtlasOrganizationProperties:组织专有属性
  • MongoDBAtlasMarketplaceDetails:Azure Marketplace 订阅详情
  • MongoDBAtlasOfferDetails:Marketplace 优惠配置
  • MongoDBAtlasUserDetails:组织用户信息
  • MongoDBAtlasPartnerProperties:MongoDB 专有属性(组织名称、ID)

使用方式

获取组织集合

var subscription = await armClient.GetDefaultSubscriptionAsync();
var resourceGroup = await subscription.GetResourceGroupAsync("my-resource-group");
MongoDBAtlasOrganizationCollection organizations = 
    resourceGroup.Value.GetMongoDBAtlasOrganizations();

创建组织

var organizationName = "my-atlas-org";
var location = AzureLocation.EastUS2;

var organizationData = new MongoDBAtlasOrganizationData(location)
{
    Properties = new MongoDBAtlasOrganizationProperties(
        marketplace: new MongoDBAtlasMarketplaceDetails(
            subscriptionId: "your-azure-subscription-id",
            offerDetails: new MongoDBAtlasOfferDetails(
                publisherId: "mongodb",
                offerId: "mongodb_atlas_azure_native_prod",
                planId: "private_plan",
                planName: "Pay as You Go (Free) (Private)",
                termUnit: "P1M",
                termId: "gmz7xq9ge3py"
            )
        ),
        user: new MongoDBAtlasUserDetails(
            emailAddress: "admin@example.com",
            upn: "admin@example.com"
        )
        {
            FirstName = "Admin",
            LastName = "User"
        }
    )
    {
        PartnerProperties = new MongoDBAtlasPartnerProperties
        {
            OrganizationName = organizationName
        }
    },
    Tags = { ["Environment"] = "Production" }
};

var operation = await organizations.CreateOrUpdateAsync(
    WaitUntil.Completed,
    organizationName,
    organizationData
);

MongoDBAtlasOrganizationResource organization = operation.Value;
Console.WriteLine($"Created: {organization.Id}");

获取已有组织

// 方式一:从集合获取
MongoDBAtlasOrganizationResource org = 
    await organizations.GetAsync("my-atlas-org");

// 方式二:从资源标识符获取
var resourceId = MongoDBAtlasOrganizationResource.CreateResourceIdentifier(
    subscriptionId: "subscription-id",
    resourceGroupName: "my-resource-group",
    organizationName: "my-atlas-org"
);
MongoDBAtlasOrganizationResource org2 = 
    armClient.GetMongoDBAtlasOrganizationResource(resourceId);
await org2.GetAsync();

列出组织

// 资源组内列出
await foreach (var org in organizations.GetAllAsync())
{
    Console.WriteLine($"Org: {org.Data.Name}");
    Console.WriteLine($"  Location: {org.Data.Location}");
    Console.WriteLine($"  State: {org.Data.Properties?.ProvisioningState}");
}

// 跨订阅列出
await foreach (var org in subscription.GetMongoDBAtlasOrganizationsAsync())
{
    Console.WriteLine($"Org: {org.Data.Name} in {org.Data.Id}");
}

更新标签

await organization.AddTagAsync("CostCenter", "12345");

await organization.SetTagsAsync(new Dictionary<string, string>
{
    ["Environment"] = "Production",
    ["Team"] = "Platform"
});

await organization.RemoveTagAsync("OldTag");

更新组织属性

var patch = new MongoDBAtlasOrganizationPatch
{
    Tags = { ["UpdatedAt"] = DateTime.UtcNow.ToString("o") },
    Properties = new MongoDBAtlasOrganizationUpdateProperties
    {
        User = new MongoDBAtlasUserDetails(
            emailAddress: "newadmin@example.com",
            upn: "newadmin@example.com"
        )
    }
};

var updateOperation = await organization.UpdateAsync(
    WaitUntil.Completed,
    patch
);

删除组织

await organization.DeleteAsync(WaitUntil.Completed);

模型属性参考

MongoDBAtlasOrganizationProperties:Marketplace(必填,Marketplace 订阅详情)、User(必填,组织管理员用户)、PartnerProperties(MongoDB 专有属性)、ProvisioningState(只读,当前预配状态)。

MongoDBAtlasMarketplaceDetails:SubscriptionId(必填,用于计费的 Azure 订阅 ID)、OfferDetails(必填,Marketplace 优惠配置)、SubscriptionStatus(只读,订阅状态)。

MongoDBAtlasOfferDetails:PublisherId(必填,发布者 ID,通常为 "mongodb")、OfferId(必填)、PlanId(必填)、PlanName(必填,计划显示名称)、TermUnit(必填,计费周期单位,如 "P1M")、TermId(必填)。

MongoDBAtlasUserDetails:EmailAddress(必填)、Upn(必填)、FirstName(可选)、LastName(可选)。

MongoDBAtlasPartnerProperties:OrganizationName(MongoDB Atlas 组织名称)、OrganizationId(只读,MongoDB Atlas 组织 ID)。

预配状态

Succeeded(预配成功)、Failed(预配失败)、Canceled(预配已取消)、Provisioning(正在预配)、Updating(正在更新)、Deleting(正在删除)、Accepted(请求已接受,预配即将开始)。

Marketplace 订阅状态

PendingFulfillmentStart(订阅待激活)、Subscribed(订阅有效)、Suspended(订阅已暂停)、Unsubscribed(订阅已取消)。

最佳实践

  • 优先使用异步方法。
  • 处理长时间运行操作:可使用 WaitUntil.Completed 阻塞等待,或使用 WaitUntil.Started 立即返回后轮询(通过 operation.HasCompleted 与 operation.UpdateStatusAsync())。
  • 检查预配状态是否为 Succeeded。
  • 使用资源标识符:CreateResourceIdentifier 可在不发起 API 调用的情况下创建标识符,需要数据时再调用 GetAsync()。

常见错误

  • ResourceNotFound:组织不存在,检查名称与资源组。
  • AuthorizationFailed:权限不足,检查资源组上的 RBAC 角色。
  • InvalidParameter:缺少必填属性,确保所有必填字段已设置。
  • MarketplaceError:Marketplace 订阅问题,验证优惠详情与订阅。

适用场景

适用于需要在 Azure 中通过 ARM 资源方式创建和管理 MongoDB Atlas 组织、并借助 Azure Marketplace 统一计费的 .NET 应用或自动化流程。

限制

该 SDK 仅管理作为 Azure ARM 资源的 MongoDB Atlas 组织,用于 Marketplace 集成。它不直接管理 Atlas 集群、数据库、集合、用户/角色;如需集群管理,请在创建组织后直接使用 MongoDB Atlas API。此外,该技能仅应在任务明确匹配上述范围时使用,不应将输出视为环境特定验证、测试或专家审查的替代;若缺少必要输入、权限、安全边界或成功标准,应停止并请求澄清。