public async Task RestorableMongodb36Tests()
        {
            DatabaseRestoreResource databaseRestoreResource1 = new DatabaseRestoreResource()
            {
                DatabaseName    = "database1",
                CollectionNames = new List <string>()
                {
                    "collection1", "collection2", "collection3"
                }
            };

            DatabaseRestoreResource databaseRestoreResource2 = new DatabaseRestoreResource()
            {
                DatabaseName    = "databaseA",
                CollectionNames = new List <string>()
            };

            List <DatabaseRestoreResource> resources = new List <DatabaseRestoreResource>()
            {
                databaseRestoreResource1,
                databaseRestoreResource2
            };

            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler);
                await RestorableMongodbTestHelper(cosmosDBManagementClient, sourceDatabaseAccountInstanceId36, resources);
            }
        }
        public async Task RestoreDatabaseAccountTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);

                DatabaseAccountGetResults databaseAccount = await cosmosDBManagementClient.DatabaseAccounts.GetAsync(resourceGroupName, "pitr-sql-stage-source");

                DateTime restoreTs = DateTime.Parse(restoreTimestamp);
                string   restoredatabaseAccountName = TestUtilities.GenerateName(prefix: "restoredaccountname");

                List <RestorableDatabaseAccountGetResult> restorableAccounts        = (await cosmosDBManagementClient.RestorableDatabaseAccounts.ListAsync()).ToList();
                RestorableDatabaseAccountGetResult        restorableDatabaseAccount = restorableAccounts.
                                                                                      SingleOrDefault(account => account.Name.Equals(databaseAccount.InstanceId, StringComparison.OrdinalIgnoreCase));

                List <Location> locations = new List <Location>
                {
                    new Location(locationName: westus2)
                };

                RestoreReqeustDatabaseAccountCreateUpdateProperties databaseAccountCreateUpdateProperties = new RestoreReqeustDatabaseAccountCreateUpdateProperties
                {
                    Locations         = locations,
                    RestoreParameters = new RestoreParameters()
                    {
                        RestoreMode           = "PointInTime",
                        RestoreTimestampInUtc = restoreTs,
                        RestoreSource         = restorableDatabaseAccount.Id
                    }
                };

                DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters
                {
                    Location = westus2,
                    Tags     = new Dictionary <string, string>
                    {
                        { "key1", "value1" },
                        { "key2", "value2" }
                    },
                    Kind       = "GlobalDocumentDB",
                    Properties = databaseAccountCreateUpdateProperties
                };

                DatabaseAccountGetResults restoredDatabaseAccount =
                    (await cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(
                         resourceGroupName, restoredatabaseAccountName, databaseAccountCreateUpdateParameters)).Body;

                Assert.NotNull(restoredDatabaseAccount);
                Assert.NotNull(restoredDatabaseAccount.RestoreParameters);
                Assert.Equal(restoredDatabaseAccount.RestoreParameters.RestoreSource.ToLower(), restorableDatabaseAccount.Id.ToLower());
                Assert.True(restoredDatabaseAccount.BackupPolicy is ContinuousModeBackupPolicy);
            }
        }
Beispiel #3
0
        public async Task RestoreDatabaseAccountFeedTests()
        {
            RecordedDelegatingHandler handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler);

                await RestoreDatabaseAccountFeedTestHelperAsync(cosmosDBManagementClient, "pitr-sql-stage-source", ApiType.Sql, 1);
                await RestoreDatabaseAccountFeedTestHelperAsync(cosmosDBManagementClient, "pitr-mongo32-stage-source", ApiType.MongoDB, 1);
                await RestoreDatabaseAccountFeedTestHelperAsync(cosmosDBManagementClient, "pitr-mongo36-stage-source", ApiType.MongoDB, 1);
            }
        }
        public void ListOperationsTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBMgmtClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);

                // Get operations
                var operations = cosmosDBMgmtClient.Operations.List();

                // Verify operations are returned
                Assert.NotNull(operations);
                Assert.NotEmpty(operations);
            }
        }
Beispiel #5
0
        public async Task RestoreDatabaseAccountTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);
                ResourceManagementClient resourcesClient          = CosmosDBTestUtilities.GetResourceManagementClient(context, handler1);
                string restoredatabaseAccountName = TestUtilities.GenerateName(prefix: "restoredaccountname");

                DatabaseAccountGetResults databaseAccount = null;
                bool isDatabaseNameExists = cosmosDBManagementClient.DatabaseAccounts.CheckNameExistsWithHttpMessagesAsync(sourceDatabaseAccountName).GetAwaiter().GetResult().Body;
                if (!isDatabaseNameExists)
                {
                    DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters1 = new DatabaseAccountCreateUpdateParameters
                    {
                        Location  = location,
                        Kind      = DatabaseAccountKind.GlobalDocumentDB,
                        Locations = new List <Location> {
                            new Location {
                                LocationName = location
                            }
                        },
                        BackupPolicy = new ContinuousModeBackupPolicy(),
                    };

                    databaseAccount = cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, sourceDatabaseAccountName, databaseAccountCreateUpdateParameters1).GetAwaiter().GetResult().Body;
                    Assert.Equal(databaseAccount.Name, sourceDatabaseAccountName);
                }
                databaseAccount = await cosmosDBManagementClient.DatabaseAccounts.GetAsync(resourceGroupName, sourceDatabaseAccountName);

                DateTime restoreTs = DateTime.UtcNow;

                List <RestorableDatabaseAccountGetResult> restorableAccounts        = (await cosmosDBManagementClient.RestorableDatabaseAccounts.ListByLocationAsync(location)).ToList();
                RestorableDatabaseAccountGetResult        restorableDatabaseAccount = restorableAccounts.
                                                                                      SingleOrDefault(account => account.AccountName.Equals(databaseAccount.Name, StringComparison.OrdinalIgnoreCase));

                DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters
                {
                    Location = location,
                    Tags     = new Dictionary <string, string>
                    {
                        { "key1", "value1" },
                        { "key2", "value2" }
                    },
                    Kind      = "GlobalDocumentDB",
                    Locations = new List <Location>
                    {
                        new Location(locationName: location)
                    },
                    CreateMode        = CreateMode.Restore,
                    RestoreParameters = new RestoreParameters()
                    {
                        RestoreMode           = "PointInTime",
                        RestoreTimestampInUtc = restoreTs,
                        RestoreSource         = restorableDatabaseAccount.Id
                    }
                };

                DatabaseAccountGetResults restoredDatabaseAccount = (await cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, restoredatabaseAccountName, databaseAccountCreateUpdateParameters)).Body;
                Assert.NotNull(restoredDatabaseAccount);
                Assert.NotNull(restoredDatabaseAccount.RestoreParameters);
                Assert.Equal(restoredDatabaseAccount.RestoreParameters.RestoreSource.ToLower(), restorableDatabaseAccount.Id.ToLower());
                Assert.True(restoredDatabaseAccount.BackupPolicy is ContinuousModeBackupPolicy);
            }
        }
Beispiel #6
0
        public void CassandraCRUDTests()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler);

                bool isDatabaseNameExists = cosmosDBManagementClient.DatabaseAccounts.CheckNameExistsWithHttpMessagesAsync(databaseAccountName).GetAwaiter().GetResult().Body;

                if (!isDatabaseNameExists)
                {
                    return;
                }

                CassandraKeyspaceCreateUpdateParameters cassandraKeyspaceCreateUpdateParameters = new CassandraKeyspaceCreateUpdateParameters
                {
                    Resource = new CassandraKeyspaceResource {
                        Id = keyspaceName
                    },
                    Options = new CreateUpdateOptions()
                };

                CassandraKeyspaceGetResults cassandraKeyspaceGetResults = cosmosDBManagementClient.CassandraResources.CreateUpdateCassandraKeyspaceWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName, cassandraKeyspaceCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraKeyspaceGetResults);
                Assert.Equal(keyspaceName, cassandraKeyspaceGetResults.Name);

                CassandraKeyspaceGetResults cassandraKeyspaceGetResults1 = cosmosDBManagementClient.CassandraResources.GetCassandraKeyspaceWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraKeyspaceGetResults1);
                Assert.Equal(keyspaceName, cassandraKeyspaceGetResults1.Name);

                VerifyEqualCassandraDatabases(cassandraKeyspaceGetResults, cassandraKeyspaceGetResults1);

                CassandraKeyspaceCreateUpdateParameters cassandraKeyspaceCreateUpdateParameters2 = new CassandraKeyspaceCreateUpdateParameters
                {
                    Location = location,
                    Tags     = tags,
                    Resource = new CassandraKeyspaceResource {
                        Id = keyspaceName2
                    },
                    Options = new CreateUpdateOptions
                    {
                        Throughput = sampleThroughput
                    }
                };

                CassandraKeyspaceGetResults cassandraKeyspaceGetResults2 = cosmosDBManagementClient.CassandraResources.CreateUpdateCassandraKeyspaceWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName2, cassandraKeyspaceCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraKeyspaceGetResults2);
                Assert.Equal(keyspaceName2, cassandraKeyspaceGetResults2.Name);

                IEnumerable <CassandraKeyspaceGetResults> cassandraKeyspaces = cosmosDBManagementClient.CassandraResources.ListCassandraKeyspacesWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraKeyspaces);

                ThroughputSettingsGetResults throughputSettingsGetResults = cosmosDBManagementClient.CassandraResources.GetCassandraKeyspaceThroughputWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(throughputSettingsGetResults);
                Assert.NotNull(throughputSettingsGetResults.Name);
                Assert.Equal(throughputSettingsGetResults.Resource.Throughput, sampleThroughput);
                Assert.Equal(cassandraThroughputType, throughputSettingsGetResults.Type);

                CassandraTableCreateUpdateParameters cassandraTableCreateUpdateParameters = new CassandraTableCreateUpdateParameters
                {
                    Resource = new CassandraTableResource
                    {
                        Id     = tableName,
                        Schema = new CassandraSchema
                        {
                            Columns = new List <Column> {
                                new Column {
                                    Name = "columnA", Type = "int"
                                }, new Column {
                                    Name = "columnB", Type = "ascii"
                                }
                            },
                            ClusterKeys = new List <ClusterKey> {
                                new ClusterKey {
                                    Name = "columnB", OrderBy = "Asc"
                                }
                            },
                            PartitionKeys = new List <CassandraPartitionKey> {
                                new CassandraPartitionKey {
                                    Name = "columnA"
                                }
                            }
                        }
                    },
                    Options = new CreateUpdateOptions()
                };

                CassandraTableGetResults cassandraTableGetResults = cosmosDBManagementClient.CassandraResources.CreateUpdateCassandraTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName, tableName, cassandraTableCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraTableGetResults);
                VerifyCassandraTableCreation(cassandraTableGetResults, cassandraTableCreateUpdateParameters);

                IEnumerable <CassandraTableGetResults> cassandraTables = cosmosDBManagementClient.CassandraResources.ListCassandraTablesWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraTables);

                foreach (CassandraTableGetResults cassandraTable in cassandraTables)
                {
                    cosmosDBManagementClient.CassandraResources.DeleteCassandraTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName, cassandraTable.Name);
                }

                foreach (CassandraKeyspaceGetResults cassandraKeyspace in cassandraKeyspaces)
                {
                    cosmosDBManagementClient.CassandraResources.DeleteCassandraKeyspaceWithHttpMessagesAsync(resourceGroupName, databaseAccountName, cassandraKeyspace.Name);
                }
            }
        }
