Skip to content

Commit e526311

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

File tree

11 files changed

+2545
-1966
lines changed

11 files changed

+2545
-1966
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/Properties/Resources.Designer.cs

Lines changed: 19 additions & 1 deletion
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,9 +770,15 @@
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>
775775
<data name="InvalidSoftDeleteRetentionDays" xml:space="preserve">
776776
<value>Invalid configuration: SoftDeleteRetentionDays can only be set to 0 when EnableSoftDelete is false.</value>
777777
</data>
778+
<data name="InvalidSoftDeleteRetentionDaysRange" xml:space="preserve">
779+
<value>Invalid configuration: SoftDeleteRetentionDays must be between 1 and 35 when EnableSoftDelete is true.</value>
780+
</data>
781+
<data name="MissingEnableSoftDeleteForZeroRetention" xml:space="preserve">
782+
<value>Invalid configuration: EnableSoftDelete must be set to false when SoftDeleteRetentionDays is 0.</value>
783+
</data>
778784
</root>

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

Lines changed: 34 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,34 @@ 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.MissingEnableSoftDelete, "EnableSoftDelete");
205+
}
206+
}
207+
else
208+
{
209+
if (SoftDeleteRetentionDays.Value != 0)
210+
{
211+
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDelete, "EnableSoftDelete");
212+
}
213+
else
214+
{
215+
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDeleteForZeroRetention, "EnableSoftDelete");
216+
}
217+
}
201218
}
202-
203219
base.ExecuteCmdlet();
204220
}
205221

@@ -244,16 +260,19 @@ public override void ExecuteCmdlet()
244260
}
245261

246262
int? softDeleteRetentionDays;
247-
if (this.EnableSoftDelete)
263+
if (this.EnableSoftDelete == true)
248264
{
249265
// If enabling soft-delete retention, use the explicitly provided value or default to 7 days if none provided.
250266
softDeleteRetentionDays = this.SoftDeleteRetentionDays ?? 7;
251267
}
268+
else if (this.EnableSoftDelete == false)
269+
{
270+
// If disabling, explicitly set retention to 0.
271+
softDeleteRetentionDays = 0;
272+
}
252273
else
253274
{
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;
275+
softDeleteRetentionDays = (int?)null;
257276
}
258277

259278

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
}

src/Sql/Sql/Server/Cmdlet/SetAzureSqlServer.cs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ public class SetAzureSqlServer : AzureSqlServerCmdletBase
130130
/// </summary>
131131
[Parameter(Mandatory = false,
132132
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.")]
133-
[PSArgumentCompleter("true", "false")]
134-
public bool EnableSoftDelete { get; set; }
133+
public bool? EnableSoftDelete { get; set; }
135134

136135
/// <summary>
137136
/// Value for soft-delete retention days for the server.
@@ -152,18 +151,35 @@ public class SetAzureSqlServer : AzureSqlServerCmdletBase
152151
/// </summary>
153152
public override void ExecuteCmdlet()
154153
{
155-
// if the user specified a retention days value, then they must also enable soft delete
156-
if (this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays > 0 && !this.EnableSoftDelete)
154+
if (SoftDeleteRetentionDays.HasValue)
157155
{
158-
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDelete, "EnableSoftDelete");
156+
if (EnableSoftDelete == true)
157+
{
158+
if (SoftDeleteRetentionDays.Value < 1 || SoftDeleteRetentionDays.Value > 35)
159+
{
160+
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDaysRange, "SoftDeleteRetentionDays");
161+
}
162+
}
163+
else if (EnableSoftDelete == false)
164+
{
165+
if (SoftDeleteRetentionDays.Value != 0)
166+
{
167+
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDelete, "EnableSoftDelete");
168+
}
169+
}
170+
else
171+
{
172+
if (SoftDeleteRetentionDays.Value != 0)
173+
{
174+
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDelete, "EnableSoftDelete");
175+
}
176+
else
177+
{
178+
throw new PSArgumentException(Properties.Resources.MissingEnableSoftDeleteForZeroRetention, "EnableSoftDelete");
179+
}
180+
}
159181
}
160-
161-
// if the user specified 0 retention days, then they must not enable soft delete
162-
if (this.EnableSoftDelete && this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays == 0)
163-
{
164-
throw new PSArgumentException(Properties.Resources.InvalidSoftDeleteRetentionDays, "SoftDeleteRetentionDays");
165-
}
166-
182+
167183
base.ExecuteCmdlet();
168184
}
169185
/// <summary>
@@ -204,16 +220,20 @@ public override void ExecuteCmdlet()
204220
updateData[0].PrimaryUserAssignedIdentityId = this.PrimaryUserAssignedIdentityId ?? model.FirstOrDefault().PrimaryUserAssignedIdentityId;
205221
updateData[0].KeyId = this.KeyId ?? updateData[0].KeyId;
206222
updateData[0].FederatedClientId = this.FederatedClientId ?? updateData[0].FederatedClientId;
207-
if (this.EnableSoftDelete)
223+
if (this.EnableSoftDelete == true)
208224
{
209225
// If enabling soft-delete retention, use the explicitly provided value or default to 7 days if none provided.
210226
updateData[0].SoftDeleteRetentionDays = this.SoftDeleteRetentionDays ?? 7;
211227
}
228+
else if(this.EnableSoftDelete == false)
229+
{
230+
// if disabling soft-delete retention, set retention to 0 days.
231+
updateData[0].SoftDeleteRetentionDays = 0;
232+
}
212233
else
213234
{
214-
// If not enabling, only explicitly set retention to 0 when the caller provided 0.
215-
// Otherwise, leave as null so the service preserves the existing retention setting.
216-
updateData[0].SoftDeleteRetentionDays = (this.SoftDeleteRetentionDays.HasValue && this.SoftDeleteRetentionDays.Value == 0) ? 0 : (int?)null;
235+
// If EnableSoftDelete is not specified, retain existing retention value.
236+
updateData[0].SoftDeleteRetentionDays = (int?)null;
217237
}
218238

219239
return updateData;

0 commit comments

Comments
 (0)