@@ -263,85 +263,8 @@ func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.
263263 connV2 := meta .(* config.MongoDBClient ).AtlasV2
264264 projectID := d .Get ("project_id" ).(string )
265265
266- dataProtectionSettings := & admin.DataProtectionSettings20231001 {
267- ProjectId : conversion .StringPtr (projectID ),
268- AuthorizedEmail : d .Get ("authorized_email" ).(string ),
269- AuthorizedUserFirstName : d .Get ("authorized_user_first_name" ).(string ),
270- AuthorizedUserLastName : d .Get ("authorized_user_last_name" ).(string ),
271- CopyProtectionEnabled : conversion .Pointer (d .Get ("copy_protection_enabled" ).(bool )),
272- EncryptionAtRestEnabled : conversion .Pointer (d .Get ("encryption_at_rest_enabled" ).(bool )),
273- PitEnabled : conversion .Pointer (d .Get ("pit_enabled" ).(bool )),
274- RestoreWindowDays : conversion .Pointer (cast .ToInt (d .Get ("restore_window_days" ))),
275- OnDemandPolicyItem : expandDemandBackupPolicyItem (d ),
276- }
266+ err := updateOrCreateDataProtectionSetting (ctx , d , connV2 , projectID )
277267
278- var backupPoliciesItem []admin.BackupComplianceScheduledPolicyItem
279- if v , ok := d .GetOk ("policy_item_hourly" ); ok {
280- item := v .([]any )
281- itemObj := item [0 ].(map [string ]any )
282- backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
283- FrequencyType : cloudbackupschedule .Hourly ,
284- RetentionUnit : itemObj ["retention_unit" ].(string ),
285- FrequencyInterval : itemObj ["frequency_interval" ].(int ),
286- RetentionValue : itemObj ["retention_value" ].(int ),
287- })
288- }
289- if v , ok := d .GetOk ("policy_item_daily" ); ok {
290- item := v .([]any )
291- itemObj := item [0 ].(map [string ]any )
292- backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
293- FrequencyType : cloudbackupschedule .Daily ,
294- RetentionUnit : itemObj ["retention_unit" ].(string ),
295- FrequencyInterval : itemObj ["frequency_interval" ].(int ),
296- RetentionValue : itemObj ["retention_value" ].(int ),
297- })
298- }
299- if v , ok := d .GetOk ("policy_item_weekly" ); ok {
300- items := v .([]any )
301- for _ , s := range items {
302- itemObj := s .(map [string ]any )
303- backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
304- FrequencyType : cloudbackupschedule .Weekly ,
305- RetentionUnit : itemObj ["retention_unit" ].(string ),
306- FrequencyInterval : itemObj ["frequency_interval" ].(int ),
307- RetentionValue : itemObj ["retention_value" ].(int ),
308- })
309- }
310- }
311- if v , ok := d .GetOk ("policy_item_monthly" ); ok {
312- items := v .([]any )
313- for _ , s := range items {
314- itemObj := s .(map [string ]any )
315- backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
316- FrequencyType : cloudbackupschedule .Monthly ,
317- RetentionUnit : itemObj ["retention_unit" ].(string ),
318- FrequencyInterval : itemObj ["frequency_interval" ].(int ),
319- RetentionValue : itemObj ["retention_value" ].(int ),
320- })
321- }
322- }
323- if v , ok := d .GetOk ("policy_item_yearly" ); ok {
324- items := v .([]any )
325- for _ , s := range items {
326- itemObj := s .(map [string ]any )
327- backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
328- FrequencyType : cloudbackupschedule .Yearly ,
329- RetentionUnit : itemObj ["retention_unit" ].(string ),
330- FrequencyInterval : itemObj ["frequency_interval" ].(int ),
331- RetentionValue : itemObj ["retention_value" ].(int ),
332- })
333- }
334- }
335- if len (backupPoliciesItem ) > 0 {
336- dataProtectionSettings .ScheduledPolicyItems = & backupPoliciesItem
337- }
338-
339- params := admin.UpdateDataProtectionSettingsApiParams {
340- GroupId : projectID ,
341- DataProtectionSettings20231001 : dataProtectionSettings ,
342- OverwriteBackupPolicies : conversion .Pointer (false ),
343- }
344- _ , _ , err := connV2 .CloudBackupsApi .UpdateDataProtectionSettingsWithParams (ctx , & params ).Execute ()
345268 if err != nil {
346269 return diag .FromErr (fmt .Errorf (errorBackupPolicyUpdate , projectID , err ))
347270 }
@@ -444,97 +367,8 @@ func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.
444367 ids := conversion .DecodeStateID (d .Id ())
445368 projectID := ids ["project_id" ]
446369
447- dataProtectionSettings := & admin.DataProtectionSettings20231001 {
448- ProjectId : conversion .StringPtr (projectID ),
449- AuthorizedEmail : d .Get ("authorized_email" ).(string ),
450- AuthorizedUserFirstName : d .Get ("authorized_user_first_name" ).(string ),
451- AuthorizedUserLastName : d .Get ("authorized_user_last_name" ).(string ),
452- OnDemandPolicyItem : expandDemandBackupPolicyItem (d ),
453- }
454-
455- if d .HasChange ("copy_protection_enabled" ) {
456- dataProtectionSettings .CopyProtectionEnabled = conversion .Pointer (d .Get ("copy_protection_enabled" ).(bool ))
457- }
458-
459- if d .HasChange ("encryption_at_rest_enabled" ) {
460- dataProtectionSettings .EncryptionAtRestEnabled = conversion .Pointer (d .Get ("encryption_at_rest_enabled" ).(bool ))
461- }
462-
463- if d .HasChange ("pit_enabled" ) {
464- dataProtectionSettings .PitEnabled = conversion .Pointer (d .Get ("pit_enabled" ).(bool ))
465- }
466-
467- if d .HasChange ("restore_window_days" ) {
468- dataProtectionSettings .RestoreWindowDays = conversion .Pointer (cast .ToInt (d .Get ("restore_window_days" )))
469- }
370+ err := updateOrCreateDataProtectionSetting (ctx , d , connV2 , projectID )
470371
471- var backupPoliciesItem []admin.BackupComplianceScheduledPolicyItem
472- if v , ok := d .GetOk ("policy_item_hourly" ); ok {
473- item := v .([]any )
474- itemObj := item [0 ].(map [string ]any )
475- backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
476- FrequencyType : cloudbackupschedule .Hourly ,
477- RetentionUnit : itemObj ["retention_unit" ].(string ),
478- FrequencyInterval : itemObj ["frequency_interval" ].(int ),
479- RetentionValue : itemObj ["retention_value" ].(int ),
480- })
481- }
482- if v , ok := d .GetOk ("policy_item_daily" ); ok {
483- item := v .([]any )
484- itemObj := item [0 ].(map [string ]any )
485- backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
486- FrequencyType : cloudbackupschedule .Daily ,
487- RetentionUnit : itemObj ["retention_unit" ].(string ),
488- FrequencyInterval : itemObj ["frequency_interval" ].(int ),
489- RetentionValue : itemObj ["retention_value" ].(int ),
490- })
491- }
492- if v , ok := d .GetOk ("policy_item_weekly" ); ok {
493- items := v .([]any )
494- for _ , s := range items {
495- itemObj := s .(map [string ]any )
496- backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
497- FrequencyType : cloudbackupschedule .Weekly ,
498- RetentionUnit : itemObj ["retention_unit" ].(string ),
499- FrequencyInterval : itemObj ["frequency_interval" ].(int ),
500- RetentionValue : itemObj ["retention_value" ].(int ),
501- })
502- }
503- }
504- if v , ok := d .GetOk ("policy_item_monthly" ); ok {
505- items := v .([]any )
506- for _ , s := range items {
507- itemObj := s .(map [string ]any )
508- backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
509- FrequencyType : cloudbackupschedule .Monthly ,
510- RetentionUnit : itemObj ["retention_unit" ].(string ),
511- FrequencyInterval : itemObj ["frequency_interval" ].(int ),
512- RetentionValue : itemObj ["retention_value" ].(int ),
513- })
514- }
515- }
516- if v , ok := d .GetOk ("policy_item_yearly" ); ok {
517- items := v .([]any )
518- for _ , s := range items {
519- itemObj := s .(map [string ]any )
520- backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
521- FrequencyType : cloudbackupschedule .Yearly ,
522- RetentionUnit : itemObj ["retention_unit" ].(string ),
523- FrequencyInterval : itemObj ["frequency_interval" ].(int ),
524- RetentionValue : itemObj ["retention_value" ].(int ),
525- })
526- }
527- }
528- if len (backupPoliciesItem ) > 0 {
529- dataProtectionSettings .ScheduledPolicyItems = & backupPoliciesItem
530- }
531-
532- params := admin.UpdateDataProtectionSettingsApiParams {
533- GroupId : projectID ,
534- DataProtectionSettings20231001 : dataProtectionSettings ,
535- OverwriteBackupPolicies : conversion .Pointer (false ),
536- }
537- _ , _ , err := connV2 .CloudBackupsApi .UpdateDataProtectionSettingsWithParams (ctx , & params ).Execute ()
538372 if err != nil {
539373 return diag .FromErr (fmt .Errorf (errorBackupPolicyUpdate , projectID , err ))
540374 }
@@ -622,3 +456,86 @@ func flattenBackupPolicyItems(items []admin.BackupComplianceScheduledPolicyItem,
622456 }
623457 return policyItems
624458}
459+
460+ func updateOrCreateDataProtectionSetting (ctx context.Context , d * schema.ResourceData , connV2 * admin.APIClient , projectID string ) error {
461+ dataProtectionSettings := & admin.DataProtectionSettings20231001 {
462+ ProjectId : conversion .StringPtr (projectID ),
463+ AuthorizedEmail : d .Get ("authorized_email" ).(string ),
464+ AuthorizedUserFirstName : d .Get ("authorized_user_first_name" ).(string ),
465+ AuthorizedUserLastName : d .Get ("authorized_user_last_name" ).(string ),
466+ CopyProtectionEnabled : conversion .Pointer (d .Get ("copy_protection_enabled" ).(bool )),
467+ EncryptionAtRestEnabled : conversion .Pointer (d .Get ("encryption_at_rest_enabled" ).(bool )),
468+ PitEnabled : conversion .Pointer (d .Get ("pit_enabled" ).(bool )),
469+ RestoreWindowDays : conversion .Pointer (cast .ToInt (d .Get ("restore_window_days" ))),
470+ OnDemandPolicyItem : expandDemandBackupPolicyItem (d ),
471+ }
472+
473+ var backupPoliciesItem []admin.BackupComplianceScheduledPolicyItem
474+ if v , ok := d .GetOk ("policy_item_hourly" ); ok {
475+ item := v .([]any )
476+ itemObj := item [0 ].(map [string ]any )
477+ backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
478+ FrequencyType : cloudbackupschedule .Hourly ,
479+ RetentionUnit : itemObj ["retention_unit" ].(string ),
480+ FrequencyInterval : itemObj ["frequency_interval" ].(int ),
481+ RetentionValue : itemObj ["retention_value" ].(int ),
482+ })
483+ }
484+ if v , ok := d .GetOk ("policy_item_daily" ); ok {
485+ item := v .([]any )
486+ itemObj := item [0 ].(map [string ]any )
487+ backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
488+ FrequencyType : cloudbackupschedule .Daily ,
489+ RetentionUnit : itemObj ["retention_unit" ].(string ),
490+ FrequencyInterval : itemObj ["frequency_interval" ].(int ),
491+ RetentionValue : itemObj ["retention_value" ].(int ),
492+ })
493+ }
494+ if v , ok := d .GetOk ("policy_item_weekly" ); ok {
495+ items := v .([]any )
496+ for _ , s := range items {
497+ itemObj := s .(map [string ]any )
498+ backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
499+ FrequencyType : cloudbackupschedule .Weekly ,
500+ RetentionUnit : itemObj ["retention_unit" ].(string ),
501+ FrequencyInterval : itemObj ["frequency_interval" ].(int ),
502+ RetentionValue : itemObj ["retention_value" ].(int ),
503+ })
504+ }
505+ }
506+ if v , ok := d .GetOk ("policy_item_monthly" ); ok {
507+ items := v .([]any )
508+ for _ , s := range items {
509+ itemObj := s .(map [string ]any )
510+ backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
511+ FrequencyType : cloudbackupschedule .Monthly ,
512+ RetentionUnit : itemObj ["retention_unit" ].(string ),
513+ FrequencyInterval : itemObj ["frequency_interval" ].(int ),
514+ RetentionValue : itemObj ["retention_value" ].(int ),
515+ })
516+ }
517+ }
518+ if v , ok := d .GetOk ("policy_item_yearly" ); ok {
519+ items := v .([]any )
520+ for _ , s := range items {
521+ itemObj := s .(map [string ]any )
522+ backupPoliciesItem = append (backupPoliciesItem , admin.BackupComplianceScheduledPolicyItem {
523+ FrequencyType : cloudbackupschedule .Yearly ,
524+ RetentionUnit : itemObj ["retention_unit" ].(string ),
525+ FrequencyInterval : itemObj ["frequency_interval" ].(int ),
526+ RetentionValue : itemObj ["retention_value" ].(int ),
527+ })
528+ }
529+ }
530+ if len (backupPoliciesItem ) > 0 {
531+ dataProtectionSettings .ScheduledPolicyItems = & backupPoliciesItem
532+ }
533+
534+ params := admin.UpdateDataProtectionSettingsApiParams {
535+ GroupId : projectID ,
536+ DataProtectionSettings20231001 : dataProtectionSettings ,
537+ OverwriteBackupPolicies : conversion .Pointer (false ),
538+ }
539+ _ , _ , err := connV2 .CloudBackupsApi .UpdateDataProtectionSettingsWithParams (ctx , & params ).Execute ()
540+ return err
541+ }
0 commit comments