Skip to content

Commit 1b48e22

Browse files
Improve validation for EnabledSoftDelete and SoftDeleteRetentionDay params (#28619)
1 parent 94589ab commit 1b48e22

File tree

12 files changed

+2528
-1972
lines changed

12 files changed

+2528
-1972
lines changed

src/Sql/Sql.Test/ScenarioTests/ServerCrudTests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ function Test-CreateServerWithDefaultSoftDeleteRetentionEnabled
693693
}
694694
finally
695695
{
696-
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $serverName -SoftDeleteRetentionDays 0
696+
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $serverName -EnableSoftDelete $False
697697
Remove-ResourceGroupForTest $rg
698698
}
699699
}
@@ -731,7 +731,7 @@ function Test-CreateServerWithCustomSoftDeleteRetentionEnabled
731731
}
732732
finally
733733
{
734-
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $serverName -SoftDeleteRetentionDays 0
734+
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $serverName -EnableSoftDelete $False
735735
Remove-ResourceGroupForTest $rg
736736
}
737737
}
@@ -768,7 +768,7 @@ function Test-UpdateServerWithSoftDeleteRetentionEnabled
768768
}
769769
finally
770770
{
771-
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -SoftDeleteRetentionDays 0
771+
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -EnableSoftDelete $False
772772
Remove-ResourceGroupForTest $rg
773773
}
774774
}
@@ -798,7 +798,7 @@ function Test-RestoreDeletedServer
798798
}
799799
finally
800800
{
801-
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -SoftDeleteRetentionDays 0
801+
Set-AzSqlServer -ResourceGroupName $rg.ResourceGroupName -ServerName $server.ServerName -EnableSoftDelete $False
802802
Remove-ResourceGroupForTest $rg
803803
}
804804
}

src/Sql/Sql.Test/SessionRecords/Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerCrudTests/TestServerCreateWithCustomSoftRetentionEnabled.json

Lines changed: 573 additions & 378 deletions
Large diffs are not rendered by default.

src/Sql/Sql.Test/SessionRecords/Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerCrudTests/TestServerCreateWithDefaultSoftRetentionEnabled.json

Lines changed: 611 additions & 347 deletions
Large diffs are not rendered by default.

src/Sql/Sql.Test/SessionRecords/Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerCrudTests/TestServerRestoreDeletedServer.json

Lines changed: 560 additions & 701 deletions
Large diffs are not rendered by default.

src/Sql/Sql.Test/SessionRecords/Microsoft.Azure.Commands.Sql.Test.ScenarioTests.ServerCrudTests/TestServerUpdateWithSoftRetentionEnabled.json

Lines changed: 697 additions & 499 deletions
Large diffs are not rendered by default.

src/Sql/Sql/ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Improved parameter validation for the EnableSoftDelete and SoftDeleteRetentionDays parameters.
2122
* Updated `New-AzSqlServer` to support soft-delete retention
2223
- Added `EnableSoftDelete` parameter to `New-AzSqlServer` to enable creation of a server with soft-delete retention
2324
- Added `SoftDeleteRetentionDays` parameter to `New-AzSqlServer` to set the soft-delete retention period (in days)

src/Sql/Sql/Properties/Resources.Designer.cs

Lines changed: 14 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Sql/Sql/Properties/Resources.resx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@
671671
<value>SQL Administrator Credentials are only optional when Azure Active Directory Only Authentication is enabled via -EnableActiveDirectoryOnlyAuthentication.</value>
672672
</data>
673673
<data name="MissingEnableSoftDelete" xml:space="preserve">
674-
<value>Invalid configuration: SoftDeleteRetentionDays can only be set to a value greater than 0 if EnableSoftDelete is set to true.</value>
674+
<value>SoftDeleteRetentionDays parameter can only be specified if EnableSoftDelete is explicitly specified.</value>
675675
</data>
676676
<data name="RemoveAzureSqlServerOutboundFirewallRuleDescription" xml:space="preserve">
677677
<value>Permanently removing allowed FQDN '{0}' from the list of Outbound Firewall Rules (Allowed FQDNs) for Azure Sql Database Server '{1}'.</value>
@@ -770,9 +770,12 @@
770770
<value>Changing the service tier to Hyperscale also converts the geo-secondary replica to Hyperscale. For more information, see https://go.microsoft.com/fwlink/?linkid=2314103</value>
771771
</data>
772772
<data name="DeletedServerNotFound" xml:space="preserve">
773-
<value>No deleted server named '{0}' found in resource group '{1}' that can be restored.</value>
773+
<value>No deleted server named '{0}' found in location '{1}' that can be restored.</value>
774774
</data>
775-
<data name="InvalidSoftDeleteRetentionDays" xml:space="preserve">
776-
<value>Invalid configuration: SoftDeleteRetentionDays can only be set to 0 when EnableSoftDelete is false.</value>
775+
<data name="InvalidSoftDeleteRetentionDaysForDisablingSoftDelete" xml:space="preserve">
776+
<value>SoftDeleteRetentionDays can only be set to 0 when EnableSoftDelete is false.</value>
777+
</data>
778+
<data name="InvalidSoftDeleteRetentionDaysRange" xml:space="preserve">
779+
<value>SoftDeleteRetentionDays must be between 1 and 35 when EnableSoftDelete is true.</value>
777780
</data>
778781
</root>

