Skip to content

[Resources] Add -ProceedIfNoChange parameter to the New-Az*Deployment cmdlets #15510

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

Merged
merged 2 commits into from
Jul 23, 2021
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 @@ -15,12 +15,14 @@
namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.CmdletBase
{
using System;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Common.Strategies;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Formatters;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Properties;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels.Deployments;
using Microsoft.Azure.Management.ResourceManager.Models;

public abstract class DeploymentCreateCmdlet: DeploymentWhatIfCmdlet
{
Expand All @@ -31,6 +33,8 @@ public abstract class DeploymentCreateCmdlet: DeploymentWhatIfCmdlet

protected abstract PSDeploymentCmdletParameters DeploymentParameters { get; }

protected abstract bool ShouldSkipConfirmationIfNoChange();

protected override void OnProcessRecord()
{
string whatIfMessage = null;
Expand All @@ -41,6 +45,21 @@ protected override void OnProcessRecord()
{
PSWhatIfOperationResult whatIfResult = this.ExecuteWhatIf();
string whatIfFormattedOutput = WhatIfOperationResultFormatter.Format(whatIfResult);

if (this.ShouldProcessGivenCurrentConfirmFlagAndPreference() &&
this.ShouldSkipConfirmationIfNoChange() &&
whatIfResult.Changes.All(x => x.ChangeType == ChangeType.NoChange || x.ChangeType == ChangeType.Ignore))
{

var whatIfInformation = new HostInformationMessage { Message = whatIfFormattedOutput };
var tags = new[] { "PSHOST" };

this.WriteInformation(whatIfInformation, tags);
this.ExecuteDeployment();

return;
}

string cursorUp = $"{(char)27}[1A";

// Use \r to override the built-in "What if:" in output.
Expand All @@ -59,24 +78,22 @@ protected void ExecuteDeployment()
{
if (!string.IsNullOrEmpty(this.DeploymentParameters.DeploymentDebugLogLevel))
{
WriteWarning(Resources.WarnOnDeploymentDebugSetting);
this.WriteWarning(Resources.WarnOnDeploymentDebugSetting);
}

if (this.DeploymentParameters.ScopeType == DeploymentScopeType.ResourceGroup)
{
WriteObject(this.ResourceManagerSdkClient.ExecuteResourceGroupDeployment(this.DeploymentParameters));
this.WriteObject(this.ResourceManagerSdkClient.ExecuteResourceGroupDeployment(this.DeploymentParameters));
}
else
{
WriteObject(this.ResourceManagerSdkClient.ExecuteDeployment(this.DeploymentParameters));
this.WriteObject(this.ResourceManagerSdkClient.ExecuteDeployment(this.DeploymentParameters));
}
}

protected bool ShouldExecuteWhatIf()
{
return ShouldProcessGivenCurrentWhatIfFlagAndPreference()
|| ShouldProcessGivenCurrentConfirmFlagAndPreference();
}
protected bool ShouldExecuteWhatIf() =>
this.ShouldProcessGivenCurrentWhatIfFlagAndPreference() ||
this.ShouldProcessGivenCurrentConfirmFlagAndPreference();

private bool ShouldProcessGivenCurrentWhatIfFlagAndPreference()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public class NewAzureManagementGroupDeploymentCmdlet : DeploymentCreateCmdlet
[ValidateChangeTypes]
public string[] WhatIfExcludeChangeType { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation if there is no changes in the What-If result. Applicable when the -Confirm switch is set.")]
public SwitchParameter ProceedIfNoChange { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

Expand Down Expand Up @@ -103,5 +106,7 @@ public class NewAzureManagementGroupDeploymentCmdlet : DeploymentCreateCmdlet
templateParametersObject: GetTemplateParameterObject(this.TemplateParameterObject),
resultFormat: this.WhatIfResultFormat,
excludeChangeTypes: this.WhatIfExcludeChangeType);

protected override bool ShouldSkipConfirmationIfNoChange() => this.ProceedIfNoChange;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public class NewAzureSubscriptionDeploymentCmdlet : DeploymentCreateCmdlet
[ValidateChangeTypes]
public string[] WhatIfExcludeChangeType { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation if there is no changes in the What-If result. Applicable when the -Confirm switch is set.")]
public SwitchParameter ProceedIfNoChange { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

Expand Down Expand Up @@ -98,5 +101,7 @@ public class NewAzureSubscriptionDeploymentCmdlet : DeploymentCreateCmdlet
templateParametersObject: GetTemplateParameterObject(this.TemplateParameterObject),
resultFormat: this.WhatIfResultFormat,
excludeChangeTypes: this.WhatIfExcludeChangeType);

protected override bool ShouldSkipConfirmationIfNoChange() => this.ProceedIfNoChange;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public class NewAzureTenantDeploymentCmdlet: DeploymentCreateCmdlet
[ValidateChangeTypes]
public string[] WhatIfExcludeChangeType { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation if there is no changes in the What-If result. Applicable when the -Confirm switch is set.")]
public SwitchParameter ProceedIfNoChange { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

Expand Down Expand Up @@ -98,5 +101,7 @@ public class NewAzureTenantDeploymentCmdlet: DeploymentCreateCmdlet
resultFormat : this.WhatIfResultFormat,
excludeChangeTypes: this.WhatIfExcludeChangeType
);

protected override bool ShouldSkipConfirmationIfNoChange() => this.ProceedIfNoChange;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public class NewAzureResourceGroupDeploymentCmdlet : DeploymentCreateCmdlet
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
public SwitchParameter Force { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation if there is no changes in the What-If result. Applicable when the -Confirm switch is set.")]
public SwitchParameter ProceedIfNoChange { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
public SwitchParameter AsJob { get; set; }

Expand Down Expand Up @@ -138,5 +141,7 @@ protected override void OnProcessRecord()
() => this.Mode == DeploymentMode.Complete);
}
}

protected override bool ShouldSkipConfirmationIfNoChange() => this.ProceedIfNoChange;
}
}
1 change: 1 addition & 0 deletions src/Resources/Resources/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
## Upcoming Release
* Fixed bug with `PSResource` where some constructors left `SubscriptionId` property unassigned/null. [#10783]
* Added support for creating and updating Template Spec in Bicep file [#15313]
* Added `-ProceedIfNoChange` parameter to deployment create cmdlets.

## Version 4.2.0
* Allowed naming the deployment when testing deployments [#11497]
Expand Down
49 changes: 46 additions & 3 deletions src/Resources/Resources/help/Get-AzDeploymentWhatIfResult.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

### ByTemplateSpecResourceIdAndParamsObject
```
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
[-ExcludeChangeType <String[]>] -TemplateParameterObject <Hashtable> -TemplateSpecId <String>
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

### ByTemplateObjectAndParameterFile
```
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
Expand All @@ -61,6 +68,13 @@ Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

### ByTemplateSpecResourceIdAndParams
```
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
[-ExcludeChangeType <String[]>] -TemplateParameterFile <String> -TemplateSpecId <String>
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

### ByTemplateObjectAndParameterUri
```
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
Expand All @@ -82,6 +96,13 @@ Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

### ByTemplateSpecResourceIdAndParamsUri
```
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
[-ExcludeChangeType <String[]>] -TemplateParameterUri <String> -TemplateSpecId <String>
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

### ByTemplateObjectWithNoParameters
```
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
Expand All @@ -96,6 +117,13 @@ Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

### ByTemplateSpecResourceId
```
Get-AzDeploymentWhatIfResult [-Name <String>] -Location <String> [-ResultFormat <WhatIfResultFormat>]
[-ExcludeChangeType <String[]>] -TemplateSpecId <String> [-SkipTemplateParameterPrompt] [-Pre]
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

## DESCRIPTION
The **Get-AzDeploymentWhatIfResult** cmdlet gets the ARM template What-If result for a template deployment at the current subscription scope. It returns a list of changes indicating what resources will be updated if the deployment is applied without making any changes to real resources. To specify the format for the returning result, use the *ResultFormat* parameter.

Expand Down Expand Up @@ -278,7 +306,7 @@ A file that has the template parameters.

```yaml
Type: System.String
Parameter Sets: ByTemplateObjectAndParameterFile, ByTemplateFileAndParameterFile, ByTemplateUriAndParameterFile
Parameter Sets: ByTemplateObjectAndParameterFile, ByTemplateFileAndParameterFile, ByTemplateUriAndParameterFile, ByTemplateSpecResourceIdAndParams
Aliases:

Required: True
Expand All @@ -293,7 +321,7 @@ A hash table which represents the parameters.

```yaml
Type: System.Collections.Hashtable
Parameter Sets: ByTemplateObjectAndParameterObject, ByTemplateFileAndParameterObject, ByTemplateUriAndParameterObject
Parameter Sets: ByTemplateObjectAndParameterObject, ByTemplateFileAndParameterObject, ByTemplateUriAndParameterObject, ByTemplateSpecResourceIdAndParamsObject
Aliases:

Required: True
Expand All @@ -308,7 +336,22 @@ Uri to the template parameter file.

```yaml
Type: System.String
Parameter Sets: ByTemplateObjectAndParameterUri, ByTemplateFileAndParameterUri, ByTemplateUriAndParameterUri
Parameter Sets: ByTemplateObjectAndParameterUri, ByTemplateFileAndParameterUri, ByTemplateUriAndParameterUri, ByTemplateSpecResourceIdAndParamsUri
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -TemplateSpecId
Resource ID of the templateSpec to be deployed.

```yaml
Type: System.String
Parameter Sets: ByTemplateSpecResourceIdAndParamsObject, ByTemplateSpecResourceIdAndParams, ByTemplateSpecResourceIdAndParamsUri, ByTemplateSpecResourceId
Aliases:

Required: True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ Get-AzManagementGroupDeploymentWhatIfResult [-Name <String>] -ManagementGroupId
[<CommonParameters>]
```

### ByTemplateSpecResourceIdAndParamsObject
```
Get-AzManagementGroupDeploymentWhatIfResult [-Name <String>] -ManagementGroupId <String> -Location <String>
[-ResultFormat <WhatIfResultFormat>] [-ExcludeChangeType <String[]>] -TemplateParameterObject <Hashtable>
-TemplateSpecId <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
```

### ByTemplateObjectAndParameterFile
```
Get-AzManagementGroupDeploymentWhatIfResult [-Name <String>] -ManagementGroupId <String> -Location <String>
Expand All @@ -67,6 +75,14 @@ Get-AzManagementGroupDeploymentWhatIfResult [-Name <String>] -ManagementGroupId
[<CommonParameters>]
```

### ByTemplateSpecResourceIdAndParams
```
Get-AzManagementGroupDeploymentWhatIfResult [-Name <String>] -ManagementGroupId <String> -Location <String>
[-ResultFormat <WhatIfResultFormat>] [-ExcludeChangeType <String[]>] -TemplateParameterFile <String>
-TemplateSpecId <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
```

### ByTemplateObjectAndParameterUri
```
Get-AzManagementGroupDeploymentWhatIfResult [-Name <String>] -ManagementGroupId <String> -Location <String>
Expand All @@ -91,6 +107,14 @@ Get-AzManagementGroupDeploymentWhatIfResult [-Name <String>] -ManagementGroupId
[<CommonParameters>]
```

### ByTemplateSpecResourceIdAndParamsUri
```
Get-AzManagementGroupDeploymentWhatIfResult [-Name <String>] -ManagementGroupId <String> -Location <String>
[-ResultFormat <WhatIfResultFormat>] [-ExcludeChangeType <String[]>] -TemplateParameterUri <String>
-TemplateSpecId <String> [-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>]
[<CommonParameters>]
```

### ByTemplateObjectWithNoParameters
```
Get-AzManagementGroupDeploymentWhatIfResult [-Name <String>] -ManagementGroupId <String> -Location <String>
Expand All @@ -105,6 +129,13 @@ Get-AzManagementGroupDeploymentWhatIfResult [-Name <String>] -ManagementGroupId
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

### ByTemplateSpecResourceId
```
Get-AzManagementGroupDeploymentWhatIfResult [-Name <String>] -ManagementGroupId <String> -Location <String>
[-ResultFormat <WhatIfResultFormat>] [-ExcludeChangeType <String[]>] -TemplateSpecId <String>
[-SkipTemplateParameterPrompt] [-Pre] [-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
```

## DESCRIPTION
The **Get-AzManagementGroupDeploymentWhatIfResult** cmdlet gets the ARM template What-If result for a template deployment at the specified management group scope. It returns a list of changes indicating what resources will be updated if the deployment is applied without making any changes to real resources. To specify the format for the returning result, use the *ResultFormat* parameter.

Expand Down Expand Up @@ -308,7 +339,7 @@ A file that has the template parameters.

```yaml
Type: System.String
Parameter Sets: ByTemplateObjectAndParameterFile, ByTemplateFileAndParameterFile, ByTemplateUriAndParameterFile
Parameter Sets: ByTemplateObjectAndParameterFile, ByTemplateFileAndParameterFile, ByTemplateUriAndParameterFile, ByTemplateSpecResourceIdAndParams
Aliases:

Required: True
Expand All @@ -323,7 +354,7 @@ A hash table which represents the parameters.

```yaml
Type: System.Collections.Hashtable
Parameter Sets: ByTemplateObjectAndParameterObject, ByTemplateFileAndParameterObject, ByTemplateUriAndParameterObject
Parameter Sets: ByTemplateObjectAndParameterObject, ByTemplateFileAndParameterObject, ByTemplateUriAndParameterObject, ByTemplateSpecResourceIdAndParamsObject
Aliases:

Required: True
Expand All @@ -338,7 +369,22 @@ Uri to the template parameter file.

```yaml
Type: System.String
Parameter Sets: ByTemplateObjectAndParameterUri, ByTemplateFileAndParameterUri, ByTemplateUriAndParameterUri
Parameter Sets: ByTemplateObjectAndParameterUri, ByTemplateFileAndParameterUri, ByTemplateUriAndParameterUri, ByTemplateSpecResourceIdAndParamsUri
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -TemplateSpecId
Resource ID of the templateSpec to be deployed.

```yaml
Type: System.String
Parameter Sets: ByTemplateSpecResourceIdAndParamsObject, ByTemplateSpecResourceIdAndParams, ByTemplateSpecResourceIdAndParamsUri, ByTemplateSpecResourceId
Aliases:

Required: True
Expand Down
Loading