All skills
microsoft avatar

/azure-resource-manager-cosmosdb-dotnet

@073741f
by microsoftmicrosoft/skills3.1k stars
351

Azure Resource Manager SDK for Cosmos DB in .NET. Use for MANAGEMENT PLANE operations: creating/managing Cosmos DB accounts, databases, containers, throughput settings, and RBAC via Azure Resource Manager. NOT for data plane operations (CRUD on documents) - use Microsoft.Azure.Cosmos for that. Triggers: "Cosmos DB account", "create Cosmos account", "manage Cosmos resources", "ARM Cosmos", "CosmosDBAccountResource", "provision Cosmos DB".

Use this Skill: https://skilld.dev/gh/microsoft/skills/azure-resource-manager-cosmosdb-dotnet

This session only. Nothing lands on disk.

referencesaccount-management.md

≈1.9k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Account Management

Detailed patterns for managing Cosmos DB accounts via Azure Resource Manager.

Create Account with Full Configuration

using Azure.ResourceManager.CosmosDB;
using Azure.ResourceManager.CosmosDB.Models;
using Azure.Core;

var accountData = new CosmosDBAccountCreateOrUpdateContent(
    location: AzureLocation.EastUS,
    locations: new[]
    {
        new CosmosDBAccountLocation
        {
            LocationName = AzureLocation.EastUS,
            FailoverPriority = 0,
            IsZoneRedundant = true
        },
        new CosmosDBAccountLocation
        {
            LocationName = AzureLocation.WestUS,
            FailoverPriority = 1,
            IsZoneRedundant = false
        }
    })
{
    Kind = CosmosDBAccountKind.GlobalDocumentDB,
    
    // Consistency
    ConsistencyPolicy = new ConsistencyPolicy(DefaultConsistencyLevel.BoundedStaleness)
    {
        MaxStalenessPrefix = 100000,
        MaxIntervalInSeconds = 300
    },
    
    // High availability
    EnableAutomaticFailover = true,
    EnableMultipleWriteLocations = false,
    
    // Backup
    BackupPolicy = new ContinuousModeBackupPolicy
    {
        ContinuousModeTier = ContinuousModeTier.Continuous7Days
    },
    
    // Networking
    PublicNetworkAccess = CosmosDBPublicNetworkAccess.Enabled,
    IsVirtualNetworkFilterEnabled = false,
    
    // Features
    EnableFreeTier = false,
    EnableAnalyticalStorage = true,
    AnalyticalStorageSchemaType = AnalyticalStorageSchemaType.WellDefined,
    
    // Tags
    Tags =
    {
        ["Environment"] = "Production",
        ["CostCenter"] = "Engineering"
    }
};

var operation = await accountCollection.CreateOrUpdateAsync(
    WaitUntil.Completed,
    "my-cosmos-account",
    accountData);

Get Existing Account

// By name from resource group
var account = await resourceGroup.GetCosmosDBAccountAsync("my-cosmos-account");

// By resource ID
var accountId = CosmosDBAccountResource.CreateResourceIdentifier(
    subscriptionId, resourceGroupName, accountName);
var account = armClient.GetCosmosDBAccountResource(accountId);
await account.GetAsync(); // Fetch latest data

Update Account

// Get current account
var account = await resourceGroup.GetCosmosDBAccountAsync("my-cosmos-account");

// Create patch data
var patchData = new CosmosDBAccountPatch
{
    EnableAutomaticFailover = true,
    ConsistencyPolicy = new ConsistencyPolicy(DefaultConsistencyLevel.Session)
};
patchData.Tags.Add("UpdatedBy", "Automation");

// Apply update
var operation = await account.Value.UpdateAsync(WaitUntil.Completed, patchData);

Delete Account

var account = await resourceGroup.GetCosmosDBAccountAsync("my-cosmos-account");
await account.Value.DeleteAsync(WaitUntil.Completed);

List Accounts

// In resource group
await foreach (var account in resourceGroup.GetCosmosDBAccounts())
{
    Console.WriteLine($"Account: {account.Data.Name}");
    Console.WriteLine($"  Location: {account.Data.Location}");
    Console.WriteLine($"  Kind: {account.Data.Kind}");
}

// In subscription
await foreach (var account in subscription.GetCosmosDBAccountsAsync())
{
    Console.WriteLine($"{account.Data.Name} in {account.Data.ResourceGroupName}");
}

Get Keys and Connection Strings

// Get keys
var keys = await account.GetKeysAsync();
Console.WriteLine($"Primary Key: {keys.Value.PrimaryMasterKey}");
Console.WriteLine($"Secondary Key: {keys.Value.SecondaryMasterKey}");
Console.WriteLine($"Primary Read-Only: {keys.Value.PrimaryReadonlyMasterKey}");
Console.WriteLine($"Secondary Read-Only: {keys.Value.SecondaryReadonlyMasterKey}");

