Example #1
0
 public CosmosDbService(
     CosmosClient dbClient,
     string databaseName,
     string containerName)
 {
     this._container = dbClient?.GetContainer(databaseName, containerName);
 }
Example #2
0
        public async Task ItemTest(bool directMode)
        {
            CosmosClient  client     = directMode ? DirectCosmosClient : GatewayCosmosClient;
            Container     container  = client.GetContainer(DatabaseId, ContainerId);
            List <string> createdIds = new List <string>()
            {
                "BasicQueryItem",
                "BasicQueryItem2",
                "BasicQueryItem3"
            };

            List <dynamic> queryResults = await this.ToListAsync(
                container.GetItemQueryStreamIterator,
                container.GetItemQueryIterator <dynamic>,
                "select * from T where STARTSWITH(T.id, \"BasicQueryItem\")",
                CosmosBasicQueryTests.RequestOptions);

            if (queryResults.Count < 3)
            {
                foreach (string id in createdIds)
                {
                    dynamic item = new
                    {
                        id = id,
                        pk = id,
                    };

                    ItemResponse <dynamic> createResponse = await container.CreateItemAsync <dynamic>(item : item);
                }

                queryResults = await this.ToListAsync(
                    container.GetItemQueryStreamIterator,
                    container.GetItemQueryIterator <dynamic>,
                    "select * from T where STARTSWITH(T.id, \"BasicQueryItem\")",
                    CosmosBasicQueryTests.RequestOptions);
            }

            List <string> ids = queryResults.Select(x => (string)x.id).ToList();

            CollectionAssert.AreEquivalent(createdIds, ids);

            //Read All
            List <dynamic> results = await this.ToListAsync(
                container.GetItemQueryStreamIterator,
                container.GetItemQueryIterator <dynamic>,
                null,
                CosmosBasicQueryTests.RequestOptions);


            ids = results.Select(x => (string)x.id).ToList();
            CollectionAssert.IsSubsetOf(createdIds, ids);

            //Read All with partition key
            results = await this.ToListAsync(
                container.GetItemQueryStreamIterator,
                container.GetItemQueryIterator <dynamic>,
                null,
                new QueryRequestOptions()
            {
                MaxItemCount = 1,
                PartitionKey = new PartitionKey("BasicQueryItem")
            });

            Assert.AreEqual(1, results.Count);

            //Read All with partition key
            results = container.GetItemLinqQueryable <dynamic>(
                allowSynchronousQueryExecution: true,
                requestOptions: new QueryRequestOptions()
            {
                MaxItemCount = 1,
                PartitionKey = new PartitionKey("BasicQueryItem")
            }).ToList();

            Assert.AreEqual(1, results.Count);

            // LINQ to feed iterator Read All with partition key
            FeedIterator <dynamic> iterator = container.GetItemLinqQueryable <dynamic>(
                allowSynchronousQueryExecution: true,
                requestOptions: new QueryRequestOptions()
            {
                MaxItemCount = 1,
                PartitionKey = new PartitionKey("BasicQueryItem")
            }).ToFeedIterator();

            List <dynamic> linqResults = new List <dynamic>();

            while (iterator.HasMoreResults)
            {
                linqResults.AddRange(await iterator.ReadNextAsync());
            }

            Assert.AreEqual(1, linqResults.Count);
            Assert.AreEqual("BasicQueryItem", linqResults.First().pk.ToString());
        }
 /// <summary>
 /// Creates new context instance.
 /// </summary>
 /// <param name="cosmosAccessor">The <see cref="CosmosClient"/> accessor.</param>
 /// <param name="databaseName">The working database name.</param>
 /// <param name="collectionName">The working container name.</param>
 public CosmosRepositoryContext(CosmosClient cosmosClient, string databaseName, string collectionName)
 {
     Client    = cosmosClient;
     Container = Client.GetContainer(databaseName, collectionName);
 }
Example #4
0
 public WishService(CosmosClient client, string dbName, string containerName)
 {
     this.container = client.GetContainer(dbName, containerName);
 }
Example #5
0
 public CosmosContainer GetContainer(string databaseName, string containerName)
 {
     return(client.GetContainer(databaseName, containerName));
 }
Example #6
0
 public EmbeddedDocuments()
 {
     client    = new CosmosClient("<<insert your connection string>>");
     container = client.GetContainer("<<insert you database name>>", "<<insert your collection name>>");
 }
 /// <summary>
 /// Delete the table passed in.
 /// </summary>
 /// <param name="tableName">Name of the table to delete.</param>
 /// <returns>Async Task.</returns>
 public async Task DeleteTable(string tableName)
 {
     var cosmosContainer = CosmosClient.GetContainer(DatabaseName, tableName);
     await cosmosContainer.DeleteContainerAsync();
 }
Example #8
0
        public async Task TestWriteForbiddenScenarioAsync()
        {
            GlobalPartitionEndpointManagerTests.SetupAccountAndCacheOperations(
                out string secondaryRegionNameForUri,
                out string globalEndpoint,
                out string secondaryRegionEndpiont,
                out string databaseName,
                out string containerName,
                out ResourceId containerResourceId,
                out Mock <IHttpHandler> mockHttpHandler,
                out IReadOnlyList <string> primaryRegionPartitionKeyRangeIds,
                out TransportAddressUri primaryRegionprimaryReplicaUri);

            Mock <TransportClient> mockTransport = new Mock <TransportClient>(MockBehavior.Strict);

            MockSetupsHelper.SetupWriteForbiddenException(
                mockTransport,
                primaryRegionprimaryReplicaUri);

            // Partition key ranges are the same in both regions so the SDK
            // does not need to go the secondary to get the partition key ranges.
            // Only the addresses need to be mocked on the secondary
            MockSetupsHelper.SetupAddresses(
                mockHttpHandler: mockHttpHandler,
                partitionKeyRangeId: primaryRegionPartitionKeyRangeIds.First(),
                regionEndpoint: secondaryRegionEndpiont,
                regionName: secondaryRegionNameForUri,
                containerResourceId: containerResourceId,
                primaryReplicaUri: out TransportAddressUri secondaryRegionPrimaryReplicaUri);

            MockSetupsHelper.SetupCreateItemResponse(
                mockTransport,
                secondaryRegionPrimaryReplicaUri);

            CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
            {
                EnablePartitionLevelFailover = true,
                ConsistencyLevel             = Cosmos.ConsistencyLevel.Strong,
                ApplicationPreferredRegions  = new List <string>()
                {
                    Regions.EastUS,
                    Regions.WestUS
                },
                HttpClientFactory             = () => new HttpClient(new HttpHandlerHelper(mockHttpHandler.Object)),
                TransportClientHandlerFactory = (original) => mockTransport.Object,
            };

            using (CosmosClient customClient = new CosmosClient(
                       globalEndpoint,
                       Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())),
                       cosmosClientOptions))
            {
                Container container = customClient.GetContainer(databaseName, containerName);

                ToDoActivity toDoActivity = new ToDoActivity()
                {
                    Id = "TestItem",
                    Pk = "TestPk"
                };

                ItemResponse <ToDoActivity> response = await container.CreateItemAsync(toDoActivity, new Cosmos.PartitionKey(toDoActivity.Pk));

                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
                mockTransport.VerifyAll();
                mockHttpHandler.VerifyAll();

                // Clears all the setups. No network calls should be done on the next operation.
                mockHttpHandler.Reset();
                mockTransport.Reset();
                mockTransport.Setup(x => x.Dispose());

                MockSetupsHelper.SetupCreateItemResponse(
                    mockTransport,
                    secondaryRegionPrimaryReplicaUri);

                ToDoActivity toDoActivity2 = new ToDoActivity()
                {
                    Id = "TestItem2",
                    Pk = "TestPk"
                };

                response = await container.CreateItemAsync(toDoActivity2, new Cosmos.PartitionKey(toDoActivity2.Pk));

                Assert.AreEqual(HttpStatusCode.Created, response.StatusCode);
            }
        }
