Skip to content

Commit 9522c24

Browse files
authored
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 <[email protected]>
1 parent 9002fda commit 9522c24

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/Resources/ResourceManager/SdkClient/NewResourceManagerSdkClient.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
using Microsoft.Azure.Management.Resources.Models;
4141
using Microsoft.Rest.Azure;
4242
using Microsoft.Rest.Azure.OData;
43+
using Microsoft.Rest.Serialization;
4344
using Microsoft.WindowsAzure.Commands.Common;
4445
using Microsoft.WindowsAzure.Commands.Utilities.Common;
4546
using Newtonsoft.Json;
@@ -494,6 +495,18 @@ private TemplateValidationInfo GetTemplateValidationResult(PSDeploymentCmdletPar
494495
return new TemplateValidationInfo(deploymentExtended.Properties?.Providers?.ToList() ?? new List<Provider>(), new List<ErrorDetail>(), deploymentExtended.Properties?.Diagnostics?.ToList() ?? new List<DeploymentDiagnosticsDefinition>());
495496
case DeploymentValidationError deploymentValidationError:
496497
return new TemplateValidationInfo(new List<Provider>(), new List<ErrorDetail>(deploymentValidationError.Error.AsArray()), new List<DeploymentDiagnosticsDefinition>());
498+
case JObject obj:
499+
// 202 Response is not deserialized in DeploymentsOperations so we should attempt to deserialize the object here before failing
500+
// Should attempt to deserialize for success(DeploymentExtended)
501+
try
502+
{
503+
var deploymentDeserialized = SafeJsonConvert.DeserializeObject<DeploymentExtended>(validationResult.ToString(), ResourceManagementClient.DeserializationSettings);
504+
return new TemplateValidationInfo(deploymentDeserialized?.Properties?.Providers?.ToList() ?? new List<Provider>(), new List<ErrorDetail>(), deploymentDeserialized?.Properties?.Diagnostics?.ToList() ?? new List<DeploymentDiagnosticsDefinition>());
505+
}
506+
catch (Newtonsoft.Json.JsonException)
507+
{
508+
throw new InvalidOperationException($"Received unexpected type {validationResult.GetType()}");
509+
}
497510
default:
498511
throw new InvalidOperationException($"Received unexpected type {validationResult.GetType()}");
499512
}

src/Resources/Resources.Test/Models.ResourceGroups/ResourceClientTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
3737
using Moq;
3838
using Newtonsoft.Json;
39+
using Newtonsoft.Json.Linq;
3940
using Xunit;
4041

4142
namespace Microsoft.Azure.Commands.Resources.Test.Models
@@ -329,6 +330,43 @@ public void TestTemplateShowsSuccessMessage()
329330
progressLoggerMock.Verify(f => f("Template is valid."), Times.Once());
330331
}
331332

333+
[Fact]
334+
[Trait(Category.AcceptanceType, Category.CheckIn)]
335+
public void TestTemplateShowsSuccessMessageWithObjectAsResponse()
336+
{
337+
Uri templateUri = new Uri("http://templateuri.microsoft.com");
338+
Deployment deploymentFromValidate = new Deployment();
339+
PSDeploymentCmdletParameters parameters = new PSDeploymentCmdletParameters()
340+
{
341+
ScopeType = DeploymentScopeType.ResourceGroup,
342+
ResourceGroupName = resourceGroupName,
343+
DeploymentMode = DeploymentMode.Incremental,
344+
TemplateFile = templateFile,
345+
};
346+
resourceGroupMock.Setup(f => f.CheckExistenceWithHttpMessagesAsync(parameters.ResourceGroupName, null, new CancellationToken()))
347+
.Returns(Task.Factory.StartNew(() => CreateAzureOperationResponse(true)));
348+
349+
deploymentsMock.Setup(f => f.ValidateWithHttpMessagesAsync(resourceGroupName, It.IsAny<string>(), It.IsAny<Deployment>(), null, new CancellationToken()))
350+
.Returns(Task.Factory.StartNew(() =>
351+
{
352+
353+
var result = new AzureOperationResponse<object>()
354+
{
355+
Body = new JObject(new JProperty("id", "DeploymentId"))
356+
};
357+
358+
result.Response = new System.Net.Http.HttpResponseMessage();
359+
result.Response.StatusCode = HttpStatusCode.Accepted;
360+
361+
return result;
362+
}))
363+
.Callback((string rg, string dn, Deployment d, Dictionary<string, List<string>> customHeaders, CancellationToken c) => { deploymentFromValidate = d; });
364+
365+
TemplateValidationInfo error = resourcesClient.ValidateDeployment(parameters);
366+
Assert.Empty(error.Errors);
367+
progressLoggerMock.Verify(f => f("Template is valid."), Times.Once());
368+
}
369+
332370
[Fact]
333371
[Trait(Category.AcceptanceType, Category.CheckIn)]
334372
public void TestTemplateShowsSuccessMessageWithDiagnostics()

src/Resources/Resources/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
## Upcoming Release
2222
* Added Diagnostics/Warnings to WhatIf/Validate results for deployments.
23+
* Fixed bug unexpected type issue: [#26752]
2324

2425
## Version 7.7.0
2526
* Updated Resources SDK to 2024-07-01.

0 commit comments

Comments
 (0)