Skip to content
Open
21 changes: 18 additions & 3 deletions src/azure-cli/azure/cli/command_modules/sql/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,33 @@
helps['sql db str-policy set'] = """
type: command
short-summary: Update short term retention settings for a live database.
long-summary: Locking backup immutability is irreversible. Confirmation is required only when --lock-immutability is true. Use --yes to skip confirmation. Include --retention-days when locking. To preserve retention, use the current value from str-policy show. When increasing retention on a locked policy, also specify --lock-immutability true. Use str-policy show to check the policy after a --no-wait update.
examples:
- name: Set short term retention for a live database.
text: az sql db str-policy set -g mygroup -s myserver -n mydb --retention-days retentionindays --diffbackup-hours diffbackuphours
text: az sql db str-policy set --resource-group mygroup --server myserver --name mydb --retention-days 7 --diffbackup-hours 24
- name: Lock backup immutability with seven-day retention, skipping confirmation.
text: az sql db str-policy set --resource-group mygroup --server myserver --name mydb --retention-days 7 --lock-immutability true --yes
"""

helps['sql db str-policy show'] = """
type: command
short-summary: Show the short term retention policy for a live database.
long-summary: The immutabilityStatus output property reports Disabled, Enabled or Locked when returned by the service.
examples:
- name: Show short term retention policy for a live database.
text: az sql db str-policy show -g mygroup -s myserver -n mydb
- name: Show the current backup immutability status.
text: az sql db str-policy show --resource-group mygroup --server myserver --name mydb --query immutabilityStatus
"""

helps['sql db str-policy wait'] = """
type: command
short-summary: Place the CLI in a waiting state until the policy is set.
examples:
- name: Wait until the default short term retention policy exists.
text: az sql db str-policy wait --resource-group mygroup --server myserver --name mydb --policy-name default --exists
- name: Wait until backup immutability is locked.
text: az sql db str-policy wait --resource-group mygroup --server myserver --name mydb --policy-name default --custom "immutabilityStatus=='Locked'"
"""

helps['sql db ltr-policy'] = """
Expand Down Expand Up @@ -1212,16 +1223,20 @@
helps['sql midb short-term-retention-policy set'] = """
type: command
short-summary: Update short term retention for automated backups on a single database.
long-summary: Locking backup immutability is irreversible and is supported only for live managed databases. Confirmation is required only when --lock-immutability is true; use --yes to skip it. Do not specify --lock-immutability with --deleted-time, even when false. Retention cannot be reduced on a dropped database whose backup immutability is locked.
examples:
- name: Set backup short term retention for live managed database.
text: az sql midb short-term-retention-policy set -g mygroup --mi myinstance -n mymanageddb --retention-days retentionindays
text: az sql midb short-term-retention-policy set -g mygroup --mi myinstance -n mymanageddb --retention-days 7
- name: Set backup short term retention for dropped managed database.
text: az sql midb short-term-retention-policy set -g mygroup --mi myinstance -n mymanageddb --deleted-time "2018-05-20T05:34:22" --retention-days retentionindays
text: az sql midb short-term-retention-policy set -g mygroup --mi myinstance -n mymanageddb --deleted-time "2018-05-20T05:34:22" --retention-days 7
- name: Lock backup immutability for a live managed database, skipping confirmation.
text: az sql midb short-term-retention-policy set --resource-group mygroup --mi myinstance --name mymanageddb --retention-days 7 --lock-immutability true --yes
"""

helps['sql midb short-term-retention-policy show'] = """
type: command
short-summary: Show short term retention for automated backups on a single database.
long-summary: The immutabilityStatus output property reports Disabled, Enabled or Locked for live and restorable dropped managed databases when returned by the service.
examples:
- name: Shows backup short term retention for live managed database.
text: az sql midb short-term-retention-policy show -g mygroup --mi myinstance -n mymanageddb
Expand Down
47 changes: 28 additions & 19 deletions src/azure-cli/azure/cli/command_modules/sql/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1506,24 +1506,29 @@ def _configure_security_policy_storage_params(arg_ctx):
# sql db str #
###############################################
with self.argument_context('sql db str-policy set') as c:
create_args_for_complex_type(
c, 'parameters', Database, [
'retention_days',
'diffbackup_hours'
])

c.argument(
'retention_days',
options_list=['--retention-days'],
required=True,
type=int,
required=False,
help='New backup short term retention policy retention in days.'
'Valid retention days for live database of (DTU) Basic can be 1-7 days; Rest models can be 1-35 days.')

c.argument(
'diffbackup_hours',
options_list=['--diffbackup-hours'],
type=int,
help='New backup short term retention policy differential backup interval in hours.'
'Valid differential backup interval for live database can be 12 or 24 hours.')
c.argument(
'lock_immutability',
arg_type=get_three_state_flag(),
help='Whether to lock backup immutability. Locking is irreversible. '
'Setting false does not unlock an already locked policy.')
c.argument(
'yes',
options_list=['--yes', '-y'],
action='store_true',
help='Do not prompt for confirmation when locking backup immutability.')

###############################################
# sql dw #
Expand Down Expand Up @@ -2825,24 +2830,28 @@ class ContainerIdentityType(Enum):
help='The id of recoverable database from geo-replicated instance')

with self.argument_context('sql midb short-term-retention-policy set') as c:
create_args_for_complex_type(
c, 'parameters', ManagedDatabase, [
'deleted_time',
'retention_days'
])

c.argument(
'deleted_time',
options_list=['--deleted-time'],
help='If specified, updates retention days for a deleted database, instead of an existing database.'
'Must match the deleted time of a deleted database on the source Managed Instance.')