Example #9
0
        public async Task ReadMany404ExceptionTest()
        {
            Database database = await this.cosmosClient.CreateDatabaseAsync(Guid.NewGuid().ToString());

            Container container = await database.CreateContainerAsync(Guid.NewGuid().ToString(), "/pk");

            for (int i = 0; i < 5; i++)
            {
                await container.CreateItemAsync(
                    ToDoActivity.CreateRandomToDoActivity("pk" + i, i.ToString()));
            }

            List <(string, PartitionKey)> itemList = new List <(string, PartitionKey)>();

            for (int i = 0; i < 5; i++)
            {
                itemList.Add((i.ToString(), new PartitionKey("pk" + i)));
            }

            // 429 test
            //List<Task> tasks = new List<Task>();
            //ConcurrentQueue<ResponseMessage> failedRequests = new ConcurrentQueue<ResponseMessage>();
            //for (int i = 0; i < 500; i++)
            //{
            //    tasks.Add(Task.Run(async () =>
            //    {
            //        ResponseMessage responseMessage = await container.ReadManyItemsStreamAsync(itemList);
            //        if (!responseMessage.IsSuccessStatusCode)
            //        {
            //            failedRequests.Enqueue(responseMessage);
            //            Assert.AreEqual(responseMessage.StatusCode, HttpStatusCode.TooManyRequests);
            //        }
            //    }));
            //}

            //await Task.WhenAll(tasks);
            //Assert.IsTrue(failedRequests.Count > 0);

            await container.ReadManyItemsAsync <ToDoActivity>(itemList); // Warm up caches

            using (CosmosClient cosmosClient = TestCommon.CreateCosmosClient())
            {
                Container newContainer = cosmosClient.GetContainer(database.Id, container.Id);
                await newContainer.DeleteContainerAsync();
            }

            using (ResponseMessage responseMessage = await container.ReadManyItemsStreamAsync(itemList))
            {
                Assert.AreEqual(responseMessage.StatusCode, HttpStatusCode.NotFound);
            }

            try
            {
                await container.ReadManyItemsAsync <ToDoActivity>(itemList);

                Assert.Fail("Typed API should throw");
            }
            catch (CosmosException ex)
            {
                Assert.AreEqual(ex.Error.Code, "NotFound");
            }

            await database.DeleteAsync();
        }
Example #10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BaseRepository{TEntity}"/> class.
 /// </summary>
 /// <param name="cosmosClient">cosmos client.</param>
 /// <param name="databaseName">Name of the database.</param>
 /// <param name="containerName">Name of the container.</param>
 public BaseRepository(CosmosClient cosmosClient, string databaseName, string containerName)
 {
     this.container = cosmosClient?.GetContainer(databaseName, containerName);
 }
 public SpecificsCosmosService(CosmosClient clientDB, string nomDB, string nomContainer)
 {
     _container = clientDB.GetContainer(nomDB, nomContainer);
 }
Example #12
0
        public async Task EnsureUnauthorized_ThrowsCosmosClientException()
        {
            string authKey  = ConfigurationManager.AppSettings["MasterKey"];
            string endpoint = ConfigurationManager.AppSettings["GatewayEndpoint"];

            // Take the key and change some middle character
            authKey = authKey.Replace("m", "M");

            CosmosClient cosmosClient = new CosmosClient(
                endpoint,
                authKey);

            CosmosException exception = await Assert.ThrowsExceptionAsync <CosmosException>(() => cosmosClient.GetContainer("test", "test").ReadItemAsync <dynamic>("test", new PartitionKey("test")));

            Assert.AreEqual(HttpStatusCode.Unauthorized, exception.StatusCode);
        }
Example #13
0
        public async Task ItemResourcePermissionTest()
        {
            //create user
            string       userId       = Guid.NewGuid().ToString();
            UserResponse userResponse = await this.cosmosDatabase.CreateUserAsync(userId);

            Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode);
            Assert.AreEqual(userId, userResponse.Resource.Id);
            User user = userResponse.User;

            //create resource
            string            containerId       = Guid.NewGuid().ToString();
            ContainerResponse containerResponse = await this.cosmosDatabase.CreateContainerAsync(containerId, "/id");

            Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode);
            Container              container    = containerResponse.Container;
            string                 itemId       = Guid.NewGuid().ToString();
            PartitionKey           partitionKey = new PartitionKey(itemId);
            ItemResponse <dynamic> itemRespnose = await container.CreateItemAsync <dynamic>(new { id = itemId }, partitionKey);

            Assert.AreEqual(HttpStatusCode.Created, itemRespnose.StatusCode);

            //create permission
            string permissionId = Guid.NewGuid().ToString();
            PermissionProperties permissionProperties = new PermissionProperties(permissionId, PermissionMode.Read, container, partitionKey, itemId);
            PermissionResponse   permissionResponse   = await user.CreatePermissionAsync(permissionProperties);

            PermissionProperties permission = permissionResponse.Resource;

            Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode);
            Assert.AreEqual(permissionId, permission.Id);
            Assert.AreEqual(permissionProperties.PermissionMode, permission.PermissionMode);

            //delete resource with PermissionMode.Read
            using (CosmosClient tokenCosmosClient = TestCommon.CreateCosmosClient(clientOptions: null, resourceToken: permission.Token))
            {
                Container tokenContainer = tokenCosmosClient.GetContainer(this.cosmosDatabase.Id, containerId);
                ItemResponse <dynamic> readPermissionItem = await tokenContainer.ReadItemAsync <dynamic>(itemId, partitionKey);

                Assert.AreEqual(itemId, readPermissionItem.Resource.id.ToString());

                try
                {
                    ItemResponse <dynamic> response = await tokenContainer.DeleteItemAsync <dynamic>(
                        itemId,
                        partitionKey);

                    Assert.Fail();
                }
                catch (CosmosException ex)
                {
                    Assert.AreEqual(HttpStatusCode.Forbidden, ex.StatusCode);
                }
            }

            //update permission to PermissionMode.All
            permissionProperties = new PermissionProperties(permissionId, PermissionMode.All, container);
            permissionResponse   = await user.GetPermission(permissionId).ReplaceAsync(permissionProperties);

            permission = permissionResponse.Resource;

            //delete resource with PermissionMode.All
            using (CosmosClient tokenCosmosClient = TestCommon.CreateCosmosClient(clientOptions: null, resourceToken: permission.Token))
            {
                FeedIterator <dynamic> feed = tokenCosmosClient
                                              .GetDatabase(this.cosmosDatabase.Id)
                                              .GetContainer(containerId)
                                              .GetItemQueryIterator <dynamic>(new QueryDefinition("select * from t"));

                while (feed.HasMoreResults)
                {
                    FeedResponse <dynamic> response = await feed.ReadNextAsync();

                    Assert.IsNotNull(response);
                }
            }
        }