Beispiel #7
0
        public async Task RestorableSqlDatabaseResourceFeedTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);
                ResourceManagementClient resourcesClient          = CosmosDBTestUtilities.GetResourceManagementClient(context, handler2);

                string resourceGroupName   = CosmosDBTestUtilities.CreateResourceGroup(resourcesClient);
                string databaseAccountName = TestUtilities.GenerateName(prefix: "accountname");

                List <Location> locations = new List <Location>();
                locations.Add(new Location(locationName: "East US 2"));
                DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters
                {
                    Location = "EAST US 2",
                    Tags     = new Dictionary <string, string>
                    {
                        { "key1", "value1" },
                        { "key2", "value2" }
                    },
                    Kind = "MongoDB",
                    ConsistencyPolicy = new ConsistencyPolicy
                    {
                        DefaultConsistencyLevel = DefaultConsistencyLevel.BoundedStaleness,
                        MaxStalenessPrefix      = 300,
                        MaxIntervalInSeconds    = 1000
                    },
                    Locations = locations
                };

                DatabaseAccountGetResults databaseAccount = cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseAccountCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(databaseAccount);

                List <DatabaseAccountGetResults> databaseFeedResult = (await cosmosDBManagementClient.DatabaseAccounts.ListByResourceGroupAsync(resourceGroupName)).ToList();
                Assert.Single(databaseFeedResult);

                DatabaseAccountUpdateParameters databaseAccountUpdateParameters = new DatabaseAccountUpdateParameters
                {
                    Location = "EAST US 2",
                    Tags     = new Dictionary <string, string>
                    {
                        { "key3", "value3" },
                        { "key4", "value4" }
                    },
                    ConsistencyPolicy = new ConsistencyPolicy
                    {
                        DefaultConsistencyLevel = DefaultConsistencyLevel.Session,
                        MaxStalenessPrefix      = 1300,
                        MaxIntervalInSeconds    = 12000
                    },
                    Locations = locations,
                    IpRules   = new List <IpAddressOrRange>
                    {
                        new IpAddressOrRange("23.43.230.120")
                    },
                    IsVirtualNetworkFilterEnabled      = false,
                    EnableAutomaticFailover            = true,
                    DisableKeyBasedMetadataWriteAccess = true,
                    NetworkAclBypass            = NetworkAclBypass.AzureServices,
                    NetworkAclBypassResourceIds = new List <string>
                    {
                        "/subscriptions/subId/resourcegroups/rgName/providers/Microsoft.Synapse/workspaces/workspaceName",
                        "/subscriptions/subId/resourcegroups/rgName/providers/Microsoft.Synapse/workspaces/workspaceName2"
                    }
                };
                DatabaseAccountGetResults updatedDatabaseAccount = cosmosDBManagementClient.DatabaseAccounts.UpdateWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseAccountUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(updatedDatabaseAccount);

                databaseFeedResult = (await cosmosDBManagementClient.DatabaseAccounts.ListByResourceGroupAsync(resourceGroupName)).ToList();
                Assert.Single(databaseFeedResult);

                await cosmosDBManagementClient.DatabaseAccounts.DeleteWithHttpMessagesAsync(resourceGroupName, databaseAccountName);
            }
        }
Beispiel #8
0
        public async Task RestorableSqlTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient  cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);
                ResourceManagementClient  resourcesClient          = CosmosDBTestUtilities.GetResourceManagementClient(context, handler1);
                DatabaseAccountGetResults databaseAccount          = await cosmosDBManagementClient.DatabaseAccounts.GetAsync(resourceGroupName, sourceDatabaseAccountName);

                RestorableDatabaseAccountGetResult restorableDatabaseAccount = (await cosmosDBManagementClient.RestorableDatabaseAccounts.GetByLocationAsync(location, sourceDatabaseAccountInstanceId));

                Assert.Equal(databaseAccount.InstanceId, restorableDatabaseAccount.Name);
                Assert.Equal(databaseAccount.Name, restorableDatabaseAccount.AccountName);
                Assert.Equal(ApiType.Sql, restorableDatabaseAccount.ApiType);
                Assert.Equal(3, restorableDatabaseAccount.RestorableLocations.Count);

                foreach (var location in restorableDatabaseAccount.RestorableLocations)
                {
                    Assert.NotNull(location.CreationTime);
                    // Assert.Null(location.DeletionTime);
                    Assert.False(string.IsNullOrEmpty(location.LocationName));
                    Assert.False(string.IsNullOrEmpty(location.RegionalDatabaseAccountInstanceId));
                }

                List <RestorableSqlDatabaseGetResult> restorableSqlDatabases =
                    (await cosmosDBManagementClient.RestorableSqlDatabases.ListAsync(location, restorableDatabaseAccount.Name)).ToList();

                RestorableSqlDatabaseGetResult restorableSqlDatabase = restorableSqlDatabases.First();

                Assert.Equal(2, restorableSqlDatabases.Count());

                string dbRid = restorableSqlDatabase.Resource.OwnerResourceId;

                List <RestorableSqlContainerGetResult> restorableSqlContainers =
                    (await cosmosDBManagementClient.RestorableSqlContainers.ListAsync(location, restorableDatabaseAccount.Name, dbRid)).ToList();

                Assert.Equal(2, restorableSqlContainers.Count());

                List <DatabaseRestoreResource> restorableSqlResources =
                    (await cosmosDBManagementClient.RestorableSqlResources.ListAsync(location, restorableDatabaseAccount.Name, location, restoreTimestamp.ToString())).ToList();

                DatabaseRestoreResource databaseRestoreResource1 = new DatabaseRestoreResource()
                {
                    DatabaseName    = "database1",
                    CollectionNames = new List <string>()
                    {
                        "container1", "container2"
                    }
                };

                DatabaseRestoreResource databaseRestoreResource2 = new DatabaseRestoreResource()
                {
                    DatabaseName    = "databaseA",
                    CollectionNames = new List <string>()
                };

                List <DatabaseRestoreResource> resources = new List <DatabaseRestoreResource>()
                {
                    databaseRestoreResource1,
                    databaseRestoreResource2
                };

                ValidateDatabaseRestoreResource(resources, restorableSqlResources);
            }
        }
