Ejemplo n.º 1
0
        /**
         * Azure CosmosDB sample -
         *  - Create a CosmosDB configured with MongoDB kind.
         *  - Get the mongodb connection string
         *  - 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.MongoDB)
                                                   .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");
                DatabaseAccountListKeysResultInner databaseAccountListKeysResult = cosmosDBAccount.ListKeys();
                string masterKey = databaseAccountListKeysResult.PrimaryMasterKey;
                string endPoint  = cosmosDBAccount.DocumentEndpoint;

                Console.WriteLine("Get the MongoDB connection string");
                DatabaseAccountListConnectionStringsResultInner databaseAccountListConnectionStringsResult = cosmosDBAccount.ListConnectionStrings();
                Console.WriteLine("MongoDB connection string: "
                                  + databaseAccountListConnectionStringsResult.ConnectionStrings[0].ConnectionString);

                //============================================================
                // Delete CosmosDB
                Console.WriteLine("Deleting the CosmosDB");
                azure.CosmosDBAccounts.DeleteById(cosmosDBAccount.Id);
                Console.WriteLine("Deleted the CosmosDB");
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting resource group: " + rgName);
                    azure.ResourceGroups.DeleteByName(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);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a dictionary of cosmos db account level information.
        /// </summary>
        /// <param name="account"> an existing cosmos db account. </param>
        /// <returns> a dictionary of account level attributes used for operations against the account and its resources. </returns>
        public Dictionary <string, string> GetAccountConnectionInformation(ICosmosDBAccount account)
        {
            DatabaseAccountListKeysResultInner databaseAccountListKeysResult = account.ListKeys();

            return(new Dictionary <string, string>
            {
                { "Endpoint", account.DocumentEndpoint },
                { "Primary Key", databaseAccountListKeysResult.PrimaryMasterKey },
                { "Secondary Key", databaseAccountListKeysResult.SecondaryMasterKey },
                { "Primary Read-only Key", databaseAccountListKeysResult.PrimaryReadonlyMasterKey },
                { "Secondary Read-only Key", databaseAccountListKeysResult.SecondaryReadonlyMasterKey }
            });
        }
Ejemplo n.º 3
0
 ///GENMHASH:3D69430D618BF8E9982C0544A367FC33:C0C35E00AF4E17F141675A2C05C7067B
 internal DatabaseAccountListKeysResultImpl(DatabaseAccountListKeysResultInner innerObject)
     : base(innerObject)
 {
 }
Ejemplo n.º 4
0
        /**
         * Azure DocumentDB sample -
         *  - Create a DocumentDB configured with a single read location
         *  - Get the credentials for the DocumentDB
         *  - Update the DocumentDB with additional read locations
         *  - add collection to the DocumentDB with throughput 4000
         *  - Delete the DocumentDB
         */
        public static void RunSample(IAzure azure)
        {
            string docDBName = SdkContext.RandomResourceName("docDb", 10);
            string rgName    = SdkContext.RandomResourceName("rgNEMV", 24);

            try
            {
                //============================================================
                // Create a DocumentDB.

                Console.WriteLine("Creating a DocumentDB...");
                IDocumentDBAccount documentDBAccount = azure.DocumentDBAccounts.Define(docDBName)
                                                       .WithRegion(Region.USWest)
                                                       .WithNewResourceGroup(rgName)
                                                       .WithKind(DatabaseAccountKind.GlobalDocumentDB)
                                                       .WithSessionConsistency()
                                                       .WithWriteReplication(Region.USEast)
                                                       .WithReadReplication(Region.USCentral)
                                                       .WithIpRangeFilter("13.91.6.132,13.91.6.1/24")
                                                       .Create();

                Console.WriteLine("Created DocumentDB");
                Utilities.Print(documentDBAccount);

                //============================================================
                // Update document db with three additional read regions

                Console.WriteLine("Updating DocumentDB with three additional read replication regions");
                documentDBAccount = documentDBAccount.Update()
                                    .WithReadReplication(Region.AsiaEast)
                                    .WithReadReplication(Region.AsiaSouthEast)
                                    .WithReadReplication(Region.UKSouth)
                                    .Apply();

                Console.WriteLine("Updated DocumentDB");
                Utilities.Print(documentDBAccount);

                //============================================================
                // Get credentials for the DocumentDB.

                Console.WriteLine("Get credentials for the DocumentDB");
                DatabaseAccountListKeysResultInner databaseAccountListKeysResult = documentDBAccount.ListKeys();
                string masterKey = databaseAccountListKeysResult.PrimaryMasterKey;
                string endPoint  = documentDBAccount.DocumentEndpoint;

                //============================================================
                // Connect to DocumentDB and add a collection

                Console.WriteLine("Connecting and adding collection");
                //CreateDBAndAddCollection(masterKey, endPoint);

                //============================================================
                // Delete DocumentDB
                Console.WriteLine("Deleting the DocumentDB");
                azure.DocumentDBAccounts.DeleteById(documentDBAccount.Id);
                Console.WriteLine("Deleted the DocumentDB");
            }
            finally
            {
                try
                {
                    Utilities.Log("Deleting resource group: " + rgName);
                    azure.ResourceGroups.DeleteByName(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);
                }
            }
        }