Example #14
0
        public async Task ContainerPartitionResourcePermissionTest(ConnectionMode connectionMode)
        {
            CosmosClientOptions cosmosClientOptions = new CosmosClientOptions()
            {
                ConnectionMode = connectionMode
            };

            CosmosClient cosmosClient = TestCommon.CreateCosmosClient(cosmosClientOptions);

            Database database = await cosmosClient.CreateDatabaseIfNotExistsAsync("PermissionTest");

            //create user
            string       userId       = Guid.NewGuid().ToString();
            UserResponse userResponse = await database.CreateUserAsync(userId);

            Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode);
            Assert.AreEqual(userId, userResponse.Resource.Id);
            User user = userResponse.User;

            //create resource
            string containerId = Guid.NewGuid().ToString();

            ContainerResponse containerResponse = await database.CreateContainerAsync(
                id : containerId,
                partitionKeyPath : "/id");

            Assert.AreEqual(HttpStatusCode.Created, containerResponse.StatusCode);
            Container container = containerResponse.Container;

            // Create items to read
            ToDoActivity itemAccess   = ToDoActivity.CreateRandomToDoActivity();
            ToDoActivity itemNoAccess = ToDoActivity.CreateRandomToDoActivity();

            await container.CreateItemAsync <ToDoActivity>(
                itemAccess,
                new PartitionKey(itemAccess.id));

            await container.CreateItemAsync <ToDoActivity>(
                itemNoAccess,
                new PartitionKey(itemNoAccess.id));

            //create permission
            string               permissionId         = Guid.NewGuid().ToString();
            PartitionKey         partitionKey         = new PartitionKey(itemAccess.id);
            PermissionProperties permissionProperties = new PermissionProperties(
                permissionId,
                PermissionMode.Read,
                container,
                partitionKey);

            PermissionResponse permissionResponse = await user.CreatePermissionAsync(permissionProperties);

            PermissionProperties permission = permissionResponse.Resource;

            Assert.AreEqual(HttpStatusCode.Created, userResponse.StatusCode);
            Assert.AreEqual(permissionId, permission.Id);
            Assert.AreEqual(permissionProperties.PermissionMode, permission.PermissionMode);

            using (CosmosClient tokenCosmosClient = TestCommon.CreateCosmosClient(clientOptions: cosmosClientOptions, resourceToken: permission.Token))
            {
                Container tokenContainer = tokenCosmosClient.GetContainer(database.Id, containerId);
                await tokenContainer.ReadItemAsync <ToDoActivity>(itemAccess.id, new PartitionKey(itemAccess.id));

                try
                {
                    await tokenContainer.ReadItemAsync <ToDoActivity>(itemNoAccess.id, new PartitionKey(itemNoAccess.id));

                    Assert.Fail();
                }
                catch (CosmosException ex)
                {
                    Assert.AreEqual(HttpStatusCode.Forbidden, ex.StatusCode);
                }

                QueryRequestOptions queryRequestOptions = new QueryRequestOptions()
                {
                    PartitionKey = new PartitionKey(itemAccess.id)
                };

                FeedIterator <ToDoActivity> feedIterator = tokenContainer.GetItemQueryIterator <ToDoActivity>(
                    queryText: "select * from T",
                    requestOptions: queryRequestOptions);

                List <ToDoActivity> result = new List <ToDoActivity>();
                while (feedIterator.HasMoreResults)
                {
                    FeedResponse <ToDoActivity> toDoActivities = await feedIterator.ReadNextAsync();

                    result.AddRange(toDoActivities);
                }

                Assert.AreEqual(1, result.Count);

                // Test query with no service interop via gateway query plan to replicate x32 app
                ContainerCore         containerCore = (ContainerInlineCore)tokenContainer;
                MockCosmosQueryClient mock          = new MockCosmosQueryClient(
                    clientContext: containerCore.ClientContext,
                    cosmosContainerCore: containerCore,
                    forceQueryPlanGatewayElseServiceInterop: true);

                Container tokenGatewayQueryPlan = new ContainerCore(
                    containerCore.ClientContext,
                    (DatabaseCore)containerCore.Database,
                    containerCore.Id,
                    mock);

                FeedIterator <ToDoActivity> feedIteratorGateway = tokenGatewayQueryPlan.GetItemQueryIterator <ToDoActivity>(
                    queryText: "select * from T",
                    requestOptions: queryRequestOptions);

                List <ToDoActivity> resultGateway = new List <ToDoActivity>();
                while (feedIteratorGateway.HasMoreResults)
                {
                    FeedResponse <ToDoActivity> toDoActivities = await feedIteratorGateway.ReadNextAsync();

                    resultGateway.AddRange(toDoActivities);
                }

                Assert.AreEqual(1, resultGateway.Count);
            }
        }
Example #15
0
 public VehicleConnectionRepository(CosmosClient dbClient,
                                    string databaseName,
                                    string containerName)
 {
     this._container = dbClient.GetContainer(databaseName, containerName);
 }
Example #16
0
        /// <summary>
        /// Gets the document container as an asynchronous operation.
        /// </summary>
        /// <returns>
        /// A <see cref="Task{TResult}"/> representing the asynchronous operation to get the container.
        /// </returns>
        private async Task <Container> GetContainerAsync()
        {
            await _initializer.EnsureCollectionExistsAsync(_client, _options.CollectionName !);

            return(_client.GetContainer(_options.DatabaseName, _options.CollectionName));
        }
