Skip to content

Commit bad2f90

Browse files
committed
Added soft delete feature
1 parent 1a50871 commit bad2f90

14 files changed

+7635
-8
lines changed

src/azure-cli/azure/cli/command_modules/sql/_help.py

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,13 +1594,31 @@
15941594
helps['sql server create'] = """
15951595
type: command
15961596
short-summary: Create a server.
1597+
long-summary: |
1598+
Create a new SQL server. The server can be configured with various security, networking,
1599+
and identity management options including soft delete protection for disaster recovery.
1600+
parameters:
1601+
- name: --enable-soft-delete
1602+
short-summary: Enable soft delete protection for the server.
1603+
long-summary: |
1604+
When enabled, allows the server to be recovered after deletion within the retention period.
1605+
This provides protection against accidental deletion.
1606+
- name: --soft-delete-retention-days
1607+
short-summary: Number of days to retain a soft-deleted server.
1608+
long-summary: |
1609+
Specifies how many days to keep the server in a soft-deleted state before permanent deletion.
1610+
Valid range is 1-7 days. Requires --enable-soft-delete to be true.
15971611
examples:
15981612
- name: Create a server.
15991613
text: az sql server create -l westus -g mygroup -n myserver -u myadminuser -p myadminpassword
16001614
- name: Create a server with tags.
16011615
text: az sql server create -l westus -g mygroup -n myserver -u myadminuser -p myadminpassword --tags key1=value1 key2=value2
16021616
- name: Create a server with disabled public network access to server.
16031617
text: az sql server create -l westus -g mygroup -n myserver -u myadminuser -p myadminpassword -e false
1618+
- name: Create a server with soft delete enabled and 7-day retention period.
1619+
text: az sql server create -l westus -g mygroup -n myserver -u myadminuser -p myadminpassword --enable-soft-delete --soft-delete-retention-days 7
1620+
- name: Create a server with minimal TLS version and soft delete protection.
1621+
text: az sql server create -l westus -g mygroup -n myserver -u myadminuser -p myadminpassword --minimal-tls-version 1.2 --enable-soft-delete --soft-delete-retention-days 3
16041622
- name: Create a server without SQL Admin, with AD admin and AD Only enabled.
16051623
text: az sql server create --enable-ad-only-auth --external-admin-principal-type User --external-admin-name myUserName --external-admin-sid c5e964e2-6bb2-1111-1111-3b16ec0e1234 -g myResourceGroup -n myServer
16061624
- name: Create a server without SQL Admin, with AD admin, AD Only enabled, User ManagedIdenties and Identity Type is SystemAssigned,UserAssigned.
@@ -1615,6 +1633,36 @@
16151633
--identity-type UserAssigned --pid /subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/testumi
16161634
"""
16171635

