Skip to content

Commit bb903a5

Browse files
committed
Fixed styling and error handling
1 parent bad2f90 commit bb903a5

13 files changed

+760
-2252
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@
15951595
type: command
15961596
short-summary: Create a server.
15971597
long-summary: |
1598-
Create a new SQL server. The server can be configured with various security, networking,
1598+
Create a new SQL server. The server can be configured with various security, networking,
15991599
and identity management options including soft delete protection for disaster recovery.
16001600
parameters:
16011601
- name: --enable-soft-delete
@@ -1637,24 +1637,24 @@
16371637
type: command
16381638
short-summary: Restore a deleted SQL server.
16391639
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
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
16421642
to the specified name in the same location where it was originally deleted.
16431643
parameters:
16441644
- name: --name -n
16451645
short-summary: Name of the server to restore to.
16461646
long-summary: |
1647-
The name for the restored server. This is typically the same name as the deleted server
1647+
The name for the restored server. This is the same name as the deleted server
16481648
you want to restore.
16491649
- name: --resource-group -g
16501650
short-summary: Name of the resource group.
16511651
long-summary: |
1652-
The resource group where the server will be restored. This is typically the same
1652+
The resource group where the server will be restored. This is the same
16531653
resource group where the deleted server was originally located.
16541654
- name: --location -l
16551655
short-summary: Location where the deleted server was originally located.
16561656
long-summary: |
1657-
Specifies the location where the deleted server was originally located. The restored
1657+
Specifies the location where the deleted server was originally located. The restored
16581658
server will be created in this same location.
16591659
examples:
16601660
- name: Restore a deleted server.
@@ -1845,7 +1845,7 @@
18451845
type: command
18461846
short-summary: Update a server.
18471847
long-summary: |
1848-
Update an existing SQL server's configuration including security settings, networking options,
1848+
Update an existing SQL server's configuration including security settings, networking options,
18491849
identity management, and soft delete protection settings.
18501850
parameters:
18511851
- name: --enable-soft-delete
@@ -1861,12 +1861,12 @@
18611861
- name: --enable-public-network -e
18621862
short-summary: Update public network access to server.
18631863
long-summary: |
1864-
Controls whether the server can be accessed from public networks. When false, only private
1864+
Controls whether the server can be accessed from public networks. When false, only private
18651865
endpoint connections are allowed.
18661866
- name: --restrict-outbound-network-access
18671867
short-summary: Update outbound network access restriction.
18681868
long-summary: |
1869-
Controls whether the server can initiate outbound network connections. When enabled,
1869+
Controls whether the server can initiate outbound network connections. When enabled,
18701870
restricts outbound connections for enhanced security.
18711871
examples:
18721872
- name: Update a server password.

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,16 +1912,15 @@ def _configure_security_policy_storage_params(arg_ctx):
19121912
c.argument('federated_client_id',
19131913
options_list=['--federated-client-id', '--fid'],
19141914
help='The federated client id used in cross tenant CMK scenario.')
1915-
1915+
19161916
c.argument('enable_soft_delete',
1917-
options_list=['--enable-soft-delete', '-esd'],
1917+
options_list=['--enable-soft-delete'],
19181918
arg_type=get_three_state_flag(),
19191919
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)
1920+
'the soft delete is enabled for 7 days by default.')
19221921

19231922
c.argument('soft_delete_retention_days',
1924-
options_list=['--soft-delete-retention-days', '--sdrd'],
1923+
options_list=['--soft-delete-retention-days'],
19251924
help='The number of days to retain soft deleted resources.',
19261925
validator=validate_soft_delete_parameters)
19271926

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,5 +263,6 @@ def get_sql_managed_database_ledger_digest_uploads_operations(cli_ctx, _):
263263
def get_sql_managed_database_move_operations(cli_ctx, _):
264264
return get_sql_management_client(cli_ctx).managed_database_move_operations
265265

266+
266267
def get_sql_deleted_servers_operations(cli_ctx, _):
267268
return get_sql_management_client(cli_ctx).deleted_servers

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
# --------------------------------------------------------------------------------------------
55

66
from azure.cli.core.util import CLIError
7+
from azure.cli.core.azclierror import (
8+
RequiredArgumentMissingError,
9+
InvalidArgumentValueError
10+
)
711

