From 6144043d4e593e97e07556d200f9f787910bbba8 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Mon, 11 Mar 2024 11:51:04 +0800 Subject: [PATCH 1/8] Support new parameters to azure_rm_backuppolicy.py --- plugins/modules/azure_rm_backuppolicy.py | 35 ++++++++++++++++++++++-- requirements-azure.txt | 2 +- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/plugins/modules/azure_rm_backuppolicy.py b/plugins/modules/azure_rm_backuppolicy.py index 4c1f6cca9..f9935961c 100644 --- a/plugins/modules/azure_rm_backuppolicy.py +++ b/plugins/modules/azure_rm_backuppolicy.py @@ -67,6 +67,7 @@ choices: - Daily - Weekly + - Hourly type: str schedule_days: description: @@ -74,6 +75,14 @@ - Does not apply to Daily frequency. type: list elements: str + monthly_retention_count: + description: + - The amount of months to retain backups. + type: int + yearly_retention_count: + description: + - The amount of years to retain backups. + type: int weekly_retention_count: description: - The amount of weeks to retain backups. @@ -191,10 +200,12 @@ def __init__(self): backup_management_type=dict(type='str', choices=['AzureIaasVM']), schedule_run_time=dict(type='int'), instant_recovery_snapshot_retention=dict(type='int'), - schedule_run_frequency=dict(type='str', choices=['Daily', 'Weekly']), + schedule_run_frequency=dict(type='str', choices=['Daily', 'Weekly', 'Hourly']), schedule_days=dict(type='list', elements='str'), weekly_retention_count=dict(type='int'), daily_retention_count=dict(type='int'), + monthly_retention_count=dict(type='int'), + yearly_retention_count=dict(type='int'), schedule_weekly_frequency=dict(type='int'), time_zone=dict(type='str', default='UTC'), ) @@ -208,6 +219,8 @@ def __init__(self): self.schedule_run_frequency = None self.schedule_days = None self.weekly_retention_count = None + self.monthly_retention_count = None + self.yearly_retention_count = None self.schedule_weekly_frequency = None self.daily_retention_count = None self.time_zone = None @@ -347,6 +360,8 @@ def create_or_update_backup_policy(self): daily_retention_schedule = None weekly_retention_schedule = None + monthly_retention_schedule = None + yearly_retention_schedule = None # Daily backups can have a daily retention or weekly but Weekly backups cannot have a daily retention if (self.daily_retention_count and self.schedule_run_frequency == "Daily"): @@ -361,8 +376,24 @@ def create_or_update_backup_policy(self): retention_times=schedule_run_times_as_datetimes, retention_duration=retention_duration) + if (self.monthly_retention_count): + retention_duration = self.recovery_services_backup_models.RetentionDuration(count=self.monthly_retention_count, + duration_type="Months") + monthly_retention_schedule = self.recovery_services_backup_models.WeeklyRetentionSchedule(days_of_the_week=self.schedule_days, + retention_times=schedule_run_times_as_datetimes, + retention_duration=retention_duration) + + if (self.yearly_retention_count): + retention_duration = self.recovery_services_backup_models.RetentionDuration(count=self.yearly_retention_count, + duration_type="Years") + yearly_retention_schedule = self.recovery_services_backup_models.WeeklyRetentionSchedule(days_of_the_week=self.schedule_days, + retention_times=schedule_run_times_as_datetimes, + retention_duration=retention_duration) + retention_policy = self.recovery_services_backup_models.LongTermRetentionPolicy(daily_schedule=daily_retention_schedule, - weekly_schedule=weekly_retention_schedule) + weekly_schedule=weekly_retention_schedule, + monthly_schedule=weekly_retention_schedule, + yearly_schedule=weekly_retention_schedule) policy_definition = None diff --git a/requirements-azure.txt b/requirements-azure.txt index 5fc0064aa..7d9417f74 100644 --- a/requirements-azure.txt +++ b/requirements-azure.txt @@ -47,6 +47,6 @@ azure-mgmt-automation==1.0.0 azure-mgmt-iothub==2.2.0 azure-iot-hub==2.6.1 azure-mgmt-recoveryservices==2.0.0 -azure-mgmt-recoveryservicesbackup==3.0.0 +azure-mgmt-recoveryservicesbackup==9.0.0 azure-mgmt-notificationhubs==7.0.0 azure-mgmt-eventhub==10.1.0 From 8838bc8a471cc5aa72672a32782f886af560ab1e Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Mon, 11 Mar 2024 13:17:43 +0800 Subject: [PATCH 2/8] small change for test --- requirements-azure.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements-azure.txt b/requirements-azure.txt index 7d9417f74..de084e003 100644 --- a/requirements-azure.txt +++ b/requirements-azure.txt @@ -47,6 +47,6 @@ azure-mgmt-automation==1.0.0 azure-mgmt-iothub==2.2.0 azure-iot-hub==2.6.1 azure-mgmt-recoveryservices==2.0.0 -azure-mgmt-recoveryservicesbackup==9.0.0 +azure-mgmt-recoveryservicesbackup==4.0.0 azure-mgmt-notificationhubs==7.0.0 azure-mgmt-eventhub==10.1.0 From 23eb7f9f76272db465fefdbe57f1e06a7cf04022 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Mon, 11 Mar 2024 13:30:19 +0800 Subject: [PATCH 3/8] Modify import package --- plugins/module_utils/azure_rm_common.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/module_utils/azure_rm_common.py b/plugins/module_utils/azure_rm_common.py index 526056108..6bb4a64d8 100644 --- a/plugins/module_utils/azure_rm_common.py +++ b/plugins/module_utils/azure_rm_common.py @@ -272,12 +272,13 @@ def default_api_version(self): from azure.mgmt.iothub import IotHubClient from azure.mgmt.iothub import models as IoTHubModels from azure.mgmt.resource.locks import ManagementLockClient - from azure.mgmt.recoveryservicesbackup import RecoveryServicesBackupClient try: # Older versions of the library exposed the modules at the root of the package import azure.mgmt.recoveryservicesbackup.models as RecoveryServicesBackupModels + from azure.mgmt.recoveryservicesbackup import RecoveryServicesBackupClient except ImportError: import azure.mgmt.recoveryservicesbackup.activestamp.models as RecoveryServicesBackupModels + from import azure.mgmt.recoveryservicesbackup.activestamp import RecoveryServicesBackupClient from azure.mgmt.search import SearchManagementClient from azure.mgmt.datalake.store import DataLakeStoreAccountManagementClient import azure.mgmt.datalake.store.models as DataLakeStoreAccountModel From dbd5a73a31e2123b92fd22e788158f30b9c9b4ea Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Mon, 11 Mar 2024 13:38:02 +0800 Subject: [PATCH 4/8] fix systax error --- plugins/module_utils/azure_rm_common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/module_utils/azure_rm_common.py b/plugins/module_utils/azure_rm_common.py index 6bb4a64d8..ccc27414d 100644 --- a/plugins/module_utils/azure_rm_common.py +++ b/plugins/module_utils/azure_rm_common.py @@ -278,7 +278,7 @@ def default_api_version(self): from azure.mgmt.recoveryservicesbackup import RecoveryServicesBackupClient except ImportError: import azure.mgmt.recoveryservicesbackup.activestamp.models as RecoveryServicesBackupModels - from import azure.mgmt.recoveryservicesbackup.activestamp import RecoveryServicesBackupClient + from azure.mgmt.recoveryservicesbackup.activestamp import RecoveryServicesBackupClient from azure.mgmt.search import SearchManagementClient from azure.mgmt.datalake.store import DataLakeStoreAccountManagementClient import azure.mgmt.datalake.store.models as DataLakeStoreAccountModel From e17b86185bc30063452113bbd94077279f7fd056 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Mon, 11 Mar 2024 15:22:05 +0800 Subject: [PATCH 5/8] new change for azure_rm_backuppolicy.py --- plugins/modules/azure_rm_backuppolicy.py | 88 ++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 5 deletions(-) diff --git a/plugins/modules/azure_rm_backuppolicy.py b/plugins/modules/azure_rm_backuppolicy.py index f9935961c..45bead2ae 100644 --- a/plugins/modules/azure_rm_backuppolicy.py +++ b/plugins/modules/azure_rm_backuppolicy.py @@ -104,6 +104,52 @@ - Timezone to apply I(schedule_run_time). default: UTC type: str + retention_schedule_format_type: + description: + - Retention schedule format for yearly retention policy. + type: str + choices: + - Daily + - Weekly + months_of_year: + description: + - List of months of year of yearly retention policy. + type: list + elements: str + retention_schedule_daily: + description: + - Daily retention format for yearly retention policy. + type: dict + suboptions: + days_of_the_month: + description: + - List of days of the month. + type: list + elements: dict + suboptions: + date: + description: + - Date of the month. + type: int + is_last: + description: + - Whether Date is last date of month. + type: bool + retention_schedule_weekly: + description: + - Weekly retention format for yearly retention policy. + type: dict + suboptions: + days_of_the_week: + description: + - List of days of the week. + type: list + elements: str + weeks_of_the_month: + description: + - List of weeks of month. + type: list + elements: str extends_documentation_fragment: - azure.azcollection.azure @@ -208,8 +254,29 @@ def __init__(self): yearly_retention_count=dict(type='int'), schedule_weekly_frequency=dict(type='int'), time_zone=dict(type='str', default='UTC'), + retention_schedule_format_type=dict(type='str', choices=['Daily', 'Weekly']), + months_of_year=dict(type='list', elements='str'), + retention_schedule_daily=dict( + type='dict', + options=dict( + days_of_the_month=dict( + type='list', + elements='dict', + options=dict( + data=dict(type='int'), + is_last=dict(type='bool'), + ) + ) + ) + ), + retention_schedule_weekly=dict( + type='dict', + options=dict( + days_of_the_week=dict(type='list', elements='str'), + weeks_of_the_month=dict(type='list', elements='str') + ) + ) ) - self.vault_name = None self.name = None self.resource_group = None @@ -224,6 +291,10 @@ def __init__(self): self.schedule_weekly_frequency = None self.daily_retention_count = None self.time_zone = None + self.retention_schedule_format_type = None + self.months_of_year = None + self.retention_schedule_daily = None + self.retention_schedule_weekly = None self.results = dict( changed=False, @@ -379,14 +450,21 @@ def create_or_update_backup_policy(self): if (self.monthly_retention_count): retention_duration = self.recovery_services_backup_models.RetentionDuration(count=self.monthly_retention_count, duration_type="Months") - monthly_retention_schedule = self.recovery_services_backup_models.WeeklyRetentionSchedule(days_of_the_week=self.schedule_days, - retention_times=schedule_run_times_as_datetimes, - retention_duration=retention_duration) + monthly_retention_schedule = self.recovery_services_backup_models.MonthlyRetentionSchedule( + retention_schedule_format_type=self.retention_schedule_format_type, + retention_schedule_daily=self.retention_schedule_daily, + retention_schedule_weekly=self.retention_schedule_weekly, + retention_times=schedule_run_times_as_datetimes, + retention_duration=retention_duration) if (self.yearly_retention_count): retention_duration = self.recovery_services_backup_models.RetentionDuration(count=self.yearly_retention_count, duration_type="Years") - yearly_retention_schedule = self.recovery_services_backup_models.WeeklyRetentionSchedule(days_of_the_week=self.schedule_days, + yearly_retention_schedule = self.recovery_services_backup_models.YearlyRetentionSchedule( + retention_schedule_format_type=self.retention_schedule_format_type, + months_of_year=self.months_of_year, + retention_schedule_daily=self.retention_schedule_daily, + retention_schedule_weekly=self.retention_schedule_weekly, retention_times=schedule_run_times_as_datetimes, retention_duration=retention_duration) From 02e73d055fcafdf32da762fcc17568f3bc36a9f9 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Mon, 11 Mar 2024 19:24:12 +0800 Subject: [PATCH 6/8] small change --- plugins/modules/azure_rm_backuppolicy.py | 26 ++++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/plugins/modules/azure_rm_backuppolicy.py b/plugins/modules/azure_rm_backuppolicy.py index 45bead2ae..23c435542 100644 --- a/plugins/modules/azure_rm_backuppolicy.py +++ b/plugins/modules/azure_rm_backuppolicy.py @@ -254,7 +254,7 @@ def __init__(self): yearly_retention_count=dict(type='int'), schedule_weekly_frequency=dict(type='int'), time_zone=dict(type='str', default='UTC'), - retention_schedule_format_type=dict(type='str', choices=['Daily', 'Weekly']), + retention_schedule_format_type=dict(type='str', choices=['Daily', 'Weekly']), months_of_year=dict(type='list', elements='str'), retention_schedule_daily=dict( type='dict', @@ -263,7 +263,7 @@ def __init__(self): type='list', elements='dict', options=dict( - data=dict(type='int'), + date=dict(type='int'), is_last=dict(type='bool'), ) ) @@ -451,22 +451,22 @@ def create_or_update_backup_policy(self): retention_duration = self.recovery_services_backup_models.RetentionDuration(count=self.monthly_retention_count, duration_type="Months") monthly_retention_schedule = self.recovery_services_backup_models.MonthlyRetentionSchedule( - retention_schedule_format_type=self.retention_schedule_format_type, - retention_schedule_daily=self.retention_schedule_daily, - retention_schedule_weekly=self.retention_schedule_weekly, - retention_times=schedule_run_times_as_datetimes, - retention_duration=retention_duration) + retention_schedule_format_type=self.retention_schedule_format_type, + retention_schedule_daily=self.retention_schedule_daily, + retention_schedule_weekly=self.retention_schedule_weekly, + retention_times=schedule_run_times_as_datetimes, + retention_duration=retention_duration) if (self.yearly_retention_count): retention_duration = self.recovery_services_backup_models.RetentionDuration(count=self.yearly_retention_count, duration_type="Years") yearly_retention_schedule = self.recovery_services_backup_models.YearlyRetentionSchedule( - retention_schedule_format_type=self.retention_schedule_format_type, - months_of_year=self.months_of_year, - retention_schedule_daily=self.retention_schedule_daily, - retention_schedule_weekly=self.retention_schedule_weekly, - retention_times=schedule_run_times_as_datetimes, - retention_duration=retention_duration) + retention_schedule_format_type=self.retention_schedule_format_type, + months_of_year=self.months_of_year, + retention_schedule_daily=self.retention_schedule_daily, + retention_schedule_weekly=self.retention_schedule_weekly, + retention_times=schedule_run_times_as_datetimes, + retention_duration=retention_duration) retention_policy = self.recovery_services_backup_models.LongTermRetentionPolicy(daily_schedule=daily_retention_schedule, weekly_schedule=weekly_retention_schedule, From d68e2636047485a59cc7d97f0cf618f623a53731 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Tue, 23 Apr 2024 09:45:28 +0800 Subject: [PATCH 7/8] Fix minor errors --- plugins/modules/azure_rm_backuppolicy.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/plugins/modules/azure_rm_backuppolicy.py b/plugins/modules/azure_rm_backuppolicy.py index 23c435542..06d396df5 100644 --- a/plugins/modules/azure_rm_backuppolicy.py +++ b/plugins/modules/azure_rm_backuppolicy.py @@ -246,7 +246,7 @@ def __init__(self): backup_management_type=dict(type='str', choices=['AzureIaasVM']), schedule_run_time=dict(type='int'), instant_recovery_snapshot_retention=dict(type='int'), - schedule_run_frequency=dict(type='str', choices=['Daily', 'Weekly', 'Hourly']), + schedule_run_frequency=dict(type='str', choices=['Daily', 'Weekly']), schedule_days=dict(type='list', elements='str'), weekly_retention_count=dict(type='int'), daily_retention_count=dict(type='int'), @@ -303,12 +303,19 @@ def __init__(self): required_if = [('schedule_run_frequency', 'Weekly', ['schedule_days', 'weekly_retention_count', 'schedule_run_time']), ('schedule_run_frequency', 'Daily', ['daily_retention_count', 'schedule_run_time']), + ('yearly_retention_count', '*', ['retention_schedule_format_type']), + ('monthly_retention_count', '*', ['retention_schedule_format_type']), + ('retention_schedule_format_type', 'Weekly', ['retention_schedule_weekly']), + ('retention_schedule_format_type', 'Daily', ['retention_schedule_daily']), ('state', 'present', ['schedule_run_frequency', 'backup_management_type']), ('log_mode', 'file', ['log_path'])] + mutually_exclusive = [('retention_schedule_daily', 'retention_schedule_weekly')] + super(AzureRMBackupPolicy, self).__init__(derived_arg_spec=self.module_arg_spec, supports_check_mode=True, supports_tags=False, + mutually_exclusive=mutually_exclusive, required_if=required_if) def exec_module(self, **kwargs): @@ -440,14 +447,14 @@ def create_or_update_backup_policy(self): daily_retention_schedule = self.recovery_services_backup_models.DailyRetentionSchedule(retention_times=schedule_run_times_as_datetimes, retention_duration=retention_duration) - if (self.weekly_retention_count): + if self.weekly_retention_count is not None: retention_duration = self.recovery_services_backup_models.RetentionDuration(count=self.weekly_retention_count, duration_type="Weeks") weekly_retention_schedule = self.recovery_services_backup_models.WeeklyRetentionSchedule(days_of_the_week=self.schedule_days, retention_times=schedule_run_times_as_datetimes, retention_duration=retention_duration) - if (self.monthly_retention_count): + if self.monthly_retention_count is not None: retention_duration = self.recovery_services_backup_models.RetentionDuration(count=self.monthly_retention_count, duration_type="Months") monthly_retention_schedule = self.recovery_services_backup_models.MonthlyRetentionSchedule( @@ -457,7 +464,7 @@ def create_or_update_backup_policy(self): retention_times=schedule_run_times_as_datetimes, retention_duration=retention_duration) - if (self.yearly_retention_count): + if self.yearly_retention_count is not None: retention_duration = self.recovery_services_backup_models.RetentionDuration(count=self.yearly_retention_count, duration_type="Years") yearly_retention_schedule = self.recovery_services_backup_models.YearlyRetentionSchedule( @@ -470,8 +477,8 @@ def create_or_update_backup_policy(self): retention_policy = self.recovery_services_backup_models.LongTermRetentionPolicy(daily_schedule=daily_retention_schedule, weekly_schedule=weekly_retention_schedule, - monthly_schedule=weekly_retention_schedule, - yearly_schedule=weekly_retention_schedule) + monthly_schedule=monthly_retention_schedule, + yearly_schedule=yearly_retention_schedule) policy_definition = None From 20f27504e548022410805feceddc121144c6aa14 Mon Sep 17 00:00:00 2001 From: Fred-sun Date: Tue, 23 Apr 2024 09:46:37 +0800 Subject: [PATCH 8/8] Add return value --- plugins/modules/azure_rm_backuppolicy_info.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugins/modules/azure_rm_backuppolicy_info.py b/plugins/modules/azure_rm_backuppolicy_info.py index ddebb2862..0a1fc24f3 100644 --- a/plugins/modules/azure_rm_backuppolicy_info.py +++ b/plugins/modules/azure_rm_backuppolicy_info.py @@ -158,6 +158,9 @@ def set_results(self, policy): self.results['location'] = policy.location self.results['name'] = policy.name self.results['type'] = policy.type + self.results['properties'] = dict() + if policy.properties is not None: + self.results['properties'] = policy.properties.as_dict() else: self.results['id'] = None