Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Resources] Handle nested array changes in What-If formatter #20689

Merged
merged 2 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private void FormatPropertyChange(PSWhatIfPropertyChange propertyChange, int max

case PropertyChangeType.Array:
this.FormatPropertyChangePath(propertyChangeType, path, null, children, maxPathLength, indentLevel);
this.FormatPropertyArrayChange(propertyChange.Children, indentLevel + 1);
this.FormatPropertyArrayChange(propertyChange, propertyChange.Children, indentLevel + 1);
break;

case PropertyChangeType.NoEffect:
Expand All @@ -351,6 +351,11 @@ private void FormatPropertyChangePath(
int maxPathLength,
int indentLevel)
{
if (string.IsNullOrEmpty(path))
{
return;
}

int paddingWidth = maxPathLength - path.Length + 1;
bool hasChildren = children != null && children.Count > 0;

Expand Down Expand Up @@ -449,8 +454,17 @@ private void FormatPropertyModify(PSWhatIfPropertyChange propertyChange, int ind
}
}

private void FormatPropertyArrayChange(IList<PSWhatIfPropertyChange> propertyChanges, int indentLevel)
private void FormatPropertyArrayChange(PSWhatIfPropertyChange parentPropertyChange, IList<PSWhatIfPropertyChange> propertyChanges, int indentLevel)
{
if (string.IsNullOrEmpty(parentPropertyChange.Path))
{
// The parent change doesn't have a path, which means the current
// array change is a nested change. We need to decrease indent_level
// and print indentation before printing "[".
indentLevel--;
FormatIndent(indentLevel);
}

if (propertyChanges.Count == 0)
{
this.Builder.AppendLine("[]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Microsoft.Azure.Commands.Resources.Test.Formatters
using ResourceManager.Cmdlets.SdkModels.Deployments;
using System;
using System.Collections.Generic;
using System.IO;
using WindowsAzure.Commands.ScenarioTest;
using Xunit;

Expand Down Expand Up @@ -441,6 +442,104 @@ public void Format_ChangeTypeModify_FormatsPropertyChanges()
// Assert.
Assert.Contains(expected, result);
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void Format_nested_array_changes_does_not_throw()
{
// Arrange.
var whatIfChanges = new List<WhatIfChange>
{
new WhatIfChange
{
ResourceId = "/subscriptions/00000000-0000-0000-0000-000000000004/resourceGroups/rg4/providers/Microsoft.DocumentDB/databaseAccounts/myaccount/sqlDatabases/accesscontrol/containers/workflows",
ChangeType = ChangeType.Modify,
Delta = new List<WhatIfPropertyChange>
{
new WhatIfPropertyChange
{
Path = "properties.resource.indexingPolicy.compositeIndexes",
PropertyChangeType = PropertyChangeType.Array,
Children = new List<WhatIfPropertyChange>
{
new WhatIfPropertyChange
{
Path = "0",
PropertyChangeType = PropertyChangeType.Modify,
Children = new List<WhatIfPropertyChange>
{
new WhatIfPropertyChange
{
Path = null,
PropertyChangeType = PropertyChangeType.Array,
Children = new List<WhatIfPropertyChange>
{
new WhatIfPropertyChange
{
Path = "0",
PropertyChangeType = PropertyChangeType.Modify,
Children = new List<WhatIfPropertyChange>
{
new WhatIfPropertyChange
{
Path = "order",
PropertyChangeType = PropertyChangeType.Delete,
Before = "ascending",
}
}
},
new WhatIfPropertyChange
{
Path = "1",
PropertyChangeType = PropertyChangeType.Modify,
Children = new List<WhatIfPropertyChange>
{
new WhatIfPropertyChange
{
Path = "order",
PropertyChangeType = PropertyChangeType.Delete,
Before = "ascending",
}
}
}
}
}
}
}
}
},
}
}
};

string expected = $@"
Scope: /subscriptions/00000000-0000-0000-0000-000000000004/resourceGroups/rg4
{Color.Purple}
~ Microsoft.DocumentDB/databaseAccounts/myaccount/sqlDatabases/accesscontrol/containers/workflows{Color.Reset}
{Color.Purple}~{Color.Reset} properties.resource.indexingPolicy.compositeIndexes{Color.Reset}:{Color.Reset} [
{Color.Purple}~{Color.Reset} 0{Color.Reset}:{Color.Reset}

[
{Color.Purple}~{Color.Reset} 0{Color.Reset}:{Color.Reset}

{Color.Orange}-{Color.Reset} order{Color.Reset}:{Color.Reset} {Color.Orange}""ascending""{Color.Reset}

{Color.Purple}~{Color.Reset} 1{Color.Reset}:{Color.Reset}

{Color.Orange}-{Color.Reset} order{Color.Reset}:{Color.Reset} {Color.Orange}""ascending""{Color.Reset}

]

]
".Replace("\r\n", Environment.NewLine);

// Act.
string result = WhatIfOperationResultFormatter.Format(
new PSWhatIfOperationResult(new WhatIfOperationResult(changes: whatIfChanges)));

// Assert.
Assert.Contains(expected, result);
}
}
}

1 change: 1 addition & 0 deletions src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-->

## Upcoming Release
* Fixed an issue where running deployment cmdlets with `-WhatIf` throws exception when formatting results with nested array changes

## Version 6.5.1
* Fixed issue introduced in previous fix for `Set-AzPolicySetDefinition` InternalServerError when the initiative is too large [#20238], which will remove space in value.
Expand Down