812
# Important note: if cmd validator exists, then individual param validators will not be
913
# executed. See C:\git\azure-cli\env\lib\site-packages\knack\invocation.py `def _validation`
@@ -148,28 +152,38 @@ def validate_managed_instance_storage_size(namespace):
148152
def validate_soft_delete_parameters(namespace):
149153
enable_soft_delete = getattr(namespace, 'enable_soft_delete', None)
150154
soft_delete_retention_days = getattr(namespace, 'soft_delete_retention_days', None)
151-
155+
152156
# Check if soft_delete_retention_days is specified without enable_soft_delete
153157
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-
158+
raise RequiredArgumentMissingError(
159+
'The --soft-delete-retention-days parameter requires --enable-soft-delete to be specified.',
160+
'Please specify both --enable-soft-delete and --soft-delete-retention-days together.')
161+
156162
# Validate soft_delete_retention_days value when specified
157163
if soft_delete_retention_days is not None:
158164
try:
159165
retention_days = int(soft_delete_retention_days)
160166
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-
167+
except (ValueError, TypeError) as exc:
168+
raise InvalidArgumentValueError(
169+
'The value for --soft-delete-retention-days must be a valid integer.') from exc
170+
164171
# Validate range based on enable_soft_delete value
165172
if enable_soft_delete is True:
166173
# 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')
174+
if not 1 <= retention_days <= 7:
175+
raise InvalidArgumentValueError(
176+
'The --soft-delete-retention-days value must be between 1 and 7 (inclusive) '
177+
'when --enable-soft-delete is true.',
178+
'Please specify a value between 1 and 7 days.')
169179
elif enable_soft_delete is False:
170180
# When enable_soft_delete is false, retention days must be 0
171181
if retention_days != 0:
172-
raise CLIError('incorrect usage: --soft-delete-retention-days must be 0 when --enable-soft-delete is false')
182+
raise InvalidArgumentValueError(
183+
'The --soft-delete-retention-days value must be 0 when --enable-soft-delete is false.',
184+
'Please set --soft-delete-retention-days to 0 or remove it when disabling soft delete.')
173185
else:
174186
# 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')
187+
raise RequiredArgumentMissingError(
188+
'The --soft-delete-retention-days parameter requires --enable-soft-delete to be specified.',
189+
'Please specify both --enable-soft-delete and --soft-delete-retention-days together.')

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

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
CLIError,
1414
sdk_no_wait,
1515
)
16+
from azure.cli.core.azclierror import (
17+
ResourceNotFoundError,
18+
ValidationError,
19+
AzCLIError
20+
)
1621

1722
from azure.mgmt.sql.models import (
1823
AdministratorName,
@@ -183,15 +188,20 @@ def _is_serverless_slo(sku_name):
183188

184189
def _check_server_exists(client, resource_group_name, server_name):
185190
'''
186-
Checks if a server already exists and raises CLIError if it does.
187-
Returns False if server doesn't exist (ResourceNotFound/NotFound),
191+
Checks if a server already exists and raises ValidationError if it does.
192+
Returns False if server doesn't exist (ResourceNotFound/NotFound),
188193
raises the original exception for other errors.
189194
'''
190195
try:
191196
existing_server = client.get(resource_group_name, server_name)
192197
if existing_server:
193-
raise CLIError('Server "{}" already exists in resource group "{}". '
194-
'Cannot restore to an existing server name.'.format(server_name, resource_group_name))
198+
raise ValidationError(
199+
f'Server "{server_name}" already exists in resource group "{resource_group_name}". '
200+
'Cannot restore to an existing server name.',
201+
'Please specify a different server name for the restore operation.')
202+
except ValidationError:
203+
# Re-raise ValidationError as-is
204+
raise
195205
except Exception as ex:
196206
# If server doesn't exist, that's what we want - continue with restore
197207
if 'ResourceNotFound' not in str(ex) and 'NotFound' not in str(ex):
@@ -215,16 +225,17 @@ def _validate_deleted_server_exists(cmd, location, server_name):
215225
except Exception as ex:
216226
# If server doesn't exist in deleted servers, raise appropriate error
217227
if 'ResourceNotFound' in str(ex) or 'NotFound' in str(ex):
218-
raise CLIError('No deleted server found with name "{}" in location "{}". '
219-
'Please verify the server was deleted with soft delete enabled and '
220-
'is within the retention period.'.format(server_name, location))
228+
raise ResourceNotFoundError(
229+
f'No deleted server found with name "{server_name}" in location "{location}".',
230+
'Please verify the server was deleted with soft delete enabled '
231+
'and is within the retention period.') from ex
221232
# Handle any other unexpected errors
222-
raise CLIError('Failed to validate deleted server "{}": {}'.format(server_name, str(ex)))
233+
raise AzCLIError(f'Failed to validate deleted server "{server_name}": {str(ex)}') from ex
223234

224235
# This shouldn't be reached if the API call succeeds but returns None
225-
raise CLIError('No deleted server found with name "{}" in location "{}". '
226-
'Please verify the server was deleted with soft delete enabled and '
227-
'is within the retention period.'.format(server_name, location))
236+
raise ResourceNotFoundError(
237+
f'No deleted server found with name "{server_name}" in location "{location}".',
238+
'Please verify the server was deleted with soft delete enabled and is within the retention period.')
228239

229240

230241
def _get_default_server_version(location_capabilities):
@@ -4465,7 +4476,7 @@ def server_create(
44654476
tenant_id=tenant_id)
44664477

44674478
kwargs['create_mode'] = ServerCreateMode.NORMAL
4468-
4479+
44694480
if enable_soft_delete is not None:
44704481
if soft_delete_retention_days is not None:
44714482
kwargs['retention_days'] = soft_delete_retention_days
@@ -4501,11 +4512,11 @@ def server_restore(
45014512

45024513
# Validate deleted server exists
45034514
_validate_deleted_server_exists(cmd, location, server_name)
4504-
4515+
45054516
# Set required parameters for restore
45064517
kwargs['location'] = location
45074518
kwargs['create_mode'] = ServerCreateMode.RESTORE
4508-
4519+
45094520
logger.info('Attempting to restore server "%s" in location "%s"',
45104521
server_name, location)
45114522

0 commit comments

Comments
 (0)