Azure.ResourceManager.MongoDBAtlas SDK
通过 Azure 市场,将 MongoDB Atlas 组织作为 Azure ARM 资源进行管理,并实现统一计费。
软件包信息
| 属性 |
值 |
| 软件包 |
Azure.ResourceManager.MongoDBAtlas |
| 版本 |
1.0.0 (GA) |
| API 版本 |
2025-06-01 |
| 资源类型 |
MongoDB.Atlas/organizations |
| NuGet |
Azure.ResourceManager.MongoDBAtlas |
安装
dotnet add package Azure.ResourceManager.MongoDBAtlas
dotnet add package Azure.Identity
dotnet add package Azure.ResourceManager
重要范围限制
此 SDK 将MongoDB Atlas 组织作为 Azure ARM 资源进行管理,以实现与市场(Marketplace)的集成。它并不直接管理:
如需管理集群,请在创建组织后直接使用 MongoDB Atlas API。
环境变量
AZURE_SUBSCRIPTION_ID= # 必填:Azure 订阅 ID
AZURE_RESOURCE_GROUP= # 必填:Azure 资源组名称
AZURE_TOKEN_CREDENTIALS=prod # 仅当在生产环境中使用 DefaultAzureCredential 时才必填
AZURE_TENANT_ID= # 用于服务主体身份验证(可选)
AZURE_CLIENT_ID= # 用于服务主体身份验证(可选)
AZURE_CLIENT_SECRET= # 用于服务主体身份验证(可选)
身份验证
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.MongoDBAtlas;
using Azure.ResourceManager.MongoDBAtlas.Models;
// 本地开发:DefaultAzureCredential。 生产环境:设置 AZURE_TOKEN_CREDENTIALS=prod 或 AZURE_TOKEN_CREDENTIALS=
var credential = new DefaultAzureCredential(
DefaultAzureCredential.DefaultEnvironmentVariableName
);
// 或者在生产环境中直接使用特定的凭据:
// 参见 https://learn.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#credential-classes
// var credential = new ManagedIdentityCredential();
var armClient = new ArmClient(credential);
核心类型
| 类型 |
用途 |
MongoDBAtlasOrganizationResource |
表示 Atlas 组织的 ARM 资源 |
MongoDBAtlasOrganizationCollection |
资源组中的组织集合 |
MongoDBAtlasOrganizationData |
组织资源的数据模型 |
MongoDBAtlasOrganizationProperties |
组织特有的属性 |
MongoDBAtlasMarketplaceDetails |
Azure Marketplace 订阅详细信息 |
MongoDBAtlasOfferDetails |
市场产品配置 |
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: "按量付费(免费)(私有)",
termUnit: "P1M",
termId: "gmz7xq9ge3py"
)
),
user: new MongoDBAtlasUserDetails(
emailAddress: "[email protected]",
upn: "[email protected]"
)
{
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($"已创建:{organization.Id}");
获取现有组织
// 选项 1:从集合获取
MongoDBAtlasOrganizationResource org =
await organizations.GetAsync("my-atlas-org");
// 选项 2:通过资源标识符获取
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.Data.Name}");
Console.WriteLine($" 位置:{org.Data.Location}");
Console.WriteLine($" 状态:{org.Data.Properties?.ProvisioningState}");
}
// 列出所有订阅中的组织
await foreach (var org in subscription.GetMongoDBAtlasOrganizationsAsync())
{
Console.WriteLine($"组织:{org.Data.Name},位于 {org.Data.Id}");
}
更新标签
// 添加单个标签
await organization.AddTagAsync("CostCenter", "12345");
// 替换所有标签
await organization.SetTagsAsync(new Dictionary{
["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: "[email protected]",
upn: "[email protected]"
)
}
};
var updateOperation = await organization.UpdateAsync(
WaitUntil.Completed,
patch
);
删除组织
// 删除(长时间运行的操作)
await organization.DeleteAsync(WaitUntil.Completed);
模型属性参考
MongoDBAtlasOrganizationProperties
| 属性 |
类型 |
描述 |
市场 |
MongoDBAtlasMarketplaceDetails |
必填。市场订阅详情 |
用户 |
MongoDBAtlas用户详情 |
必填。组织管理员用户 |
合作伙伴属性 |
MongoDBAtlasPartnerProperties |
MongoDB 特定属性 |
配置状态 |
MongoDBAtlasResourceProvisioningState |
只读。当前配置状态 |
MongoDBAtlasMarketplaceDetails
| 属性 |
类型 |
描述 |
SubscriptionId |
字符串 |
必填。用于计费的 Azure 订阅 ID |
优惠详情 |
MongoDBAtlasOfferDetails |
必填。Marketplace 产品配置 |
订阅状态 |
MarketplaceSubscriptionStatus |
只读。订阅状态 |
MongoDBAtlasOfferDetails
| 属性 |
类型 |
描述 |
发布者ID |
字符串 |
必填。发布者 ID(通常为“mongodb”) |
OfferId |
字符串 |
必填。优惠 ID |
PlanId |
字符串 |
必填。方案 ID |
PlanName |
字符串 |
必填。计划的显示名称 |
TermUnit |
字符串 |
必填。计费周期单位(例如,“P1M”) |
TermId |
字符串 |
必填。计费周期标识符 |
MongoDBAtlasUserDetails
| 属性 |
类型 |
描述 |
EmailAddress |
字符串 |
必填。用户电子邮件地址 |
UPN |
字符串 |
必填。用户主体名称 |
FirstName |
字符串 |
可选。用户名 |
LastName |
字符串 |
可选。用户的姓氏 |
MongoDBAtlasPartnerProperties
| 属性 |
类型 |
描述 |
组织名称 |
字符串 |
MongoDB Atlas 组织的名称 |
组织ID |
字符串 |
只读。MongoDB Atlas 组织 ID |
配置状态
| 状态 |
描述 |
成功 |
资源已成功配置 |
失败 |
配置失败 |
已取消 |
配置已被取消 |
正在配置 |
正在为资源进行配置 |
正在更新 |
正在更新资源 |
正在删除 |
正在删除资源 |
已接受 |
请求已接受,正在开始配置 |
市场订阅状态
| 状态 |
描述 |
待履行开始 |
订阅待激活 |
已订阅 |
订阅已激活 |
已暂停 |
订阅已暂停 |
已取消订阅 |
订阅已取消 |
最佳实践
使用异步方法
// 建议对所有操作都使用异步方式
var org = await organizations.GetAsync("my-org");
await org.Value.AddTagAsync("key", "value");
处理长时间运行的操作
// 等待操作完成
var operation = await organizations.CreateOrUpdateAsync(
WaitUntil.Completed, // 阻塞直至操作完成
name,
data
);
// 或先启动,稍后轮询
var operation = await organizations.CreateOrUpdateAsync(
WaitUntil.Completed, // 立即返回
name,
data
);
// 轮询是否完成
while (!operation.HasCompleted)
{
await Task.Delay(TimeSpan.FromSeconds(5));
await operation.UpdateStatusAsync();
}
检查配置状态
var org = await organizations.GetAsync("my-org");
if (org.Value.Data.Properties?.ProvisioningState ==
MongoDBAtlasResourceProvisioningState.Succeeded)
{
Console.WriteLine("组织已就绪");
}
使用资源标识符
// 无需调用 API 即可创建标识符
var resourceId = MongoDBAtlasOrganizationResource.CreateResourceIdentifier(
subscriptionId,
resourceGroupName,
organizationName
);
// 获取资源句柄(目前尚无数据)
var orgResource = armClient.GetMongoDBAtlasOrganizationResource(resourceId);
// 需要时获取数据
var response = await orgResource.GetAsync();
常见错误
| 错误 |
原因 |
解决方案 |
ResourceNotFound |
组织不存在 |
请验证名称和资源组 |
授权失败 |
权限不足 |
请检查资源组上的 RBAC 角色 |
参数无效 |
缺少必填属性 |
请确保所有必填字段均已设置 |
MarketplaceError |
Marketplace 订阅问题 |
请核实优惠详情和订阅信息 |
相关资源
- Microsoft Learn:Azure 上的 MongoDB Atlas
- API 参考
- Azure .NET SDK
在 GitHub 上查看
---
name: azure-mgmt-mongodbatlas-dotnet
description: Manage MongoDB Atlas Organizations as Azure ARM resources with unified billing through Azure Marketplace. Create, update, list, or delete Atlas organizations using the Azure.ResourceManager.MongoDBAtlas SDK.
license: MIT
---
# Azure.ResourceManager.MongoDBAtlas SDK
Manage MongoDB Atlas Organizations as Azure ARM resources with unified billing through Azure Marketplace.
## Package Information
| Property | Value |
|----------|-------|
| Package | `Azure.ResourceManager.MongoDBAtlas` |
| Version | 1.0.0 (GA) |
| API Version | 2025-06-01 |
| Resource Type | `MongoDB.Atlas/organizations` |
| NuGet | [Azure.ResourceManager.MongoDBAtlas](https://www.nuget.org/packages/Azure.ResourceManager.MongoDBAtlas) |
## Installation
```bash
dotnet add package Azure.ResourceManager.MongoDBAtlas
dotnet add package Azure.Identity
dotnet add package Azure.ResourceManager
```
## Important Scope Limitation
This SDK manages **MongoDB Atlas Organizations as Azure ARM resources** for marketplace integration. It does NOT directly manage:
- Atlas clusters
- Databases
- Collections
- Users/roles
For cluster management, use the MongoDB Atlas API directly after creating the organization.
## Environment Variables
```bash
AZURE_SUBSCRIPTION_ID=<your-subscription-id> # Required: Azure subscription ID
AZURE_RESOURCE_GROUP=<your-resource-group> # Required: Azure resource group name
AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production
AZURE_TENANT_ID=<your-tenant-id> # For service principal auth (optional)
AZURE_CLIENT_ID=<your-client-id> # For service principal auth (optional)
AZURE_CLIENT_SECRET=<your-client-secret> # For service principal auth (optional)
```
## Authentication
```csharp
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.MongoDBAtlas;
using Azure.ResourceManager.MongoDBAtlas.Models;
// Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>
var credential = new DefaultAzureCredential(
DefaultAzureCredential.DefaultEnvironmentVariableName
);
// Or use a specific credential directly in production:
// See https://learn.microsoft.com/dotnet/api/overview/azure/identity-readme?view=azure-dotnet#credential-classes
// var credential = new ManagedIdentityCredential();
var armClient = new ArmClient(credential);
```
## Core Types
| Type | Purpose |
|------|---------|
| `MongoDBAtlasOrganizationResource` | ARM resource representing an Atlas organization |
| `MongoDBAtlasOrganizationCollection` | Collection of organizations in a resource group |
| `MongoDBAtlasOrganizationData` | Data model for organization resource |
| `MongoDBAtlasOrganizationProperties` | Organization-specific properties |
| `MongoDBAtlasMarketplaceDetails` | Azure Marketplace subscription details |
| `MongoDBAtlasOfferDetails` | Marketplace offer configuration |
| `MongoDBAtlasUserDetails` | User information for the organization |
| `MongoDBAtlasPartnerProperties` | MongoDB-specific properties (org name, ID) |
## Workflows
### Get Organization Collection
```csharp
// Get resource group
var subscription = await armClient.GetDefaultSubscriptionAsync();
var resourceGroup = await subscription.GetResourceGroupAsync("my-resource-group");
// Get organizations collection
MongoDBAtlasOrganizationCollection organizations =
resourceGroup.Value.GetMongoDBAtlasOrganizations();
```
### Create Organization
```csharp
var organizationName = "my-atlas-org";
var location = AzureLocation.EastUS2;
// Build organization data
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: "[email protected]",
upn: "[email protected]"
)
{
FirstName = "Admin",
LastName = "User"
}
)
{
PartnerProperties = new MongoDBAtlasPartnerProperties
{
OrganizationName = organizationName
}
},
Tags = { ["Environment"] = "Production" }
};
// Create the organization (long-running operation)
var operation = await organizations.CreateOrUpdateAsync(
WaitUntil.Completed,
organizationName,
organizationData
);
MongoDBAtlasOrganizationResource organization = operation.Value;
Console.WriteLine($"Created: {organization.Id}");
```
### Get Existing Organization
```csharp
// Option 1: From collection
MongoDBAtlasOrganizationResource org =
await organizations.GetAsync("my-atlas-org");
// Option 2: From resource identifier
var resourceId = MongoDBAtlasOrganizationResource.CreateResourceIdentifier(
subscriptionId: "subscription-id",
resourceGroupName: "my-resource-group",
organizationName: "my-atlas-org"
);
MongoDBAtlasOrganizationResource org2 =
armClient.GetMongoDBAtlasOrganizationResource(resourceId);
await org2.GetAsync(); // Fetch data
```
### List Organizations
```csharp
// List in resource group
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}");
}
// List across subscription
await foreach (var org in subscription.GetMongoDBAtlasOrganizationsAsync())
{
Console.WriteLine($"Org: {org.Data.Name} in {org.Data.Id}");
}
```
### Update Tags
```csharp
// Add a single tag
await organization.AddTagAsync("CostCenter", "12345");
// Replace all tags
await organization.SetTagsAsync(new Dictionary<string, string>
{
["Environment"] = "Production",
["Team"] = "Platform"
});
// Remove a tag
await organization.RemoveTagAsync("OldTag");
```
### Update Organization Properties
```csharp
var patch = new MongoDBAtlasOrganizationPatch
{
Tags = { ["UpdatedAt"] = DateTime.UtcNow.ToString("o") },
Properties = new MongoDBAtlasOrganizationUpdateProperties
{
// Update user details if needed
User = new MongoDBAtlasUserDetails(
emailAddress: "[email protected]",
upn: "[email protected]"
)
}
};
var updateOperation = await organization.UpdateAsync(
WaitUntil.Completed,
patch
);
```
### Delete Organization
```csharp
// Delete (long-running operation)
await organization.DeleteAsync(WaitUntil.Completed);
```
## Model Properties Reference
### MongoDBAtlasOrganizationProperties
| Property | Type | Description |
|----------|------|-------------|
| `Marketplace` | `MongoDBAtlasMarketplaceDetails` | Required. Marketplace subscription details |
| `User` | `MongoDBAtlasUserDetails` | Required. Organization admin user |
| `PartnerProperties` | `MongoDBAtlasPartnerProperties` | MongoDB-specific properties |
| `ProvisioningState` | `MongoDBAtlasResourceProvisioningState` | Read-only. Current provisioning state |
### MongoDBAtlasMarketplaceDetails
| Property | Type | Description |
|----------|------|-------------|
| `SubscriptionId` | `string` | Required. Azure subscription ID for billing |
| `OfferDetails` | `MongoDBAtlasOfferDetails` | Required. Marketplace offer configuration |
| `SubscriptionStatus` | `MarketplaceSubscriptionStatus` | Read-only. Subscription status |
### MongoDBAtlasOfferDetails
| Property | Type | Description |
|----------|------|-------------|
| `PublisherId` | `string` | Required. Publisher ID (typically "mongodb") |
| `OfferId` | `string` | Required. Offer ID |
| `PlanId` | `string` | Required. Plan ID |
| `PlanName` | `string` | Required. Display name of the plan |
| `TermUnit` | `string` | Required. Billing term unit (e.g., "P1M") |
| `TermId` | `string` | Required. Term identifier |
### MongoDBAtlasUserDetails
| Property | Type | Description |
|----------|------|-------------|
| `EmailAddress` | `string` | Required. User email address |
| `Upn` | `string` | Required. User principal name |
| `FirstName` | `string` | Optional. User first name |
| `LastName` | `string` | Optional. User last name |
### MongoDBAtlasPartnerProperties
| Property | Type | Description |
|----------|------|-------------|
| `OrganizationName` | `string` | Name of the MongoDB Atlas organization |
| `OrganizationId` | `string` | Read-only. MongoDB Atlas organization ID |
## Provisioning States
| State | Description |
|-------|-------------|
| `Succeeded` | Resource provisioned successfully |
| `Failed` | Provisioning failed |
| `Canceled` | Provisioning was canceled |
| `Provisioning` | Resource is being provisioned |
| `Updating` | Resource is being updated |
| `Deleting` | Resource is being deleted |
| `Accepted` | Request accepted, provisioning starting |
## Marketplace Subscription Status
| Status | Description |
|--------|-------------|
| `PendingFulfillmentStart` | Subscription pending activation |
| `Subscribed` | Active subscription |
| `Suspended` | Subscription suspended |
| `Unsubscribed` | Subscription canceled |
## Best Practices
### Use Async Methods
```csharp
// Prefer async for all operations
var org = await organizations.GetAsync("my-org");
await org.Value.AddTagAsync("key", "value");
```
### Handle Long-Running Operations
```csharp
// Wait for completion
var operation = await organizations.CreateOrUpdateAsync(
WaitUntil.Completed, // Blocks until done
name,
data
);
// Or start and poll later
var operation = await organizations.CreateOrUpdateAsync(
WaitUntil.Started, // Returns immediately
name,
data
);
// Poll for completion
while (!operation.HasCompleted)
{
await Task.Delay(TimeSpan.FromSeconds(5));
await operation.UpdateStatusAsync();
}
```
### Check Provisioning State
```csharp
var org = await organizations.GetAsync("my-org");
if (org.Value.Data.Properties?.ProvisioningState ==
MongoDBAtlasResourceProvisioningState.Succeeded)
{
Console.WriteLine("Organization is ready");
}
```
### Use Resource Identifiers
```csharp
// Create identifier without API call
var resourceId = MongoDBAtlasOrganizationResource.CreateResourceIdentifier(
subscriptionId,
resourceGroupName,
organizationName
);
// Get resource handle (no data yet)
var orgResource = armClient.GetMongoDBAtlasOrganizationResource(resourceId);
// Fetch data when needed
var response = await orgResource.GetAsync();
```
## Common Errors
| Error | Cause | Solution |
|-------|-------|----------|
| `ResourceNotFound` | Organization doesn't exist | Verify name and resource group |
| `AuthorizationFailed` | Insufficient permissions | Check RBAC roles on resource group |
| `InvalidParameter` | Missing required properties | Ensure all required fields are set |
| `MarketplaceError` | Marketplace subscription issue | Verify offer details and subscription |
## Related Resources
- [Microsoft Learn: MongoDB Atlas on Azure](https://learn.microsoft.com/en-us/azure/partner-solutions/mongodb-atlas/)
- [API Reference](https://learn.microsoft.com/en-us/dotnet/api/azure.resourcemanager.mongodbatlas)
- [Azure SDK for .NET](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/mongodbatlas)