Skip to content

Commit 2514bc6

Browse files
committed
Improved validation for EnabledSoftDelete and SoftDeleteRetentionDays params
1 parent ef1e68c commit 2514bc6

File tree

12 files changed

+2527
-1973
lines changed

12 files changed

+2527
-1973
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: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ 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+
public bool? EnableSoftDelete { get; set; }
167167

168168
/// <summary>
169169
/// Soft-delete retention days for the server
@@ -188,18 +188,27 @@ public override void ExecuteCmdlet()
188188
throw new PSArgumentException(Properties.Resources.MissingSQLAdministratorCredentials, "SqlAdministratorCredentials");
189189
}
190190

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)
191+
if (SoftDeleteRetentionDays.HasValue)
193192
{
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");
193+
if (EnableSoftDelete == true)
194+
{
195+
if (SoftDeleteRetentionDays.Value < 1 || SoftDeleteRetentionDays.Value > 35)
196+
{
197+
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDaysRange, "SoftDeleteRetentionDays");
198+
}
199+
}
200+
else if (EnableSoftDelete == false)
201+
{
202+
if (SoftDeleteRetentionDays.Value != 0)
203+
{
204+
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDaysForDisablingSoftDelete, "EnableSoftDelete");
205+
}
206+
}
207+
else
208+
{
209+
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDelete, "EnableSoftDelete");
210+
}
201211
}
202-
203212
base.ExecuteCmdlet();
204213
}
205214

@@ -244,16 +253,19 @@ public override void ExecuteCmdlet()
244253
}
245254

246255
int? softDeleteRetentionDays;
247-
if (this.EnableSoftDelete)
256+
if (this.EnableSoftDelete == true)
248257
{
249258
// If enabling soft-delete retention, use the explicitly provided value or default to 7 days if none provided.
250259
softDeleteRetentionDays = this.SoftDeleteRetentionDays ?? 7;
251260
}
261+
else if (this.EnableSoftDelete == false)
262+
{
263+
// If disabling, explicitly set retention to 0.
264+
softDeleteRetentionDays = 0;
265+
}
252266
else
253267
{
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;
268+
softDeleteRetentionDays = (int?)null;
257269
}
258270

259271

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)