Example #17
0
        static void Main(string[] args)
        {
            //Read Configuration
            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json")
                                .Build();

            //Read Data
            string         volcanosString = File.ReadAllText("data.json");
            List <Volcano> volcanos       = JsonConvert.DeserializeObject <List <Volcano> >(volcanosString);

            #region Push 20 Objects / Upsert
            using (CosmosClient cosmosClient = new CosmosClient(configuration["CosmosHost"], configuration["CosmosKey"]))
            {
                var objectToPush = volcanos.Take(20);
                System.Console.WriteLine(objectToPush);

                var containerHandle = cosmosClient.GetContainer(configuration["DatabaseId"], configuration["ContainerId"]);
                objectToPush.ToList().ForEach(v =>
                {
                    var result = containerHandle.UpsertItemAsync(v).Result;
                    System.Console.WriteLine(JsonConvert.SerializeObject(result));
                    System.Console.WriteLine();
                });
                #endregion

                #region Read Single Item
                var readdata = containerHandle.ReadItemAsync <Volcano>(objectToPush.First().id, new PartitionKey(objectToPush.First().type)).Result;
                System.Console.WriteLine();
                System.Console.WriteLine("Read Data");
                System.Console.WriteLine(readdata.Resource);
                #endregion

                #region Read based on a query
                QueryDefinition queryDefinition = new QueryDefinition("select * from c");

                using (var iterator = containerHandle.GetItemQueryIterator <Volcano>(queryDefinition))
                {
                    int count = 0;
                    while (iterator.HasMoreResults)
                    {
                        foreach (var item in iterator.ReadNextAsync().Result)
                        {
                            System.Console.WriteLine();
                            System.Console.WriteLine("--------------------------");
                            System.Console.WriteLine($"Item {count++}");
                            System.Console.WriteLine("--------------------------");
                            System.Console.WriteLine(item);
                            System.Console.WriteLine("--------------------------");
                        }
                    }
                }
                #endregion
            }

            #region Push objects in Bulk
            using (CosmosClient cosmosClient = new CosmosClient(configuration["CosmosHost"], configuration["CosmosKey"],
                                                                //Allow Bulk
                                                                new CosmosClientOptions()
            {
                AllowBulkExecution = true
            }))
            {
                var startTime = DateTime.UtcNow;
                System.Console.WriteLine("Started //el Upsert");

                var         containerHandle = cosmosClient.GetContainer(configuration["DatabaseId"], configuration["ContainerId"]);
                List <Task> concInserts     = new List <Task>();

                foreach (var volcano in volcanos)
                {
                    concInserts.Add(containerHandle.UpsertItemAsync(volcano, new PartitionKey(volcano.type)));
                }

                Task.WhenAll(concInserts).Wait();
                var endTime = DateTime.UtcNow;
                System.Console.WriteLine("Done //el Upsert");
                System.Console.WriteLine((endTime - startTime).Humanize());
            }
            #endregion
        }
Example #18
0
 public ProductsController(CosmosClient cosmos)
 {
     this.cosmos    = cosmos;
     this.container = cosmos.GetContainer(DATABASE_ID, CONTAINER_NAME);
 }
Example #19
0
        /// <summary>
        /// Run samples for Order By queries.
        /// </summary>
        /// <returns>a Task object.</returns>
        private async Task <RunSummary> ExecuteAsync(BenchmarkConfig config, string accountKey)
        {
            using (CosmosClient cosmosClient = config.CreateCosmosClient(accountKey))
            {
                if (config.CleanupOnStart)
                {
                    Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database);
                    await database.DeleteStreamAsync();
                }

                ContainerResponse containerResponse = await Program.CreatePartitionedContainerAsync(config, cosmosClient);

                Container container = containerResponse;

                int?currentContainerThroughput = await container.ReadThroughputAsync();

                Console.WriteLine($"Using container {config.Container} with {currentContainerThroughput} RU/s");

                int taskCount = config.GetTaskCount(currentContainerThroughput.Value);

                Console.WriteLine("Starting Inserts with {0} tasks", taskCount);
                Console.WriteLine();

                string partitionKeyPath = containerResponse.Resource.PartitionKeyPath;
                int    opsPerTask       = config.ItemCount / taskCount;

                // TBD: 2 clients SxS some overhead
                RunSummary runSummary;
                using (DocumentClient documentClient = config.CreateDocumentClient(accountKey))
                {
                    Func <IBenchmarkOperatrion> benchmarkOperationFactory = this.GetBenchmarkFactory(
                        config,
                        partitionKeyPath,
                        cosmosClient,
                        documentClient);

                    IExecutionStrategy execution = IExecutionStrategy.StartNew(config, benchmarkOperationFactory);
                    runSummary = await execution.ExecuteAsync(taskCount, opsPerTask, config.TraceFailures, 0.01);
                }

                if (config.CleanupOnFinish)
                {
                    Console.WriteLine($"Deleting Database {config.Database}");
                    Microsoft.Azure.Cosmos.Database database = cosmosClient.GetDatabase(config.Database);
                    await database.DeleteStreamAsync();
                }

                runSummary.WorkloadType = config.WorkloadType;
                runSummary.id           = $"{DateTime.UtcNow.ToString("yyyy-MM-dd:HH-mm")}-{config.CommitId}";
                runSummary.Commit       = config.CommitId;
                runSummary.CommitDate   = config.CommitDate;
                runSummary.CommitTime   = config.CommitTime;

                runSummary.Date        = DateTime.UtcNow.ToString("yyyy-MM-dd");
                runSummary.Time        = DateTime.UtcNow.ToString("HH-mm");
                runSummary.BranchName  = config.BranchName;
                runSummary.TotalOps    = config.ItemCount;
                runSummary.Concurrency = taskCount;
                runSummary.Database    = config.Database;
                runSummary.Container   = config.Container;
                runSummary.AccountName = config.EndPoint;
                runSummary.pk          = config.ResultsPartitionKeyValue;

                string consistencyLevel = config.ConsistencyLevel;
                if (string.IsNullOrWhiteSpace(consistencyLevel))
                {
                    AccountProperties accountProperties = await cosmosClient.ReadAccountAsync();

                    consistencyLevel = accountProperties.Consistency.DefaultConsistencyLevel.ToString();
                }
                runSummary.ConsistencyLevel = consistencyLevel;


                if (config.PublishResults)
                {
                    Container resultsContainer = cosmosClient.GetContainer(config.Database, config.ResultsContainer);
                    await resultsContainer.CreateItemAsync(runSummary, new PartitionKey(runSummary.pk));
                }

                return(runSummary);
            }
        }