Beispiel #9
0
        public void MongoCRUDTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);
                ResourceManagementClient resourcesClient          = CosmosDBTestUtilities.GetResourceManagementClient(context, handler2);

                string resourceGroupName   = "CosmosDBResourceGroup3668";
                string databaseAccountName = "db001";

                bool isDatabaseNameExists = cosmosDBManagementClient.DatabaseAccounts.CheckNameExistsWithHttpMessagesAsync(databaseAccountName).GetAwaiter().GetResult().Body;

                DatabaseAccountGetResults databaseAccount = null;
                if (!isDatabaseNameExists)
                {
                    List <Location> locations = new List <Location>();
                    locations.Add(new Location(locationName: "East US"));
                    DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters
                    {
                        Location  = "EAST US",
                        Kind      = "MongoDB",
                        Locations = locations
                    };

                    databaseAccount = cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseAccountCreateUpdateParameters).GetAwaiter().GetResult().Body;
                }

                string databaseName  = "databaseName3668";
                string databaseName2 = "databaseName23668";
                MongoDBDatabaseCreateUpdateParameters mongoDBDatabaseCreateUpdateParameters = new MongoDBDatabaseCreateUpdateParameters
                {
                    Resource = new MongoDBDatabaseResource {
                        Id = databaseName
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "foo", "bar" }
                    }
                };

                MongoDBDatabaseGetResults mongoDBDatabaseGetResults = cosmosDBManagementClient.MongoDBResources.CreateUpdateMongoDBDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, mongoDBDatabaseCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.Equal(databaseName, mongoDBDatabaseGetResults.Name);
                Assert.NotNull(mongoDBDatabaseGetResults);
                Assert.Equal("Microsoft.DocumentDB/databaseAccounts/mongodbDatabases", mongoDBDatabaseGetResults.Type);

                MongoDBDatabaseGetResults mongoDBDatabaseGetResults1 = cosmosDBManagementClient.MongoDBResources.GetMongoDBDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(mongoDBDatabaseGetResults1);
                Assert.Equal(databaseName, mongoDBDatabaseGetResults1.Name);
                Assert.Equal("Microsoft.DocumentDB/databaseAccounts/mongodbDatabases", mongoDBDatabaseGetResults1.Type);


                VerifyEqualMongoDBDatabases(mongoDBDatabaseGetResults, mongoDBDatabaseGetResults1);

                MongoDBDatabaseCreateUpdateParameters mongoDBDatabaseCreateUpdateParameters2 = new MongoDBDatabaseCreateUpdateParameters
                {
                    Resource = new MongoDBDatabaseResource {
                        Id = databaseName2
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "Throughput", "700" }
                    }
                };

                MongoDBDatabaseGetResults mongoDBDatabaseGetResults2 = cosmosDBManagementClient.MongoDBResources.CreateUpdateMongoDBDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2, mongoDBDatabaseCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(mongoDBDatabaseGetResults2);
                Assert.Equal(databaseName2, mongoDBDatabaseGetResults2.Name);
                Assert.Equal("Microsoft.DocumentDB/databaseAccounts/mongodbDatabases", mongoDBDatabaseGetResults2.Type);

                IEnumerable <MongoDBDatabaseGetResults> mongoDBDatabases = cosmosDBManagementClient.MongoDBResources.ListMongoDBDatabasesWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(mongoDBDatabases);

                ThroughputSettingsGetResults throughputSettingsGetResults = cosmosDBManagementClient.MongoDBResources.GetMongoDBDatabaseThroughputWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(throughputSettingsGetResults);
                Assert.NotNull(throughputSettingsGetResults.Name);
                Assert.Equal("Microsoft.DocumentDB/databaseAccounts/mongodbDatabases/throughputSettings", throughputSettingsGetResults.Type);

                string collectionName = "collectionName3668";

                MongoDBCollectionCreateUpdateParameters mongoDBCollectionCreateUpdateParameters = new MongoDBCollectionCreateUpdateParameters
                {
                    Resource = new MongoDBCollectionResource
                    {
                        Id = collectionName,
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "foo", "bar" },
                    }
                };

                MongoDBCollectionGetResults mongoDBCollectionGetResults = cosmosDBManagementClient.MongoDBResources.CreateUpdateMongoDBCollectionWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, collectionName, mongoDBCollectionCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(mongoDBCollectionGetResults);

                IEnumerable <MongoDBCollectionGetResults> mongoDBCollections = cosmosDBManagementClient.MongoDBResources.ListMongoDBCollectionsWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(mongoDBCollections);

                foreach (MongoDBCollectionGetResults mongoDBCollection in mongoDBCollections)
                {
                    cosmosDBManagementClient.MongoDBResources.DeleteMongoDBCollectionWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, mongoDBCollection.Name);
                }

                foreach (MongoDBDatabaseGetResults mongoDBDatabase in mongoDBDatabases)
                {
                    cosmosDBManagementClient.MongoDBResources.DeleteMongoDBDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, mongoDBDatabase.Name);
                }
            }
        }
        public void SqlCRUDTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);
                ResourceManagementClient resourcesClient          = CosmosDBTestUtilities.GetResourceManagementClient(context, handler2);

                string resourceGroupName   = "CosmosDBResourceGroup822"; // Using a pre-existing ResourceGroup and DatabaseAccount, because Database Account provisioning takes some time
                string databaseAccountName = "db9934";

                bool isDatabaseNameExists = cosmosDBManagementClient.DatabaseAccounts.CheckNameExistsWithHttpMessagesAsync(databaseAccountName).GetAwaiter().GetResult().Body;

                DatabaseAccountGetResults databaseAccount = null;
                if (!isDatabaseNameExists)
                {
                    List <Location> locations = new List <Location>();
                    locations.Add(new Location(locationName: "East US"));
                    DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters
                    {
                        Location  = "EAST US",
                        Kind      = "GlobalDocumentDB",
                        Locations = locations
                    };

                    databaseAccount = cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseAccountCreateUpdateParameters).GetAwaiter().GetResult().Body;
                }

                string databaseName  = "databaseName822";
                string databaseName2 = "databaseName2822";
                SqlDatabaseCreateUpdateParameters sqlDatabaseCreateUpdateParameters = new SqlDatabaseCreateUpdateParameters
                {
                    Resource = new SqlDatabaseResource {
                        Id = databaseName
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "foo", "bar" }
                    }
                };

                SqlDatabaseGetResults sqlDatabaseGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, sqlDatabaseCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlDatabaseGetResults);
                Assert.Equal(databaseName, sqlDatabaseGetResults.Name);

                SqlDatabaseGetResults sqlDatabaseGetResults2 = cosmosDBManagementClient.SqlResources.GetSqlDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlDatabaseGetResults2);
                Assert.Equal(databaseName, sqlDatabaseGetResults2.Name);

                VerifyEqualSqlDatabases(sqlDatabaseGetResults, sqlDatabaseGetResults2);

                SqlDatabaseCreateUpdateParameters sqlDatabaseCreateUpdateParameters2 = new SqlDatabaseCreateUpdateParameters
                {
                    Location = "EAST US",
                    Tags     = new Dictionary <string, string>
                    {
                        { "key3", "value3" },
                        { "key4", "value4" }
                    },
                    Resource = new SqlDatabaseResource {
                        Id = databaseName2
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "Throughput", "700" }
                    }
                };

                SqlDatabaseGetResults sqlDatabaseGetResults3 = cosmosDBManagementClient.SqlResources.CreateUpdateSqlDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2, sqlDatabaseCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlDatabaseGetResults3);
                Assert.Equal(databaseName2, sqlDatabaseGetResults3.Name);

                IEnumerable <SqlDatabaseGetResults> sqlDatabases = cosmosDBManagementClient.SqlResources.ListSqlDatabasesWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlDatabases);

                ThroughputSettingsGetResults throughputSettingsGetResults = cosmosDBManagementClient.SqlResources.GetSqlDatabaseThroughputWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(throughputSettingsGetResults);
                Assert.NotNull(throughputSettingsGetResults.Name);
                Assert.Equal("Microsoft.DocumentDB/databaseAccounts/sqlDatabases/throughputSettings", throughputSettingsGetResults.Type);

                string containerName = "containerName822";

                SqlContainerCreateUpdateParameters sqlContainerCreateUpdateParameters = new SqlContainerCreateUpdateParameters
                {
                    Resource = new SqlContainerResource {
                        Id           = containerName,
                        PartitionKey = new ContainerPartitionKey
                        {
                            Kind  = "Hash",
                            Paths = new List <string> {
                                "/address/zipCode"
                            }
                        }
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "Throughput", "700" }
                    }
                };

                SqlContainerGetResults sqlContainerGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlContainerWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, sqlContainerCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlContainerGetResults);

                IEnumerable <SqlContainerGetResults> sqlContainers = cosmosDBManagementClient.SqlResources.ListSqlContainersWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlContainers);

                string storedProcedureName = "storedProcedureName822";

                SqlStoredProcedureCreateUpdateParameters sqlStoredProcedureCreateUpdateParameters = new SqlStoredProcedureCreateUpdateParameters
                {
                    Resource = new SqlStoredProcedureResource
                    {
                        Id   = storedProcedureName,
                        Body = "function () { var context = getContext(); " +
                               "var response = context.getResponse();" +
                               "response.setBody('Hello, World');" +
                               "}"
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "foo", "bar" }
                    }
                };

                SqlStoredProcedureGetResults sqlStoredProcedureGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlStoredProcedureWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, storedProcedureName, sqlStoredProcedureCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlStoredProcedureGetResults);

                IEnumerable <SqlStoredProcedureGetResults> sqlStoredProcedures = cosmosDBManagementClient.SqlResources.ListSqlStoredProceduresWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlStoredProcedures);

                foreach (SqlStoredProcedureGetResults sqlStoredProcedure in sqlStoredProcedures)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlStoredProcedureWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, sqlStoredProcedure.Name);
                }

                string userDefinedFunctionName = "userDefinedFunctionName822";

                SqlUserDefinedFunctionCreateUpdateParameters sqlUserDefinedFunctionCreateUpdateParameters = new SqlUserDefinedFunctionCreateUpdateParameters
                {
                    Resource = new SqlUserDefinedFunctionResource
                    {
                        Id   = userDefinedFunctionName,
                        Body = "function () { var context = getContext(); " +
                               "var response = context.getResponse();" +
                               "response.setBody('Hello, World');" +
                               "}"
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "foo", "bar" }
                    }
                };

                SqlUserDefinedFunctionGetResults sqlUserDefinedFunctionGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlUserDefinedFunctionWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, userDefinedFunctionName, sqlUserDefinedFunctionCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlStoredProcedureGetResults);

                IEnumerable <SqlUserDefinedFunctionGetResults> sqlUserDefinedFunctions = cosmosDBManagementClient.SqlResources.ListSqlUserDefinedFunctionsWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlUserDefinedFunctions);

                foreach (SqlUserDefinedFunctionGetResults sqlUserDefinedFunction in sqlUserDefinedFunctions)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlUserDefinedFunctionWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, sqlUserDefinedFunction.Name);
                }

                string triggerName = "triggerName822";

                SqlTriggerCreateUpdateParameters sqlTriggerCreateUpdateParameters = new SqlTriggerCreateUpdateParameters
                {
                    Resource = new SqlTriggerResource
                    {
                        Id = triggerName,
                        TriggerOperation = "All",
                        TriggerType      = "Pre",
                        Body             = "function () { var context = getContext(); " +
                                           "var response = context.getResponse();" +
                                           "response.setBody('Hello, World');" +
                                           "}"
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "foo", "bar" }
                    }
                };

                SqlTriggerGetResults sqlTriggerGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlTriggerWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, triggerName, sqlTriggerCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlTriggerGetResults);

                IEnumerable <SqlTriggerGetResults> sqlTriggers = cosmosDBManagementClient.SqlResources.ListSqlTriggersWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlTriggers);

                foreach (SqlTriggerGetResults sqlTrigger in sqlTriggers)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlTriggerWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, sqlTrigger.Name);
                }

                foreach (SqlContainerGetResults sqlContainer in sqlContainers)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlContainerWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, sqlContainer.Name);
                }

                foreach (SqlDatabaseGetResults sqlDatabase in sqlDatabases)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, sqlDatabase.Name);
                }
            }
        }
        public void TableCRUDTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);

                bool isDatabaseNameExists = cosmosDBManagementClient.DatabaseAccounts.CheckNameExistsWithHttpMessagesAsync(databaseAccountName).GetAwaiter().GetResult().Body;

                if (!isDatabaseNameExists)
                {
                    return;
                }

                TableCreateUpdateParameters tableCreateUpdateParameters = new TableCreateUpdateParameters
                {
                    Resource = new TableResource {
                        Id = tableName
                    },
                    Options = new CreateUpdateOptions()
                };

                TableGetResults tableGetResults = cosmosDBManagementClient.TableResources.CreateUpdateTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, tableName, tableCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(tableGetResults);
                Assert.Equal(tableName, tableGetResults.Name);

                TableGetResults tableGetResults2 = cosmosDBManagementClient.TableResources.GetTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, tableName).GetAwaiter().GetResult().Body;
                Assert.NotNull(tableGetResults2);
                Assert.Equal(tableName, tableGetResults2.Name);

                VerifyEqualTables(tableGetResults, tableGetResults2);

                TableCreateUpdateParameters tableCreateUpdateParameters2 = new TableCreateUpdateParameters
                {
                    Location = location,
                    Tags     = tags,
                    Resource = new TableResource {
                        Id = tableName2
                    },
                    Options = new CreateUpdateOptions
                    {
                        Throughput = sampleThroughput
                    }
                };

                TableGetResults tableGetResults3 = cosmosDBManagementClient.TableResources.CreateUpdateTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, tableName2, tableCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(tableGetResults3);
                Assert.Equal(tableName2, tableGetResults3.Name);

                IEnumerable <TableGetResults> tables = cosmosDBManagementClient.TableResources.ListTablesWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(tables);

                ThroughputSettingsGetResults throughputSettingsGetResults = cosmosDBManagementClient.TableResources.GetTableThroughputWithHttpMessagesAsync(resourceGroupName, databaseAccountName, tableName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(throughputSettingsGetResults);
                Assert.NotNull(throughputSettingsGetResults.Name);
                Assert.Equal(throughputSettingsGetResults.Resource.Throughput, sampleThroughput);
                Assert.Equal(tableThroughputType, throughputSettingsGetResults.Type);

                foreach (TableGetResults table in tables)
                {
                    cosmosDBManagementClient.TableResources.DeleteTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, table.Name);
                }
            }
        }
