From 719e6a0efe1383f083601c409dba80efd46731c8 Mon Sep 17 00:00:00 2001 From: Tate Smalligan <161764628+tsmallig33@users.noreply.github.com> Date: Thu, 28 Nov 2024 03:11:33 -0600 Subject: [PATCH] Fix Deployment Type Issue #26752 (#26776) * Fix Deployment Type Issue #27652 * Update to JObject * Update to only deserialize DeploymentExtended * Remove unused method --------- Co-authored-by: Tate Smalligan --- .../SdkClient/NewResourceManagerSdkClient.cs | 13 +++++++ .../ResourceClientTests.cs | 38 +++++++++++++++++++ src/Resources/Resources/ChangeLog.md | 1 + 3 files changed, 52 insertions(+) diff --git a/src/Resources/ResourceManager/SdkClient/NewResourceManagerSdkClient.cs b/src/Resources/ResourceManager/SdkClient/NewResourceManagerSdkClient.cs index 3bb85584173e..96f9d7e7c207 100644 --- a/src/Resources/ResourceManager/SdkClient/NewResourceManagerSdkClient.cs +++ b/src/Resources/ResourceManager/SdkClient/NewResourceManagerSdkClient.cs @@ -40,6 +40,7 @@ using Microsoft.Azure.Management.Resources.Models; using Microsoft.Rest.Azure; using Microsoft.Rest.Azure.OData; +using Microsoft.Rest.Serialization; using Microsoft.WindowsAzure.Commands.Common; using Microsoft.WindowsAzure.Commands.Utilities.Common; using Newtonsoft.Json; @@ -494,6 +495,18 @@ private TemplateValidationInfo GetTemplateValidationResult(PSDeploymentCmdletPar return new TemplateValidationInfo(deploymentExtended.Properties?.Providers?.ToList() ?? new List(), new List(), deploymentExtended.Properties?.Diagnostics?.ToList() ?? new List()); case DeploymentValidationError deploymentValidationError: return new TemplateValidationInfo(new List(), new List(deploymentValidationError.Error.AsArray()), new List()); + case JObject obj: + // 202 Response is not deserialized in DeploymentsOperations so we should attempt to deserialize the object here before failing + // Should attempt to deserialize for success(DeploymentExtended) + try + { + var deploymentDeserialized = SafeJsonConvert.DeserializeObject(validationResult.ToString(), ResourceManagementClient.DeserializationSettings); + return new TemplateValidationInfo(deploymentDeserialized?.Properties?.Providers?.ToList() ?? new List(), new List(), deploymentDeserialized?.Properties?.Diagnostics?.ToList() ?? new List()); + } + catch (Newtonsoft.Json.JsonException) + { + throw new InvalidOperationException($"Received unexpected type {validationResult.GetType()}"); + } default: throw new InvalidOperationException($"Received unexpected type {validationResult.GetType()}"); } diff --git a/src/Resources/Resources.Test/Models.ResourceGroups/ResourceClientTests.cs b/src/Resources/Resources.Test/Models.ResourceGroups/ResourceClientTests.cs index 82f66a4fbccf..28bcb0b31f2b 100644 --- a/src/Resources/Resources.Test/Models.ResourceGroups/ResourceClientTests.cs +++ b/src/Resources/Resources.Test/Models.ResourceGroups/ResourceClientTests.cs @@ -36,6 +36,7 @@ using Microsoft.WindowsAzure.Commands.Test.Utilities.Common; using Moq; using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using Xunit; namespace Microsoft.Azure.Commands.Resources.Test.Models @@ -329,6 +330,43 @@ public void TestTemplateShowsSuccessMessage() progressLoggerMock.Verify(f => f("Template is valid."), Times.Once()); } + [Fact] + [Trait(Category.AcceptanceType, Category.CheckIn)] + public void TestTemplateShowsSuccessMessageWithObjectAsResponse() + { + Uri templateUri = new Uri("http://templateuri.microsoft.com"); + Deployment deploymentFromValidate = new Deployment(); + PSDeploymentCmdletParameters parameters = new PSDeploymentCmdletParameters() + { + ScopeType = DeploymentScopeType.ResourceGroup, + ResourceGroupName = resourceGroupName, + DeploymentMode = DeploymentMode.Incremental, + TemplateFile = templateFile, + }; + resourceGroupMock.Setup(f => f.CheckExistenceWithHttpMessagesAsync(parameters.ResourceGroupName, null, new CancellationToken())) + .Returns(Task.Factory.StartNew(() => CreateAzureOperationResponse(true))); + + deploymentsMock.Setup(f => f.ValidateWithHttpMessagesAsync(resourceGroupName, It.IsAny(), It.IsAny(), null, new CancellationToken())) + .Returns(Task.Factory.StartNew(() => + { + + var result = new AzureOperationResponse() + { + Body = new JObject(new JProperty("id", "DeploymentId")) + }; + + result.Response = new System.Net.Http.HttpResponseMessage(); + result.Response.StatusCode = HttpStatusCode.Accepted; + + return result; + })) + .Callback((string rg, string dn, Deployment d, Dictionary> customHeaders, CancellationToken c) => { deploymentFromValidate = d; }); + + TemplateValidationInfo error = resourcesClient.ValidateDeployment(parameters); + Assert.Empty(error.Errors); + progressLoggerMock.Verify(f => f("Template is valid."), Times.Once()); + } + [Fact] [Trait(Category.AcceptanceType, Category.CheckIn)] public void TestTemplateShowsSuccessMessageWithDiagnostics() diff --git a/src/Resources/Resources/ChangeLog.md b/src/Resources/Resources/ChangeLog.md index b78cde49ec13..71bea3620638 100644 --- a/src/Resources/Resources/ChangeLog.md +++ b/src/Resources/Resources/ChangeLog.md @@ -20,6 +20,7 @@ ## Upcoming Release * Added Diagnostics/Warnings to WhatIf/Validate results for deployments. +* Fixed bug unexpected type issue: [#26752] ## Version 7.7.0 * Updated Resources SDK to 2024-07-01.