Skip to content

Commit 4a46cb4

Browse files
authoredApr 9, 2025
[Resources] Add -SuppressDiagnostics Parameter to Test-Deployment Cmdlets (Azure#27526)
1 parent b7484fe commit 4a46cb4

15 files changed

+793
-607
lines changed
 

Diff for: ‎src/Resources/ResourceManager/Implementation/CmdletBase/TestDeploymentCmdletBase.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public abstract class TestDeploymentCmdletBase : DeploymentCmdletBase, IDynamicP
2121
"or ProviderNoRbac(Performs full validation using RBAC read checks instead of RBAC write checks for provider validation).")]
2222
public string ValidationLevel { get; set; }
2323

24+
[Parameter(Mandatory = false, HelpMessage = "When set, validation diagnostics will not be shown for valid deployments.")]
25+
public SwitchParameter SuppressDiagnostics { get; set; }
26+
2427
public override object GetDynamicParameters()
2528
{
2629
if (!string.IsNullOrEmpty(QueryString))
@@ -32,15 +35,15 @@ public override object GetDynamicParameters()
3235

3336
public void WriteOutput(TemplateValidationInfo validationInfo)
3437
{
35-
if (validationInfo.Errors.Count == 0)
38+
if (validationInfo.Errors.Count == 0 && !SuppressDiagnostics.IsPresent)
3639
{
3740
var builder = new ColoredStringBuilder();
3841

3942
var formatter = new WhatIfOperationResultFormatter(builder);
4043

4144
formatter.FormatDiagnostics(validationInfo.Diagnostics, new List<PSWhatIfChange>());
4245

43-
WriteObject(builder.ToString());
46+
WriteWarning(builder.ToString());
4447
}
4548
else
4649
{

Diff for: ‎src/Resources/Resources.Test/ResourceGroupDeployments/TestAzureResourceGroupDeploymentCommandTests.cs

+51-2
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,55 @@ public void ValidatesPSResourceGroupDeploymentWithUserTemplateWithDiagnostics()
135135

136136
string expected = $@"
137137
138+
Diagnostics (3):
139+
{Color.DarkYellow}(resource1) bad input (202)
140+
{Color.Reset}{Color.DarkYellow}(resource2) bad input 2 (203)
141+
{Color.Reset}{Color.Red}(resource3) bad input 3 (203)
142+
{Color.Reset}"
143+
.Replace("\r\n", Environment.NewLine);
144+
145+
commandRuntimeMock.Verify(f => f.WriteWarning(expected), Times.Once());
146+
commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<List<PSResourceManagerError>>()), Times.Never());
147+
}
148+
149+
[Fact]
150+
[Trait(Category.AcceptanceType, Category.CheckIn)]
151+
public void ValidatesPSResourceGroupDeploymentWithUserTemplateWithDiagnosticsSuppressed()
152+
{
153+
PSDeploymentCmdletParameters expectedParameters = new PSDeploymentCmdletParameters()
154+
{
155+
TemplateFile = templateFile
156+
};
157+
PSDeploymentCmdletParameters actualParameters = new PSDeploymentCmdletParameters();
158+
159+
List<DeploymentDiagnosticsDefinition> diagnostics = new List<DeploymentDiagnosticsDefinition>()
160+
{
161+
new DeploymentDiagnosticsDefinition(code: "202", message: "bad input", target: "resource1", level: "Warning"),
162+
new DeploymentDiagnosticsDefinition(code: "203", message: "bad input 2", target: "resource2", level: "Warning"),
163+
new DeploymentDiagnosticsDefinition(code: "203", message: "bad input 3", target: "resource3", level: "Error")
164+
};
165+
166+
DeploymentPropertiesExtended deploymentPropertiesExtended = new DeploymentPropertiesExtended(diagnostics: diagnostics);
167+
168+
DeploymentValidateResult expectedDeploymentValidateResult = new DeploymentValidateResult(properties: deploymentPropertiesExtended);
169+
TemplateValidationInfo expectedResults = new(expectedDeploymentValidateResult);
170+
171+
resourcesClientMock.Setup(f => f.ValidateDeployment(
172+
It.IsAny<PSDeploymentCmdletParameters>()))
173+
.Returns(expectedResults)
174+
.Callback((PSDeploymentCmdletParameters p) => { actualParameters = p; });
175+
176+
cmdlet.ResourceGroupName = resourceGroupName;
177+
cmdlet.TemplateFile = expectedParameters.TemplateFile;
178+
179+
cmdlet.SuppressDiagnostics = true;
180+
cmdlet.ExecuteCmdlet();
181+
182+
Assert.Equal(expectedParameters.TemplateFile, actualParameters.TemplateFile);
183+
Assert.NotNull(actualParameters.TemplateParameterObject);
184+
185+
string expected = $@"
186+
138187
Diagnostics (3):
139188
{Color.DarkYellow}(resource1) bad input (202)
140189
{Color.Reset}{Color.DarkYellow}(resource2) bad input 2 (203)
@@ -145,8 +194,8 @@ public void ValidatesPSResourceGroupDeploymentWithUserTemplateWithDiagnostics()
145194
JToken expectedToken = new JValue(expected);
146195
PSObject expectedObject = new PSObject(JTokenExtensions.ConvertPropertyValueForPsObject(propertyValue: expectedToken));
147196

148-
commandRuntimeMock.Verify(f => f.WriteObject(expectedObject, true), Times.Once());
149-
commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<List<PSResourceManagerError>>()), Times.Never());
197+
commandRuntimeMock.Verify(f => f.WriteObject(expectedObject, true), Times.Never());
198+
commandRuntimeMock.Verify(f => f.WriteObject(new List<PSResourceManagerError>()), Times.Once());
150199
}
151200