Beispiel #12
0
        public void CassandraCRUDTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);
                ResourceManagementClient resourcesClient          = CosmosDBTestUtilities.GetResourceManagementClient(context, handler2);

                string resourceGroupName   = "CosmosDBResourceGroup2510";
                string databaseAccountName = "db2725";

                bool isDatabaseNameExists = cosmosDBManagementClient.DatabaseAccounts.CheckNameExistsWithHttpMessagesAsync(databaseAccountName).GetAwaiter().GetResult().Body;

                DatabaseAccountGetResults databaseAccount = null;
                if (!isDatabaseNameExists)
                {
                    return;
                    // SDK doesnt support creation of Cassandra, Table, Gremlin Accounts, use accounts created using Azure portal

                    // List<Location> locations = new List<Location>();
                    // locations.Add(new Location(locationName: "East US"));
                    // DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters
                    // {
                    //     Location = "EAST US",
                    //     Locations = locations
                    // };

                    //databaseAccount = cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseAccountCreateUpdateParameters).GetAwaiter().GetResult().Body;
                }

                string keyspaceName  = "keyspaceName2510";
                string keyspaceName2 = "keyspaceName22510";
                CassandraKeyspaceCreateUpdateParameters cassandraKeyspaceCreateUpdateParameters = new CassandraKeyspaceCreateUpdateParameters
                {
                    Resource = new CassandraKeyspaceResource {
                        Id = keyspaceName
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "foo", "bar" }
                    }
                };

                CassandraKeyspaceGetResults cassandraKeyspaceGetResults = cosmosDBManagementClient.CassandraResources.CreateUpdateCassandraKeyspaceWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName, cassandraKeyspaceCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraKeyspaceGetResults);
                Assert.Equal(keyspaceName, cassandraKeyspaceGetResults.Name);

                CassandraKeyspaceGetResults cassandraKeyspaceGetResults1 = cosmosDBManagementClient.CassandraResources.GetCassandraKeyspaceWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraKeyspaceGetResults1);
                Assert.Equal(keyspaceName, cassandraKeyspaceGetResults1.Name);

                VerifyEqualCassandraDatabases(cassandraKeyspaceGetResults, cassandraKeyspaceGetResults1);

                CassandraKeyspaceCreateUpdateParameters cassandraKeyspaceCreateUpdateParameters2 = new CassandraKeyspaceCreateUpdateParameters
                {
                    Location = "EAST US",
                    Tags     = new Dictionary <string, string>
                    {
                        { "key3", "value3" },
                        { "key4", "value4" }
                    },
                    Resource = new CassandraKeyspaceResource {
                        Id = keyspaceName2
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "Throughput", "700" }
                    }
                };

                CassandraKeyspaceGetResults cassandraKeyspaceGetResults2 = cosmosDBManagementClient.CassandraResources.CreateUpdateCassandraKeyspaceWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName2, cassandraKeyspaceCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraKeyspaceGetResults2);
                Assert.Equal(keyspaceName2, cassandraKeyspaceGetResults2.Name);

                IEnumerable <CassandraKeyspaceGetResults> cassandraKeyspaces = cosmosDBManagementClient.CassandraResources.ListCassandraKeyspacesWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraKeyspaces);

                ThroughputSettingsGetResults throughputSettingsGetResults = cosmosDBManagementClient.CassandraResources.GetCassandraKeyspaceThroughputWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(throughputSettingsGetResults);
                Assert.NotNull(throughputSettingsGetResults.Name);
                Assert.Equal("Microsoft.DocumentDB/databaseAccounts/cassandraKeyspaces/throughputSettings", throughputSettingsGetResults.Type);

                string tableName = "tableName2510";

                CassandraTableCreateUpdateParameters cassandraTableCreateUpdateParameters = new CassandraTableCreateUpdateParameters
                {
                    Resource = new CassandraTableResource
                    {
                        Id     = tableName,
                        Schema = new CassandraSchema
                        {
                            Columns = new List <Column> {
                                new Column {
                                    Name = "columnA", Type = "int"
                                }, new Column {
                                    Name = "columnB", Type = "ascii"
                                }
                            },
                            ClusterKeys = new List <ClusterKey> {
                                new ClusterKey {
                                    Name = "columnB", OrderBy = "Asc"
                                }
                            },
                            PartitionKeys = new List <CassandraPartitionKey> {
                                new CassandraPartitionKey {
                                    Name = "columnA"
                                }
                            }
                        }
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "foo", "bar" }
                    }
                };

                CassandraTableGetResults cassandraTableGetResults = cosmosDBManagementClient.CassandraResources.CreateUpdateCassandraTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName, tableName, cassandraTableCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraTableGetResults);

                IEnumerable <CassandraTableGetResults> cassandraTables = cosmosDBManagementClient.CassandraResources.ListCassandraTablesWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName).GetAwaiter().GetResult().Body;
                Assert.NotNull(cassandraTables);

                foreach (CassandraTableGetResults cassandraTable in cassandraTables)
                {
                    cosmosDBManagementClient.CassandraResources.DeleteCassandraTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, keyspaceName, cassandraTable.Name);
                }

                foreach (CassandraKeyspaceGetResults cassandraKeyspace in cassandraKeyspaces)
                {
                    cosmosDBManagementClient.CassandraResources.DeleteCassandraKeyspaceWithHttpMessagesAsync(resourceGroupName, databaseAccountName, cassandraKeyspace.Name);
                }
            }
        }
        public void SqlRoleTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);

                SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters = new SqlRoleDefinitionCreateUpdateParameters
                {
                    RoleName         = "roleName",
                    Type             = RoleDefinitionType.CustomRole,
                    AssignableScopes = new List <string>
                    {
                        string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}", cosmosDBManagementClient.SubscriptionId, resourceGroupName, databaseAccountName2),
                    },
                    Permissions = new List <Permission>
                    {
                        new Permission
                        {
                            DataActions = new List <string>
                            {
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/create",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/read"
                            }
                        }
                    }
                };

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId, resourceGroupName, databaseAccountName2, sqlRoleDefinitionCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults);
                Assert.Equal(roleDefinitionId, sqlRoleDefinitionGetResults.Name);
                VerifyCreateUpdateRoleDefinition(sqlRoleDefinitionCreateUpdateParameters, sqlRoleDefinitionGetResults);

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults2 = cosmosDBManagementClient.SqlResources.GetSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId, resourceGroupName, databaseAccountName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults2);
                Assert.Equal(roleDefinitionId, sqlRoleDefinitionGetResults2.Name);

                VerifyEqualSqlRoleDefinitions(sqlRoleDefinitionGetResults, sqlRoleDefinitionGetResults2);

                SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters2 = new SqlRoleDefinitionCreateUpdateParameters
                {
                    RoleName         = "roleName2",
                    Type             = RoleDefinitionType.CustomRole,
                    AssignableScopes = new List <string>
                    {
                        string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}", cosmosDBManagementClient.SubscriptionId, resourceGroupName, databaseAccountName2)
                    },
                    Permissions = new List <Permission>
                    {
                        new Permission
                        {
                            DataActions = new List <string>
                            {
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/create",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/read",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/delete",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/replace"
                            }
                        }
                    }
                };

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults3 = cosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId2, resourceGroupName, databaseAccountName2, sqlRoleDefinitionCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults3);
                Assert.Equal(roleDefinitionId2, sqlRoleDefinitionGetResults3.Name);
                VerifyCreateUpdateRoleDefinition(sqlRoleDefinitionCreateUpdateParameters2, sqlRoleDefinitionGetResults3);

                SqlRoleDefinitionCreateUpdateParameters sqlRoleDefinitionCreateUpdateParameters3 = new SqlRoleDefinitionCreateUpdateParameters
                {
                    RoleName         = "roleName3",
                    Type             = RoleDefinitionType.CustomRole,
                    AssignableScopes = new List <string>
                    {
                        string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}", cosmosDBManagementClient.SubscriptionId, resourceGroupName, databaseAccountName)
                    },
                    Permissions = new List <Permission>
                    {
                        new Permission
                        {
                            DataActions = new List <string>
                            {
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/create",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/delete",
                                "Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/replace"
                            }
                        }
                    }
                };

                SqlRoleDefinitionGetResults sqlRoleDefinitionGetResults4 = cosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleDefinitionWithHttpMessagesAsync(roleDefinitionId, resourceGroupName, databaseAccountName2, sqlRoleDefinitionCreateUpdateParameters3).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitionGetResults4);
                Assert.Equal(roleDefinitionId, sqlRoleDefinitionGetResults4.Name);
                VerifyCreateUpdateRoleDefinition(sqlRoleDefinitionCreateUpdateParameters3, sqlRoleDefinitionGetResults4);

                IEnumerable <SqlRoleDefinitionGetResults> sqlRoleDefinitions = cosmosDBManagementClient.SqlResources.ListSqlRoleDefinitionsWithHttpMessagesAsync(resourceGroupName, databaseAccountName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleDefinitions);
                foreach (SqlRoleDefinitionGetResults sqlRoleDefinition in sqlRoleDefinitions)
                {
                    VerifyEqualSqlRoleDefinitions(sqlRoleDefinition.Name == sqlRoleDefinitionGetResults3.Name ? sqlRoleDefinitionGetResults3 : sqlRoleDefinitionGetResults4, sqlRoleDefinition);
                }

                SqlRoleAssignmentCreateUpdateParameters sqlRoleAssignmentCreateUpdateParameters = new SqlRoleAssignmentCreateUpdateParameters
                {
                    RoleDefinitionId = sqlRoleDefinitionGetResults.Id,
                    Scope            = string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}/dbs/{3}", cosmosDBManagementClient.SubscriptionId, resourceGroupName, databaseAccountName2, databaseName),
                    PrincipalId      = principalId
                };

                SqlRoleAssignmentGetResults sqlRoleAssignmentGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleAssignmentWithHttpMessagesAsync(roleAssignmentId, resourceGroupName, databaseAccountName2, sqlRoleAssignmentCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignmentGetResults);
                Assert.Equal(roleAssignmentId, sqlRoleAssignmentGetResults.Name);
                VerifyCreateUpdateRoleAssignment(sqlRoleAssignmentCreateUpdateParameters, sqlRoleAssignmentGetResults);

                SqlRoleAssignmentGetResults sqlRoleAssignmentGetResults2 = cosmosDBManagementClient.SqlResources.GetSqlRoleAssignmentWithHttpMessagesAsync(roleAssignmentId, resourceGroupName, databaseAccountName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignmentGetResults2);
                Assert.Equal(roleAssignmentId, sqlRoleAssignmentGetResults2.Name);

                VerifyEqualSqlRoleAssignments(sqlRoleAssignmentGetResults, sqlRoleAssignmentGetResults2);

                SqlRoleAssignmentCreateUpdateParameters sqlRoleAssignmentCreateUpdateParameters2 = new SqlRoleAssignmentCreateUpdateParameters
                {
                    RoleDefinitionId = sqlRoleDefinitionGetResults3.Id,
                    Scope            = string.Format("/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.DocumentDB/databaseAccounts/{2}", cosmosDBManagementClient.SubscriptionId, resourceGroupName, databaseAccountName2),
                    PrincipalId      = principalId2
                };

                SqlRoleAssignmentGetResults sqlRoleAssignmentGetResults3 = cosmosDBManagementClient.SqlResources.CreateUpdateSqlRoleAssignmentWithHttpMessagesAsync(roleAssignmentId2, resourceGroupName, databaseAccountName2, sqlRoleAssignmentCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignmentGetResults3);
                Assert.Equal(roleAssignmentId2, sqlRoleAssignmentGetResults3.Name);
                VerifyCreateUpdateRoleAssignment(sqlRoleAssignmentCreateUpdateParameters2, sqlRoleAssignmentGetResults3);

                IEnumerable <SqlRoleAssignmentGetResults> sqlRoleAssignments = cosmosDBManagementClient.SqlResources.ListSqlRoleAssignmentsWithHttpMessagesAsync(resourceGroupName, databaseAccountName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlRoleAssignments);
                foreach (SqlRoleAssignmentGetResults sqlRoleAssignment in sqlRoleAssignments)
                {
                    VerifyEqualSqlRoleAssignments(sqlRoleAssignment.Name == sqlRoleAssignmentGetResults.Name ? sqlRoleAssignmentGetResults : sqlRoleAssignmentGetResults3, sqlRoleAssignment);
                }

                foreach (SqlRoleAssignmentGetResults sqlRoleAssignment in sqlRoleAssignments)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlRoleAssignmentWithHttpMessagesAsync(sqlRoleAssignment.Name, resourceGroupName, databaseAccountName2).GetAwaiter().GetResult();
                }

                foreach (SqlRoleDefinitionGetResults sqlRoleDefinition in sqlRoleDefinitions)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlRoleDefinitionWithHttpMessagesAsync(sqlRoleDefinition.Name, resourceGroupName, databaseAccountName2).GetAwaiter().GetResult();
                }
            }
        }
        public void TableCRUDTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);
                ResourceManagementClient resourcesClient          = CosmosDBTestUtilities.GetResourceManagementClient(context, handler2);

                string resourceGroupName   = "CosmosDBResourceGroup2510";
                string databaseAccountName = "db2527";

                bool isDatabaseNameExists = cosmosDBManagementClient.DatabaseAccounts.CheckNameExistsWithHttpMessagesAsync(databaseAccountName).GetAwaiter().GetResult().Body;

                //DatabaseAccountGetResults databaseAccount = null;
                if (!isDatabaseNameExists)
                {
                    return;
                    // SDK doesnt support creation of Cassandra, Table, Gremlin Accounts, use accounts created using Azure portal

                    // List<Location> locations = new List<Location>();
                    // locations.Add(new Location(locationName: "East US"));
                    // DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters
                    // {
                    //     Location = "EAST US",
                    //     Locations = locations
                    // };

                    //databaseAccount = cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseAccountCreateUpdateParameters).GetAwaiter().GetResult().Body;
                }

                string tableName  = "tableName2527";
                string tableName2 = "tableName22527";
                TableCreateUpdateParameters tableCreateUpdateParameters = new TableCreateUpdateParameters
                {
                    Resource = new TableResource {
                        Id = tableName
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "foo", "bar" }
                    }
                };

                TableGetResults tableGetResults = cosmosDBManagementClient.TableResources.CreateUpdateTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, tableName, tableCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(tableGetResults);
                Assert.Equal(tableName, tableGetResults.Name);

                TableGetResults tableGetResults2 = cosmosDBManagementClient.TableResources.GetTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, tableName).GetAwaiter().GetResult().Body;
                Assert.NotNull(tableGetResults2);
                Assert.Equal(tableName, tableGetResults2.Name);

                VerifyEqualTables(tableGetResults, tableGetResults2);

                TableCreateUpdateParameters tableCreateUpdateParameters2 = new TableCreateUpdateParameters
                {
                    Location = "EAST US",
                    Tags     = new Dictionary <string, string>
                    {
                        { "key3", "value3" },
                        { "key4", "value4" }
                    },
                    Resource = new TableResource {
                        Id = tableName2
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "Throughput", "700" }
                    }
                };

                TableGetResults tableGetResults3 = cosmosDBManagementClient.TableResources.CreateUpdateTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, tableName2, tableCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(tableGetResults3);
                Assert.Equal(tableName2, tableGetResults3.Name);

                IEnumerable <TableGetResults> tables = cosmosDBManagementClient.TableResources.ListTablesWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(tables);

                ThroughputSettingsGetResults throughputSettingsGetResults = cosmosDBManagementClient.TableResources.GetTableThroughputWithHttpMessagesAsync(resourceGroupName, databaseAccountName, tableName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(throughputSettingsGetResults);
                Assert.NotNull(throughputSettingsGetResults.Name);
                Assert.Equal("Microsoft.DocumentDB/databaseAccounts/tables/throughputSettings", throughputSettingsGetResults.Type);

                foreach (TableGetResults table in tables)
                {
                    cosmosDBManagementClient.TableResources.DeleteTableWithHttpMessagesAsync(resourceGroupName, databaseAccountName, table.Name);
                }
            }
        }
