Skip to content

Commit f6da6cc

Browse files
author
Rachid Cesin Gorostieta
committed
Add Rollback option to resource group level resources
1 parent 50100a4 commit f6da6cc

File tree

10 files changed

+206
-24
lines changed

10 files changed

+206
-24
lines changed

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Implementation/ResourceGroupDeployments/NewAzureResourceGroupDeploymentCmdlet.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public class NewAzureResourceGroupDeploymentCmdlet : ResourceWithParameterCmdlet
4545
[ValidateSet("RequestContent", "ResponseContent", "All", "None", IgnoreCase = true)]
4646
public string DeploymentDebugLogLevel { get; set; }
4747

48+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of a deployment to roll back to on error, or use as a flag to roll back to the last successful deployment.")]
49+
public string RollbackAction { get; set; }
50+
4851
[Parameter(Mandatory = false, HelpMessage = "Do not ask for confirmation.")]
4952
public SwitchParameter Force { get; set; }
5053

@@ -74,7 +77,14 @@ public override void ExecuteCmdlet()
7477
TemplateFile = TemplateUri ?? this.TryResolvePath(TemplateFile),
7578
TemplateParameterObject = GetTemplateParameterObject(TemplateParameterObject),
7679
ParameterUri = TemplateParameterUri,
77-
DeploymentDebugLogLevel = GetDeploymentDebugLogLevel(DeploymentDebugLogLevel)
80+
DeploymentDebugLogLevel = GetDeploymentDebugLogLevel(DeploymentDebugLogLevel),
81+
OnErrorDeployment = RollbackAction != null
82+
? new OnErrorDeployment
83+
{
84+
Type = string.IsNullOrWhiteSpace(RollbackAction) ? OnErrorDeploymentType.LastSuccessful : OnErrorDeploymentType.SpecificDeployment,
85+
DeploymentName = string.IsNullOrWhiteSpace(RollbackAction) ? null : RollbackAction
86+
}
87+
: null
7888
};
7989

8090
if (!string.IsNullOrEmpty(parameters.DeploymentDebugLogLevel))

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/Implementation/ResourceGroupDeployments/TestAzureResourceGroupDeploymentCmdlet.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public class TestAzureResourceGroupDeploymentCmdlet : ResourceWithParameterCmdle
3535
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The deployment mode.")]
3636
public DeploymentMode Mode { get; set; }
3737

38+
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The name of a deployment to roll back to on error, or use as a flag to roll back to the last successful deployment.")]
39+
public string RollbackAction { get; set; }
40+
3841
public TestAzureResourceGroupDeploymentCmdlet()
3942
{
4043
this.Mode = DeploymentMode.Incremental;
@@ -47,7 +50,14 @@ public override void ExecuteCmdlet()
4750
ResourceGroupName = ResourceGroupName,
4851
TemplateFile = TemplateUri ?? this.TryResolvePath(TemplateFile),
4952
TemplateParameterObject = GetTemplateParameterObject(TemplateParameterObject),
50-
ParameterUri = TemplateParameterUri
53+
ParameterUri = TemplateParameterUri,
54+
OnErrorDeployment = RollbackAction != null
55+
? new OnErrorDeployment
56+
{
57+
Type = string.IsNullOrWhiteSpace(RollbackAction) ? OnErrorDeploymentType.LastSuccessful : OnErrorDeploymentType.SpecificDeployment,
58+
DeploymentName = string.IsNullOrWhiteSpace(RollbackAction) ? null : RollbackAction
59+
}
60+
: null
5161
};
5262

5363
WriteObject(ResourceManagerSdkClient.ValidateDeployment(parameters, Mode));

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/SdkClient/ResourceManagerSdkClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ private Deployment CreateBasicDeployment(PSDeploymentCmdletParameters parameters
450450
}
451451

452452
deployment.Location = parameters.Location;
453+
deployment.Properties.OnErrorDeployment = parameters.OnErrorDeployment;
453454

454455
return deployment;
455456
}

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/SdkModels/Deployments/PSDeploymentCmdletParameters.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ public class PSDeploymentCmdletParameters
2525
public string ParameterUri { get; set; }
2626

2727
public string DeploymentDebugLogLevel { get; set; }
28+
29+
public OnErrorDeployment OnErrorDeployment { get; set; }
2830
}
2931
}