// Get connection strings
var connectionStrings = await account.GetConnectionStringsAsync();
foreach (var cs in connectionStrings.Value.ConnectionStrings)
{
    Console.WriteLine($"{cs.Description}:");
    Console.WriteLine($"  {cs.ConnectionString}");
}

// Regenerate key
await account.RegenerateKeyAsync(
    WaitUntil.Completed,
    new CosmosDBAccountRegenerateKeyContent(CosmosDBAccountKeyKind.Primary));

Failover Operations

// Manual failover (for testing)
var failoverContent = new CosmosDBFailoverPolicies(new[]
{
    new CosmosDBFailoverPolicy
    {
        LocationName = AzureLocation.WestUS,
        FailoverPriority = 0
    },
    new CosmosDBFailoverPolicy
    {
        LocationName = AzureLocation.EastUS,
        FailoverPriority = 1
    }
});

await account.FailoverPriorityChangeAsync(WaitUntil.Completed, failoverContent);

Network Configuration

Private Endpoint

// Note: Private endpoints are typically created via Azure.ResourceManager.Network
// The Cosmos account needs to be configured to accept private endpoint connections

var accountData = new CosmosDBAccountCreateOrUpdateContent(location, locations)
{
    PublicNetworkAccess = CosmosDBPublicNetworkAccess.Disabled,
    IsVirtualNetworkFilterEnabled = true
};

IP Firewall Rules

var accountData = new CosmosDBAccountCreateOrUpdateContent(location, locations)
{
    IPRules =
    {
        new CosmosDBIPAddressOrRange { IPAddressOrRange = "104.42.195.92" },
        new CosmosDBIPAddressOrRange { IPAddressOrRange = "40.76.54.131" },
        new CosmosDBIPAddressOrRange { IPAddressOrRange = "52.176.6.30/32" }
    }
};

Virtual Network Rules

var accountData = new CosmosDBAccountCreateOrUpdateContent(location, locations)
{
    IsVirtualNetworkFilterEnabled = true,
    VirtualNetworkRules =
    {
        new CosmosDBVirtualNetworkRule
        {
            Id = new ResourceIdentifier(
                "/subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.Network/virtualNetworks/{vnet}/subnets/{subnet}"),
            IgnoreMissingVnetServiceEndpoint = false
        }
    }
};

Consistency Levels

Level Description Use Case
Strong Linearizable reads Financial transactions
BoundedStaleness Reads lag by K versions or T time Gaming leaderboards
Session Read your own writes User sessions (default)
ConsistentPrefix Reads never see out-of-order writes Social feeds
Eventual No ordering guarantee Analytics, non-critical reads
// Bounded Staleness example
ConsistencyPolicy = new ConsistencyPolicy(DefaultConsistencyLevel.BoundedStaleness)
{
    MaxStalenessPrefix = 100000,  // Max versions behind
    MaxIntervalInSeconds = 300    // Max time behind (5 min)
}

Backup Policies

Periodic Backup

BackupPolicy = new PeriodicModeBackupPolicy
{
    PeriodicModeProperties = new PeriodicModeProperties
    {
        BackupIntervalInMinutes = 240,  // 4 hours
        BackupRetentionIntervalInHours = 720,  // 30 days
        BackupStorageRedundancy = BackupStorageRedundancy.Geo
    }
}

Continuous Backup

BackupPolicy = new ContinuousModeBackupPolicy
{
    ContinuousModeTier = ContinuousModeTier.Continuous30Days
}

Account Kinds

Kind API Description
GlobalDocumentDB SQL (Core) Default, most common
MongoDB MongoDB MongoDB wire protocol
Parse SQL Parse Server compatibility
// MongoDB account
var accountData = new CosmosDBAccountCreateOrUpdateContent(location, locations)
{
    Kind = CosmosDBAccountKind.MongoDB,
    ApiServerVersion = CosmosDBServerVersion.V4_2
};

Source: SKILL.md on GitHub

1 warning15d4 checks · Risk SAFE
  • Gen Agent Trust Hub15d

    This skill provides comprehensive documentation and code patterns for the official Azure Resource Manager SDK for Cosmos DB in .NET. It describes management plane operations such as resource provisioning and credential retrieval, which are standard for administrative cloud tasks. The skill follows established security practices, including the use of DefaultAzureCredential and the avoidance of hardcoded secrets.

  • Socket15d

    No alerts

  • Snyk15d

    Risk: LOW · No issues

  • Runlayer7mo

    5/5 files flagged

Signed by skilld at 073741f. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub yesterday.

Activeupdated 5 months ago
Other metadata
metadata
{
  "author": "Microsoft",
  "version": "1.0.0",
  "package": "Azure.ResourceManager.CosmosDB"
}

README badge

README badge for microsoft/skills/azure-resource-manager-cosmosdb-dotnet