From 68dd8e2f5d4d0999b6821270e553f33c78c4fe91 Mon Sep 17 00:00:00 2001 From: Rebecca Xu Date: Wed, 6 Dec 2023 14:48:13 -0800 Subject: [PATCH 1/2] Adding the make-backups-immutable parameter to ltr policy --- src/azure-cli/azure/cli/command_modules/sql/_help.py | 2 +- src/azure-cli/azure/cli/command_modules/sql/_params.py | 7 ++++++- src/azure-cli/azure/cli/command_modules/sql/custom.py | 10 ++++++++++ .../sql/tests/latest/test_sql_commands.py | 10 +++++++--- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/sql/_help.py b/src/azure-cli/azure/cli/command_modules/sql/_help.py index b9e17fc2a4a..0cd69cdc4a3 100644 --- a/src/azure-cli/azure/cli/command_modules/sql/_help.py +++ b/src/azure-cli/azure/cli/command_modules/sql/_help.py @@ -240,7 +240,7 @@ short-summary: Update long term retention settings for a database. examples: - name: Set long term retention for a database. - text: az sql db ltr-policy set -g mygroup -s myserver -n mydb --weekly-retention "P1W" --monthly-retention "P6M" --yearly-retention "P1Y" --week-of-year 26 + text: az sql db ltr-policy set -g mygroup -s myserver -n mydb --weekly-retention "P1W" --monthly-retention "P6M" --yearly-retention "P1Y" --week-of-year 26 --make-backups-immutable true """ helps['sql db ltr-policy show'] = """ diff --git a/src/azure-cli/azure/cli/command_modules/sql/_params.py b/src/azure-cli/azure/cli/command_modules/sql/_params.py index 2b7b1f13a86..aed356ce554 100644 --- a/src/azure-cli/azure/cli/command_modules/sql/_params.py +++ b/src/azure-cli/azure/cli/command_modules/sql/_params.py @@ -1220,7 +1220,8 @@ def _configure_security_policy_storage_params(arg_ctx): 'weekly_retention', 'monthly_retention', 'yearly_retention', - 'week_of_year']) + 'week_of_year', + 'make_backups_immutable']) c.argument('weekly_retention', help='Retention for the weekly backup. ' @@ -1240,6 +1241,10 @@ def _configure_security_policy_storage_params(arg_ctx): c.argument('week_of_year', help='The Week of Year, 1 to 52, in which to take the yearly LTR backup.') + c.argument('make_backups_immutable', + help='Whether to make the LTR backups immutable.', + arg_type=get_three_state_flag()) + with self.argument_context('sql db ltr-backup') as c: c.argument('location_name', required=True, diff --git a/src/azure-cli/azure/cli/command_modules/sql/custom.py b/src/azure-cli/azure/cli/command_modules/sql/custom.py index 1136c56dd9c..abc883079e5 100644 --- a/src/azure-cli/azure/cli/command_modules/sql/custom.py +++ b/src/azure-cli/azure/cli/command_modules/sql/custom.py @@ -2966,6 +2966,7 @@ def update_long_term_retention( monthly_retention=None, yearly_retention=None, week_of_year=None, + make_backups_immutable=None, **kwargs): ''' Updates long term retention for managed database @@ -2976,6 +2977,13 @@ def update_long_term_retention( if yearly_retention and not week_of_year: raise CLIError('Please specify week of year for yearly retention.') + if make_backups_immutable: + confirmation = prompt_y_n("""Immutable LTR backups can't be changed or deleted. + You'll be charged for LTR backups for the full retention period. + Do you want to proceed?""") + if not confirmation: + return + kwargs['weekly_retention'] = weekly_retention kwargs['monthly_retention'] = monthly_retention @@ -2984,6 +2992,8 @@ def update_long_term_retention( kwargs['week_of_year'] = week_of_year + kwargs['make_backups_immutable'] = make_backups_immutable + policy = client.begin_create_or_update( database_name=database_name, server_name=server_name, diff --git a/src/azure-cli/azure/cli/command_modules/sql/tests/latest/test_sql_commands.py b/src/azure-cli/azure/cli/command_modules/sql/tests/latest/test_sql_commands.py index 604de39db32..e9d515177fc 100644 --- a/src/azure-cli/azure/cli/command_modules/sql/tests/latest/test_sql_commands.py +++ b/src/azure-cli/azure/cli/command_modules/sql/tests/latest/test_sql_commands.py @@ -1197,6 +1197,7 @@ def test_sql_db_long_term_retention( 'monthly_retention': 'P1M', 'yearly_retention': 'P2M', 'week_of_year': 12, + 'make_backups_immutable': 'False', 'encryption_protector' : 'https://test123343strehan.vault.azure.net/keys/testk1/604b0e26e2a24eeaab30b80c8d7bb1c1', 'keys' : '"https://test123343strehan.vault.azure.net/keys/k2/66f51a6e70f04067af8eaf77805e88b1" "https://test123343strehan.vault.azure.net/keys/testk1/604b0e26e2a24eeaab30b80c8d7bb1c1" "https://test123343strehan.vault.azure.net/keys/testk1/96151496df864e32aa62a3c1857b2931"', 'umi' : '/subscriptions/e1775f9f-a286-474d-b6f0-29c42ac74554/resourcegroups/ArmTemplate/providers/Microsoft.ManagedIdentity/userAssignedIdentities/shobhittest' @@ -1206,12 +1207,14 @@ def test_sql_db_long_term_retention( self.cmd( 'sql db ltr-policy set -g {rg} -s {server_name} -n {database_name}' ' --weekly-retention {weekly_retention} --monthly-retention {monthly_retention}' - ' --yearly-retention {yearly_retention} --week-of-year {week_of_year}', + ' --yearly-retention {yearly_retention} --week-of-year {week_of_year}' + ' --make-backups-immutable {make_backups_immutable}', checks=[ self.check('resourceGroup', '{rg}'), self.check('weeklyRetention', '{weekly_retention}'), self.check('monthlyRetention', '{monthly_retention}'), - self.check('yearlyRetention', '{yearly_retention}')]) + self.check('yearlyRetention', '{yearly_retention}'), + self.check('makeBackupsImmutable', '{make_backups_immutable}')]) # test get long term retention policy on live database self.cmd( @@ -1220,7 +1223,8 @@ def test_sql_db_long_term_retention( self.check('resourceGroup', '{rg}'), self.check('weeklyRetention', '{weekly_retention}'), self.check('monthlyRetention', '{monthly_retention}'), - self.check('yearlyRetention', '{yearly_retention}')]) + self.check('yearlyRetention', '{yearly_retention}'), + self.check('makeBackupsImmutable', '{make_backups_immutable}')]) # test list long term retention backups for location # with resource group From ccdfd31f568d294b8b7210a51fbd5c1a3033d1d3 Mon Sep 17 00:00:00 2001 From: Rebecca Xu Date: Wed, 6 Dec 2023 14:48:13 -0800 Subject: [PATCH 2/2] Adding the make-backups-immutable parameter to ltr policy --- src/azure-cli/azure/cli/command_modules/sql/_help.py | 2 +- src/azure-cli/azure/cli/command_modules/sql/_params.py | 7 ++++++- src/azure-cli/azure/cli/command_modules/sql/custom.py | 10 ++++++++++ .../sql/tests/latest/test_sql_commands.py | 10 +++++++--- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/azure-cli/azure/cli/command_modules/sql/_help.py b/src/azure-cli/azure/cli/command_modules/sql/_help.py index 38c6768f96b..1a4da576cf7 100644 --- a/src/azure-cli/azure/cli/command_modules/sql/_help.py +++ b/src/azure-cli/azure/cli/command_modules/sql/_help.py @@ -242,7 +242,7 @@ short-summary: Update long term retention settings for a database. examples: - name: Set long term retention for a database. - text: az sql db ltr-policy set -g mygroup -s myserver -n mydb --weekly-retention "P1W" --monthly-retention "P6M" --yearly-retention "P1Y" --week-of-year 26 + text: az sql db ltr-policy set -g mygroup -s myserver -n mydb --weekly-retention "P1W" --monthly-retention "P6M" --yearly-retention "P1Y" --week-of-year 26 --make-backups-immutable true """ helps['sql db ltr-policy show'] = """ diff --git a/src/azure-cli/azure/cli/command_modules/sql/_params.py b/src/azure-cli/azure/cli/command_modules/sql/_params.py index 015f5bb4794..a4c8b664691 100644 --- a/src/azure-cli/azure/cli/command_modules/sql/_params.py +++ b/src/azure-cli/azure/cli/command_modules/sql/_params.py @@ -1261,7 +1261,8 @@ def _configure_security_policy_storage_params(arg_ctx): 'weekly_retention', 'monthly_retention', 'yearly_retention', - 'week_of_year']) + 'week_of_year', + 'make_backups_immutable']) c.argument('weekly_retention', help='Retention for the weekly backup. ' @@ -1281,6 +1282,10 @@ def _configure_security_policy_storage_params(arg_ctx): c.argument('week_of_year', help='The Week of Year, 1 to 52, in which to take the yearly LTR backup.') + c.argument('make_backups_immutable', + help='Whether to make the LTR backups immutable.', + arg_type=get_three_state_flag()) + with self.argument_context('sql db ltr-backup') as c: c.argument('location_name', required=True, diff --git a/src/azure-cli/azure/cli/command_modules/sql/custom.py b/src/azure-cli/azure/cli/command_modules/sql/custom.py index 744d530f72b..6e1c350a3be 100644 --- a/src/azure-cli/azure/cli/command_modules/sql/custom.py +++ b/src/azure-cli/azure/cli/command_modules/sql/custom.py @@ -3000,6 +3000,7 @@ def update_long_term_retention( monthly_retention=None, yearly_retention=None, week_of_year=None, + make_backups_immutable=None, **kwargs): ''' Updates long term retention for managed database @@ -3010,6 +3011,13 @@ def update_long_term_retention( if yearly_retention and not week_of_year: raise CLIError('Please specify week of year for yearly retention.') + if make_backups_immutable: + confirmation = prompt_y_n("""Immutable LTR backups can't be changed or deleted. + You'll be charged for LTR backups for the full retention period. + Do you want to proceed?""") + if not confirmation: + return + kwargs['weekly_retention'] = weekly_retention kwargs['monthly_retention'] = monthly_retention @@ -3018,6 +3026,8 @@ def update_long_term_retention( kwargs['week_of_year'] = week_of_year + kwargs['make_backups_immutable'] = make_backups_immutable + policy = client.begin_create_or_update( database_name=database_name, server_name=server_name, diff --git a/src/azure-cli/azure/cli/command_modules/sql/tests/latest/test_sql_commands.py b/src/azure-cli/azure/cli/command_modules/sql/tests/latest/test_sql_commands.py index c09c054067d..7da03d0cbef 100644 --- a/src/azure-cli/azure/cli/command_modules/sql/tests/latest/test_sql_commands.py +++ b/src/azure-cli/azure/cli/command_modules/sql/tests/latest/test_sql_commands.py @@ -1233,6 +1233,7 @@ def test_sql_db_long_term_retention( 'monthly_retention': 'P1M', 'yearly_retention': 'P2M', 'week_of_year': 12, + 'make_backups_immutable': 'False', 'encryption_protector' : 'https://test123343strehan.vault.azure.net/keys/testk1/604b0e26e2a24eeaab30b80c8d7bb1c1', 'keys' : '"https://test123343strehan.vault.azure.net/keys/k2/66f51a6e70f04067af8eaf77805e88b1" "https://test123343strehan.vault.azure.net/keys/testk1/604b0e26e2a24eeaab30b80c8d7bb1c1" "https://test123343strehan.vault.azure.net/keys/testk1/96151496df864e32aa62a3c1857b2931"', 'umi' : '/subscriptions/e1775f9f-a286-474d-b6f0-29c42ac74554/resourcegroups/ArmTemplate/providers/Microsoft.ManagedIdentity/userAssignedIdentities/shobhittest' @@ -1242,12 +1243,14 @@ def test_sql_db_long_term_retention( self.cmd( 'sql db ltr-policy set -g {rg} -s {server_name} -n {database_name}' ' --weekly-retention {weekly_retention} --monthly-retention {monthly_retention}' - ' --yearly-retention {yearly_retention} --week-of-year {week_of_year}', + ' --yearly-retention {yearly_retention} --week-of-year {week_of_year}' + ' --make-backups-immutable {make_backups_immutable}', checks=[ self.check('resourceGroup', '{rg}'), self.check('weeklyRetention', '{weekly_retention}'), self.check('monthlyRetention', '{monthly_retention}'), - self.check('yearlyRetention', '{yearly_retention}')]) + self.check('yearlyRetention', '{yearly_retention}'), + self.check('makeBackupsImmutable', '{make_backups_immutable}')]) # test get long term retention policy on live database self.cmd( @@ -1256,7 +1259,8 @@ def test_sql_db_long_term_retention( self.check('resourceGroup', '{rg}'), self.check('weeklyRetention', '{weekly_retention}'), self.check('monthlyRetention', '{monthly_retention}'), - self.check('yearlyRetention', '{yearly_retention}')]) + self.check('yearlyRetention', '{yearly_retention}'), + self.check('makeBackupsImmutable', '{make_backups_immutable}')]) # test list long term retention backups for location # with resource group