Example #20
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            [CosmosDB(
                 databaseName: "coffeebook-db",
                 collectionName: "Users",
                 ConnectionStringSetting = "cosmosdb-connection-string")] IAsyncCollector <User> userOut,
            ILogger log,
            ExecutionContext context)
        {
            try
            {
                string  requestBody = await new StreamReader(req.Body).ReadToEndAsync();
                dynamic userIn      = JsonConvert.DeserializeObject(requestBody, typeof(UserInfo));

                string meessage = userIn.Id + "のユーザー情報を登録します";
                log.LogInformation(meessage);

                #region ユーザーIDの重複チェック

                // 設定値読み込み
                var config = new ConfigurationBuilder()
                             .SetBasePath(context.FunctionAppDirectory)
                             .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                             .AddEnvironmentVariables()
                             .Build();

                var connectionString = config.GetConnectionString(Consts.COSMOSDB_CONNECTION_STRING);
                var client           = new CosmosClient(connectionString);
                var usersContainer   = client.GetContainer(Consts.COFFEEBOOK_DB, Consts.USERS_CONTAINER);

                var query = usersContainer.GetItemQueryIterator <User>(new QueryDefinition(
                                                                           "select * from r where r.userId = @userId")
                                                                       .WithParameter("@userId", (string)userIn.Id));

                List <User> results = new List <User>();
                while (query.HasMoreResults)
                {
                    var response = await query.ReadNextAsync();

                    results.AddRange(response.ToList());
                }

                if (results.Count() != 0)
                {
                    return(new ConflictObjectResult("このユーザーIDは既に使用されています。"));
                }

                #endregion

                // ハッシュの計算
                byte[] salt           = UserService.GenerateSalt();
                byte[] hashedPassword = UserService.GenerateHashedValue(userIn.Password, salt);

                var user = new User
                {
                    UserId         = userIn.Id,
                    HashedPassword = hashedPassword,
                    Salt           = salt
                };

                await userOut.AddAsync(user);

                return(new OkResult());
            }
            catch (Exception ex)
            {
                log.LogInformation(ex.ToString());
                return(new BadRequestResult());
            }
        }
        public async Task PointOperationThrottledDiagnostic(bool disableDiagnostics)
        {
            string errorMessage        = "Mock throttle exception" + Guid.NewGuid().ToString();
            Guid   exceptionActivityId = Guid.NewGuid();
            // Set a small retry count to reduce test time
            CosmosClient throttleClient = TestCommon.CreateCosmosClient(builder =>
                                                                        builder.WithThrottlingRetryOptions(TimeSpan.FromSeconds(5), 5)
                                                                        .WithTransportClientHandlerFactory(transportClient => new TransportClientWrapper(
                                                                                                               transportClient,
                                                                                                               (uri, resourceOperation, request) => TransportClientHelper.ReturnThrottledStoreResponseOnItemOperation(
                                                                                                                   uri,
                                                                                                                   resourceOperation,
                                                                                                                   request,
                                                                                                                   exceptionActivityId,
                                                                                                                   errorMessage)))
                                                                        );

            ItemRequestOptions requestOptions = new ItemRequestOptions();

            if (disableDiagnostics)
            {
                requestOptions.DiagnosticContextFactory = () => EmptyCosmosDiagnosticsContext.Singleton;
            }
            ;

            Container containerWithThrottleException = throttleClient.GetContainer(
                this.database.Id,
                this.Container.Id);

            //Checking point operation diagnostics on typed operations
            ToDoActivity testItem = ToDoActivity.CreateRandomToDoActivity();

            try
            {
                ItemResponse <ToDoActivity> createResponse = await containerWithThrottleException.CreateItemAsync <ToDoActivity>(
                    item : testItem,
                    requestOptions : requestOptions);

                Assert.Fail("Should have thrown a request timeout exception");
            }
            catch (CosmosException ce) when((int)ce.StatusCode == (int)Documents.StatusCodes.TooManyRequests)
            {
                string exception = ce.ToString();

                Assert.IsNotNull(exception);
                Assert.IsTrue(exception.Contains(exceptionActivityId.ToString()));
                Assert.IsTrue(exception.Contains(errorMessage));

                string diagnosics = ce.Diagnostics.ToString();

                if (disableDiagnostics)
                {
                    Assert.IsTrue(string.IsNullOrEmpty(diagnosics));
                }
                else
                {
                    Assert.IsFalse(string.IsNullOrEmpty(diagnosics));
                    Assert.IsTrue(exception.Contains(diagnosics));
                    DiagnosticValidator.ValidatePointOperationDiagnostics(ce.DiagnosticsContext);
                }
            }
        }
Example #22
0
 private Container GetContainer()
 => _cosmosClient.GetContainer("Rtl", "Shows");
Example #23
0
 public FlagRepository(CosmosClient cosmosClient) : base(cosmosClient)
 {
     _flagContainer = cosmosClient.GetContainer(DatabaseId, ContainerId);
 }
Example #24
0
        public async Task <IActionResult> GetSessionChartById(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "session/{id}/chart/{timeseries?}")] HttpRequest req,
            [CosmosDB(
                 databaseName: "Inferno",
                 collectionName: "sessions",
                 ConnectionStringSetting = "CosmosDBConnection",
                 Id = "{id}", PartitionKey = "inferno1")] Object sessionObject,
            int?timeseries,
            ILogger log)
        {
            if (sessionObject == null)
            {
                log.LogInformation($"sessionObject not found");
                return(new NotFoundResult());
            }
            var session = JsonConvert.DeserializeObject <Session>(sessionObject.ToString());

            if (session == null)
            {
                log.LogInformation($"Session not found");
                return(new NotFoundResult());
            }

            log.LogInformation("SmokerId " + session.SmokerId);
            log.LogInformation("SessionId " + session.Id);
            var StatusPartitionKey = $"{session.SmokerId}-{session.StartTime:yyyy-MM}";

            log.LogInformation($"Status PartitionKey = {StatusPartitionKey}");

            var EndTime = session.EndTime.HasValue ? session.EndTime : DateTime.UtcNow;

            log.LogInformation($"StartTime = {session.StartTime} EndTime = {EndTime}");

            var container = _cosmosClient.GetContainer("Inferno", "status");
            // Create a query, defining the partition key so we don't execute a fan-out query (saving RUs),
            //      where the entity type is a Trip and the status is not Completed, Canceled, or Inactive.
            // var query = container.GetItemLinqQueryable<SmokerStatus>(requestOptions: new QueryRequestOptions { PartitionKey = new Microsoft.Azure.Cosmos.PartitionKey(StatusPartitionKey) })
            //     .Where(p => p.CurrentTime >= session.StartTime
            //                 && p.CurrentTime <= EndTime)
            //     .ToFeedIterator();
            var query = container.GetItemLinqQueryable <SmokerStatus>(requestOptions: new QueryRequestOptions {
                PartitionKey = new Microsoft.Azure.Cosmos.PartitionKey(StatusPartitionKey)
            })
                        .Where(p => p.SessionId == session.Id)
                        .ToFeedIterator();

            List <SmokerStatus> SmokerStatuses = new List <SmokerStatus>();
            var count = 0;

            while (query.HasMoreResults)
            {
                foreach (var status in await query.ReadNextAsync())
                {
                    count++;
                    SmokerStatuses.Add(status);
                }
            }
            log.LogInformation("Statuses " + count);

            if (!timeseries.HasValue)
            {
                return(new OkObjectResult(SmokerStatuses));
            }

            if (timeseries > 0 && timeseries <= 60)
            {
                TimeSpan            interval   = new TimeSpan(0, timeseries.Value, 0);
                List <SmokerStatus> SortedList = SmokerStatuses.OrderBy(o => o.CurrentTime).ToList();
                var result = SortedList.GroupBy(x => x.CurrentTime.Ticks / interval.Ticks)
                             .Select(x => x.First());
                return(new OkObjectResult(result));
            }
            // Return a 400 bad request result to the client with additional information
            return(new BadRequestObjectResult("Please pass a timeseries in range of 1 to 60"));
        }