Beispiel #15
0
        public void GraphCRUDTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);
                ResourceManagementClient resourcesClient          = CosmosDBTestUtilities.GetResourceManagementClient(context, handler2);

                string resourceGroupName   = "CosmosDBResourceGroup2510";
                string databaseAccountName = "db1002";

                bool isDatabaseNameExists = cosmosDBManagementClient.DatabaseAccounts.CheckNameExistsWithHttpMessagesAsync(databaseAccountName).GetAwaiter().GetResult().Body;

                //DatabaseAccountGetResults databaseAccount = null;
                if (!isDatabaseNameExists)
                {
                    return;
                    // SDK doesnt support creation of Cassandra, Table, Gremlin Accounts, use accounts created using Azure portal

                    // List<Location> locations = new List<Location>();
                    // locations.Add(new Location(locationName: "East US"));
                    // DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters
                    // {
                    //     Location = "EAST US",
                    //     Locations = locations
                    // };

                    //databaseAccount = cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseAccountCreateUpdateParameters).GetAwaiter().GetResult().Body;
                }

                string databaseName  = "databaseName1002";
                string databaseName2 = "databaseName21002";
                GremlinDatabaseCreateUpdateParameters gremlinDatabaseCreateUpdateParameters = new GremlinDatabaseCreateUpdateParameters
                {
                    Resource = new GremlinDatabaseResource {
                        Id = databaseName
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "foo", "bar" }
                    }
                };

                GremlinDatabaseGetResults gremlinDatabaseGetResults = cosmosDBManagementClient.GremlinResources.CreateUpdateGremlinDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, gremlinDatabaseCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinDatabaseGetResults);
                Assert.Equal(databaseName, gremlinDatabaseGetResults.Name);

                GremlinDatabaseGetResults gremlinDatabaseGetResults1 = cosmosDBManagementClient.GremlinResources.GetGremlinDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinDatabaseGetResults1);
                Assert.Equal(databaseName, gremlinDatabaseGetResults1.Name);

                VerifyEqualGremlinDatabases(gremlinDatabaseGetResults, gremlinDatabaseGetResults1);

                GremlinDatabaseCreateUpdateParameters gremlinDatabaseCreateUpdateParameters2 = new GremlinDatabaseCreateUpdateParameters
                {
                    Location = "EAST US",
                    Tags     = new Dictionary <string, string>
                    {
                        { "key3", "value3" },
                        { "key4", "value4" }
                    },
                    Resource = new GremlinDatabaseResource {
                        Id = databaseName2
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "Throughput", "700" }
                    }
                };

                GremlinDatabaseGetResults gremlinDatabaseGetResults2 = cosmosDBManagementClient.GremlinResources.CreateUpdateGremlinDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2, gremlinDatabaseCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinDatabaseGetResults2);
                Assert.Equal(databaseName2, gremlinDatabaseGetResults2.Name);

                IEnumerable <GremlinDatabaseGetResults> gremlinDatabases = cosmosDBManagementClient.GremlinResources.ListGremlinDatabasesWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinDatabases);

                ThroughputSettingsGetResults throughputSettingsGetResults = cosmosDBManagementClient.GremlinResources.GetGremlinDatabaseThroughputWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(throughputSettingsGetResults);
                Assert.NotNull(throughputSettingsGetResults.Name);
                Assert.Equal("Microsoft.DocumentDB/databaseAccounts/gremlinDatabases/throughputSettings", throughputSettingsGetResults.Type);

                string gremlinGraphName = "gremlinGraphName1002";

                GremlinGraphCreateUpdateParameters gremlinGraphCreateUpdateParameters = new GremlinGraphCreateUpdateParameters
                {
                    Resource = new GremlinGraphResource
                    {
                        Id           = gremlinGraphName,
                        PartitionKey = new ContainerPartitionKey
                        {
                            Kind  = "Hash",
                            Paths = new List <string> {
                                "/address"
                            }
                        }
                    },
                    Options = new Dictionary <string, string>()
                    {
                        { "Throughput", "700" }
                    }
                };

                GremlinGraphGetResults gremlinGraphGetResults = cosmosDBManagementClient.GremlinResources.CreateUpdateGremlinGraphWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, gremlinGraphName, gremlinGraphCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinGraphGetResults);

                IEnumerable <GremlinGraphGetResults> gremlinGraphs = cosmosDBManagementClient.GremlinResources.ListGremlinGraphsWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinGraphs);

                foreach (GremlinGraphGetResults gremlinGraph in gremlinGraphs)
                {
                    cosmosDBManagementClient.GremlinResources.DeleteGremlinGraphWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, gremlinGraph.Name);
                }

                foreach (GremlinDatabaseGetResults gremlinDatabase in gremlinDatabases)
                {
                    cosmosDBManagementClient.GremlinResources.DeleteGremlinDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, gremlinDatabase.Name);
                }
            }
        }
        public void DatabaseAccountCRUDTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);
                ResourceManagementClient resourcesClient          = CosmosDBTestUtilities.GetResourceManagementClient(context, handler2);

                string resourceGroupName   = CosmosDBTestUtilities.CreateResourceGroup(resourcesClient);
                string databaseAccountName = TestUtilities.GenerateName(prefix: "accountname");

                List <Location> locations = new List <Location>();
                locations.Add(new Location(locationName: location));
                DefaultRequestDatabaseAccountCreateUpdateProperties databaseAccountCreateUpdateProperties = new DefaultRequestDatabaseAccountCreateUpdateProperties
                {
                    ConsistencyPolicy = new ConsistencyPolicy
                    {
                        DefaultConsistencyLevel = DefaultConsistencyLevel.BoundedStaleness,
                        MaxStalenessPrefix      = 300,
                        MaxIntervalInSeconds    = 1000
                    },
                    Locations = locations,
                    IpRules   = new List <IpAddressOrRange>
                    {
                        new IpAddressOrRange("23.43.230.120")
                    },
                    IsVirtualNetworkFilterEnabled = true,
                    EnableAutomaticFailover       = false,
                    EnableMultipleWriteLocations  = true,
                    EnableCassandraConnector      = true,
                    ConnectorOffer = "Small",
                    DisableKeyBasedMetadataWriteAccess = false
                };

                DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters
                {
                    Location = location,
                    Tags     = new Dictionary <string, string>
                    {
                        { "key1", "value1" },
                        { "key2", "value2" }
                    },
                    Kind       = "MongoDB",
                    Properties = databaseAccountCreateUpdateProperties
                };

                DatabaseAccountGetResults databaseAccount = cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseAccountCreateUpdateParameters).GetAwaiter().GetResult().Body;

                VerifyCosmosDBAccount(databaseAccount, databaseAccountCreateUpdateParameters);
                Assert.Equal(databaseAccountName, databaseAccount.Name);

                DatabaseAccountGetResults readDatabaseAccount = cosmosDBManagementClient.DatabaseAccounts.GetWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                VerifyCosmosDBAccount(readDatabaseAccount, databaseAccountCreateUpdateParameters);
                Assert.Equal(databaseAccountName, readDatabaseAccount.Name);

                DatabaseAccountUpdateParameters databaseAccountUpdateParameters = new DatabaseAccountUpdateParameters
                {
                    Location = location,
                    Tags     = new Dictionary <string, string>
                    {
                        { "key3", "value3" },
                        { "key4", "value4" }
                    },
                    ConsistencyPolicy = new ConsistencyPolicy
                    {
                        DefaultConsistencyLevel = DefaultConsistencyLevel.Session,
                        MaxStalenessPrefix      = 1300,
                        MaxIntervalInSeconds    = 12000
                    },
                    Locations = locations,
                    IpRules   = new List <IpAddressOrRange>
                    {
                        new IpAddressOrRange("23.43.230.120")
                    },
                    IsVirtualNetworkFilterEnabled = false,
                    EnableAutomaticFailover       = true,
                    EnableCassandraConnector      = true,
                    ConnectorOffer = "Small",
                    DisableKeyBasedMetadataWriteAccess = true
                };

                DatabaseAccountGetResults updatedDatabaseAccount = cosmosDBManagementClient.DatabaseAccounts.UpdateWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseAccountUpdateParameters).GetAwaiter().GetResult().Body;

                VerifyCosmosDBAccount(updatedDatabaseAccount, databaseAccountUpdateParameters);
                Assert.Equal(databaseAccountName, databaseAccount.Name);

                IEnumerable <DatabaseAccountGetResults> databaseAccounts = cosmosDBManagementClient.DatabaseAccounts.ListWithHttpMessagesAsync().GetAwaiter().GetResult().Body;
                Assert.NotNull(databaseAccounts);

                IEnumerable <DatabaseAccountGetResults> databaseAccountsByResourceGroupName = cosmosDBManagementClient.DatabaseAccounts.ListByResourceGroupWithHttpMessagesAsync(resourceGroupName).GetAwaiter().GetResult().Body;
                Assert.NotNull(databaseAccountsByResourceGroupName);

                DatabaseAccountListKeysResult databaseAccountListKeysResult = cosmosDBManagementClient.DatabaseAccounts.ListKeysWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;

                Assert.NotNull(databaseAccountListKeysResult.PrimaryMasterKey);
                Assert.NotNull(databaseAccountListKeysResult.SecondaryMasterKey);
                Assert.NotNull(databaseAccountListKeysResult.PrimaryReadonlyMasterKey);
                Assert.NotNull(databaseAccountListKeysResult.SecondaryReadonlyMasterKey);

                DatabaseAccountListConnectionStringsResult databaseAccountListConnectionStringsResult = cosmosDBManagementClient.DatabaseAccounts.ListConnectionStringsWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(databaseAccountListConnectionStringsResult);

                DatabaseAccountListReadOnlyKeysResult databaseAccountGetReadOnlyKeysResult = cosmosDBManagementClient.DatabaseAccounts.GetReadOnlyKeysWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(databaseAccountGetReadOnlyKeysResult);

                DatabaseAccountListReadOnlyKeysResult databaseAccountListReadOnlyKeysResult = cosmosDBManagementClient.DatabaseAccounts.ListReadOnlyKeysWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(databaseAccountListReadOnlyKeysResult);

                cosmosDBManagementClient.DatabaseAccounts.RegenerateKeyWithHttpMessagesAsync(resourceGroupName, databaseAccountName, new DatabaseAccountRegenerateKeyParameters {
                    KeyKind = "primary"
                });
                cosmosDBManagementClient.DatabaseAccounts.RegenerateKeyWithHttpMessagesAsync(resourceGroupName, databaseAccountName, new DatabaseAccountRegenerateKeyParameters {
                    KeyKind = "secondary"
                });
                cosmosDBManagementClient.DatabaseAccounts.RegenerateKeyWithHttpMessagesAsync(resourceGroupName, databaseAccountName, new DatabaseAccountRegenerateKeyParameters {
                    KeyKind = "primaryReadonly"
                });
                cosmosDBManagementClient.DatabaseAccounts.RegenerateKeyWithHttpMessagesAsync(resourceGroupName, databaseAccountName, new DatabaseAccountRegenerateKeyParameters {
                    KeyKind = "secondaryReadonly"
                });

                DatabaseAccountListKeysResult databaseAccountListRegeneratedKeysResult = cosmosDBManagementClient.DatabaseAccounts.ListKeysWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;

                cosmosDBManagementClient.DatabaseAccounts.DeleteWithHttpMessagesAsync(resourceGroupName, databaseAccountName);
            }
        }