src/Sql/Sql/Server/Cmdlet/NewAzureSqlServer.cs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ public class NewAzureSqlServer : AzureSqlServerCmdletBase
163163
/// </summary>
164164
[Parameter(Mandatory = false,
165165
HelpMessage = "Specify whether to enable soft-delete retention for the server. When enabled, a dropped server can be restored within the retention window (defaults to 7 days if not specified). To set a custom retention period use -SoftDeleteRetentionDays.")]
166-
public bool EnableSoftDelete { get; set; }
166+
[PSArgumentCompleter("true", "false")]
167+
public bool? EnableSoftDelete { get; set; }
167168

168169
/// <summary>
169170
/// Soft-delete retention days for the server
@@ -188,18 +189,27 @@ public override void ExecuteCmdlet()
188189
throw new PSArgumentException(Properties.Resources.MissingSQLAdministratorCredentials, "SqlAdministratorCredentials");
189190
}
190191

191-
// if the user specified a retention days value, then they must also enable soft delete
192-
if (this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays > 0 && !this.EnableSoftDelete)
192+
if (SoftDeleteRetentionDays.HasValue)
193193
{
194-
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDelete, "EnableSoftDelete");
195-
}
196-
197-
// if the user specified 0 retention days, then they must not enable soft delete
198-
if (this.EnableSoftDelete && this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays == 0)
199-
{
200-
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDays, "SoftDeleteRetentionDays");
194+
if (EnableSoftDelete == true)
195+
{
196+
if (SoftDeleteRetentionDays.Value < 1 || SoftDeleteRetentionDays.Value > 35)
197+
{
198+
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDaysRange, "SoftDeleteRetentionDays");
199+
}
200+
}
201+
else if (EnableSoftDelete == false)
202+
{
203+
if (SoftDeleteRetentionDays.Value != 0)
204+
{
205+
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDaysForDisablingSoftDelete, "SoftDeleteRetentionDays");
206+
}
207+
}
208+
else
209+
{
210+
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDelete, "EnableSoftDelete");
211+
}
201212
}
202-
203213
base.ExecuteCmdlet();
204214
}
205215

@@ -244,16 +254,19 @@ public override void ExecuteCmdlet()
244254
}
245255

246256
int? softDeleteRetentionDays;
247-
if (this.EnableSoftDelete)
257+
if (this.EnableSoftDelete == true)
248258
{
249259
// If enabling soft-delete retention, use the explicitly provided value or default to 7 days if none provided.
250260
softDeleteRetentionDays = this.SoftDeleteRetentionDays ?? 7;
251261
}
262+
else if (this.EnableSoftDelete == false)
263+
{
264+
// If disabling, explicitly set retention to 0.
265+
softDeleteRetentionDays = 0;
266+
}
252267
else
253268
{
254-
// If not enabling, only explicitly set retention to 0 when the caller provided 0.
255-
// Otherwise, leave as null so the service preserves the existing retention setting.
256-
softDeleteRetentionDays = (this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays.Value == 0) ? 0 : (int?)null;
269+
softDeleteRetentionDays = (int?)null;
257270
}
258271

259272

src/Sql/Sql/Server/Cmdlet/RestoreAzureSqlServer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public class RestoreAzureSqlServer : AzureSqlServerCmdletBase
8686
{
8787
throw new PSArgumentException(
8888
string.Format(Properties.Resources.DeletedServerNotFound,
89-
this.ServerName, this.ResourceGroupName),
89+
this.ServerName, this.Location),
9090
"ServerName");
9191
}
9292

@@ -99,10 +99,10 @@ public class RestoreAzureSqlServer : AzureSqlServerCmdletBase
9999
{
100100
throw new PSArgumentException(
101101
string.Format(Properties.Resources.DeletedServerNotFound,
102-
this.ServerName, this.ResourceGroupName),
102+
this.ServerName, this.Location),
103103
"ServerName");
104104
}
105-
105+
106106
// Unexpected exception encountered
107107
throw;
108108
}

0 commit comments

Comments
 (0)