152201
[Fact]

Diff for: ‎src/Resources/Resources/ChangeLog.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
-->
2020

2121
## Upcoming Release
22+
* Added SuppressDiagnostics Parameter to Test-Deployment cmdlets.
2223

2324
## Version 7.10.0
2425
* Fixed the issue that Get-AzReource not working with `-ExpandProperties`. [#11248]

Diff for: ‎src/Resources/Resources/help/Get-AzDeploymentWhatIfResult.md

+54-34
Original file line numberDiff line numberDiff line change
@@ -15,132 +15,137 @@ Gets a template What-If result for a deployment at subscription scope.
1515
### ByTemplateFileWithNoParameters (Default)
1616
```
1717
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
18-
[-ExcludeChangeType <String[]>] -TemplateFile <String> [-SkipTemplateParameterPrompt] [-Pre]
19-
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
18+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateFile <String>
19+
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
20+
[<CommonParameters>]
2021
```
2122

2223
### ByTemplateObjectAndParameterObject
2324
```
2425
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
25-
[-ExcludeChangeType <String[]>] -TemplateParameterObject <Hashtable> -TemplateObject <Hashtable>
26-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
26+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterObject <Hashtable>
27+
-TemplateObject <Hashtable> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
2728
[<CommonParameters>]
2829
```
2930

3031
### ByTemplateFileAndParameterObject
3132
```
3233
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
33-
[-ExcludeChangeType <String[]>] -TemplateParameterObject <Hashtable> -TemplateFile <String>
34-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
34+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterObject <Hashtable>
35+
-TemplateFile <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
3536
[<CommonParameters>]
3637
```
3738

3839
### ByTemplateUriAndParameterObject
3940
```
4041
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
41-
[-ExcludeChangeType <String[]>] -TemplateParameterObject <Hashtable> -TemplateUri <String>
42-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
42+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterObject <Hashtable>
43+
-TemplateUri <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
4344
[<CommonParameters>]
4445
```
4546

4647
### ByTemplateSpecResourceIdAndParamsObject
4748
```
4849
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
49-
[-ExcludeChangeType <String[]>] -TemplateParameterObject <Hashtable> -TemplateSpecId <String>
50-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
50+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterObject <Hashtable>
51+
-TemplateSpecId <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
5152
[<CommonParameters>]
5253
```
5354

5455
### ByTemplateObjectAndParameterFile
5556
```
5657
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
57-
[-ExcludeChangeType <String[]>] -TemplateParameterFile <String> -TemplateObject <Hashtable>
58-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
58+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterFile <String>
59+
-TemplateObject <Hashtable> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
5960
[<CommonParameters>]
6061
```
6162

6263
### ByTemplateFileAndParameterFile
6364
```
6465
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
65-
[-ExcludeChangeType <String[]>] -TemplateParameterFile <String> -TemplateFile <String>
66-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
66+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterFile <String>
67+
-TemplateFile <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
6768
[<CommonParameters>]
6869
```
6970

7071
### ByTemplateUriAndParameterFile
7172
```
7273
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
73-
[-ExcludeChangeType <String[]>] -TemplateParameterFile <String> -TemplateUri <String>
74-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
74+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterFile <String>
75+
-TemplateUri <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
7576
[<CommonParameters>]
7677
```
7778

7879
### ByTemplateSpecResourceIdAndParams
7980
```
8081
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
81-
[-ExcludeChangeType <String[]>] -TemplateParameterFile <String> -TemplateSpecId <String>
82-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
82+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterFile <String>
83+
-TemplateSpecId <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
8384
[<CommonParameters>]
8485
```
8586

8687
### ByParameterFileWithNoTemplate
8788
```
8889
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
89-
[-ExcludeChangeType <String[]>] -TemplateParameterFile <String> [-SkipTemplateParameterPrompt] [-Pre]
90-
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
90+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterFile <String>
91+
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
92+
[<CommonParameters>]
9193
```
9294

9395
### ByTemplateObjectAndParameterUri
9496
```
9597
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
96-
[-ExcludeChangeType <String[]>] -TemplateParameterUri <String> -TemplateObject <Hashtable>
97-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
98+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterUri <String>
99+
-TemplateObject <Hashtable> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
98100
[<CommonParameters>]
99101
```
100102

101103
### ByTemplateFileAndParameterUri
102104
```
103105
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
104-
[-ExcludeChangeType <String[]>] -TemplateParameterUri <String> -TemplateFile <String>
105-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
106+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterUri <String>
107+
-TemplateFile <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
106108
[<CommonParameters>]
107109
```
108110

109111
### ByTemplateUriAndParameterUri
110112
```
111113
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
112-
[-ExcludeChangeType <String[]>] -TemplateParameterUri <String> -TemplateUri <String>
113-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
114+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterUri <String>
115+
-TemplateUri <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
114116
[<CommonParameters>]
115117
```
116118

117119
### ByTemplateSpecResourceIdAndParamsUri
118120
```
119121
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
120-
[-ExcludeChangeType <String[]>] -TemplateParameterUri <String> -TemplateSpecId <String>
121-
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
122+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateParameterUri <String>
123+
-TemplateSpecId <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
122124
[<CommonParameters>]
123125
```
124126

125127
### ByTemplateObjectWithNoParameters
126128
```
127129
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
128-
[-ExcludeChangeType <String[]>] -TemplateObject <Hashtable> [-SkipTemplateParameterPrompt] [-Pre]
129-
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
130+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateObject <Hashtable>
131+
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
132+
[<CommonParameters>]
130133
```
131134

132135
### ByTemplateUriWithNoParameters
133136
```
134137
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
135-
[-ExcludeChangeType <String[]>] -TemplateUri <String> [-SkipTemplateParameterPrompt] [-Pre]
136-
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
138+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateUri <String>
139+
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
140+
[<CommonParameters>]
137141
```
138142

139143
### ByTemplateSpecResourceId
140144
```
141145
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
142-
[-ExcludeChangeType <String[]>] -TemplateSpecId <String> [-SkipTemplateParameterPrompt] [-Pre]
143-
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
146+
[-ExcludeChangeType <String[]>] [-ValidationLevel <String>] -TemplateSpecId <String>
147+
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
148+
[<CommonParameters>]
144149
```
145150

146151
## DESCRIPTION
@@ -403,6 +408,21 @@ Accept pipeline input: True (ByPropertyName)
403408
Accept wildcard characters: False
404409
```
405410
411+
### -ValidationLevel
412+
Sets the validation level for validate/what-if. ValidationLevel can be Template(Skips provider validation), Provider(Performs full validation), or ProviderNoRbac(Performs full validation using RBAC read checks instead of RBAC write checks for provider validation).
413+
414+
```yaml
415+
Type: System.String
416+
Parameter Sets: (All)
417+
Aliases:
418+
419+
Required: False
420+
Position: Named
421+
Default value: None
422+
Accept pipeline input: False
423+
Accept wildcard characters: False
424+
```
425+
406426
### CommonParameters
407427
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
408428

0 commit comments

Comments
 (0)