help='If specified, updates retention days for a deleted database, instead of an existing database. '
'Must match the deleted time of a deleted database on the source Managed Instance.')
c.argument(
'retention_days',
options_list=['--retention-days'],
type=int,
required=True,
help='New backup short term retention policy in days.'
'Valid policy for live database is 7-35 days, valid policy for dropped databases is 0-35 days.')
help='New backup short term retention policy in days. '
'Valid policy for live database is 7-35 days, valid policy for dropped databases is 0-35 days.')
c.argument(
'lock_immutability',
arg_type=get_three_state_flag(),
help='Whether to lock backup immutability for a live managed database. '
'Locking is irreversible. Cannot be used with --deleted-time, even when false.')
c.argument(
'yes',
options_list=['--yes', '-y'],
action='store_true',
help='Do not prompt for confirmation when locking backup immutability.')

with self.argument_context('sql midb short-term-retention-policy show') as c:
c.argument(
Expand Down
80 changes: 80 additions & 0 deletions src/azure-cli/azure/cli/command_modules/sql/_str_policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

"""Compatibility adapters for the generated STR commands."""

from azure.cli.core.aaz import AAZCommand, AAZHttpOperation
from azure.cli.core.azclierror import InvalidArgumentValueError

from .aaz.latest.sql.db.str_policy import Set as _DatabasePolicySet, Show as _DatabasePolicyShow
from .aaz.latest.sql.midb.short_term_retention_policy import (
Set as _ManagedDatabasePolicySet, Show as _ManagedDatabasePolicyShow,
)
from .aaz.latest.sql.midb.short_term_retention_policy.restorable_dropped import (
Set as _DroppedDatabasePolicySet, Show as _DroppedDatabasePolicyShow,
)


class _ShortTermRetentionPolicy(AAZCommand):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

svmicano

  1. Please generate this with AAZ, this should generally end up in a /aaz sub directory. You can refer to the following for more information https://azure.github.io/aaz-dev-tools/
  2. Provide the appropriate upstream PR after completing step 1

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in e2b0117fd6 (AAZ migration commit 9451dbe62e). The six STR GET/PUT bases are now generated with aaz-dev-tools 4.7.1 under the SQL aaz directory. The remaining custom layer preserves the existing public handlers and the already-tested final policy GET behavior; request/response schemas are generated.

Upstream command models: Azure/aaz#1130. Both PR descriptions are cross-linked.

Validation: 47 offline tests passed, including all five recorded scenarios and execution of all four generated public-command examples; SQL AAZ metadata verification, flake8, compilation, and adapter pylint passed. No new live resource operations were performed. Full legacy-module pylint has no new diagnostics versus its parent; local environment limitations are documented in the PR.

def _handler(self, command_args):
# Internal command_args bypass CLI parsing/defaults; None means omitted, not JSON null.
args = {key: value for key, value in command_args.items() if value is not None}
args.setdefault("policy_name", "default")
return super()._handler(args)


class _PolicySetOperation(AAZHttpOperation): # pylint: disable=abstract-method
def __call__(self, *args, **kwargs):
session = self.client.send_request(request=self.make_request(), stream=False, **kwargs)
if session.http_response.status_code in (200, 202):
# Keep ARM's final policy GET after Azure-AsyncOperation, rather than the
# spec's Location result, which can report an inconsistent immutability status.
return self.client.build_lro_polling(
self.ctx.args.no_wait, session, self.on_200, self.on_error,
path_format_arguments=self.url_parameters,
)
return self.on_error(session.http_response)


class DatabasePolicyShow(_ShortTermRetentionPolicy, _DatabasePolicyShow):
pass


class DatabasePolicySet(_ShortTermRetentionPolicy, _DatabasePolicySet):
class BackupShortTermRetentionPoliciesCreateOrUpdate(
_PolicySetOperation, _DatabasePolicySet.BackupShortTermRetentionPoliciesCreateOrUpdate):
@property
def content(self):
content = super().content
properties = content["properties"]
if "diffBackupIntervalInHours" in properties:
# The Swagger number enum generates floats, but SQL requires integer JSON tokens.
interval = properties["diffBackupIntervalInHours"]
integer_interval = int(interval)
if integer_interval != interval:
raise InvalidArgumentValueError("--diffbackup-hours must be a whole number of hours.")
properties["diffBackupIntervalInHours"] = integer_interval
return content


class ManagedDatabasePolicyShow(_ShortTermRetentionPolicy, _ManagedDatabasePolicyShow):
pass


class ManagedDatabasePolicySet(_ShortTermRetentionPolicy, _ManagedDatabasePolicySet):
class ManagedBackupShortTermRetentionPoliciesCreateOrUpdate(
_PolicySetOperation, _ManagedDatabasePolicySet.ManagedBackupShortTermRetentionPoliciesCreateOrUpdate):
pass


class DroppedDatabasePolicyShow(_ShortTermRetentionPolicy, _DroppedDatabasePolicyShow):
pass


class DroppedDatabasePolicySet(_ShortTermRetentionPolicy, _DroppedDatabasePolicySet):
class ManagedRestorableDroppedDatabaseBackupShortTermRetentionPoliciesCreateOrUpdate(
_PolicySetOperation,
_DroppedDatabasePolicySet.ManagedRestorableDroppedDatabaseBackupShortTermRetentionPoliciesCreateOrUpdate):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from azure.cli.core.aaz import *


class __CMDGroup(AAZCommandGroup):
"""Manage databases.
"""
pass


__all__ = ["__CMDGroup"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from .__cmd_group import *
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from azure.cli.core.aaz import *


class __CMDGroup(AAZCommandGroup):
"""Manage SQL database short-term retention policies.
"""
pass


__all__ = ["__CMDGroup"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from .__cmd_group import *
from ._set import *
from ._show import *
Loading
Loading