From 16949770b60b98d400e7b7d3be0d0ccebf3f800e Mon Sep 17 00:00:00 2001 From: David Schneiderbauer Date: Fri, 23 Aug 2024 08:15:04 +0200 Subject: [PATCH 1/2] consider Http Status Code OK when waiting for long running operations due to a change in the Api Management Rest API --- tools/code/common/Http.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/code/common/Http.cs b/tools/code/common/Http.cs index 40961367..05932aae 100644 --- a/tools/code/common/Http.cs +++ b/tools/code/common/Http.cs @@ -253,7 +253,7 @@ public static Request CreateRequest(this HttpPipeline pipeline, Uri uri, Request private static async ValueTask WaitForLongRunningOperation(this HttpPipeline pipeline, Response response, CancellationToken cancellationToken) { var updatedResponse = response; - while ((updatedResponse.Status == ((int)HttpStatusCode.Accepted)) + while (updatedResponse.Status is (int)HttpStatusCode.OK or (int)HttpStatusCode.Created or (int)HttpStatusCode.Accepted && updatedResponse.Headers.TryGetValue("Location", out var locationHeaderValue) && Uri.TryCreate(locationHeaderValue, UriKind.Absolute, out var locationUri) && locationUri is not null) From 4b4225902c77caa82e521027fa13a7a087e6d537 Mon Sep 17 00:00:00 2001 From: David Schneiderbauer Date: Thu, 5 Sep 2024 11:36:45 +0200 Subject: [PATCH 2/2] check ProvisioningState when waiting for long running operations and response status is either OK or Created --- tools/code/common/Http.cs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/tools/code/common/Http.cs b/tools/code/common/Http.cs index 05932aae..1772efc8 100644 --- a/tools/code/common/Http.cs +++ b/tools/code/common/Http.cs @@ -253,7 +253,8 @@ public static Request CreateRequest(this HttpPipeline pipeline, Uri uri, Request private static async ValueTask WaitForLongRunningOperation(this HttpPipeline pipeline, Response response, CancellationToken cancellationToken) { var updatedResponse = response; - while (updatedResponse.Status is (int)HttpStatusCode.OK or (int)HttpStatusCode.Created or (int)HttpStatusCode.Accepted + while (((updatedResponse.Status is (int)HttpStatusCode.OK or (int)HttpStatusCode.Created && IsProvisioningInProgress(updatedResponse)) || + updatedResponse.Status == (int)HttpStatusCode.Accepted) && updatedResponse.Headers.TryGetValue("Location", out var locationHeaderValue) && Uri.TryCreate(locationHeaderValue, UriKind.Absolute, out var locationUri) && locationUri is not null) @@ -278,6 +279,23 @@ private static async ValueTask WaitForLongRunningOperation(this HttpPi return updatedResponse; } + + private static bool IsProvisioningInProgress(Response response) + { + try + { + return response.Content.ToObjectFromJson() + .TryGetJsonObjectProperty("properties") + .Bind(json => json.TryGetStringProperty("ProvisioningState")) + .ToOption() + .Where(state => state.Equals("InProgress", StringComparison.OrdinalIgnoreCase)) + .IsSome; + } + catch (JsonException) + { + return false; + } + } } public sealed class ILoggerHttpPipelinePolicy(ILogger logger) : HttpPipelinePolicy