-
Notifications
You must be signed in to change notification settings - Fork 192
/
CreateCosmosDBWithEventualConsistency.cs
163 lines (140 loc) · 6.34 KB
/
CreateCosmosDBWithEventualConsistency.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.CosmosDB.Fluent;
using Microsoft.Azure.Management.CosmosDB.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.Network.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core.ResourceActions;
using Microsoft.Azure.Management.Samples.Common;
using Microsoft.Rest.Azure;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace CosmosDBWithEventualConsistency
{
public class Program
{
const String DATABASE_ID = "TestDB";
const String COLLECTION_ID = "TestCollection";
/**
* Azure CosmosDB sample -
* - Create a CosmosDB configured with eventual consistency
* - Get the credentials for the CosmosDB
* - add collection to the CosmosDB
* - Delete the CosmosDB.
*/
public static void RunSample(IAzure azure)
{
string cosmosDBName = SdkContext.RandomResourceName("docDb", 10);
string rgName = SdkContext.RandomResourceName("rgNEMV", 24);
try
{
//============================================================
// Create a CosmosDB.
Console.WriteLine("Creating a CosmosDB...");
ICosmosDBAccount cosmosDBAccount = azure.CosmosDBAccounts.Define(cosmosDBName)
.WithRegion(Region.USWest)
.WithNewResourceGroup(rgName)
.WithKind(DatabaseAccountKind.GlobalDocumentDB)
.WithEventualConsistency()
.WithWriteReplication(Region.USEast)
.WithReadReplication(Region.USCentral)
.Create();
Console.WriteLine("Created CosmosDB");
Utilities.Print(cosmosDBAccount);
//============================================================
// Get credentials for the CosmosDB.
Console.WriteLine("Get credentials for the CosmosDB");
var databaseAccountListKeysResult = cosmosDBAccount.ListKeys();
string masterKey = databaseAccountListKeysResult.PrimaryMasterKey;
string endPoint = cosmosDBAccount.DocumentEndpoint;
//============================================================
// Connect to CosmosDB and add a collection
Console.WriteLine("Connecting and adding collection");
//CreateDBAndAddCollection(masterKey, endPoint);
//============================================================
// Delete CosmosDB
Console.WriteLine("Deleting the CosmosDB");
// work around CosmosDB service issue returning 404 CloudException on delete operation
try
{
azure.CosmosDBAccounts.DeleteById(cosmosDBAccount.Id);
}
catch (CloudException)
{
}
Console.WriteLine("Deleted the CosmosDB");
}
finally
{
try
{
Utilities.Log("Deleting resource group: " + rgName);
azure.ResourceGroups.BeginDeleteByName(rgName);
Utilities.Log("Deleted resource group: " + rgName);
}
catch (NullReferenceException)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
catch (Exception e)
{
Utilities.Log(e.StackTrace);
}
}
}
private void CreateDBAndAddCollection(string masterKey, string endPoint)
{
DocumentClient documentClient = new DocumentClient(new System.Uri(endPoint),
masterKey, ConnectionPolicy.Default,
ConsistencyLevel.Session);
// Define a new database using the id above.
Database myDatabase = new Database();
myDatabase.Id = DATABASE_ID;
myDatabase = documentClient.CreateDatabaseAsync(myDatabase, null)
.GetAwaiter().GetResult();
Console.WriteLine("Created a new database:");
Console.WriteLine(myDatabase.ToString());
// Define a new collection using the id above.
DocumentCollection myCollection = new DocumentCollection();
myCollection.Id = COLLECTION_ID;
// Set the provisioned throughput for this collection to be 1000 RUs.
RequestOptions requestOptions = new RequestOptions();
requestOptions.OfferThroughput = 4000;
// Create a new collection.
myCollection = documentClient.CreateDocumentCollectionAsync(
"dbs/" + DATABASE_ID, myCollection, requestOptions)
.GetAwaiter().GetResult();
}
public static void Main(string[] args)
{
try
{
//=================================================================
// Authenticate
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
// Print selected subscription
Utilities.Log("Selected subscription: " + azure.SubscriptionId);
RunSample(azure);
}
catch (Exception e)
{
Utilities.Log(e.Message);
Utilities.Log(e.StackTrace);
}
}
}
}