Beispiel #17
0
        public void GraphCRUDTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);

                bool isDatabaseNameExists = cosmosDBManagementClient.DatabaseAccounts.CheckNameExistsWithHttpMessagesAsync(databaseAccountName).GetAwaiter().GetResult().Body;

                if (!isDatabaseNameExists)
                {
                    return;
                }

                GremlinDatabaseCreateUpdateParameters gremlinDatabaseCreateUpdateParameters = new GremlinDatabaseCreateUpdateParameters
                {
                    Resource = new GremlinDatabaseResource {
                        Id = databaseName
                    },
                    Options = new CreateUpdateOptions
                    {
                        AdditionalProperties = additionalProperties
                    }
                };

                GremlinDatabaseGetResults gremlinDatabaseGetResults = cosmosDBManagementClient.GremlinResources.CreateUpdateGremlinDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, gremlinDatabaseCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinDatabaseGetResults);
                Assert.Equal(databaseName, gremlinDatabaseGetResults.Name);

                GremlinDatabaseGetResults gremlinDatabaseGetResults1 = cosmosDBManagementClient.GremlinResources.GetGremlinDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinDatabaseGetResults1);
                Assert.Equal(databaseName, gremlinDatabaseGetResults1.Name);

                VerifyEqualGremlinDatabases(gremlinDatabaseGetResults, gremlinDatabaseGetResults1);

                GremlinDatabaseCreateUpdateParameters gremlinDatabaseCreateUpdateParameters2 = new GremlinDatabaseCreateUpdateParameters
                {
                    Location = location,
                    Tags     = tags,
                    Resource = new GremlinDatabaseResource {
                        Id = databaseName2
                    },
                    Options = new CreateUpdateOptions
                    {
                        Throughput = sampleThroughput.ToString()
                    }
                };

                GremlinDatabaseGetResults gremlinDatabaseGetResults2 = cosmosDBManagementClient.GremlinResources.CreateUpdateGremlinDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2, gremlinDatabaseCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinDatabaseGetResults2);
                Assert.Equal(databaseName2, gremlinDatabaseGetResults2.Name);

                IEnumerable <GremlinDatabaseGetResults> gremlinDatabases = cosmosDBManagementClient.GremlinResources.ListGremlinDatabasesWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinDatabases);

                ThroughputSettingsGetResults throughputSettingsGetResults = cosmosDBManagementClient.GremlinResources.GetGremlinDatabaseThroughputWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(throughputSettingsGetResults);
                Assert.NotNull(throughputSettingsGetResults.Name);
                Assert.Equal(throughputSettingsGetResults.Resource.Throughput, sampleThroughput);
                Assert.Equal(graphThroughputType, throughputSettingsGetResults.Type);

                GremlinGraphCreateUpdateParameters gremlinGraphCreateUpdateParameters = new GremlinGraphCreateUpdateParameters
                {
                    Resource = new GremlinGraphResource
                    {
                        Id           = gremlinGraphName,
                        DefaultTtl   = -1,
                        PartitionKey = new ContainerPartitionKey
                        {
                            Kind  = "Hash",
                            Paths = new List <string> {
                                "/address"
                            }
                        },
                        IndexingPolicy = new IndexingPolicy
                        {
                            Automatic     = true,
                            IndexingMode  = IndexingMode.Consistent,
                            IncludedPaths = new List <IncludedPath>
                            {
                                new IncludedPath {
                                    Path = "/*"
                                }
                            },
                            ExcludedPaths = new List <ExcludedPath>
                            {
                                new ExcludedPath {
                                    Path = "/pathToNotIndex/*"
                                }
                            },
                            CompositeIndexes = new List <IList <CompositePath> >
                            {
                                new List <CompositePath>
                                {
                                    new CompositePath {
                                        Path = "/orderByPath1", Order = CompositePathSortOrder.Ascending
                                    },
                                    new CompositePath {
                                        Path = "/orderByPath2", Order = CompositePathSortOrder.Descending
                                    }
                                },
                                new List <CompositePath>
                                {
                                    new CompositePath {
                                        Path = "/orderByPath3", Order = CompositePathSortOrder.Ascending
                                    },
                                    new CompositePath {
                                        Path = "/orderByPath4", Order = CompositePathSortOrder.Descending
                                    }
                                }
                            }
                        }
                    },
                    Options = new CreateUpdateOptions
                    {
                        Throughput = sampleThroughput.ToString()
                    }
                };

                GremlinGraphGetResults gremlinGraphGetResults = cosmosDBManagementClient.GremlinResources.CreateUpdateGremlinGraphWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, gremlinGraphName, gremlinGraphCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinGraphGetResults);
                VerifyGremlinGraphCreation(gremlinGraphGetResults, gremlinGraphCreateUpdateParameters);

                IEnumerable <GremlinGraphGetResults> gremlinGraphs = cosmosDBManagementClient.GremlinResources.ListGremlinGraphsWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(gremlinGraphs);

                foreach (GremlinGraphGetResults gremlinGraph in gremlinGraphs)
                {
                    cosmosDBManagementClient.GremlinResources.DeleteGremlinGraphWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, gremlinGraph.Name);
                }

                foreach (GremlinDatabaseGetResults gremlinDatabase in gremlinDatabases)
                {
                    cosmosDBManagementClient.GremlinResources.DeleteGremlinDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, gremlinDatabase.Name);
                }
            }
        }
        public void SqlCRUDTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);

                bool isDatabaseNameExists = cosmosDBManagementClient.DatabaseAccounts.CheckNameExistsWithHttpMessagesAsync(databaseAccountName).GetAwaiter().GetResult().Body;

                DatabaseAccountGetResults databaseAccount = null;
                if (!isDatabaseNameExists)
                {
                    DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters
                    {
                        Location  = location,
                        Kind      = DatabaseAccountKind.GlobalDocumentDB,
                        Locations = new List <Location>()
                        {
                            { new Location(locationName: location) }
                        }
                    };

                    databaseAccount = cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseAccountCreateUpdateParameters).GetAwaiter().GetResult().Body;
                    Assert.Equal(databaseAccount.Name, databaseAccountName);
                }

                SqlDatabaseCreateUpdateParameters sqlDatabaseCreateUpdateParameters = new SqlDatabaseCreateUpdateParameters
                {
                    Resource = new SqlDatabaseResource {
                        Id = databaseName
                    },
                    Options = new CreateUpdateOptions()
                };

                SqlDatabaseGetResults sqlDatabaseGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, sqlDatabaseCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlDatabaseGetResults);
                Assert.Equal(databaseName, sqlDatabaseGetResults.Name);

                SqlDatabaseGetResults sqlDatabaseGetResults2 = cosmosDBManagementClient.SqlResources.GetSqlDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlDatabaseGetResults2);
                Assert.Equal(databaseName, sqlDatabaseGetResults2.Name);

                VerifyEqualSqlDatabases(sqlDatabaseGetResults, sqlDatabaseGetResults2);

                SqlDatabaseCreateUpdateParameters sqlDatabaseCreateUpdateParameters2 = new SqlDatabaseCreateUpdateParameters
                {
                    Location = location,
                    Tags     = tags,
                    Resource = new SqlDatabaseResource {
                        Id = databaseName2
                    },
                    Options = new CreateUpdateOptions
                    {
                        Throughput = sampleThroughput
                    }
                };

                SqlDatabaseGetResults sqlDatabaseGetResults3 = cosmosDBManagementClient.SqlResources.CreateUpdateSqlDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2, sqlDatabaseCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlDatabaseGetResults3);
                Assert.Equal(databaseName2, sqlDatabaseGetResults3.Name);

                IEnumerable <SqlDatabaseGetResults> sqlDatabases = cosmosDBManagementClient.SqlResources.ListSqlDatabasesWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlDatabases);

                ThroughputSettingsGetResults throughputSettingsGetResults = cosmosDBManagementClient.SqlResources.GetSqlDatabaseThroughputWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(throughputSettingsGetResults);
                Assert.NotNull(throughputSettingsGetResults.Name);
                Assert.Equal(sqlThroughputType, throughputSettingsGetResults.Type);

                SqlContainerCreateUpdateParameters sqlContainerCreateUpdateParameters = new SqlContainerCreateUpdateParameters
                {
                    Resource = new SqlContainerResource {
                        Id           = containerName,
                        PartitionKey = new ContainerPartitionKey
                        {
                            Kind  = "Hash",
                            Paths = new List <string> {
                                "/address/zipCode"
                            }
                        },
                        IndexingPolicy = new IndexingPolicy
                        {
                            Automatic     = true,
                            IndexingMode  = IndexingMode.Consistent,
                            IncludedPaths = new List <IncludedPath>
                            {
                                new IncludedPath {
                                    Path = "/*"
                                }
                            },
                            ExcludedPaths = new List <ExcludedPath>
                            {
                                new ExcludedPath {
                                    Path = "/pathToNotIndex/*"
                                }
                            },
                            CompositeIndexes = new List <IList <CompositePath> >
                            {
                                new List <CompositePath>
                                {
                                    new CompositePath {
                                        Path = "/orderByPath1", Order = CompositePathSortOrder.Ascending
                                    },
                                    new CompositePath {
                                        Path = "/orderByPath2", Order = CompositePathSortOrder.Descending
                                    }
                                },
                                new List <CompositePath>
                                {
                                    new CompositePath {
                                        Path = "/orderByPath3", Order = CompositePathSortOrder.Ascending
                                    },
                                    new CompositePath {
                                        Path = "/orderByPath4", Order = CompositePathSortOrder.Descending
                                    }
                                }
                            }
                        }
                    },
                    Options = new CreateUpdateOptions
                    {
                        Throughput = sampleThroughput
                    }
                };

                SqlContainerGetResults sqlContainerGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlContainerWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, sqlContainerCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlContainerGetResults);

                IEnumerable <SqlContainerGetResults> sqlContainers = cosmosDBManagementClient.SqlResources.ListSqlContainersWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlContainers);

                SqlStoredProcedureCreateUpdateParameters sqlStoredProcedureCreateUpdateParameters = new SqlStoredProcedureCreateUpdateParameters
                {
                    Resource = new SqlStoredProcedureResource
                    {
                        Id   = storedProcedureName,
                        Body = "function () { var context = getContext(); " +
                               "var response = context.getResponse();" +
                               "response.setBody('Hello, World');" +
                               "}"
                    },
                    Options = new CreateUpdateOptions()
                };

                SqlStoredProcedureGetResults sqlStoredProcedureGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlStoredProcedureWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, storedProcedureName, sqlStoredProcedureCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlStoredProcedureGetResults);
                Assert.Equal(sqlStoredProcedureGetResults.Resource.Body, sqlStoredProcedureGetResults.Resource.Body);

                IEnumerable <SqlStoredProcedureGetResults> sqlStoredProcedures = cosmosDBManagementClient.SqlResources.ListSqlStoredProceduresWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlStoredProcedures);

                foreach (SqlStoredProcedureGetResults sqlStoredProcedure in sqlStoredProcedures)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlStoredProcedureWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, sqlStoredProcedure.Name);
                }

                SqlUserDefinedFunctionCreateUpdateParameters sqlUserDefinedFunctionCreateUpdateParameters = new SqlUserDefinedFunctionCreateUpdateParameters
                {
                    Resource = new SqlUserDefinedFunctionResource
                    {
                        Id   = userDefinedFunctionName,
                        Body = "function () { var context = getContext(); " +
                               "var response = context.getResponse();" +
                               "response.setBody('Hello, World');" +
                               "}"
                    },
                    Options = new CreateUpdateOptions()
                };

                SqlUserDefinedFunctionGetResults sqlUserDefinedFunctionGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlUserDefinedFunctionWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, userDefinedFunctionName, sqlUserDefinedFunctionCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlUserDefinedFunctionGetResults);
                Assert.Equal(sqlUserDefinedFunctionGetResults.Resource.Body, sqlUserDefinedFunctionGetResults.Resource.Body);


                IEnumerable <SqlUserDefinedFunctionGetResults> sqlUserDefinedFunctions = cosmosDBManagementClient.SqlResources.ListSqlUserDefinedFunctionsWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlUserDefinedFunctions);

                foreach (SqlUserDefinedFunctionGetResults sqlUserDefinedFunction in sqlUserDefinedFunctions)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlUserDefinedFunctionWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, sqlUserDefinedFunction.Name);
                }

                SqlTriggerCreateUpdateParameters sqlTriggerCreateUpdateParameters = new SqlTriggerCreateUpdateParameters
                {
                    Resource = new SqlTriggerResource
                    {
                        Id = triggerName,
                        TriggerOperation = "All",
                        TriggerType      = "Pre",
                        Body             = "function () { var context = getContext(); " +
                                           "var response = context.getResponse();" +
                                           "response.setBody('Hello, World');" +
                                           "}"
                    },
                    Options = new CreateUpdateOptions()
                };

                SqlTriggerGetResults sqlTriggerGetResults = cosmosDBManagementClient.SqlResources.CreateUpdateSqlTriggerWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, triggerName, sqlTriggerCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlTriggerGetResults);
                Assert.Equal(sqlTriggerGetResults.Resource.TriggerType, sqlTriggerCreateUpdateParameters.Resource.TriggerType);
                Assert.Equal(sqlTriggerGetResults.Resource.TriggerOperation, sqlTriggerCreateUpdateParameters.Resource.TriggerOperation);
                Assert.Equal(sqlTriggerGetResults.Resource.Body, sqlTriggerCreateUpdateParameters.Resource.Body);

                IEnumerable <SqlTriggerGetResults> sqlTriggers = cosmosDBManagementClient.SqlResources.ListSqlTriggersWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName).GetAwaiter().GetResult().Body;
                Assert.NotNull(sqlTriggers);

                foreach (SqlTriggerGetResults sqlTrigger in sqlTriggers)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlTriggerWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, containerName, sqlTrigger.Name);
                }

                foreach (SqlContainerGetResults sqlContainer in sqlContainers)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlContainerWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, sqlContainer.Name);
                }

                foreach (SqlDatabaseGetResults sqlDatabase in sqlDatabases)
                {
                    cosmosDBManagementClient.SqlResources.DeleteSqlDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, sqlDatabase.Name);
                }
            }
        }