1636+
helps['sql server restore'] = """
1637+
type: command
1638+
short-summary: Restore a deleted SQL server.
1639+
long-summary: |
1640+
Restore a deleted SQL server from a soft-deleted state. The server must have been deleted
1641+
with soft delete enabled and must be within the retention period. The server will be restored
1642+
to the specified name in the same location where it was originally deleted.
1643+
parameters:
1644+
- name: --name -n
1645+
short-summary: Name of the server to restore to.
1646+
long-summary: |
1647+
The name for the restored server. This is typically the same name as the deleted server
1648+
you want to restore.
1649+
- name: --resource-group -g
1650+
short-summary: Name of the resource group.
1651+
long-summary: |
1652+
The resource group where the server will be restored. This is typically the same
1653+
resource group where the deleted server was originally located.
1654+
- name: --location -l
1655+
short-summary: Location where the deleted server was originally located.
1656+
long-summary: |
1657+
Specifies the location where the deleted server was originally located. The restored
1658+
server will be created in this same location.
1659+
examples:
1660+
- name: Restore a deleted server.
1661+
text: az sql server restore -g mygroup -n myserver -l westus2
1662+
- name: Restore a deleted server with no-wait option for asynchronous operation.
1663+
text: az sql server restore -g mygroup -n myserver -l westus2 --no-wait
1664+
"""
1665+
16181666
helps['sql server dns-alias'] = """
16191667
type: group
16201668
short-summary: Manage a server's DNS aliases.
@@ -1796,10 +1844,41 @@
17961844
helps['sql server update'] = """
17971845
type: command
17981846
short-summary: Update a server.
1799-
examples:
1800-
- name: Update a server. (autogenerated)
1847+
long-summary: |
1848+
Update an existing SQL server's configuration including security settings, networking options,
1849+
identity management, and soft delete protection settings.
1850+
parameters:
1851+
- name: --enable-soft-delete
1852+
short-summary: Enable or disable soft delete protection for the server.
1853+
long-summary: |
1854+
When enabled, allows the server to be recovered after deletion within the retention period.
1855+
This provides protection against accidental deletion. Can be enabled or disabled on existing servers.
1856+
- name: --soft-delete-retention-days
1857+
short-summary: Update the number of days to retain a soft-deleted server.
1858+
long-summary: |
1859+
Specifies how many days to keep the server in a soft-deleted state before permanent deletion.
1860+
Valid range is 1-7 days. Requires soft delete to be enabled.
1861+
- name: --enable-public-network -e
1862+
short-summary: Update public network access to server.
1863+
long-summary: |
1864+
Controls whether the server can be accessed from public networks. When false, only private
1865+
endpoint connections are allowed.
1866+
- name: --restrict-outbound-network-access
1867+
short-summary: Update outbound network access restriction.
1868+
long-summary: |
1869+
Controls whether the server can initiate outbound network connections. When enabled,
1870+
restricts outbound connections for enhanced security.
1871+
examples:
1872+
- name: Update a server password.
18011873
text: az sql server update --admin-password myadminpassword --name MyAzureSQLServer --resource-group MyResourceGroup
1802-
crafted: true
1874+
- name: Enable soft delete protection with 7-day retention.
1875+
text: az sql server update --name MyAzureSQLServer --resource-group MyResourceGroup --enable-soft-delete --soft-delete-retention-days 7
1876+
- name: Disable public network access for enhanced security.
1877+
text: az sql server update --name MyAzureSQLServer --resource-group MyResourceGroup --enable-public-network false
1878+
- name: Update TLS version and enable outbound network restrictions.
1879+
text: az sql server update --name MyAzureSQLServer --resource-group MyResourceGroup --minimal-tls-version 1.2 --restrict-outbound-network-access
1880+
- name: Modify soft delete retention period.
1881+
text: az sql server update --name MyAzureSQLServer --resource-group MyResourceGroup --enable-soft-delete --soft-delete-retention-days 5
18031882
- name: Update a server with User Managed Identies and Identity Type is SystemAssigned,UserAssigned.
18041883
text: az sql server update -g myResourceGroup -n myServer -i \\
18051884
--user-assigned-identity-id /subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/testumi \\
@@ -1808,6 +1887,12 @@
18081887
text: az sql server update -g myResourceGroup -n myServer -i \\
18091888
--user-assigned-identity-id /subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/testumi \\
18101889
--identity-type UserAssigned --pid /subscriptions/xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/testumi
1890+
- name: Configure server for maximum security compliance.
1891+
text: az sql server update --name MyAzureSQLServer --resource-group MyResourceGroup \\
1892+
--minimal-tls-version 1.2 --enable-public-network false \\
1893+
--restrict-outbound-network-access --enable-soft-delete --soft-delete-retention-days 7
1894+
- name: Disable soft delete protection (not recommended for production).
1895+
text: az sql server update --name MyAzureSQLServer --resource-group MyResourceGroup --enable-soft-delete false
18111896
"""
18121897

18131898
helps['sql server vnet-rule'] = """

src/azure-cli/azure/cli/command_modules/sql/_params.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
create_args_for_complex_type,
7474
validate_managed_instance_storage_size,
7575
validate_backup_storage_redundancy,
76-
validate_subnet
76+
validate_subnet,
77+
validate_soft_delete_parameters
7778
)
7879

7980
#####
@@ -1911,6 +1912,18 @@ def _configure_security_policy_storage_params(arg_ctx):
19111912
c.argument('federated_client_id',
19121913
options_list=['--federated-client-id', '--fid'],
19131914
help='The federated client id used in cross tenant CMK scenario.')
1915+
1916+
c.argument('enable_soft_delete',
1917+
options_list=['--enable-soft-delete', '-esd'],
1918+
arg_type=get_three_state_flag(),
1919+
help='Set whether soft delete is enabled or not. When true,'
1920+
'the soft delete is enabled for 7 days by default.',
1921+
is_preview=True)
1922+
1923+
c.argument('soft_delete_retention_days',
1924+
options_list=['--soft-delete-retention-days', '--sdrd'],
1925+
help='The number of days to retain soft deleted resources.',
1926+
validator=validate_soft_delete_parameters)
19141927