Example #25
0
 /// <summary>
 ///     Gets a container from the default database using the specified name.
 /// </summary>
 /// <param name="containerName">The name of the container.</param>
 /// <returns>An instance of the specified container.</returns>
 public Container GetContainer(string containerName)
 {
     return(_client.GetContainer(DatabaseName, containerName));
 }
Example #26
0
        public async Task QueryRequestRateTest(bool directMode)
        {
            string firstItemIdAndPk = "BasicQueryItem" + Guid.NewGuid();

            // Prevent the test from changing the static client
            {
                CosmosClient client    = directMode ? DirectCosmosClient : GatewayCosmosClient;
                Container    container = client.GetContainer(DatabaseId, ContainerId);

                List <string> createdIds = new List <string>()
                {
                    firstItemIdAndPk,
                    "BasicQueryItem2" + Guid.NewGuid(),
                    "BasicQueryItem3" + Guid.NewGuid()
                };

                foreach (string id in createdIds)
                {
                    dynamic item = new
                    {
                        id = id,
                        pk = id,
                    };

                    await container.CreateItemAsync <dynamic>(item : item);
                }
            }

            CosmosClient clientWithThrottle;

            if (directMode)
            {
                clientWithThrottle = TestCommon.CreateCosmosClient();
            }
            else
            {
                clientWithThrottle = TestCommon.CreateCosmosClient((builder) => builder.WithConnectionModeGateway());
            }

            Container containerWithThrottle = clientWithThrottle.GetContainer(DatabaseId, ContainerId);

            // Do a read to warm up all the caches to prevent them from getting the throttle errors
            using (await containerWithThrottle.ReadItemStreamAsync(firstItemIdAndPk, new PartitionKey(firstItemIdAndPk))) { }

            Documents.IStoreModel        storeModel = clientWithThrottle.ClientContext.DocumentClient.StoreModel;
            Mock <Documents.IStoreModel> mockStore  = new Mock <Documents.IStoreModel>();

            clientWithThrottle.ClientContext.DocumentClient.StoreModel = mockStore.Object;

            // Cause 429 after the first call
            int    callCount    = 0;
            string activityId   = null;
            string errorMessage = "QueryRequestRateTest Resource Not Found";

            mockStore.Setup(x => x.ProcessMessageAsync(It.IsAny <Documents.DocumentServiceRequest>(), It.IsAny <CancellationToken>()))
            .Returns <Documents.DocumentServiceRequest, CancellationToken>((dsr, token) =>
            {
                callCount++;

                if (callCount > 1)
                {
                    INameValueCollection headers = new DictionaryNameValueCollection();
                    headers.Add(Documents.HttpConstants.HttpHeaders.RetryAfterInMilliseconds, "42");
                    activityId = Guid.NewGuid().ToString();
                    headers.Add(Documents.HttpConstants.HttpHeaders.ActivityId, activityId);
                    Documents.DocumentServiceResponse response = new Documents.DocumentServiceResponse(
                        body: TestCommon.GenerateStreamFromString(@"{""Errors"":[""" + errorMessage + @"""]}"),
                        headers: headers,
                        statusCode: (HttpStatusCode)429,
                        clientSideRequestStatistics: dsr.RequestContext.ClientRequestStatistics);

                    return(Task.FromResult(response));
                }

                return(storeModel.ProcessMessageAsync(dsr, token));
            });

            List <dynamic> results = new List <dynamic>();

            try
            {
                using (FeedIterator <dynamic> feedIterator = containerWithThrottle.GetItemQueryIterator <dynamic>(
                           "select * from T where STARTSWITH(T.id, \"BasicQueryItem\")",
                           requestOptions: new QueryRequestOptions()
                {
                    MaxItemCount = 1,
                    MaxConcurrency = 1
                }))
                {
                    while (feedIterator.HasMoreResults)
                    {
                        FeedResponse <dynamic> response = await feedIterator.ReadNextAsync();

                        Assert.IsTrue(response.Count <= 1);
                        Assert.IsTrue(response.Resource.Count() <= 1);

                        results.AddRange(response);
                    }
                }
                Assert.Fail("Should throw 429 exception after the first page.");
            }
            catch (CosmosException ce)
            {
                Assert.IsTrue(ce.RetryAfter.HasValue);
                Assert.AreEqual(42, ce.RetryAfter.Value.TotalMilliseconds);
                Assert.AreEqual(activityId, ce.ActivityId);
                Assert.IsNotNull(ce.DiagnosticsContext);
                Assert.IsTrue(ce.Message.Contains(errorMessage));
            }

            callCount = 0;
            FeedIterator streamIterator = containerWithThrottle.GetItemQueryStreamIterator(
                "select * from T where STARTSWITH(T.id, \"BasicQueryItem\")",
                requestOptions: new QueryRequestOptions()
            {
                MaxItemCount   = 1,
                MaxConcurrency = 1
            });

            // First request should be a success
            using (ResponseMessage response = await streamIterator.ReadNextAsync())
            {
                response.EnsureSuccessStatusCode();
                Assert.IsNotNull(response.Content);
            }

            // Second page should be a failure
            using (ResponseMessage response = await streamIterator.ReadNextAsync())
            {
                Assert.AreEqual(429, (int)response.StatusCode);
                Assert.AreEqual("42", response.Headers.RetryAfterLiteral);
                Assert.AreEqual(activityId, response.Headers.ActivityId);
                Assert.IsNotNull(response.DiagnosticsContext);
                Assert.IsTrue(response.ErrorMessage.Contains(errorMessage));
            }
        }
 public CosmosDbClient(CosmosClient dbClient, string databaseName, string containerName)
 {
     _container = dbClient.GetContainer(databaseName, containerName);
 }
Example #28
0
        public async Task TripProcessor([CosmosDBTrigger(
                                             databaseName: WellKnown.COSMOSDB_DB_NAME,
                                             collectionName: WellKnown.COSMOSDB_COLLECTION_NAME_TELEMETRY,
                                             ConnectionStringSetting = WellKnown.COSMOSDB_CONNECTIONSTRING_NAME,
                                             LeaseCollectionName = WellKnown.COSMOSDB_COLLECTION_NAME_LEASES,
                                             LeaseCollectionPrefix = WellKnown.COSMOSDB_LEASE_PREFIX_TRIPS,
                                             LeasesCollectionThroughput = LEASES_COLLECTION_THROUGHPUT,
                                             CreateLeaseCollectionIfNotExists = true,
                                             StartFromBeginning = true)] IReadOnlyList <Document> vehicleEvents,
                                        ILogger log)
        {
            log.LogInformation($"Evaluating {vehicleEvents.Count} events from Cosmos DB to optionally update Trip and Consignment metadata.");

            // Retrieve the Trip records by VIN, compare the odometer reading to the starting odometer reading to calculate miles driven,
            // and update the Trip and Consignment status and send an alert if needed once completed.
            var sendTripAlert     = false;
            var database          = WellKnown.COSMOSDB_DB_NAME;
            var metadataContainer = WellKnown.COSMOSDB_COLLECTION_NAME_METADATA;

            if (vehicleEvents.Count > 0)
            {
                foreach (var group in vehicleEvents.GroupBy(singleEvent => singleEvent.GetPropertyValue <string>(nameof(VehicleEvent.vin))))
                {
                    var vin          = group.Key;
                    var odometerHigh = group.Max(item => item.GetPropertyValue <double>(nameof(VehicleEvent.odometer)));
                    var averageRefrigerationUnitTemp =
                        group.Average(item => item.GetPropertyValue <double>(nameof(VehicleEvent.refrigerationUnitTemp)));

                    // First, retrieve the metadata Cosmos DB container reference:
                    var container = _cosmosClient.GetContainer(database, metadataContainer);

                    // Create a query, defining the partition key so we don't execute a fan-out query (saving RUs), where the entity type is a Trip and the status is not Completed, Canceled, or Inactive.
                    var query = container.GetItemLinqQueryable <Trip>(requestOptions: new QueryRequestOptions {
                        PartitionKey = new Microsoft.Azure.Cosmos.PartitionKey(vin)
                    })
                                .Where(p => p.status != WellKnown.Status.Completed &&
                                       p.status != WellKnown.Status.Canceled &&
                                       p.status != WellKnown.Status.Inactive &&
                                       p.entityType == WellKnown.EntityTypes.Trip)
                                .ToFeedIterator();

                    if (query.HasMoreResults)
                    {
                        // Only retrieve the first result.
                        var trip = (await query.ReadNextAsync()).FirstOrDefault();

                        if (trip != null)
                        {
                            // Retrieve the Consignment record.
                            var document = await container.ReadItemAsync <Consignment>(trip.consignmentId,
                                                                                       new Microsoft.Azure.Cosmos.PartitionKey(trip.consignmentId));

                            var consignment       = document.Resource;
                            var updateTrip        = false;
                            var updateConsignment = false;

                            // Calculate how far along the vehicle is for this trip.
                            var milesDriven = odometerHigh - trip.odometerBegin;
                            if (milesDriven >= trip.plannedTripDistance)
                            {
                                // The trip is completed!
                                trip.status        = WellKnown.Status.Completed;
                                trip.odometerEnd   = odometerHigh;
                                trip.tripEnded     = DateTime.UtcNow;
                                consignment.status = WellKnown.Status.Completed;

                                // Update the trip and consignment records.
                                updateTrip        = true;
                                updateConsignment = true;

                                sendTripAlert = true;
                            }
                            else
                            {
                                if (DateTime.UtcNow >= consignment.deliveryDueDate && trip.status != WellKnown.Status.Delayed)
                                {
                                    // The trip is delayed!
                                    trip.status        = WellKnown.Status.Delayed;
                                    consignment.status = WellKnown.Status.Delayed;

                                    // Update the trip and consignment records.
                                    updateTrip        = true;
                                    updateConsignment = true;

                                    sendTripAlert = true;
                                }
                            }

                            if (trip.tripStarted == null)
                            {
                                // Set the trip start date.
                                trip.tripStarted = DateTime.UtcNow;
                                // Set the trip and consignment status to Active.
                                trip.status        = WellKnown.Status.Active;
                                consignment.status = WellKnown.Status.Active;

                                updateTrip        = true;
                                updateConsignment = true;

                                sendTripAlert = true;
                            }

                            // Update the trip and consignment records.
                            if (updateTrip)
                            {
                                await container.ReplaceItemAsync(trip, trip.id, new Microsoft.Azure.Cosmos.PartitionKey(trip.partitionKey));
                            }

                            if (updateConsignment)
                            {
                                await container.ReplaceItemAsync(consignment, consignment.id, new Microsoft.Azure.Cosmos.PartitionKey(consignment.partitionKey));
                            }

                            // Send a trip alert.
                            if (sendTripAlert)
                            {
                                // Have the HttpClient factory create a new client instance.
                                var httpClient = _httpClientFactory.CreateClient(WellKnown.LOGIC_APP_CLIENT);

                                // Create the payload to send to the Logic App.
                                var payload = new LogicAppAlert
                                {
                                    consignmentId        = trip.consignmentId,
                                    customer             = trip.consignment.customer,
                                    deliveryDueDate      = trip.consignment.deliveryDueDate,
                                    hasHighValuePackages = trip.packages.Any(p => p.highValue),
                                    id = trip.id,
                                    lastRefrigerationUnitTemperatureReading = averageRefrigerationUnitTemp,
                                    location = trip.location,
                                    lowestPackageStorageTemperature = trip.packages.Min(p => p.storageTemperature),
                                    odometerBegin       = trip.odometerBegin,
                                    odometerEnd         = trip.odometerEnd,
                                    plannedTripDistance = trip.plannedTripDistance,
                                    tripStarted         = trip.tripStarted,
                                    tripEnded           = trip.tripEnded,
                                    status             = trip.status,
                                    vin                = trip.vin,
                                    temperatureSetting = trip.temperatureSetting,
                                    recipientEmail     = Environment.GetEnvironmentVariable(WellKnown.RECIPIENT_EMAIL_NAME)
                                };

                                var postBody = JsonConvert.SerializeObject(payload);

                                await httpClient.PostAsync(Environment.GetEnvironmentVariable(WellKnown.LOGIC_APP_URL_NAME), new StringContent(postBody, Encoding.UTF8, "application/json"));
                            }
                        }
                    }
                }
            }
        }
        public InProcTestFhirServer(DataStore dataStore, Type startupType)
            : base(new Uri("http://localhost/"))
        {
            var projectDir = GetProjectPath("src", startupType);
            var corsPath   = Path.GetFullPath("corstestconfiguration.json");

            var launchSettings = JObject.Parse(File.ReadAllText(Path.Combine(projectDir, "Properties", "launchSettings.json")));

            var configuration = launchSettings["profiles"][dataStore.ToString()]["environmentVariables"].Cast <JProperty>().ToDictionary(p => p.Name, p => p.Value.ToString());

            configuration["TestAuthEnvironment:FilePath"] = "testauthenvironment.json";
            configuration["FhirServer:Security:Enabled"]  = "true";
            configuration["FhirServer:Security:Authentication:Authority"] = "https://inprochost";

            // For local development we will use the Azure Storage Emulator for export.
            configuration["FhirServer:Operations:Export:StorageAccountConnection"] = "UseDevelopmentStorage=true";

            // enable reindex for testing
            configuration["FhirServer:Operations:Reindex:Enabled"] = "true";

            if (startupType.IsDefined(typeof(RequiresIsolatedDatabaseAttribute)))
            {
                // Alter the configuration so that the server will create a new, isolated database/container.
                // Ensure tha the database/container is deleted at the end of the test run (when this instance is disposed)

                if (dataStore == DataStore.SqlServer)
                {
                    var connectionStringBuilder = new SqlConnectionStringBuilder(configuration["SqlServer:ConnectionString"]);
                    var databaseName            = connectionStringBuilder.InitialCatalog += "_" + startupType.Name;
                    configuration["SqlServer:ConnectionString"] = connectionStringBuilder.ToString();

                    _cleanupDatabase = async() =>
                    {
                        connectionStringBuilder.InitialCatalog = "master";
                        await using var connection             = new SqlConnection(connectionStringBuilder.ToString());

                        await connection.OpenAsync();

                        SqlConnection.ClearAllPools();

                        await using SqlCommand command = connection.CreateCommand();
                        command.CommandTimeout         = 600;
                        command.CommandText            = $"DROP DATABASE IF EXISTS {databaseName}";
                        await command.ExecuteNonQueryAsync();
                    };
                }
                else if (dataStore == DataStore.CosmosDb)
                {
                    var collectionId = configuration["FhirServer:CosmosDb:CollectionId"] = $"fhir{ModelInfoProvider.Version}_{startupType.Name}";

                    _cleanupDatabase = async() =>
                    {
                        string ValueOrFallback(string configKey, string fallbackValue)
                        {
                            string value = _builtConfiguration[configKey];

                            return(string.IsNullOrEmpty(value) ? fallbackValue : value);
                        }

                        var host       = ValueOrFallback("CosmosDb:Host", CosmosDbLocalEmulator.Host);
                        var key        = ValueOrFallback("CosmosDb:Key", CosmosDbLocalEmulator.Key);
                        var databaseId = ValueOrFallback("CosmosDb:DatabaseId", null) ?? throw new InvalidOperationException("expected CosmosDb:DatabaseId to be set in configuration");

                        using var client = new CosmosClient(host, key);
                        Container container = client.GetContainer(databaseId, collectionId);
                        await container.DeleteContainerAsync();
                    };
                }
            }

            var builder = WebHost.CreateDefaultBuilder()
                          .UseContentRoot(Path.GetDirectoryName(startupType.Assembly.Location))
                          .ConfigureAppConfiguration(configurationBuilder =>
            {
                configurationBuilder.AddDevelopmentAuthEnvironmentIfConfigured(new ConfigurationBuilder().AddInMemoryCollection(configuration).Build());
                configurationBuilder.AddJsonFile(corsPath);
                configurationBuilder.AddInMemoryCollection(configuration);
            })
                          .UseStartup(startupType)
                          .ConfigureServices(serviceCollection =>
            {
                // ensure that HttpClients
                // use a message handler for the test server
                serviceCollection
                .AddHttpClient(Options.DefaultName)
                .ConfigurePrimaryHttpMessageHandler(() => Server.CreateHandler())
                .SetHandlerLifetime(Timeout.InfiniteTimeSpan);         // So that it is not disposed after 2 minutes;

                serviceCollection.PostConfigure <JwtBearerOptions>(
                    JwtBearerDefaults.AuthenticationScheme,
                    options => options.BackchannelHttpHandler = Server.CreateHandler());
            });

            Server = new TestServer(builder);

            _builtConfiguration = Server.Services.GetRequiredService <IConfiguration>();
        }
Example #30
0
        private static async Task <BlogCosmosDbService> InitializeCosmosBlogClientInstanceAsync(IConfigurationSection configurationSection)
        {
            string databaseName = configurationSection.GetSection("DatabaseName").Value;
            string account      = configurationSection.GetSection("Account").Value;
            string key          = configurationSection.GetSection("Key").Value;

            CosmosClientBuilder clientBuilder = new CosmosClientBuilder(account, key);
            CosmosClient        client        = clientBuilder
                                                .WithApplicationName(databaseName)
                                                .WithApplicationName(Regions.EastUS)
                                                .WithConnectionModeDirect()
                                                .WithSerializerOptions(new CosmosSerializationOptions()
            {
                PropertyNamingPolicy = CosmosPropertyNamingPolicy.CamelCase
            })
                                                .Build();
            var blogCosmosDbService = new BlogCosmosDbService(client, databaseName);


            DatabaseResponse database = await client.CreateDatabaseIfNotExistsAsync(databaseName);

            //IMPORTANT: Container name is also specified in the BlogCosmosDbService
            await database.Database.DefineContainer(name : "Users", partitionKeyPath : "/userId")
            .WithUniqueKey()
            .Path("/username")
            .Attach()
            .CreateIfNotExistsAsync();

            //check to see if the posts container exists since if this is a new instance we want to insert a Hello World post

            var insertHelloWorldPost = false;

            try
            {
                //check to see if the posts container already exists.  This will throw an exception if it does not exist.
                var postsContainerTemp = client.GetContainer(databaseName, "Posts");
                var _ = await postsContainerTemp.ReadContainerAsync();
            }
            catch (Exception ex2)
            {
                insertHelloWorldPost = true;
            }

            await database.Database.CreateContainerIfNotExistsAsync("Posts", "/postId");

            //posts get upserted into the Feed container from the Change Feed
            await database.Database.CreateContainerIfNotExistsAsync("Feed", "/type");


            //Upsert the sprocs in the posts container.
            var postsContainer = database.Database.GetContainer("Posts");

            await UpsertStoredProcedureAsync(postsContainer, @"CosmosDbScripts\sprocs\createComment.js");
            await UpsertStoredProcedureAsync(postsContainer, @"CosmosDbScripts\sprocs\createLike.js");
            await UpsertStoredProcedureAsync(postsContainer, @"CosmosDbScripts\sprocs\deleteLike.js");
            await UpsertStoredProcedureAsync(postsContainer, @"CosmosDbScripts\sprocs\updateUsernames.js");


            //add the feed container post-trigger (for truncated the number of items in the Feed container).
            var feedContainer = database.Database.GetContainer("Feed");

            await UpsertTriggerAsync(feedContainer, @"CosmosDbScripts\triggers\truncateFeed.js", TriggerOperation.All, TriggerType.Post);

            if (insertHelloWorldPost)
            {
                var helloWorldPostHtml = @"
                        <p>Hi there!</p>
                        <p>This is sample code for the article <a target='_blank' href='https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-model-partition-example'>How to model and partition data on Azure Cosmos DB using a real-world example</a>. </p>
                        <p>To login as the Blog Administrator, register and login as the username <b>jsmith</b>. The Admin username can be changed in the BlogWebApp appsettings.json file.</p>
                        <p>Please post any issues that you have with this sample code to the repository at <a target='_blank' href='https://github.com/jwidmer/AzureCosmosDbBlogExample/issues'>https://github.com/jwidmer/AzureCosmosDbBlogExample/issues</a> </p>
                ";

                var helloWorldPost = new BlogPost
                {
                    PostId         = Guid.NewGuid().ToString(),
                    Title          = "Hello World!",
                    Content        = helloWorldPostHtml,
                    AuthorId       = Guid.NewGuid().ToString(),
                    AuthorUsername = "******",
                    DateCreated    = DateTime.UtcNow,
                };


                //insert the hello world post so the first time the blog is not empty
                await postsContainer.UpsertItemAsync <BlogPost>(helloWorldPost, new PartitionKey(helloWorldPost.PostId));
            }

            return(blogCosmosDbService);
        }