Beispiel #19
0
        public void RetrieveContinuousBackupInfoTest()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient        cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);
                ContinuousBackupRestoreLocation restoreLocation          = new ContinuousBackupRestoreLocation(location);

                SqlContainerCreateUpdateParameters sqlContainerCreateUpdateParameters = new SqlContainerCreateUpdateParameters()
                {
                    Resource = new SqlContainerResource(containerName, partitionKey: new ContainerPartitionKey(new List <String>()
                    {
                        "/id"
                    })),
                    Options = new CreateUpdateOptions
                    {
                        Throughput = 400
                    }
                };

                SqlContainerGetResults createContainerResult = cosmosDBManagementClient.SqlResources.CreateUpdateSqlContainer(
                    resourceGroupName,
                    databaseAccountName,
                    databaseName,
                    containerName,
                    sqlContainerCreateUpdateParameters);
                Assert.NotNull(createContainerResult);
                Assert.NotNull(createContainerResult.Id);

                BackupInformation backupInformation = cosmosDBManagementClient.SqlResources.RetrieveContinuousBackupInformation(
                    resourceGroupName,
                    databaseAccountName,
                    databaseName,
                    containerName,
                    restoreLocation);

                Assert.NotNull(backupInformation);
                Assert.NotNull(backupInformation.ContinuousBackupInformation);
                Assert.True(Int32.Parse(backupInformation.ContinuousBackupInformation.LatestRestorableTimestamp) > 0);
                int prevRestoreTime = Int32.Parse(backupInformation.ContinuousBackupInformation.LatestRestorableTimestamp);

                ThroughputSettingsUpdateParameters throughputSettingsUpdateParameters = new ThroughputSettingsUpdateParameters()
                {
                    Resource = new ThroughputSettingsResource()
                    {
                        Throughput = 4000
                    }
                };
                ThroughputSettingsGetResults throughputSettingsGetResults = cosmosDBManagementClient.SqlResources.UpdateSqlContainerThroughput(
                    resourceGroupName,
                    databaseAccountName,
                    databaseName,
                    containerName,
                    throughputSettingsUpdateParameters);
                Assert.NotNull(throughputSettingsGetResults);
                Assert.NotNull(throughputSettingsGetResults.Id);

                backupInformation = cosmosDBManagementClient.SqlResources.RetrieveContinuousBackupInformation(
                    resourceGroupName,
                    databaseAccountName,
                    databaseName,
                    containerName,
                    restoreLocation);

                Assert.NotNull(backupInformation);
                Assert.NotNull(backupInformation.ContinuousBackupInformation);
                Assert.True(Int32.Parse(backupInformation.ContinuousBackupInformation.LatestRestorableTimestamp) >= prevRestoreTime);

                cosmosDBManagementClient.SqlResources.DeleteSqlContainer(resourceGroupName, databaseAccountName, databaseName, containerName);
            }
        }
        public void MongoCRUDTests()
        {
            var handler1 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient = CosmosDBTestUtilities.GetCosmosDBClient(context, handler1);

                bool isDatabaseNameExists = cosmosDBManagementClient.DatabaseAccounts.CheckNameExistsWithHttpMessagesAsync(databaseAccountName).GetAwaiter().GetResult().Body;

                DatabaseAccountGetResults databaseAccount = null;
                if (!isDatabaseNameExists)
                {
                    DatabaseAccountCreateUpdateParameters databaseAccountCreateUpdateParameters = new DatabaseAccountCreateUpdateParameters
                    {
                        Location   = location,
                        Kind       = DatabaseAccountKind.MongoDB,
                        Properties = new DefaultRequestDatabaseAccountCreateUpdateProperties
                        {
                            Locations = new List <Location>
                            {
                                { new Location(locationName: location) }
                            }
                        }
                    };

                    databaseAccount = cosmosDBManagementClient.DatabaseAccounts.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseAccountCreateUpdateParameters).GetAwaiter().GetResult().Body;
                    Assert.Equal(databaseAccount.Name, databaseAccountName);
                }

                MongoDBDatabaseCreateUpdateParameters mongoDBDatabaseCreateUpdateParameters = new MongoDBDatabaseCreateUpdateParameters
                {
                    Resource = new MongoDBDatabaseResource {
                        Id = databaseName
                    },
                    Options = new CreateUpdateOptions()
                };

                MongoDBDatabaseGetResults mongoDBDatabaseGetResults = cosmosDBManagementClient.MongoDBResources.CreateUpdateMongoDBDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, mongoDBDatabaseCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.Equal(databaseName, mongoDBDatabaseGetResults.Name);
                Assert.NotNull(mongoDBDatabaseGetResults);

                MongoDBDatabaseGetResults mongoDBDatabaseGetResults1 = cosmosDBManagementClient.MongoDBResources.GetMongoDBDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(mongoDBDatabaseGetResults1);
                Assert.Equal(databaseName, mongoDBDatabaseGetResults1.Name);

                VerifyEqualMongoDBDatabases(mongoDBDatabaseGetResults, mongoDBDatabaseGetResults1);

                MongoDBDatabaseCreateUpdateParameters mongoDBDatabaseCreateUpdateParameters2 = new MongoDBDatabaseCreateUpdateParameters
                {
                    Resource = new MongoDBDatabaseResource {
                        Id = databaseName2
                    },
                    Options = new CreateUpdateOptions
                    {
                        Throughput = sampleThroughput
                    }
                };

                MongoDBDatabaseGetResults mongoDBDatabaseGetResults2 = cosmosDBManagementClient.MongoDBResources.CreateUpdateMongoDBDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2, mongoDBDatabaseCreateUpdateParameters2).GetAwaiter().GetResult().Body;
                Assert.NotNull(mongoDBDatabaseGetResults2);
                Assert.Equal(databaseName2, mongoDBDatabaseGetResults2.Name);

                IEnumerable <MongoDBDatabaseGetResults> mongoDBDatabases = cosmosDBManagementClient.MongoDBResources.ListMongoDBDatabasesWithHttpMessagesAsync(resourceGroupName, databaseAccountName).GetAwaiter().GetResult().Body;
                Assert.NotNull(mongoDBDatabases);

                ThroughputSettingsGetResults throughputSettingsGetResults = cosmosDBManagementClient.MongoDBResources.GetMongoDBDatabaseThroughputWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName2).GetAwaiter().GetResult().Body;
                Assert.NotNull(throughputSettingsGetResults);
                Assert.NotNull(throughputSettingsGetResults.Name);
                Assert.Equal(throughputSettingsGetResults.Resource.Throughput, sampleThroughput);
                Assert.Equal(mongoDatabaseThroughputType, throughputSettingsGetResults.Type);

                MongoDBCollectionCreateUpdateParameters mongoDBCollectionCreateUpdateParameters = new MongoDBCollectionCreateUpdateParameters
                {
                    Resource = new MongoDBCollectionResource
                    {
                        Id = collectionName,
                    },
                    Options = new CreateUpdateOptions()
                };

                MongoDBCollectionGetResults mongoDBCollectionGetResults = cosmosDBManagementClient.MongoDBResources.CreateUpdateMongoDBCollectionWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, collectionName, mongoDBCollectionCreateUpdateParameters).GetAwaiter().GetResult().Body;
                Assert.NotNull(mongoDBCollectionGetResults);
                VerfiyMongoCollectionCreation(mongoDBCollectionGetResults, mongoDBCollectionCreateUpdateParameters);

                IEnumerable <MongoDBCollectionGetResults> mongoDBCollections = cosmosDBManagementClient.MongoDBResources.ListMongoDBCollectionsWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName).GetAwaiter().GetResult().Body;
                Assert.NotNull(mongoDBCollections);

                foreach (MongoDBCollectionGetResults mongoDBCollection in mongoDBCollections)
                {
                    cosmosDBManagementClient.MongoDBResources.DeleteMongoDBCollectionWithHttpMessagesAsync(resourceGroupName, databaseAccountName, databaseName, mongoDBCollection.Name);
                }

                foreach (MongoDBDatabaseGetResults mongoDBDatabase in mongoDBDatabases)
                {
                    cosmosDBManagementClient.MongoDBResources.DeleteMongoDBDatabaseWithHttpMessagesAsync(resourceGroupName, databaseAccountName, mongoDBDatabase.Name);
                }
            }
        }
