From 29ce06db7776dc109b965e4102438b2988ddc8f0 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Thu, 4 Apr 2024 10:53:14 +0100 Subject: [PATCH 1/2] Use CreateContainerIfNotExistsAsync on the Cosmos SDK to be compatible with RBAC Fixes #33354 --- .../Properties/CosmosStrings.Designer.cs | 8 ++++++++ .../Properties/CosmosStrings.resx | 3 +++ .../Storage/Internal/CosmosClientWrapper.cs | 18 ++++++++++++------ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs b/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs index aecd79717d0..ff635c4bc91 100644 --- a/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs +++ b/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs @@ -31,6 +31,14 @@ public static string AnalyticalTTLMismatch(object? ttl1, object? entityType1, ob GetString("AnalyticalTTLMismatch", nameof(ttl1), nameof(entityType1), nameof(entityType2), nameof(ttl2), nameof(container)), ttl1, entityType1, entityType2, ttl2, container); + /// + /// The Azure Cosmos DB operation failed with error code '{error}'. + /// + public static string BadResponse(object? errorCode) + => string.Format( + GetString("BadResponse", nameof(errorCode)), + errorCode); + /// /// The Cosmos database does not support 'CanConnect' or 'CanConnectAsync'. /// diff --git a/src/EFCore.Cosmos/Properties/CosmosStrings.resx b/src/EFCore.Cosmos/Properties/CosmosStrings.resx index 0709e132b3a..800df867c58 100644 --- a/src/EFCore.Cosmos/Properties/CosmosStrings.resx +++ b/src/EFCore.Cosmos/Properties/CosmosStrings.resx @@ -120,6 +120,9 @@ The time to live for analytical store was configured to '{ttl1}' on '{entityType1}', but on '{entityType2}' it was configured to '{ttl2}'. All entity types mapped to the same container '{container}' must be configured with the same time to live for analytical store. + + The Azure Cosmos DB operation failed with error code '{error}'. + The Cosmos database does not support 'CanConnect' or 'CanConnectAsync'. diff --git a/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs b/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs index 1c1fbfb30fa..eb0010eb45b 100644 --- a/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs +++ b/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs @@ -8,6 +8,7 @@ using System.Text; using Microsoft.EntityFrameworkCore.Cosmos.Diagnostics.Internal; using Microsoft.EntityFrameworkCore.Cosmos.Infrastructure.Internal; +using Microsoft.EntityFrameworkCore.Cosmos.Internal; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -201,23 +202,28 @@ private static async Task CreateContainerIfNotExistsOnceAsync( CancellationToken cancellationToken = default) { var (parameters, wrapper) = parametersTuple; - using var response = await wrapper.Client.GetDatabase(wrapper._databaseId).CreateContainerStreamAsync( + var response = await wrapper.Client.GetDatabase(wrapper._databaseId).CreateContainerIfNotExistsAsync( new Azure.Cosmos.ContainerProperties(parameters.Id, "/" + parameters.PartitionKey) { PartitionKeyDefinitionVersion = PartitionKeyDefinitionVersion.V2, DefaultTimeToLive = parameters.DefaultTimeToLive, AnalyticalStoreTimeToLiveInSeconds = parameters.AnalyticalStoreTimeToLiveInSeconds }, - parameters.Throughput, + throughput: parameters.Throughput?.Throughput, cancellationToken: cancellationToken) .ConfigureAwait(false); - if (response.StatusCode == HttpStatusCode.Conflict) + + if (response.StatusCode == HttpStatusCode.Created) { - return false; + return true; } - response.EnsureSuccessStatusCode(); - return response.StatusCode == HttpStatusCode.Created; + if (response.StatusCode != HttpStatusCode.OK) + { + throw new InvalidOperationException(CosmosStrings.BadResponse(response.StatusCode)); + } + + return false; } /// From 6b14c280680e6e4b2b13d75d82abc20f8ddefea7 Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Fri, 5 Apr 2024 10:56:26 +0100 Subject: [PATCH 2/2] Updates based on review feedback. --- .../Properties/CosmosStrings.Designer.cs | 8 -------- src/EFCore.Cosmos/Properties/CosmosStrings.resx | 3 --- .../Storage/Internal/CosmosClientWrapper.cs | 13 +------------ 3 files changed, 1 insertion(+), 23 deletions(-) diff --git a/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs b/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs index ff635c4bc91..aecd79717d0 100644 --- a/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs +++ b/src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs @@ -31,14 +31,6 @@ public static string AnalyticalTTLMismatch(object? ttl1, object? entityType1, ob GetString("AnalyticalTTLMismatch", nameof(ttl1), nameof(entityType1), nameof(entityType2), nameof(ttl2), nameof(container)), ttl1, entityType1, entityType2, ttl2, container); - /// - /// The Azure Cosmos DB operation failed with error code '{error}'. - /// - public static string BadResponse(object? errorCode) - => string.Format( - GetString("BadResponse", nameof(errorCode)), - errorCode); - /// /// The Cosmos database does not support 'CanConnect' or 'CanConnectAsync'. /// diff --git a/src/EFCore.Cosmos/Properties/CosmosStrings.resx b/src/EFCore.Cosmos/Properties/CosmosStrings.resx index 800df867c58..0709e132b3a 100644 --- a/src/EFCore.Cosmos/Properties/CosmosStrings.resx +++ b/src/EFCore.Cosmos/Properties/CosmosStrings.resx @@ -120,9 +120,6 @@ The time to live for analytical store was configured to '{ttl1}' on '{entityType1}', but on '{entityType2}' it was configured to '{ttl2}'. All entity types mapped to the same container '{container}' must be configured with the same time to live for analytical store. - - The Azure Cosmos DB operation failed with error code '{error}'. - The Cosmos database does not support 'CanConnect' or 'CanConnectAsync'. diff --git a/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs b/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs index eb0010eb45b..2f19ee60b13 100644 --- a/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs +++ b/src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs @@ -8,7 +8,6 @@ using System.Text; using Microsoft.EntityFrameworkCore.Cosmos.Diagnostics.Internal; using Microsoft.EntityFrameworkCore.Cosmos.Infrastructure.Internal; -using Microsoft.EntityFrameworkCore.Cosmos.Internal; using Newtonsoft.Json; using Newtonsoft.Json.Linq; @@ -213,17 +212,7 @@ private static async Task CreateContainerIfNotExistsOnceAsync( cancellationToken: cancellationToken) .ConfigureAwait(false); - if (response.StatusCode == HttpStatusCode.Created) - { - return true; - } - - if (response.StatusCode != HttpStatusCode.OK) - { - throw new InvalidOperationException(CosmosStrings.BadResponse(response.StatusCode)); - } - - return false; + return response.StatusCode == HttpStatusCode.Created; } ///