src/ResourceManager/Resources/Commands.ResourceManager/Cmdlets/SdkModels/Deployments/PSResourceGroupDeployment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels
2222
public class PSResourceGroupDeployment: PSDeploymentObject
2323
{
2424
public string ResourceGroupName { get; set; }
25+
26+
public OnErrorDeploymentExtended OnErrorDeployment { get; set; }
2527
}
2628
}

src/ResourceManager/Resources/Commands.Resources.Test/ResourceGroupDeployments/NewAzureResourceGroupDeploymentCommandTests.cs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class NewAzureResourceGroupDeploymentCommandTests : RMTestBase
4242

4343
private string deploymentName = "fooDeployment";
4444

45+
private string lastDeploymentName = "oldfooDeployment";
46+
4547
private string templateFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Resources\sampleTemplateFile.json");
4648

4749
public NewAzureResourceGroupDeploymentCommandTests(ITestOutputHelper output)
@@ -107,6 +109,139 @@ public void CreatesNewPSResourceGroupDeploymentWithUserTemplate()
107109
Assert.Equal(expectedParameters.DeploymentName, actualParameters.DeploymentName);
108110
Assert.Equal(expectedParameters.TemplateFile, actualParameters.TemplateFile);
109111
Assert.NotNull(actualParameters.TemplateParameterObject);
112+
Assert.Null(actualParameters.OnErrorDeployment);
113+
114+
commandRuntimeMock.Verify(f => f.WriteObject(expected), Times.Once());
115+
}
116+
117+
[Fact]
118+
[Trait(Category.AcceptanceType, Category.CheckIn)]
119+
public void CreatesNewPSResourceGroupDeploymentWithUserTemplateSpecificRollback()
120+
{
121+
PSDeploymentCmdletParameters expectedParameters = new PSDeploymentCmdletParameters()
122+
{
123+
TemplateFile = templateFile,
124+
DeploymentName = deploymentName,
125+
OnErrorDeployment = new OnErrorDeployment
126+
{
127+
Type = OnErrorDeploymentType.SpecificDeployment,
128+
DeploymentName = lastDeploymentName
129+
}
130+
};
131+
PSDeploymentCmdletParameters actualParameters = new PSDeploymentCmdletParameters();
132+
PSResourceGroupDeployment expected = new PSResourceGroupDeployment()
133+
{
134+
Mode = DeploymentMode.Incremental,
135+
DeploymentName = deploymentName,
136+
CorrelationId = "123",
137+
Outputs = new Dictionary<string, DeploymentVariable>()
138+
{
139+
{ "Variable1", new DeploymentVariable() { Value = "true", Type = "bool" } },
140+
{ "Variable2", new DeploymentVariable() { Value = "10", Type = "int" } },
141+
{ "Variable3", new DeploymentVariable() { Value = "hello world", Type = "string" } }
142+
},
143+
Parameters = new Dictionary<string, DeploymentVariable>()
144+
{
145+
{ "Parameter1", new DeploymentVariable() { Value = "true", Type = "bool" } },
146+
{ "Parameter2", new DeploymentVariable() { Value = "10", Type = "int" } },
147+
{ "Parameter3", new DeploymentVariable() { Value = "hello world", Type = "string" } }
148+
},
149+
ProvisioningState = ProvisioningState.Succeeded.ToString(),
150+
ResourceGroupName = resourceGroupName,
151+
TemplateLink = new TemplateLink()
152+
{
153+
ContentVersion = "1.0",
154+
Uri = "http://mytemplate.com"
155+
},
156+
Timestamp = new DateTime(2014, 2, 13),
157+
OnErrorDeployment = new OnErrorDeploymentExtended
158+
{
159+
Type = OnErrorDeploymentType.SpecificDeployment,
160+
DeploymentName = lastDeploymentName
161+
}
162+
};
163+
resourcesClientMock.Setup(f => f.ExecuteDeployment(
164+
It.IsAny<PSDeploymentCmdletParameters>()))
165+
.Returns(expected)
166+
.Callback((PSDeploymentCmdletParameters p) => { actualParameters = p; });
167+
168+
cmdlet.ResourceGroupName = resourceGroupName;
169+
cmdlet.Name = expectedParameters.DeploymentName;
170+
cmdlet.TemplateFile = expectedParameters.TemplateFile;
171+
cmdlet.RollbackAction = lastDeploymentName;
172+
cmdlet.ExecuteCmdlet();
173+
174+
Assert.Equal(expectedParameters.DeploymentName, actualParameters.DeploymentName);
175+
Assert.Equal(expectedParameters.TemplateFile, actualParameters.TemplateFile);
176+
Assert.NotNull(actualParameters.TemplateParameterObject);
177+
Assert.NotNull(actualParameters.OnErrorDeployment);
178+
Assert.Equal(expectedParameters.OnErrorDeployment.Type, actualParameters.OnErrorDeployment.Type);
179+
Assert.Equal(expectedParameters.OnErrorDeployment.DeploymentName, actualParameters.OnErrorDeployment.DeploymentName);
180+
181+
commandRuntimeMock.Verify(f => f.WriteObject(expected), Times.Once());
182+
}
183+
184+
[Fact]
185+
[Trait(Category.AcceptanceType, Category.CheckIn)]
186+
public void CreatesNewPSResourceGroupDeploymentWithUserTemplateEmptyRollback()
187+
{
188+
PSDeploymentCmdletParameters expectedParameters = new PSDeploymentCmdletParameters()
189+
{
190+
TemplateFile = templateFile,
191+
DeploymentName = deploymentName,
192+
OnErrorDeployment = new OnErrorDeployment
193+
{
194+
Type = OnErrorDeploymentType.LastSuccessful,
195+
}
196+
};
197+
PSDeploymentCmdletParameters actualParameters = new PSDeploymentCmdletParameters();
198+
PSResourceGroupDeployment expected = new PSResourceGroupDeployment()
199+
{
200+
Mode = DeploymentMode.Incremental,
201+
DeploymentName = deploymentName,
202+
CorrelationId = "123",
203+
Outputs = new Dictionary<string, DeploymentVariable>()
204+
{
205+
{ "Variable1", new DeploymentVariable() { Value = "true", Type = "bool" } },
206+
{ "Variable2", new DeploymentVariable() { Value = "10", Type = "int" } },
207+
{ "Variable3", new DeploymentVariable() { Value = "hello world", Type = "string" } }
208+
},
209+
Parameters = new Dictionary<string, DeploymentVariable>()
210+
{
211+
{ "Parameter1", new DeploymentVariable() { Value = "true", Type = "bool" } },
212+
{ "Parameter2", new DeploymentVariable() { Value = "10", Type = "int" } },
213+
{ "Parameter3", new DeploymentVariable() { Value = "hello world", Type = "string" } }
214+
},
215+
ProvisioningState = ProvisioningState.Succeeded.ToString(),
216+
ResourceGroupName = resourceGroupName,
217+
TemplateLink = new TemplateLink()
218+
{
219+
ContentVersion = "1.0",
220+
Uri = "http://mytemplate.com"
221+
},
222+
Timestamp = new DateTime(2014, 2, 13),
223+
OnErrorDeployment = new OnErrorDeploymentExtended
224+
{
225+
Type = OnErrorDeploymentType.LastSuccessful,
226+
}
227+
};
228+
resourcesClientMock.Setup(f => f.ExecuteDeployment(
229+
It.IsAny<PSDeploymentCmdletParameters>()))
230+
.Returns(expected)
231+
.Callback((PSDeploymentCmdletParameters p) => { actualParameters = p; });
232+
233+
cmdlet.ResourceGroupName = resourceGroupName;
234+
cmdlet.Name = expectedParameters.DeploymentName;
235+
cmdlet.TemplateFile = expectedParameters.TemplateFile;
236+
cmdlet.RollbackAction = string.Empty;
237+
cmdlet.ExecuteCmdlet();
238+
239+
Assert.Equal(expectedParameters.DeploymentName, actualParameters.DeploymentName);
240+
Assert.Equal(expectedParameters.TemplateFile, actualParameters.TemplateFile);
241+
Assert.NotNull(actualParameters.TemplateParameterObject);
242+
Assert.NotNull(actualParameters.OnErrorDeployment);
243+
Assert.Equal(expectedParameters.OnErrorDeployment.Type, actualParameters.OnErrorDeployment.Type);
244+
Assert.Equal(expectedParameters.OnErrorDeployment.DeploymentName, actualParameters.OnErrorDeployment.DeploymentName);
110245

111246
commandRuntimeMock.Verify(f => f.WriteObject(expected), Times.Once());
112247
}