Beispiel #21
0
        public void ManagedCassandraCRUDTests()
        {
            var handler = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };
            var handler2 = new RecordedDelegatingHandler {
                StatusCodeToReturn = HttpStatusCode.OK
            };

            using (MockContext context = MockContext.Start(this.GetType()))
            {
                // Create client
                CosmosDBManagementClient cosmosDBManagementClient =
                    CosmosDBTestUtilities.GetCosmosDBClient(context, handler);
                ResourceManagementClient resourcesClient =
                    CosmosDBTestUtilities.GetResourceManagementClient(context, handler2);

                string resourceGroupName = CosmosDBTestUtilities.CreateResourceGroup(resourcesClient);

                try
                {
                    string clusterName = TestUtilities.GenerateName("managedcluster");
                    string dcName      = TestUtilities.GenerateName("managedDC");
                    this.output.WriteLine($"Creating cluster {clusterName} in resource group {resourceGroupName}.");

                    string subnetId = CreateVirtualNetwork(resourcesClient, resourceGroupName);
                    this.output.WriteLine($"Created subnet {subnetId}.");

                    var clusterProperties = new ClusterResourceProperties
                    {
                        DelegatedManagementSubnetId = subnetId, InitialCassandraAdminPassword = "******",
                        ExternalSeedNodes           = new List <SeedNode>
                        {
                            new SeedNode {
                                IpAddress = "10.0.1.1"
                            }
                        }
                    };
                    var clusterPutResource = new ClusterResource
                    {
                        Location = "East US 2", Properties = clusterProperties
                    };
                    this.output.WriteLine($"Cluster create request body:");
                    this.output.WriteLine(JsonConvert.SerializeObject(clusterPutResource, Formatting.Indented));

                    ClusterResource clusterResource = cosmosDBManagementClient.CassandraClusters
                                                      .CreateUpdateWithHttpMessagesAsync(resourceGroupName: resourceGroupName,
                                                                                         clusterName: clusterName, body: clusterPutResource).GetAwaiter().GetResult().Body;

                    this.output.WriteLine($"Cluster create response:");
                    this.output.WriteLine(JsonConvert.SerializeObject(clusterResource, Formatting.Indented));

                    Assert.Equal(subnetId, clusterResource.Properties.DelegatedManagementSubnetId);
                    Assert.Null(clusterResource.Properties.InitialCassandraAdminPassword);
                    Assert.Equal("Cassandra", clusterResource.Properties.AuthenticationMethod);
                    Assert.Equal("Succeeded", clusterResource.Properties.ProvisioningState);
                    Assert.NotNull(clusterResource.Properties.ExternalSeedNodes);
                    Assert.Equal(1, clusterResource.Properties.ExternalSeedNodes.Count);
                    Assert.Equal("10.0.1.1", clusterResource.Properties.ExternalSeedNodes[0].IpAddress);

                    clusterPutResource.Properties.ExternalSeedNodes = new List <SeedNode>
                    {
                        new SeedNode {
                            IpAddress = "192.168.12.1"
                        }
                    };
                    this.output.WriteLine("");
                    this.output.WriteLine("Updating cluster. Put body:");
                    this.output.WriteLine(JsonConvert.SerializeObject(clusterPutResource, Formatting.Indented));

                    ClusterResource clusterResource2 = cosmosDBManagementClient.CassandraClusters
                                                       .CreateUpdateWithHttpMessagesAsync(resourceGroupName: resourceGroupName,
                                                                                          clusterName: clusterName, body: clusterPutResource).GetAwaiter().GetResult().Body;

                    this.output.WriteLine("Response:");
                    this.output.WriteLine(JsonConvert.SerializeObject(clusterResource2, Formatting.Indented));

                    Assert.Equal(clusterName, clusterResource2.Name);
                    Assert.Equal("East US 2", clusterResource2.Location);
                    Assert.Equal(subnetId, clusterResource2.Properties.DelegatedManagementSubnetId);
                    Assert.Null(clusterResource2.Properties.InitialCassandraAdminPassword);
                    Assert.Equal("Cassandra", clusterResource2.Properties.AuthenticationMethod);
                    Assert.Null(clusterResource2.Properties.CassandraVersion);
                    Assert.Equal("Succeeded", clusterResource2.Properties.ProvisioningState);
                    Assert.NotNull(clusterResource2.Properties.ExternalSeedNodes);
                    Assert.NotEmpty(clusterResource2.Properties.ExternalSeedNodes);

                    DataCenterResource dataCenterPutResource = new DataCenterResource
                    {
                        Properties = new DataCenterResourceProperties
                        {
                            DataCenterLocation = "East US 2", DelegatedSubnetId = subnetId, NodeCount = 3,
                        }
                    };
                    this.output.WriteLine($"Creating data center {dcName}. Put request:");
                    this.output.WriteLine(JsonConvert.SerializeObject(dataCenterPutResource, Formatting.Indented));
                    DataCenterResource dcResource = cosmosDBManagementClient.CassandraDataCenters
                                                    .CreateUpdateWithHttpMessagesAsync(resourceGroupName, clusterName, dcName,
                                                                                       dataCenterPutResource).GetAwaiter().GetResult().Body;
                    this.output.WriteLine("Response:");
                    this.output.WriteLine(JsonConvert.SerializeObject(dcResource, Formatting.Indented));

                    Assert.Equal("East US 2", dcResource.Properties.DataCenterLocation);
                    Assert.Equal(subnetId, dcResource.Properties.DelegatedSubnetId);
                    Assert.Equal(3, dcResource.Properties.NodeCount);
                    Assert.Equal(3, dcResource.Properties.SeedNodes.Count);

                    this.output.WriteLine($"Deleting data center {dcName}.");
                    cosmosDBManagementClient.CassandraDataCenters
                    .DeleteWithHttpMessagesAsync(resourceGroupName, clusterName, dcName).GetAwaiter().GetResult();

                    this.output.WriteLine($"Deleting cluster {clusterName}.");
                    cosmosDBManagementClient.CassandraClusters
                    .DeleteWithHttpMessagesAsync(resourceGroupName, clusterName).GetAwaiter().GetResult();

                    this.output.WriteLine("Deleting deployment of vnets.");
                    cosmosDBManagementClient.CassandraClusters
                    .DeleteWithHttpMessagesAsync(resourceGroupName, clusterName).GetAwaiter().GetResult();
                }
                finally
                {
                    this.output.WriteLine("Deleting resource group.");
                    resourcesClient.Deployments.Delete(resourceGroupName,
                                                       ManagedCassandraResourcesOperationsTests.VnetDeploymentName);
                    resourcesClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }