Giter VIP home page Giter VIP logo

azure_sdk_sample21_create_cosmosdb_account's Introduction

Azure SDK for .NET: How to create an Azure CosmosDB account

using System;
using System.Threading.Tasks;
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.CosmosDB;
using Azure.ResourceManager.CosmosDB.Models;
using Azure.ResourceManager.Models;
using Azure.ResourceManager.Resources;

//1. Obtaine Azure credentials
TokenCredential cred = new DefaultAzureCredential();

//2. Azure Authentication
ArmClient client = new ArmClient(cred);

//3. Set your Azure subscription number
string subscriptionId = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

//4. Set the ResourceGroup name where to create the new Azure CosmosDB account
//It is mandatory to create this ResourceGroup before creating the ComosDB account
string resourceGroupName = "rg1";

//5. Create the ResourceGroup Identifier
ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId, resourceGroupName);

//6. Get the ResourceGroup
ResourceGroupResource resourceGroupResource = client.GetResourceGroupResource(resourceGroupResourceId);

//7. Get the collection of this CosmosDBAccountResource
CosmosDBAccountCollection collection = resourceGroupResource.GetCosmosDBAccounts();

//8. Set the data input for creating the CosmosDB account: accountName, account location, etc
string accountName = "newcosmosdbwithazuresdk";
CosmosDBAccountCreateOrUpdateContent content = new CosmosDBAccountCreateOrUpdateContent(new AzureLocation("westeurope"), new CosmosDBAccountLocation[]
{
new CosmosDBAccountLocation()
{
LocationName = new AzureLocation("westeurope"),
FailoverPriority = 0,
IsZoneRedundant = false,
}
})
{
    CreateMode = CosmosDBAccountCreateMode.Default,
};

//9. Create/Update a CosmosDB account
ArmOperation<CosmosDBAccountResource> lro = await collection.CreateOrUpdateAsync(WaitUntil.Completed, accountName, content);

//10. Get the ComosDB Account Resource
CosmosDBAccountResource result = lro.Value;

//11. Get the CosmosDB Account Data
CosmosDBAccountData resourceData = result.Data;

//12. We print the new CosmosDB account Id
Console.WriteLine($"Succeeded on id: {resourceData.Id}");

azure_sdk_sample21_create_cosmosdb_account's People

Contributors

luiscoco avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.