Skip to content

Commit

Permalink
Fix Deployment Type Issue #26752 (#26776)
Browse files Browse the repository at this point in the history
* Fix Deployment Type Issue #27652

* Update to JObject

* Update to only deserialize DeploymentExtended

* Remove unused method

---------

Co-authored-by: Tate Smalligan <tasmalligan@microsoft.com>
  • Loading branch information
2 people authored and azure-powershell-bot committed Nov 28, 2024
1 parent 5d2f4d3 commit 719e6a0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -494,6 +495,18 @@ private TemplateValidationInfo GetTemplateValidationResult(PSDeploymentCmdletPar
return new TemplateValidationInfo(deploymentExtended.Properties?.Providers?.ToList() ?? new List<Provider>(), new List<ErrorDetail>(), deploymentExtended.Properties?.Diagnostics?.ToList() ?? new List<DeploymentDiagnosticsDefinition>());
case DeploymentValidationError deploymentValidationError:
return new TemplateValidationInfo(new List<Provider>(), new List<ErrorDetail>(deploymentValidationError.Error.AsArray()), new List<DeploymentDiagnosticsDefinition>());
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<DeploymentExtended>(validationResult.ToString(), ResourceManagementClient.DeserializationSettings);
return new TemplateValidationInfo(deploymentDeserialized?.Properties?.Providers?.ToList() ?? new List<Provider>(), new List<ErrorDetail>(), deploymentDeserialized?.Properties?.Diagnostics?.ToList() ?? new List<DeploymentDiagnosticsDefinition>());
}
catch (Newtonsoft.Json.JsonException)
{
throw new InvalidOperationException($"Received unexpected type {validationResult.GetType()}");
}
default:
throw new InvalidOperationException($"Received unexpected type {validationResult.GetType()}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<string>(), It.IsAny<Deployment>(), null, new CancellationToken()))
.Returns(Task.Factory.StartNew(() =>
{

var result = new AzureOperationResponse<object>()
{
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<string, List<string>> 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()
Expand Down
1 change: 1 addition & 0 deletions src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 719e6a0

Please sign in to comment.