19151928
with self.argument_context('sql server create') as c:
19161929
c.argument('location',
@@ -1923,7 +1936,9 @@ def _configure_security_policy_storage_params(arg_ctx):
19231936
'administrator_login_password',
19241937
'location',
19251938
'minimal_tls_version',
1926-
'tags'
1939+
'tags',
1940+
'enable_soft_delete',
1941+
'soft_delete_retention_days'
19271942
])
19281943

19291944
c.argument('administrator_login',
@@ -1963,6 +1978,12 @@ def _configure_security_policy_storage_params(arg_ctx):
19631978
options_list=['--expand-ad-admin'],
19641979
help='Expand the Active Directory Administrator for the server.')
19651980

1981+
with self.argument_context('sql server restore') as c:
1982+
c.argument('location',
1983+
arg_type=get_location_type_with_default_from_resource_group(self.cli_ctx),
1984+
required=True,
1985+
help='Location where the deleted server was originally located.')
1986+
19661987
with self.argument_context('sql server list') as c:
19671988
c.argument('expand_ad_admin',
19681989
options_list=['--expand-ad-admin'],

src/azure-cli/azure/cli/command_modules/sql/_util.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,6 @@ def get_sql_managed_database_ledger_digest_uploads_operations(cli_ctx, _):
262262

263263
def get_sql_managed_database_move_operations(cli_ctx, _):
264264
return get_sql_management_client(cli_ctx).managed_database_move_operations
265+
266+
def get_sql_deleted_servers_operations(cli_ctx, _):
267+
return get_sql_management_client(cli_ctx).deleted_servers

src/azure-cli/azure/cli/command_modules/sql/_validators.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,38 @@ def validate_managed_instance_storage_size(namespace):
138138
pass
139139
else:
140140
raise CLIError('incorrect usage: --storage must be specified in increments of 32 GB')
141+
142+
143+
###############################################
144+
# sql server #
145+
###############################################
146+
147+
148+
def validate_soft_delete_parameters(namespace):
149+
enable_soft_delete = getattr(namespace, 'enable_soft_delete', None)
150+
soft_delete_retention_days = getattr(namespace, 'soft_delete_retention_days', None)
151+
152+
# Check if soft_delete_retention_days is specified without enable_soft_delete
153+
if soft_delete_retention_days is not None and enable_soft_delete is None:
154+
raise CLIError('incorrect usage: --soft-delete-retention-days can only be specified when --enable-soft-delete is also specified')
155+
156+
# Validate soft_delete_retention_days value when specified
157+
if soft_delete_retention_days is not None:
158+
try:
159+
retention_days = int(soft_delete_retention_days)
160+
namespace.soft_delete_retention_days = retention_days
161+
except (ValueError, TypeError):
162+
raise CLIError('incorrect usage: --soft-delete-retention-days must be a valid integer')
163+
164+
# Validate range based on enable_soft_delete value
165+
if enable_soft_delete is True:
166+
# When enable_soft_delete is true, retention days must be 1-7
167+
if not (1 <= retention_days <= 7):
168+
raise CLIError('incorrect usage: --soft-delete-retention-days must be between 1 and 7 (inclusive) when --enable-soft-delete is true')
169+
elif enable_soft_delete is False:
170+
# When enable_soft_delete is false, retention days must be 0
171+
if retention_days != 0:
172+
raise CLIError('incorrect usage: --soft-delete-retention-days must be 0 when --enable-soft-delete is false')
173+
else:
174+
# This shouldn't happen since we check above, but for safety
175+
raise CLIError('incorrect usage: --soft-delete-retention-days can only be specified when --enable-soft-delete is also specified')

src/azure-cli/azure/cli/command_modules/sql/commands.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,9 @@ def load_command_table(self, _):
572572
g.custom_command('create', 'server_create',
573573
table_transformer=server_table_format,
574574
supports_no_wait=True)
575+
g.custom_command('restore', 'server_restore',
576+
table_transformer=server_table_format,
577+
supports_no_wait=True)
575578
g.command('delete', 'begin_delete',
576579
confirmation=True)
577580
g.custom_show_command('show', 'server_get',

0 commit comments

Comments
 (0)