src/ResourceManager/Resources/Commands.Resources/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
- Additional information about change #1
1919
-->
2020
## Current Release
21+
* Update New-AzureRmResourceGroupDeployment with new parameter RollbackAction
22+
- Add support for OnErrorDeployment with the new parameter.
2123

2224
## Version 6.4.2
2325
* Fixed issue with creating managed applications from the MarketPlace.

src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/PSResourceGroupDeployment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,7 @@ public string OutputsString
5252
{
5353
get { return ResourcesExtensions.ConstructDeploymentVariableTable(Outputs); }
5454
}
55+
56+
public OnErrorDeploymentExtended OnErrorDeployment { get; set; }
5557
}
5658
}

src/ResourceManager/Resources/Commands.Resources/Models.ResourceGroups/ResourcesExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ private static PSResourceGroupDeployment CreatePSResourceGroupDeployment(
230230
deploymentObject.ProvisioningState = properties.ProvisioningState;
231231
deploymentObject.TemplateLink = properties.TemplateLink;
232232
deploymentObject.CorrelationId = properties.CorrelationId;
233+
deploymentObject.OnErrorDeployment = properties.OnErrorDeployment;
233234

234235
if (properties.DebugSetting() != null && !string.IsNullOrEmpty(properties.DebugSetting().DetailLevel()))
235236
{

src/ResourceManager/Resources/Commands.Resources/help/New-AzureRmResourceGroupDeployment.md

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,63 +16,65 @@ Adds an Azure deployment to a resource group.
1616
### ByTemplateFileWithNoParameters (Default)
1717
```
1818
New-AzureRmResourceGroupDeployment [-Name <String>] -ResourceGroupName <String> [-Mode <DeploymentMode>]
19-
[-DeploymentDebugLogLevel <String>] [-Force] [-AsJob] -TemplateFile <String> [-ApiVersion <String>] [-Pre]
20-
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
19+
[-DeploymentDebugLogLevel <String>] [-RollbackAction <String>] [-Force] [-AsJob] -TemplateFile <String>
20+
[-ApiVersion <String>] [-Pre] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
21+
[<CommonParameters>]
2122
```
2223

2324
### ByTemplateFileAndParameterObject
2425
```
2526
New-AzureRmResourceGroupDeployment [-Name <String>] -ResourceGroupName <String> [-Mode <DeploymentMode>]
26-
[-DeploymentDebugLogLevel <String>] [-Force] [-AsJob] -TemplateParameterObject <Hashtable>
27-
-TemplateFile <String> [-ApiVersion <String>] [-Pre] [-DefaultProfile <IAzureContextContainer>] [-WhatIf]
28-
[-Confirm] [<CommonParameters>]
27+
[-DeploymentDebugLogLevel <String>] [-RollbackAction <String>] [-Force] [-AsJob]
28+
-TemplateParameterObject <Hashtable> -TemplateFile <String> [-ApiVersion <String>] [-Pre]
29+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
2930
```
3031

3132
### ByTemplateUriAndParameterObject
3233
```
3334
New-AzureRmResourceGroupDeployment [-Name <String>] -ResourceGroupName <String> [-Mode <DeploymentMode>]
34-
[-DeploymentDebugLogLevel <String>] [-Force] [-AsJob] -TemplateParameterObject <Hashtable>
35-
-TemplateUri <String> [-ApiVersion <String>] [-Pre] [-DefaultProfile <IAzureContextContainer>] [-WhatIf]
36-
[-Confirm] [<CommonParameters>]
35+
[-DeploymentDebugLogLevel <String>] [-RollbackAction <String>] [-Force] [-AsJob]
36+
-TemplateParameterObject <Hashtable> -TemplateUri <String> [-ApiVersion <String>] [-Pre]
37+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
3738
```
3839

3940
### ByTemplateFileAndParameterFile
4041
```
4142
New-AzureRmResourceGroupDeployment [-Name <String>] -ResourceGroupName <String> [-Mode <DeploymentMode>]
42-
[-DeploymentDebugLogLevel <String>] [-Force] [-AsJob] -TemplateParameterFile <String> -TemplateFile <String>
43-
[-ApiVersion <String>] [-Pre] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
44-
[<CommonParameters>]
43+
[-DeploymentDebugLogLevel <String>] [-RollbackAction <String>] [-Force] [-AsJob]
44+
-TemplateParameterFile <String> -TemplateFile <String> [-ApiVersion <String>] [-Pre]
45+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
4546
```
4647

4748
### ByTemplateUriAndParameterFile
4849
```
4950
New-AzureRmResourceGroupDeployment [-Name <String>] -ResourceGroupName <String> [-Mode <DeploymentMode>]
50-
[-DeploymentDebugLogLevel <String>] [-Force] [-AsJob] -TemplateParameterFile <String> -TemplateUri <String>
51-
[-ApiVersion <String>] [-Pre] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
52-
[<CommonParameters>]
51+
[-DeploymentDebugLogLevel <String>] [-RollbackAction <String>] [-Force] [-AsJob]
52+
-TemplateParameterFile <String> -TemplateUri <String> [-ApiVersion <String>] [-Pre]
53+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
5354
```
5455

5556
### ByTemplateFileAndParameterUri
5657
```
5758
New-AzureRmResourceGroupDeployment [-Name <String>] -ResourceGroupName <String> [-Mode <DeploymentMode>]
58-
[-DeploymentDebugLogLevel <String>] [-Force] [-AsJob] -TemplateParameterUri <String> -TemplateFile <String>
59-
[-ApiVersion <String>] [-Pre] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
60-
[<CommonParameters>]
59+
[-DeploymentDebugLogLevel <String>] [-RollbackAction <String>] [-Force] [-AsJob]
60+
-TemplateParameterUri <String> -TemplateFile <String> [-ApiVersion <String>] [-Pre]
61+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
6162
```
6263

6364
### ByTemplateUriAndParameterUri
6465
```
6566
New-AzureRmResourceGroupDeployment [-Name <String>] -ResourceGroupName <String> [-Mode <DeploymentMode>]
66-
[-DeploymentDebugLogLevel <String>] [-Force] [-AsJob] -TemplateParameterUri <String> -TemplateUri <String>
67-
[-ApiVersion <String>] [-Pre] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
68-
[<CommonParameters>]
67+
[-DeploymentDebugLogLevel <String>] [-RollbackAction <String>] [-Force] [-AsJob]
68+
-TemplateParameterUri <String> -TemplateUri <String> [-ApiVersion <String>] [-Pre]
69+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
6970
```
7071

7172
### ByTemplateUriWithNoParameters
7273
```
7374
New-AzureRmResourceGroupDeployment [-Name <String>] -ResourceGroupName <String> [-Mode <DeploymentMode>]
74-
[-DeploymentDebugLogLevel <String>] [-Force] [-AsJob] -TemplateUri <String> [-ApiVersion <String>] [-Pre]
75-
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
75+
[-DeploymentDebugLogLevel <String>] [-RollbackAction <String>] [-Force] [-AsJob] -TemplateUri <String>
76+
[-ApiVersion <String>] [-Pre] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm]
77+
[<CommonParameters>]
7678
```
7779

7880
## DESCRIPTION
@@ -252,6 +254,21 @@ Accept pipeline input: True (ByPropertyName)
252254
Accept wildcard characters: False
253255
```
254256
257+
### -RollbackAction
258+
The name of a deployment to roll back to on error, or use as a flag to roll back to the last successful deployment.
259+
260+
```yaml
261+
Type: System.String
262+
Parameter Sets: (All)
263+
Aliases:
264+
265+
Required: False
266+
Position: Named
267+
Default value: None
268+
Accept pipeline input: True (ByPropertyName)
269+
Accept wildcard characters: False
270+
```
271+
255272
### -TemplateFile
256273
Specifies the full path of a JSON template file.
257274
This can be a custom template or a gallery template that is saved as a JSON file, such as one created by using the **Save-AzureRmResourceGroupGalleryTemplate** cmdlet.

0 commit comments

Comments
 (0)